Tuesday, 24 December 2013

Program of while loop in “c”

        
Program of while loop in “c”
·       /* to print number from 1 to 5*/
#include<stdio.h>
main()
{
int i;
i=1;
while(i<=5)
{
printf(“%d”,i);
++i;
}
}

·       /* To print even number less than 100 */
#include<stdio.h>
main()
{
int  i=2;
while(i<100)
{
printf(“%d”,i);
i=i+2;
}
}

·       /* To print the sum of digit */
#include<stdio.h>
main()
{
int num,r,s=0;
printf(“enter the digit”);
scanf(“%d”,&num);
while(num>0)
{
r = num%10;
s=s+r;
num=num/10;
}
printf(“sum=%d”,s);
}


·       /* to print  the reverse from of a given number */
#include<stdio.h>
main()
{
int num,r,rev=0;
printf(“enter the number”);
scanf(“%d”,&num);
while(num>0)
{
r=num%10;
rev=rev*10+r;
num=num/10;
}
printf(“reverse num=%d”,rev);
}

·       /* to check the number is pallidrone or not*/
#include<stdio.h>
#include<conio.h>
main()
{
int num,r,rev=0,t;
clrscr();
printf(“enter the number”);
scanf(“%d”,&num);
t=num;
while(num>0)
{
r= num%10;
rev=rev*10+r;
num=num/10;
}
if(t==rev)
printf(“%d is pallidrone”,t);
else
printf(“%d is not pallidrone”,t);
}


·       /*To check the number is armstrong or not */
#include<stdio.h>
main()
{
int num,r,s=0,t;
printf(“enter the number”);
scanf(“%d”,&num);
t=num;
while(num>0)
{
r= num%10;
s=s+r*r*r;
num=num/10;
}
if(t==s)
printf(“%d is armstrong”,t);
else
printf(“%d is not armstrong”,t);

No comments:

Post a Comment