C Programming
While Loop in C
While loop is useful when we run the particular statement number of time on the basis of certain condition. In while loop we don’t know exact no. of repetition.
The General Format is:
while(condition)
{
//body to repeat;
}
Example:
Q. A c program to reverse a no.
Sol.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,temp,rev;
rev=0;
printf(“Enter a no.”);
scanf(“%d”, &num);
while(num)
{
temp=num%10;
rev=(rev*10)+temp;
num=num/10;
}
printf(“Reverse=%d”, rev);
getch();
}
Input: 254
Output: Reverse=452