Professional Documents
Culture Documents
#include<stdio.h>
#include<conio.h>
void main( )
{
float r,a,c;
float PI = 3.14;
clrscr();
printf("Enter the radius of the circle\n");
scanf(“%f”,&r);
a = PI * r * r;
c = 2 * PI * r;
printf("Area = %f \n",a);
printf("Ciecumference = %f \n",c);
getch();
}
INPUT
Enter the radius of circle
5
OUTPUT
a = 392.5
c = 31.4
2. Program to find the largest, smallest and Second Largest of three
numbers
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c;
int l,s,sl;
clrscr();
printf("Enter three numbers\n");
scanf(“%d%d%d”,&a,&b,&c);
l = a;
if ( b > l ) l = b;
if ( c > l ) l = c;
printf("Largest = %d \n", l);
s = a;
if ( b < s ) s = b;
if ( c < s ) s = c;
printf("Smallest = %d \n", s);
sl = ( a + b + c) – ( l + s);
printf("Second Largest = %d \n", sl);
getch();
}
INPUT
Enter three numbers
24 44 67
OUTPUT
Largest = 67
Smallest = 24
Second largest = 44
3. Program to check whether a year is a leap year
#include<stdio.h>
#include<conio.h>
void main( )
{
int y ,r ;
clrscr();
printf("Enter any year \n");
scanf(“%d”,&y);
r = y % 4;
if ( r == 0 )
printf(" %d is a Leap Year \n", y);
else
printf(" %d is not a Leap Year \n", y);
getch();
}
INPUT
Enter any year
2012
OUTPUT
2012 is a Leap Year
4. Program to enter the marks of four subjects, calculate the total
percentage and output the result
#include<stdio.h>
#include<conio.h>
void main( )
{
int m1,m2,m3,m4, total;
float per;
clrscr();
printf("Enter marks of four subjects\n");
scanf(“%d%d%d%d”,&m1,&m2,&m3,&m4);
total = m1+m2+m3+m4;
per = (float)total / 4;
if ( per >= 60 )
printf(" First Class \n");
else if ( per >= 50 )
printf(" Second Class \n");
else if ( per >= 40 )
printf(" Pass Class \n");
else
printf(" Fails \n");
getch();
}
INPUT
Enter marks of four subjects
50 60 65 70
OUTPUT
First class
5. Program to find the sum of all numbers from 1 to N
#include <stdio.h>
#include <conio.h>
void main()
{
int n, sum, I, r;
i =1;
sum=0;
while(i < =n)
{
sum=sum + i;
++i;
}
getch();
}
INPUT
Enter the limit
5
OUTPUT
Sum = 15
6. Program to find the sum of all digits of a Number
#include <stdio.h>
#include <conio.h>
void main()
{
int num, n, sum, r;
num=n;
sum=0;
while(n !=0)
{
r = n % 10;
sum = sum + r;
n = n/10;
}
getch();
}
INPUT
Enter a number
12
OUTPUT
Sum of all digits of 12 = 3
7. Program to reverse Number
#include <stdio.h>
#include <conio.h>
void main()
{
int num,n,rev,r;
num=n;
rev=0;
while(n !=0)
{
r = n % 10;
rev = rev * 10 + r;
n = n/10;
}
INPUT
Enter a number
23
OUTPUT
Reverse of number 23 is 32
8. Program to find XN using for statement
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int i, n;
float x, p;
p =1;
for( i=1; i<=abs(n); i++)
p = p*x;
if ( n>0)
printf("%f to the power of %d = %f \n", x, n, p);
else
printf("%f to the power of %d = %f \n", x, n, 1/p);
getch();
}
INPUT
Enter X and N
2 5
OUTPUT
2 to the power of 5 = 32
9. Program to calculate Julian date
#include <stdio.h>
#include <conio.h>
void main( )
{
int dd, mm, yy, jd;
clrscr();
jd = dd;
switch(mm-1)
{
case 11 : jd = jd +30;
case 10 : jd = jd +31;
case 9 : jd = jd +30;
case 8 : jd = jd +31;
case 7 : jd = jd +31;
case 6 : jd = jd +30;
case 5 : jd = jd +31;
case 4 : jd = jd +30;
case 3 : jd = jd +31;
case 2 : if(yy%4==0)
jd = jd + 29;
else
jd = jd + 28;
case 1 : jd = jd + 31;
}
getch () ;
}
INPUT
Enter date as dd,mm,yy
01 03 2013
OUTPUT
Julian Date = 60
10. Program to find factorial of a number
#include <stdio.h>
#include <conio.h>
void main()
{
int i, n, fact;
fact =1;
for( i=1; i<=n ; i++)
fact = fact * i;
getch();
}
INPUT
Input a number
5
OUTPUT
Factorial of 5 = 120
11. Program to generate the pattern 1
1 2
1 2 3
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j, n;
getch();
}
INPUT
Enter the number of rows
4
OUTPUT
1
1 2
1 2 3
1 2 3 4
12. Program to Find the sum of the series 1/x + 1/x2 + ..... + 1/xn
#include <stdio.h>
#include <conio.h>
void main()
{
float t, x, sum;
int i,n;
t=x;
for(i=1; i <=n ;i++)
{
sum=sum + 1/t;
t=t*x;
}
INPUT
Input number of terms in series
2
Enter the value of x
2
OUTPUT
Sum value = 0.750
13. Program to implement Linear Search
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],ele,pos,n,i;
clrscr();
printf("How many elements u wish to enter\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
printf("Enter element to search\n");
scanf("%d",&ele);
pos= -1;
for(i=0; i<n; i++)
if(a[i]==ele) pos=i;
if(pos==-1)
printf("Element not present\n");
else
printf("Element found in position %d\n",pos);
getch();
}
INPUT
How many elements u wish to enter
5
Enter the elements
21 31 23 45 67
Enter the element to search
45
OUTPUT
Element found in position 3
14. Program to implement Binary Search
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],ele,pos,n,i;
int low,high,mid;
clrscr();
printf("How many elements u wish to enter\n");
scanf("%d",&n);
printf("Enter the elements in order\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
printf("Enter element to search\n");
scanf("%d",&ele);
low=0;
high=n-1;
pos = -1;
while(low<=high)
{
mid=(low+high)/2;
if(ele == a[mid])
{ pos = mid;
break;
}
else if(ele < a[mid])
high=mid-1;
else
low=mid+1;
}
if(pos==-1)
printf("Element not present\n");
else
printf("Element found in position %d\n",pos);
getch();
}
INPUT
How many elements u wish to enter
5
Enter the elements in order
25 35 41 56 60
Enter the element to search
56
OUTPUT
Element found in position 3
15. Program to implement Bubble Sort
#include <stdio.h>
#include <conio.h>
void main()
{
int a[100], i, j, n, temp;
printf("\nUnSorted list\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\nSorted list\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
INPUT
Enter number of elements in array
4
Enter array elements
12 80 23 41
OUTPUT
Unsorted list
12 80 23 41
Sorted list
12 16 23 41
16. Program to add two matrices
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int n,m,i,j;
clrscr();
printf("Enter order of the matrix \n");
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
for(j=0;j<m ;j++)
c[i][j]=a[i][j]+b[i][j];
INPUT
Enter order of the matrix
2 2
Enter fist matrix elements
1234
Enter second matrix elements
5678
OUTPUT
Sum matrix
6 8
10 12
17. Program to multiply two matrices
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,k,n1,m1,n2,m2;
clrscr();
printf("Enter order of the first matrix \n");
scanf("%d%d",&m1,&n1);
printf("Enter order of the second matrix \n");
scanf("%d%d",&m2,&n2);
if(n1!=m2)
printf("Cannot find the product!!!!!!!!!! \n");
else
{
printf("Enter first matrix elements\n");
for(i=0;i<m1;i++)
for(j=0;j<n1;j++)
scanf("%d",&a[i][j]);
printf("Enter Second matrix elements\n");
for(i=0;i<m2;i++)
for(j=0;j<n2;j++)
scanf("%d",&b[i][j]);
INPUT
Enter order of the first matrix
2 3
Enter order of the second matrix
3 2
Enter first matrix elements
123456
Enter second matrix elements
654321
OUTPUT
Product matrix is
20 14
56 41
18. Program to transpose a matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10], b[10][10];
int i,j,m,n;
clrscr();
printf("Enter the order of the matrix \n");
scanf(“%d%d”,&m,&n);
printf("Enter matrix elements\n");
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d",&a[i][j]);
getch();
}
INPUT
Enter the order of the matrix
2 3
Enter matrix elements
1 2 3 4 5 6
OUTPUT
Input matrix
1 2 3
4 5 6
Transposed matrix
1 4
2 5
3 6
19. Program to find GCD of two numbers using functions
#include <stdio.h>
#include <conio.h>
int GCD(int,int);
void main()
{
int n1,n2;
clrscr();
printf("Input two numbers \n");
scanf("%d%d",&n1,&n2);
printf("GCD of %d and %d is %d \n",n1,n2,GCD(n1,n2));
getch();
}
INPUT
Input two numbers
45 54
OUTPUT
GCD of 45 and 54 is 9
20. Program to find the factorial of a number using recursion
#include <stdio.h>
#include <conio.h>
long FACT(int);
void main()
{
int n;
clrscr();
printf("Input a non-negative number \n");
scanf("%d",&n);
if(n<0)
printf("Invlid number!!!!!!!! \n");
else
printf("Factorial of %d is %ld \n",n,FACT(n));
getch();
}
/* Recursive function*/
long FACT(int m)
{
if(m==1)
return(1);
else
return(m * FACT(m-1));
}
INPUT
Input a non-negative number
6
OUTPUT
Factorial of 6 is 720