Control statement
While loop
While loop first checks whether the initial condition is true or false and finding it to be true, it will enter the loop and
execute the statement.
Syntax
Initialization
While(condition)
{
Statement 1;
…………………..;
Statement n;
}
1. Write a C program to find the first 10 natural number using while loop.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num=1;
while(num<=10)
{
printf("%d\t",num);
num++;
}
return 0;
}
2. Write a C program to checks whether the entered number by user is prime or composite using while loop.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,num;
printf("Enter the number:\n");
scanf("%d",&num);
i=2;
while(i<=num-1)
{
if(num%i==0)
{
printf("%d is a composite number",num);
break;
}
i++;
}
if(i==num)
printf("%d is a prime number",num);
return 0;
}
3. Write a C program to check whether the entered number by the user is palindrome or not.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, rev=0,digit,num;
printf("Enter a number:\n");
scanf("%d",&n);
num=n;
while(n!=0)
{
digit=n%10;
rev=rev*10+digit;
n=n/10;
}
if(rev==num)
printf("It is a palindrome number",num);
else
printf("It is not a palindrome number",num);
return 0;
}
4. Write a C program to check whether the entered number by user is Armstrong or not.
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
int n, sum=0,a,r;
printf("Enter a number:\n");
scanf("%d",&n);
a=n;
while(n!=0)
{
r=n%10;
sum=sum+pow(r,3);
n=n/10;
}
if(sum==a)
printf("%d is an armstrong number",a);
else
printf("%d is an armstrong number",a);
return 0;
}