While Loop in C language
While loop
While loop is also known as Entry Control loop because condition is checked at the entry of loop.
syntax
while(condition)
{
statement;
Increment/decrement;
}
Program1-Write a program to print 0to 10 number using while loop
code-
#include<stdio.h>
void main()
{
int i=0,number=10;
while(i<=10)
{
printf("%d\n",i);
i++;
}
}
Output
0
1
2
3
4
5
6
7
8
9
10
---------------------------------------------------------------------------------
program2-write a program to find out the factorial of number using while loop.
code-
#include<stdio.h>
void main()
{
int fact=1,i=1,number;
printf("Enter a number to find factorial:");
scanf("%d",&number);
while(i<=number)
{
fact=fact*i;
i++;
}
printf("The factorial of %d is %d",number,fact);
output-
Enter a number to find factorial:5
The factorial of 5 is 120
--------------------------------------------------------------------------------------------
program3-write a program to find sum of digit any number using while loop.
code-
#include<stdio.h>
void main()
{
int sum=0,digit,no;
printf("Enter a number");
scanf("%d",&no);
while(no!=0)
{
digit=no %10;
sum=sum+digit;
no=no/10;
}
printf("The sum of digit of number is: %d",sum);
}
output- Enter a number8963
The sum of digit of number is: 26
---------------------------------------------------------------------------------------------------------
program4-write a program to find reverse of input number using while loop
code-
#include<stdio.h>
void main()
{
int rev=0,digit,no;
printf("Enter a number to find the Reverse");
scanf("%d",&no);
while(no!=0)
{
digit=no %10;
rev=rev*10+digit;
no=no/10;
}
printf("Reverse of the number is %d",rev );
}
output-
Enter a number to find the Reverse 4596
Reverse of the number is 6954
---------------------------------------------------------------------------------------------------------
program5-write a program to find input number is palindrome number or not using while loop
code-
#include<stdio.h>
void main()
{
int rev=0,digit,no,copy;
printf("Enter a number ");
scanf("%d",&no);
copy=no;
while(no!=0)
{ digit=no %10;
rev=rev*10+digit;
no=no/10;
}
if(rev==copy)
{printf("Enter number is the Palindrome");
}
else
printf("Enter number is not Palindrome ");
}
output-
Enter a number 12355321
Enter number is the Palindrome
---------------------------------------------------------------------------------------------
Comments
Post a Comment