14.10.11

ASUS WALLPAPERS













Mobile Nations Monday Brief: September 19, 2011


Codes to understand working of Loops


Here are two questions that might help you in understanding the working of loops. For loop, while loop and do while loop. You just need to dry run them.

Write a loop to print all numbers from 1 to 100, one per line, Write a blank line after every five lines.

#include <iostream.h>
void main ( )
{
            int i, x=1;
            for (i=1; i<=100; i=i++)
            {
                        cout << i << endl;
                        if ((i/5)==x)
                        {
                                    cout << "    " << endl;
                                    x=x+1;
                        }
            }
}

Write a program to generate a list of first 100 odd numbers using while, do while and For loops.

#include <iostream.h>
void main ( )
{
            int i=1;
            while (i<200)
            {
                        cout << i << endl;
                        i=i+2;
            }
           
            for (i=1; i<200; i=i+2)
            {
                        cout << i << endl;
            }
             i=1;
            do
            {
                        cout << i << endl;
                        i=i+2;
            }while (i<200);
}

Twitter Delicious Facebook Digg Stumbleupon Favorites More