1) Write a program to print 1 to 10 numbers using while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
clrscr();
while(a<=10)
{
printf("%d\n",a);
a++;
}
getch();
}
2) Write a program to read any 7 numbers and print the average value using for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,i,b,sum=0;
clrscr();
printf("Enter 7 number:");
for(i=0;i<7;i++)
{
scanf("%d",&a);
sum=sum+a;
}
b=sum/7;
printf("Avg=%d",b);
getch();
}
3) Write a program to reverse a given integer number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,rem,rev=0;
clrscr();
printf("Enter the no:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
rem=n%10;
rev=(rev*10)+rem;
n=n/10;
}
printf("%d",rev);
getch();
}
4) Write a program to print Fibonacci series of given number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n1=0,n2=1,n3,i,num;
clrscr();
printf("Enter the number of elements:");
scanf("%d",&num);
printf("\n%d %d",n1,n2); //printing 0 and 1
for(i=2;i<num;++i) //loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
getch();
}
5) Write a program to find factorial of a number.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,fac=1;
clrscr();
printf("Enter n:");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
fac=fac*i;
}
printf("%d",fac);
getch();
}
6) Write a program to check whether a number is a Krishnamurti number or not. Krishnamurti number is one whose sum of factorial of digits equals the number. Example: 145 1! + 4! + 5! = 1 + 24 + 120 = 145
#include<stdio.h>
#include<conio.h>
void main()
{
int i,x,n,sum=0,fac,rem;
clrscr();
printf("Enter a Number:");
scanf("%d",&n);
x=n;
do{
rem=n%10;
fac=1;
for(i=1;i<=rem;i++)
{
fac=fac*i;
}
sum=sum+fac;
n=n/10;
}
while(n>0);{
if(sum==x)
printf("%d is Krishnamurthy number",x);
else
printf("%d is Not Krishnamurthy number",x);
}
getch();
}
7) Write a program to check whether the number is Armstrong or not. Example:153—-1 3 + 5 3 +3 3 = 1 + 125 + 27 153
#include<stdio.h>
#include<conio.h>
void main()
{
int i,x,n,sum=0,f=1,rem;
clrscr();
printf("Enter a Number:");
scanf("%d",&n);
x=n;
do{
rem=n%10;
for(i=1;i<=n;i++)
{
f=rem*rem*rem;
}
sum=sum+f;
n=n/10;
}
while(n>0);{
if(x==sum)
printf("%d is Armongstrong number",x);
else
printf("%d is Not Armongstrong number",x);
}
getch();
}
8) Write a program to list all prime numbers within given range.
#include<stdio.h>
#include<conio.h>
void main()
{
int end,start,i,n;
clrscr();
printf("Enter Range Number : ");
scanf("%d%d",&start,&end);
printf("Prime Number between %d & %d are:",start,end);
for(n=start;n<=end;n++)
{
int prime=1;
for(i=2;i*i<=n;i++)
{
if(n%i==0){
prime=0;
break;
}
}
if(prime && n>1){
printf("\n%d",n);
}
}
getch();
}
9) Write a program to draw following patterns :
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("enter the rows:");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m;
char n;
clrscr();
for(i=1;i<=5;i++)
{
if(i%2==0)
{
n='a';
for(j=i;j>0;j--)
{
printf("%c ",n);
n++;
}
}
else
{
m=1;
for(j=i;j>0;j--)
{
printf("%d ",m);
m++;
}
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=5;i>=1;i--)
{
for(k=i;k<=5;k++)
{
printf(" ");
}
for(j=i;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=1;i<=5;i++)
{
for(k=i;k<=5;k++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%c",j+64);
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=1;i<=4;i++)
{
for(k=i;k<=5;k++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d",j);
}
for(j=i-1;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
if(i==j||(i%2==0&&j%2==0)||(i%2!=0&&j%2!=0))
printf("1");
else
printf("0");
}
printf("\n");
}
getch();
}