You are on page 1of 19

/* Program for finding factorial of an integer Number */

#include<stdio.h>;
#include<conio.h>;
main()
{
int i,y,num;
clrscr();
printf("Enter a integer Number: ");
scanf("%d",&num);
y=1;
for(i=1;i<=num;i++)
y=y*i;
printf("Factorial of %d is %d",num,y);
getch();
return 0;
}
1
1!

/* Calculate the series 1 + +

1
1
1
+ + +........ */
2! 3! 4!

#include<stdio.h>;
#include<conio.h>;
main()
{
int y,i,num;
float sum;
clrscr();
y=1;sum=1.0;
printf("Enter the Number of terms: ");
scanf("%d",&num);
for(i=1;i<num;i++)
{
y=y*i;
sum=sum+1.0/y;
}
printf("Sum of the first %d terms of the series is %f",num,sum);
getch();
return 0;
}
/* Calculate the series 1+1/1!+2/2!+3/3!+4/4!....... */
#include<stdio.h>;
#include<conio.h>;

main()
{
int y,i,num;
float sum;
clrscr();
y=1;sum=1.0;
printf("Enter the Number of terms: ");
scanf("%d",&num);
for(i=1;i<num;i++)
{
y=y*i;
sum=sum+(float)i/y;
}
printf("Sum of the first %d terms of the series is %f",num,sum);
getch();
return 0;
}
/* Program for finding factorial values from 1 to a given Number */
#include<stdio.h>;
#include<conio.h>;
main()
{
int i,j,num;
long int y;
clrscr();
printf("Enter a Number: ");
scanf("%d",&num);
printf("\n\nNumber Factorial");
printf("\n----------------");
for(i=1;i<=num;i++)
{
y=1;
for(j=1;j<=i;j++)
y=y*j;
printf("\n %d %ld\t",i,y);
}
getch();
return 0;
}
/* Find out largest from a set of integers */
#include<stdio.h>;
#include<conio.h>;
main()
{

int num,ref,i,n;
clrscr();
ref=1;
printf("\nHow many integers ? ");
scanf("%d",&n);
printf("\nEnter %d integer numbers: ",n);
for(i=1;i<=n;i++)
{
scanf("%d",&num);
if(num>ref)
ref=num;
}
printf("\nLargest from given %d integers is: %d ",n,ref);
getch();
return 0;
}
/* Find out Smallest from a set of integers */
#include<stdio.h>;
#include<conio.h>;
main()
{
int num,ref,i,n;
clrscr();
ref=1000;
printf("\nHow many integers ? ");
scanf("%d",&n);
printf("\nEnter %d integer numbers: ",n);
for(i=1;i<=n;i++)
{
scanf("%d",&num);
if(num<ref)
ref=num;
}
printf("\nSmallest from given %d integers is: %d ",n,ref);
getch();
return 0;
}
/* Arrange Numbers in Ascending Order */
#include<stdio.h>;
#include<conio.h>;
main()
{

int i,j,k,l,n,temp,num[100];
clrscr();
printf("\nHow many Numbers? ");
scanf("%d",&n);
printf("\nEnter %d Numbers ",n);
for(i=1;i<=n;i++)
scanf("%d",&num[i]);
for(j=1;j<=n-1;j++)
{
for(k=1;k<=n-j;k++)
{
if(num[k]>=num[k+1])
{
temp=num[k];
num[k]=num[k+1];
num[k+1]=temp;
}
}
}
printf("\nAscending Order of %d is:",n);
for(l=1;l<=n;l++)
printf("\t%d",num[l]);
getch();
return 0;
}
/* Arrange Numbers in Descending Order */
#include<stdio.h>;
#include<conio.h>;
main()
{
int i,j,k,l,n,temp,num[100];
clrscr();
printf("\nHow many Numbers? ");
scanf("%d",&n);
printf("\nEnter %d Numbers ",n);
for(i=1;i<=n;i++)
scanf("%d",&num[i]);
for(j=1;j<=n-1;j++)
{
for(k=1;k<=n-j;k++)
{
if(num[k]<=num[k+1])
{
temp=num[k];
num[k]=num[k+1];

num[k+1]=temp;
}
}
}
printf("\nDescending Order of %d is :",n);
for(l=1;l<=n;l++)
printf("\t%d",num[l]);
getch();
return 0;
}
/* Arrange Numbers in Descending Order then print the middle number*/
#include<stdio.h>;
#include<conio.h>;
main()
{
int i,j,k,l,n,temp,num[100];
clrscr();
printf("\nEnter 5 Numbers ");
for(i=1;i<=5;i++)
scanf("%d",&num[i]);
for(j=1;j<=4;j++)
{
for(k=1;k<=5-j;k++)
{
if(num[k]<=num[k+1])
{
temp=num[k];
num[k]=num[k+1];
num[k+1]=temp;
}
}
}
printf("\nDescending Order is :");
for(l=1;l<=5;l++)
printf("\t%d",num[l]);
printf("\nMiddle Number is: %d ",num[3]);
getch();
return 0;
}
/* Arrange Numbers in Ascending & Descending Order */
#include<stdio.h>;
#include<conio.h>;

main()
{
int i,j,k,l,n,temp,num[100];
clrscr();
printf("\nEnter 5 Numbers ");
for(i=1;i<=5;i++)
scanf("%d",&num[i]);
for(j=1;j<=4;j++)
{
for(k=1;k<=5-j;k++)
{
if(num[k]<=num[k+1])
{
temp=num[k];
num[k]=num[k+1];
num[k+1]=temp;
}
}
}
printf("\n\nAscending Order is :");
for(l=5;l>=1;l--)
printf("\t%d",num[l]);
printf("\n\nDescending Order is: ");
for(l=1;l<=5;l++)
printf("\t%d",num[l]);
getch();
return 0;
}
/* Find Average the smaller 5 values & bigger 5 values from 10 Numbers */
#include<stdio.h>;
#include<conio.h>;
main()
{
int i,j,k,l,n,temp,sum1=0,sum2=0,num[11];
float avg1,avg2;
clrscr();
printf("\nEnter 10 Numbers ",n);
for(i=1;i<=10;i++)
scanf("%d",&num[i]);
for(j=1;j<=9;j++)
{
for(k=1;k<=10-j;k++)
{
if(num[k]>=num[k+1])

{
temp=num[k];
num[k]=num[k+1];
num[k+1]=temp;
}
}
}
for(l=1;l<=5;l++)
sum1=sum1+num[l];
avg1=(float)sum1/5;
printf("\n\nAverage of smaller 5 Numbers is %f ",avg1);
for(l=6;l<=10;l++)
sum2=sum2+num[l];
avg2=(float)sum2/5;
printf("\n\nAverage of larger 5 Numbers is %f ",avg2);
getch();
return 0;
}
/* Find roots of an Quadratic Equation */
#include<stdio.h>;
#include<conio.h>;
#include<math.h>
main()
{
float a,b,c,d,root1,root2;
clrscr();
printf("Enter coefficients a, b & c respectively: ");
scanf("%f %f %f",&a,&b,&c);
d=b*b-4.0*a*c;
if(d==0)
{
root1=-b/2.0*a;
root2=root1;
printf("\nRoots are %.2f & %.2f",root1,root2);
}
else if(d>0)
{
root1=(-b+sqrt(d))/2.0*a;
root2=(-b-sqrt(d))/2.0*a;
printf("\nRoots are %.2f & %.2f",root1,root2);
}
else
printf("\nRoots are imaginary");
getch();
return 0;
}

/* Find the least difference numbers among a set of numbers */


#include<stdio.h>;
#include<conio.h>;
#include<math.h>
main()
{
int i,j,n,min,num[20],x,y,a[20][20];
clrscr();
printf("\nHow many Numbers? ");
scanf("%d",&n);
printf("\nEnter %d Numbers",n);
for(i=1;i<=n;i++)
scanf("%d",&num[i]);
min=100;
for(i=1;i<=n-1;i++)
{
for(j=i+1;j<=n;j++)
{
a[i][j]=abs(num[i]-num[j]);
if(min>a[i][j])
{
min=a[i][j];
x=i;y=j;
}
}
}
printf("\n\nLeast Difference Numbers are %d & %d",num[x],num[y]);
printf("\n\nTheir difference is %d",a[x][y]);
getch();
return 0;
}
/* Test a Number as Prime */
#include<stdio.h>;
#include<conio.h>;
main()
{
int num,test,p,d;
clrscr();
printf("\n\nEnter a Number: ");
scanf("%d",&num);
test=1;
for(p=2;p<=num/2;p++)
{
d=num%p;

if(d==0)
test = 0;
}
if(test==1)
printf("\nNumber %d is Prime",num);
else
printf("\nNumber %d is not prime",num);
getch();
return 0;
}
/* Test a number either prime Number or not */
#include<stdio.h>;
#include<conio.h>;
main()
{
int num,i,x,p;
clrscr();
printf("Enter a Number: ");
scanf("%d",&num);
again1:
x=2;
p=num%x;
if(p==0)
goto again2;
continue;
else
x++;
if(num>x)
goto again1;
else
again2:
printf("\n\nNumber is not Prime");
getch();
return 0;
}
/* Print the prime Numbers berween 1 & to a certain Number */
#include<stdio.h>;
#include<conio.h>;
main()
{
int n,num,test,p,d;
clrscr();
printf("\n\nEnter a Number: ");

scanf("%d",&num);
for(n=1;n<=num;n++)
{
test = 1;
for(p=2;p<=n/2;p++)
{
d=n%p;
if(d==0)
test = 0;
}
if(test==1)
printf("\n\nNumber %d is Prime",n);
else
printf("\n\nNumber %d is not prime",n);
}
getch();
return 0;
}
/* Program which can calculate average of a set of numbers */
#include<stdio.h>;
#include<conio.h>;
main()
{
int num,i;
float sum=0.0,avg,n[100];
clrscr();
printf("\nHow many Numbers? ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
scanf("%f",&n[i]);
sum=sum+n[i];
avg=sum/num;
}
printf("\n\nAverage of %d Number is %.2f",num,avg);
getch();
return 0;
}
/* Programs for find out even numbers from 10 positive integer and then calculate the
averages of even and odd numbers */
#include<stdio.h>;
#include<conio.h>;
main()
{

10

int i,num[11],even,count = 0,count1 = 0,sum = 0,sum1 = 0;


float avg,avg1;
clrscr();
printf("\n\nEnter 10 Real Numbers: ");
for(i=1;i<=10;i++)
scanf("%d",&num[i]);
printf("\n\nEven Numbers are: ");
for(i=1;i<=10;i++)
{
even=num[i]%2;
if(even == 0)
{
count++;
sum = sum + num[i];
printf("%d\t",num[i]);
}
else
{
count1++;
sum1 = sum1 + num[i];
}
}
avg=(float)sum/count;
printf("\n\nAverage of the total %d even Numbers is %.2f",count,avg);
avg1=(float)sum1/count1;
printf("\n\nAverage of the total %d odd Numbers is %.2f",count1,avg1);
getch();
return 0;
}
/* Find out the middle entry from some input numbers */
#include<stdio.h>;
#include<conio.h>;
main()
{
int i,num,n[100];
clrscr();
printf("\n\nHow many Numbers: ");
scanf("%d",&num);
printf("\n\nEnter the %d Numbers: ",num);
for(i=1;i<=num;i++)
scanf("%d",&n[i]);
if((num%2) == 1)
printf("\n\nMiddle Entry is %d",n[(num/2)+1]);
else
printf("\n\nMiddle Entries are: %d and %d",n[num/2],n[(num/2)+1]);
getch();

11

return 0;
}
/* Program covert days into month and day */
#include<stdio.h>;
#include<conio.h>;
main()
{
int days,day,month;
clrscr();
printf("\n\nEnter days : ");
scanf("%d",&days);
month = days/30;
day = days%30;
switch(month)
{
case 1:
printf("\nMonth is January");
break;
case 2:
printf("\nMonth is February");
break;
case 3:
printf("\nMonth is March");
break;
case 4:
printf("\nMonth is Aprill");
break;
case 5:
printf("\nMonth is May");
break;
case 6:
printf("\nMonth is June");
break;
case 7:
printf("\nMonth is July");
break;
case 8:
printf("\nMonth is August");
break;
case 9:
printf("\nMonth is September");
break;
case 10:
printf("\nMonth is October");
break;
case 11:

12

printf("\nMonth is November");
break;
default:
printf("\nMonth is December");
}
printf(" and Day is %d",day);
getch();
return 0;
}
Sum the series:

3b 2 5b 4 7b 6
+
+
+ ........... + n th .
2c
5c
8c

#include<stdio.h>;
#include<conio.h>;
main()
{

/* Not fully solved */

clrscr();
printf("\n\nEnter the value of b and c");
scanf("%d %d",&b,&c);
for(i=2;i<=)
{
denominator=(i+1)*(b**i);
numerator=i*c;
sum=sum+denominator/numerator;
}
getch();
return 0;
}
Find the position of the corresponding color in the boxes:
1st Position 2nd Position 3rd Position
Red
Blue
Pink
#include<stdio.h>;
#include<conio.h>;
main()
{
char color;
clrscr();
color = getchar();
switch(color)
{
case 'R':
printf("\n\n1st position for Red");
break;

13

case 'B':
printf("\n\n2nd position for Blue");
break;
default :
printf("\n\n3rd position for Pink");
break;
}
getch();
return 0;
}
/* Multiplication of Real Numbers using function */
#include<stdio.h>;
#include<conio.h>;
mul(int x,int y);
main()
{
int a,b,c;
clrscr();
printf("\n\nEnter two integer : ");
scanf("%d %d",&a,&b);
c = mul(a, b);
printf("\n\nMultiplication of %d and %d is %d",a,b,c);
getch();
return 0;
}
mul(int x,int y)
{
int p;
p=x*y;
return(p);
}
/* Addition, Subtraction, Multiplication and Division of Real Numbers using User defined
function and switch statement */
#include<stdio.h>;
#include<conio.h>;
add(int x,int y);
sub(int x,int y);
mul(int x,int y);
div(int x,int y);

14

main()
{
int a,b;
float c;
char character;
clrscr();
printf("Enter\n A for Addition \n S for Subtraction");
printf("\n M for Multiplication \nand any key for Division : ");
character = getchar();
printf("\n\nEnter two integer : ");
scanf("%d %d",&a,&b);
switch(character)
{
case 'A':
{
c = add(a,b);
printf("\n\nAddition of %d and %d is %.2f",a,b,c);
break;
}
case 'S':
{
c = sub(a,b);
printf("\n\nSubtraction of %d and %d is %.2f",a,b,c);
break;
}
case 'M':
{
c = mul(a,b);
printf("\n\nMultiplication of %d and %d is %.2f",a,b,c);
break;
}
default :
{
c = div(a,b);
printf("\n\nDivision of %d and %d is %.2f",a,b,c);
break;
}
}
getch();
return 0;
}
add(int x,int y)
{
int p;
p=x+y;
return(p);

15

}
sub(int x,int y)
{
int p;
p=x-y;
return(p);
}
mul(int x,int y)
{
int p;
p=x*y;
return(p);
}
div(int x,int y)
{
int p;
p=x/y;
return(p);
}
/* Sum up diagonally of a square matrix */
#include<stdio.h>;
#include<conio.h>;
main()
{
int i,j,size,m[5][5],sum = 0,sum1 = 0;
clrscr();
printf("\n\nEnter size of square matrix : ");
scanf("%d",&size);
printf("\n\nEnter elements of the matrix : ");
for(i=1;i<=size;i++)
{
for(j=1;j<=size;j++)
scanf("%d",&m[i][j]);
}
printf("\n\nEntered Matrix is :\n\n");
for(i=1;i<=size;i++)
{
for(j=1;j<=size;j++)
printf("% d",m[i][j]);
printf("\n\n");
}
for(i=1;i<=size;i++)
{
for(j=1;j<=size;j++)
if(i == j)

16

sum=sum + m[i][j];
}
printf("\n\nSum of the 1st diagonal elements = %d",sum);
for(i=1;i<=size;i++)
{
for(j=1;j<=size;j++)
if( i+j-1 == size)
sum1=sum1 + m[i][j];
}
printf("\n\nSum of the 2nd diagonal elements = %d",sum1);
getch();
return 0;
}
/* Sum up any numbers that are in any row or column */
#include<stdio.h>;
#include<conio.h>;
main()
{
int i,j,size,m[5][5],sum,r,c;
char ch;
clrscr();
printf("\n\nPress R for Row addition \n\nand any key for Column addition : ");
ch = getchar();
printf("\n\nEnter size of square matrix : ");
scanf("%d",&size);
printf("\n\nEnter elements of the matrix : ");
for(i=1;i<=size;i++)
{
for(j=1;j<=size;j++)
scanf("%d",&m[i][j]);
}
printf("\n\nEntered Matrix is :\n\n");
for(i=1;i<=size;i++)
{
for(j=1;j<=size;j++)
printf("% d",m[i][j]);
printf("\n\n");
}
switch (ch)
{
case 'R':
{
sum = 0;

17

printf("\n\nWhat row? ");


scanf("%d",&r);
for(j=1;j<=size;j++)
sum=sum + m[r][j];
printf("\n\nSum of %d elements = %d",r,sum);
break;
}
default :
{
sum = 0;
printf("\n\nWhat column? ");
scanf("%d",&c);
for(i=1;i<=size;i++)
sum=sum + m[i][c];
printf("\n\nSum of %d elements = %d",c,sum);
break;
}
}
getch();
return 0;
}
// Program which reads ur full name and then print it with uppercase letter for each word
#include<stdio.h>;
#include<conio.h>;
#include<ctype.h>
main()
{
// \0 indicates termination of string
int count;
// != indicates not equal to
char name[25];
// gets and puts are two statements for scanning and printing a name
//clrscr();
printf("\n Your Name ?? ");
gets(name);
name[0] = toupper(name[0]);
count=1;
while(name[count] != '\0')
{
if(name[count-1] == ' '|| name[count-1] =='-'|| name[count-1] =='.')
name[count] = toupper(name[count]);
else name[count]=tolower(name[count]);
count=count+1;
}
puts(name);
//printf("\n Hello %s",name);
getch();
return 0;
}
// Program which scan and print name

18

#include<stdio.h>;
#include<conio.h>;
#include<ctype.h>
main()
{
char name[25];
//clrscr();
printf("\n\n Your Name ?? ");
scanf("%s",name);
// Here & sign should not given
name[0] = toupper(name[0]);
printf("\n\n Hello Mr. %s",name);
getch();
return 0;
}

19

You might also like