Do while loop
Do while loop
#include<stdio.h>
#include<conio.h>
int main()
{
int n,r,s=0;
printf("Enter any number");
scanf("%d",&n);
while(n>0)
{
r= n%10;
s=s*10+r;
n=n/10;
}
printf("Revers=%d",s);
getch();
}
2. WAP to check if the given number is palindrome or not.
#include<stdio.h>
#include<conio.h>
int main()
{
int n,r,s=0,c;
printf("Enter any number");
scanf("%d",&n);
c=n;
while(n!=0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(c==s)
{
printf("Given number is pallindrome number");
}
else
{
printf("Given number is not pallindrome number");
}
getch();
}
3. WAP to find the whether a given number is Armstrong or not.
#include<stdio.h>
#include<conio.h>
int main()
{
int n,r,s=0,c;
printf("Enter any number");
scanf("%d",&n);
c=n;
while(n!=0)
{
r=n%10;
s=s + r*r*r;
n=n/10;
}
if(c==s)
{
printf("given number is Armstrong number");
}
else
{
printf("Given number is not Armstrong number");
}
getch();
}
Do while loop
This is a post-conditioned loop i.e. first the statements inside the loop are executed once and then the
condition is checked. If the condition is true, the process is repeated till the condition is true, otherwise
loop is terminated.
Syntax:
Initialization;
do
{
Statements;
.................
................
}
While(condition);
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
i =1;
do
{
printf("%d\t", i);
i=i+1;
}
while(i<=10);
getch();
}
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for (i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}
1
12
123
1234
12345
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for (i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
*
**
***
****
*****
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for (i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}