Translate

Wednesday 16 May 2012

Looping in C++


while:- It is a repeated structure statement which repeats a statement given as long as the condition is true.
                        while(condition)
                                     statement1;
                statement1 is executed till the condition is true.
                example:
                        int i = 1;
while (i <= 20)
{
cout<< i << “\t”;
++i;
}

                Output: 1             2              3.........    20



do while:- Like while statement it repeats a statement given as long as the condition is satisfied unlike in while statement the condition is checked at the end of the structure.
                                do
                                {
                                                statement1;
                                }
                                while(condition);
statement1 is executed till the condition is true. Here while statement contains a semicolon.
example:
                         int i = 1;
do
{
cout<< i << “\t”;
++i;
} while(i <= 20);

           Output: 1             2              3.........    20



for:- It is a repeated structure which can repeat a statement as long as the given condition is satisfied.
                         for(statement1; condition; statement2)
                                    statement3;
                where statement1 initialises control variable, condition is used to check whether statement written after loop can be repeated or not , statement2 is used to modify the control variable. Statement3 is a simple statement (having only one statement) or compound statement (a set of statements written within braces {} ) which can be repeated as long as condition is true or satisfied.
example:
                         for(i = 1; i <= 20; i++)
{
cout<< i << “\t”;
++i;
}

           Output: 1             2              3.........    20


Nested loop:- When one repeated structure is written within one or more such repeated structures then the entire structure is known as nested loop.
        example:
            int i = 1;
           while(i <= 3)
           {
            int j = 1; 
            while(j <= 2)
            {
          cout<< i << “;” << j << “  ”;
          j++;
            }
            i++;
            }             

Output: 1;1  1;2   2;1  2;2  3;1  3;2



Lets have some examples.
Q. Write a program to read two positive integers and check whether they are coprime or not.
#include<iostream>
void main()
{
            int a, b, p, q;
            cout<< “Enter two numbers”;
            cin>> a >> b;
p = a;
q = b;
while (b != 0)
{
int r = a % b;
a = b;
b = r;
}
if(a == 1)
                        cout<< p << “ and ”<< q << “are coprime”;
else
                        cout<< p << “ and ”<< q << “are not coprime”;
}


Q.Write a program to read a list of numbers terminated by -1 and determine positive average of positive numbers.
#include<iostream>
void main()
{
                int a, s = 0, i = 0;
                cout<< “Enter a list of numbers terminated by -1”;
                do          
                {
                                cin>> a;
                                if(a < 0)
                                                continue;
                                s = s + a;
                                ++i;
                }while(a != -1);
                cout<< “No of +ve numbers read”<< i << endl << “Avg=”<< s/float(i);
}


Q. Write a program to display odd factors of a positive integer.
#include<iostream>
#include<iomanip.h>
void main()
{
                int a, n;
                cout<< “Enter the value of a”;
                cin>> a;
                for(i = 1; i <= n; i += 2)
                                if(a % i == 0)
                                                cout<<setw(5)<<i;
}


Q. Write a program to print following pattern.

54321
5432
543
54
5

#include<iostream>
#include<iomanip.h>
void main()
{
int i, j;
for (i = 1;i <= 5;i++)
{
for (j = 5; j >= i; j--)
cout<< setw(5) << j;
cout<< endl;
}
}



Q. Write a program to display factorial of a given positive integer.

Q. Write a program to read a positive integer and check whether it is prime or not.

Q. Write a program to display binary equivalent of a given decimal no.

Q. Write a program to find sum of the digits and reverse of an n digit number.

Q. Write a program to display followin pattern.
           1
         212
       32123
     4321234
   543212345

No comments:

Post a Comment

Total Pageviews