while loop


A while loop is a control statement using which the programmer can give instructions to the computer to execute a set of statement repeatedly as long as specified condition is satisfied. Ones the specified condition is false control comes out of the loop.

Syntax:-

Initialization
while(condition)
 {
  Statement…
  updating
 }

Flowchart:-


while loop

W.A.P to print 1 to 5.

while loop program

Output

1
2
3
4
5

W.A.P to enter a no. and print sum of each digit.

 #include<stdio.h>
 #include<conio.h>
  void main()
  {
   int n,s=0,a;
   printf("enter no");
   scanf("%d",&n);
   while(n!=0)
  {
   a=n%10;
   n=n/10;
   s=s+a;
  }
   printf("%d",s);
   getch();
 }

Output

enter no
1234
10

Enter a no and print reverse order

 #include<stdio.h>
 #include<conio.h>
  void main()
 {
   int n,rev=0,a,n;
   printf("enter no");
   scanf("%d",&n);
   while(n!=0)
  {
   a=n%10;
   n=n/10;
   rev=rev*10+a;
  }
   printf("%d",rev);
   getch();
  }

Output

enter no
321
123

W.A.P to check palindrome no or not.

 #include<stdio.h>
 #include<conio.h>
   void main()
 {
   int rev=0,a,n,t;
   printf("enter no");
   scanf("%d",&n);
   t=n while(n!=0)
  {
   a=n%10;
   n=n/10;
   rev=rev*10+a;
  }
  if(rev==t)
  {
   printf("palindrome");
  }
  else
  {
   printf("not a palindrome");
  }
   getch();
  }

Output

enter no
121
palindrome