## Programs Using For, While and Do...While Loops

Program to find factorial of given number
#include stdio.h
#include conio.h
void main()
{
int i,n;
long int fact=1;
clrscr();
printf("Enter Value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("Factorial of %d is %ld",n,fact);
getch();
}

Output

Enter Value of N
4
Factorial of 4 is 24
Program to generate fibonaci searise upto desired terms.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,n,i;
clrscr();
printf("Enter Value of First term:");
scanf("%d",&a);
printf("Enter Value of Second term:");
scanf("%d",&b);
printf("Enter How many terms you want?");
scanf("%d",&n);
printf("%d %d ",a,b);
for(i=1;i<=n-2;i++)
{
c=a+b;
printf(" %d ",c);
a=b;
b=c;
}
getch();
}

Output

Enter Value of First Term:0
Enter Value of Second Term:1
Enter How many terms you want:5
0  1  1  2  3
Program to find weather given number is prime or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,count=0;
clrscr();
printf("Enter Number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
printf("Number is Prime");
else
printf("Number is not Prime");
getch();
}

Output

Enter Number: 3
Number is Prime
Program to find sum upto given number starting from 1.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr();
printf("Enter Up to how many number you want sum:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("Sum=%d",sum);
getch();
}

Output

Enter Up to how many number you want sum: 5
Sum=15
Program to display following triangle:
1
22
333
4444
55555
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(" %d ",i);
}
printf("\n");
}
getch();
}
Program to display following triangle:
1
12
123
1234
12345
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(" %d ",j);
}
printf("\n");
}
getch();
}
Program to display following triangle:

3   5
7   9   11
13 15 17 19
21 23 25 27 29
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k=1;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(" %d ",k);
k=k+2
}
printf("\n");
}
getch();
}
Program to display multiplication table
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=10;i++)
{
for(j=1;j<=10;j++)
{
printf("%4d",i*j);
}
printf("\n");
}
getch();
}

Output

 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 8 12 16 20 24 28 32 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100

Program to find power of given number
#include<stdio.h>
#include<conio.h>
void main()
{
int x,n,power=1,i;
clrscr();
printf("Enter Number:");
scanf("%d",&n);
printf("Enter Power:");
scanf("%d",&x);
for(i=1;i<=x;i++)
{
power=power*n;
}
printf("Power=%d",power);
getch();
}

Output

Enter Number
4
Enter Power
2
Power=16
Program to count total number of odd digits in a given number and find sum of them.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,d,oddcount=0,oddsum=0;
clrscr();
printf("Enter Number:");
scanf("%d",&n);
while(n>0)
{
d=n%10;
if(d%2!=0)
{
oddcount++;
oddsum=oddsum+d;
}
n=n/10;
}
printf("Total Odd Digits in Number is %d\n",oddcount);
printf("Sum of Odd Digits in Number is %d\n",oddsum);
getch();
}

Output

Enter Number: 123
Total Odd Digits in Number is 2
Sum of Odd Digits in Number is 4
Program to find reverse of given number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, d;
clrscr();
printf("Enter Number:");
scanf("%d",&n);
while(n>0)
{
d=n%10;
printf("%d",d);
n=n/10;
}
getch();
}

Output

Enter Number:
123
321
Program to find weather given number is perfect or not. Hint: sum of digits = multiplication of digits
#include<stdio.h>
#include<conio.h>
void main()
{
int n,d,sum=0,mul=1;
clrscr();
printf("Enter Number:");
scanf("%d",&n);
while(n>0)
{
d=n%10;
sum=sum+d;
mul=mul*d;
n=n/10;
}
printf("Sum=%d\n",sum);
printf("Multiplication=%d\n",mul);
if(sum==mul)
printf("Perfect number");
else
printf("Not perfect Number");
getch();
}

Output

Enter Number: 123
sum = 6 (1 + 2 + 3 = 6)
Multiplication = 6 (1 * 2 * 3 = 6)
Perfect Number
Program to find weather given number is Armstrong or not. Hint: 153 = 13 + 53 + 33 = 1 + 125 + 27 = 153
#include<stdio.h>
#include<conio.h>
void main()
{
int n,d,sum=0,temp;
clrscr();
printf("Enter Number:");
scanf("%d",&n);
temp=n;
while(n>0)
{
d=n%10;
sum=sum+(d*d*d);
n=n/10;
}
if(sum==temp)
printf("Armstrong number");
else
printf("Not Armstrongr Number");
getch();
}

Output

Enter Number: 153
Armstrong Number
Program to find maximum digit from given number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,d,max=0;
clrscr();
printf("Enter Number:");
scanf("%d",&n);
while(n>0)
{
d=n%10;
if(d>max)
max=d;
n=n/10;
}
printf("maximum digit in given number is %d",max);
getch();
}

Output

Enter Number: 498
Maximum digit in given number is 9
Program to find odd number between 1 to 50.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=50;i++)
{
if(i%2!=0)
printf("%d ",i);
else
continue;
}
getch();
}

Output

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
Program to Display number that is divisible by 5 and 7 between 1 to 100
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=100;i++)
{
if(i%5==0 && i%7==0)
printf("%d ",i);
}
getch();
}

Output

35 70