Pages

January 21, 2013

Loop Control- While & Do while loop


loop control statements banner of while and do while loop

These are used for repeating some portion of the program either by specified number of times or until a particular condition is true.
1. while statement
2. do while statement
3. for loop


while loop

This loop is that in which we can execute a set of statements again and again starting from given starting value of the variable up-to that value of the variable which is given in the termination condition.
While loop starts with the the keyword ‘while’ followed by expression and a block is used there after it includes set of statements. It’s format is:

while(expression)
{
  Statement1;
  Statement2;
  Statement3;
}

In this while loop starting value of the variable is initialized above the loop. Termination condition is given with the ‘while’ keyword and increment and decrement of the variable takes place inside the loop.




/*Program to enter the limit by the user and find the sum of the series*/

#include<stdio.h>
#include<conio.h>
void main()
{
     int i=1,n,sum=0;
     printf("\n enter the limit of the series");
     scanf("%d",&n);
     while(i<=n)
     {
                sum=sum+i;
                i++;
                }
     printf("\n sum of the series of limit %d is %d",n,sum);
     getch();
     }


output of limit of series and add it using while loop in C language



/*Program to enter limit of the series by the user and to find the sum of squares*/

#include<stdio.h>
#include<conio.h>
void main()
{
     int i=1,n,sum=0,sq=0;
     printf("\n enter the limit of the series");
     scanf("%d",&n);
     while(i<=n)
     {
                sq=i*i;
                sum=sum+sq;
                i++;
                }
     printf("\n sum of square of series upto limit %d is %d",n,sum);
     getch();
     }
output of sum of squares upto limit using while loop in C language



/*Program to enter any number by the user and find it’s factorial*/

#include<stdio.h>
#include<conio.h>
void main()
{
     int i=1,n,fact=1;
     printf("\n enter the number to whose factorial is to be find");
     scanf("%d",&n);
     while(i<=n)
     {
                fact=fact*i;
                i++;
                }
     printf("\n factorail of %d is %d",n,fact);
     getch();
     }
output of factorial of a number using while loop in C language



/*Program to find the sum of the digits of the number entered*/

#include<stdio.h>
#include<conio.h>
void main()
{
     int n,n1,r,sum=0;
     printf("\n enter the number whose sum you wnt to find");
     scanf("%d",&n);
     n1=n;
     while(n1>0)
     {
                r=n1%10;
                sum=sum+r;
                n1=n1/10;
                }
     printf("\n sum of the digit is %d",sum);
     getch();
     }
output of sum of digits of a number using while statement in C language


/*Program to find the reverse of the entered number*/

#include<stdio.h>
#include<conio.h>
void main()
{
     int n,n1,rev=0;
     printf("\n enter any number");
     scanf("%d",&n);
     while(n>0)
     {
               n1=n%10;
               rev=rev*10+n1;
               n=n/10;
               }
     printf("\n reverse is %d",rev);
     getch();
     }
output to show reverse of a number using while loop in C language



PRECAUTIONS

1. Use appropriate header files.
2. Always use increment in while loop.
3. Use “;” after increment statement.
4. Define appropriate variables.
5. While loop will execute until some condition is fulfilled.
6. Program will not run if the initial condition is false.




Do while looping

This loop is also an inbuilt structure in the language and it is just like for loop and while loop. But this loop is different from while loop because while loop checks the terminating condition at the top but this loop checks the condition at the bottom. This loop is a free entry loop and unlike while loop this loop will execute at least once even if the condition is false.
Some basic difference is:


difference between while and do while loop control statements



/* Program to set the limit and find sum up to that limit*/

#include<stdio.h>
#include<conio.h>
void main()
{
     int i=1,n,sum=0;
     printf("\n enter the limit upto which you want to find sum");
     scanf("%d",&n);
     do
     {
                    sum=sum+i;
                    i++;
                    }
    while(i<=n);
    printf("\n sum upto limit %d is %d",n,sum);
    getch();
     }
output of limit of sum using do while loop in C language


/*Program to find the factorial of a number entered by the user*/

#include<stdio.h>
#include<conio.h>
void main()
{
     int i=1,n,fact=1;
     printf("\n enter any number whose limit you want to find");
     scanf("%d",&n);
     do
     {
                    fact=fact*i;
                    i++;
                    }
     while(i<=n);
     printf("\n factorial is %d",fact);
     getch();
     }
output of factorial of a number using do while loop in C language


/*Program to enter limit and find sum of squares*/

#include<stdio.h>
#include<conio.h>
void main()
{
     int i=1,n,sq,sum=0;
     printf("\n enter the limit where you want to end the series");
     scanf("%d",&n);
     do
     {
                    sq=i*i;
                    sum=sum+sq;
                    i++;
                    }
     while(i<=n);
     printf("\n sum of squares is %d",sum);
     getch();
     }
output of squares of series upto a limit using do while loop in C language

     

/*Program to find the sum of digits of entered number*/

#include<stdio.h>
#include<conio.h>
void main()
{
     int n,n1,sum=0,r;
     printf("\n enter the number whose sum you want to find");
     scanf("%d",&n);
     do
     {
                    n1=n%10;
                    sum=sum+n1;
                    n=n/10;
                    }
     while(n>0);
     printf("\n sum of it's digit is %d",sum);
     getch();
     }
output of sum of digits of a number using do while loop in C language


PRECAUTIONS

1. Use accurate header files.
2. Always use increment in do loop.
3. Use “;” after increment statement.
4. Define appropriate variables.
5. While loop will execute until some condition is fulfilled.
6. Program will run once if the initial condition is false.
7. In do while statements always use ‘;’ after while statement. 


No comments:

Post a Comment