You are on page 1of 29

SUM OF TWO NUMBERS

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c=0;
clrscr();
printf("Enter the values of a and b\n");
scanf("%d %d",&a,&b);
c=a+b;
printf("Addition of two numbers=%d",c);
getch();
}

Output:
Enter the values of a and b
5
9
Addition of two numbers=14
GREATEST AMONG THREE NUMBERS

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the values of a,b,c:\n");
scanf("%d %d %d",&a,&b,&c);
if((a>b)&&(a>c))
printf("%d is greatest",a);
else if(b>c)
printf("%d is greatest",b);
else
printf("%d is greatest",c);
getch();
}

Output:
Enter the values of a,b,c:
10
30
20
30 is greatest
LEAP YEAR OR NOT

#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter the year:\n");
scanf("%d",&year);
if(year%4==0)
printf("%d is leap year",year);
else
printf("%d is not leap year",year);
getch();
}

OUTPUT:
Enter the year:
2013
2013 is not leap year
Enter the year:
2016
2016 is leap year
ODD OR EVEN NUMBER

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter the number:\n");
scanf("%d",&n);
if(n%2==0)
printf("Even number");
else
printf("Odd number");
getch();
}

OUTPUT:
Enter the number:
9
Odd number
MENU DRIVEN USING SWITCH CASE

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,op;
clrscr();
printf("Enter the values of a and b:\n");
scanf("%d %d",&a,&b);
printf("1.Addition\n");
printf("2.Subtraction\n");
printf("3.Multiplication\n");
printf("4.Division\n");
printf("5.EXIT");
do
{
printf("\nEnter the option:");
scanf("%d",&op);
switch(op)
{
case 1:
c=a+b;
printf("\nAddition of two numbers=%d",c);
break;
case 2:
c=a-b;
printf("\nSubtraction of two numbers=%d",c);
break;
case 3:
c=a*b;
printf("\nMultiplication of two numbers=%d",c);
break;
case 4:
c=a/b;
printf("\nDivision of two numbers=%d",c);
break;
case 5:
exit(0);
break;
}
}while(op<5);
getch();
}
OUTPUT:
Enter the values of a and b:
6
3
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.EXIT
Enter the option:1
Addition of two numbers=9

Enter the option:2


Subtraction of two numbers=3

Enter the option:3


Multiplication of two numbers=18

Enter the option:4


Division of two numbers=2.000000
STRING FUNCTIONS USING SWITCH STATEMENT

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10]="GOOD",s2[10],s3[10]="MORNING";
int l,op,c;
clrscr();
printf("1.String length\n");
printf("2.String copy\n");
printf("3.String cancatenate\n");
printf("4.String compare\n");
printf("5.String Reverse\n");
printf("6.Upper to Lowercase\n");
printf("5.EXIT");
do
{
printf("\nEnter the option:");
scanf("%d",&op);
switch(op)
{
case 1:
l=strlen(s1);
printf("\nlength=%d",l);
break;
case 2:
strcpy(s2,s1);
printf("\ncopied string=%s",s2);
break;
case 3:
strcat(s1,s3);
printf("\nconcatenated string=%s",s1);
break;
case 4:
c=strcmp(s1,s3);
printf("\ncompare strings=%d",c);
break;
case 5:
puts(strrev(s3));
break;
case 6:
puts(strlwr(s1));
break;
case 7:
exit(0);
}
}while(op<7);
getch();
}

Output:
1.String length
2.String copy
3.String cancatenate
4.String compare
5.String Reverse
6.Upper to Lowercase
5.EXIT

Enter the option:1


length=4

Enter the option:2


copied string=GOOD
Enter the option:3
concatenated string=GOODMORNING
Enter the option:4
compare strings=-6
Enter the option:5
GNINROM
Enter the option:6
good morning
LINEAR SEARCH
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,x;
int a[5];
clrscr();
printf("Enter the array size:\n");
scanf("%d",&n);
printf("Array elements are:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be searched:");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(a[i]==x)
break;
}
if(i==n)
printf("The number is not found:\n");
else
printf("The number is found and its position is:%d",i);
getch();
}

OUTPUT:
Enter the array size:
5
Array elements are:
98
45
16
73
64
Enter the number to be searched:73
The number is found and its position is:3
BINARY SEARCH

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,mid,lower,upper,flag=0,key;
int a[20];
clrscr();
printf("Enter the array size:\n");
scanf("%d",&n);
printf("Array elements are:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be searched:\n");
scanf("%d",&key);
lower=0;
upper=n;
for(mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2)
{
if(a[mid]==key)
{
printf("The current position of that value is:%d",mid);
flag=1;
break;
}
if(a[mid]>key)
upper=mid-1;
else
lower=mid+1;
}
if(flag==0)
printf("The Value is not found");
getch();
}
OUTPUT:
Enter the array size:
5
Array elements are:
34
56
67
78
89
Enter the number to be searched:
89
The current position of that value is:4
SORTING

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,temp;
int a[20];
clrscr();
printf("Enter the array size:\n");
scanf("%d",&n);
printf("Array elements are:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nThe sorted elements are:");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
OUTPUT
Enter the array size:
5
Array elements are:
98
56
78
4
1

The sorted elements are:


1
4
56
78
98
AREA AND CIRCUMFERENCE OF A CIRCLE

#include<stdio.h>
#include<conio.h>
void main()
{
float a,c,r;
clrscr();
printf("Enter the radius value:");
scanf("%f",&r);
a=3.14*r*r;
c=2*3.14*r;
printf("\nArea of a circle=%f",a);
printf("\nCircumference of a circle=%f",c);
getch();
}

OUTPUT
Enter the radius value:7
Area of a circle=153.860001
Circumference of a circle=43.959999
CELSIUS TO FAHRENHEIT VALUE

#include<stdio.h>
#include<conio.h>
void main()
{
float f,c;
clrscr();
printf("Enter the celsius value:");
scanf("%f",&c);
f=(1.8*c)+32;
printf("\nFahrenheit value=%f",f);
getch();
}

OUTPUT:
Enter the Celsius value: 56
Fahrenheit value=132.800003
FIBONACCI SERIES

#include<stdio.h>
#include<conio.h>
void main()
{
int f1=0,f2=1,f3=0,n,i;
clrscr();
printf("Enter the n value:");
scanf("%d",&n);
printf("%d",f1);
printf("\t%d",f2);
i=2;
while(i<n)
{
f3=f1+f2;
printf("\t%d",f3);
f1=f2;
f2=f3;
i=i+1;
}
getch();
}

OUTPUT:
Enter the n value:8
0 1 1 2 3 5 8 13
REVERSE OF THE DIGITS

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,s=0;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
printf("\nReversed number=%d",s);
getch();
}

OUTPUT:
Enter the number:456
Reversed number=654
SUM OF THE DIGITS

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,s=0;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
s=s+r;
n=n/10;
}
printf("\nSum of the digits=%d",s);
getch();
}

OUTPUT:
Enter the number:453
Sum of the digits=12
PALINDROME OR NOT

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[15],b[15];
int i,j,l;
clrscr();
printf("Enter the string:");
scanf("%s",a);
l=strlen(a);
j=0;
for(i=l-1;i>=0;i--)
{
b[j]=a[i];
j++;
}
b[l]='\0';
if(strcmp(a,b)==0)
printf("The given string is palindrome");
else
printf("The given string is not palindrome");
getch();
}

OUTPUT:
Enter the string: madam
The given string is palindrome
MATRIX ADDITION
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("\n Enter the a matrix");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("\n %d",&a[i][j]);
printf("\n Enter the b matrix");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("\n %d",&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\n The resultant matrix is:");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
}
getch();
}

OUTPUT:
Enter the a matrix
123
123
123
Enter the b matrix
123
123
123
The resultant matrix is:
2 4 6
2 4 6
2 4 6
MATRIX SUBTRACTION

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("\n Enter the a matrix");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("\n %d",&a[i][j]);
printf("\n Enter the b matrix");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("\n %d",&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]-b[i][j];
printf("\n The resultant matrix is:");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
}
getch();
}

OUTPUT:

Enter the a matrix


123
456
789
Enter the b matrix
111
111
111

The resultant matrix is:


0 1 2
3 4 5
6 7 8
MATRIX MULTIPLICATION

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j,k;
clrscr();
printf("\n Enter the a matrix");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("\n %d",&a[i][j]);
printf("\n Enter the b matrix");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("\n %d",&b[i][j]);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("\n The resultant matrix is:");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
}
getch();
}
OUTPUT:
Enter the a matrix
123
123
123
Enter the b matrix
123
123
123
The resultant matrix is:
6 12 18
6 12 18
6 12 18
TRANSPOSE OF A MATRIX

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf("\n Enter the a matrix");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("\n %d",&a[i][j]);
printf("\n The resultant matrix is:");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d\t",a[j][i]);
}
}
getch();
}

OUTPUT:

Enter the a matrix


234
567
891

The resultant matrix is:


2 5 8
3 6 9
4 7 1
FACTORIAL USING RECURSION

#include<stdio.h>
#include<conio.h>
void main()
{
int n,f;
int fact(int n);
clrscr();
printf("Enter the n value");
scanf("%d",&n);
f=fact(n);
printf("Factorial value is:%d",f);
getch();
}
int fact(int n)
{
if(n==1)
return(1);
else
return(n*fact(n-1));
}

OUTPUT:
Enter the n value5
Factorial value is:120
ARMSTRONG NUMBER
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,r,sum=0;
clrscr();
printf("Enter the number");
scanf("%d",&i);
n=i;
while(n!=0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(i==sum)
printf("The number is Armstrong number");
else
printf("The number is not Armstrong number");
getch();
}

OUTPUT:
Enter the number 153
The number is Armstrong number
SWAPPING USING POINTER
#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void main()
{
int a,b;
clrscr();
printf("Enter the two numbers:");
scanf("%d%d",&a,&b);
printf("Before swapping, a=%d \t b=%d",a,b);
swap(&a,&b);
printf("\nAfter swapping,a=%d \t b=%d",a,b);
getch();
}
void swap(int *x, int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}

OUTPUT
Enter the two numbers: 10 15
Before swapping, a=10 b=15
After swapping, a=15 b=10
STUDENT MARK LIST USING STRUCTURE

#include<stdio.h>
#include<conio.h>
struct stud
{
int regno, m1,m2,m3,tot;
char name[10], grade;
float avg;
}s[10];
void main()
{
int n,i;
clrscr();
printf("Enter the number of students:");
scanf ("%d",&n);
printf("Enter the student regno,name,mark:\n");
for(i=0;i<n;i++)
{
scanf("%d%s%d%d%d",&s[i].regno,&s[i].name,&s[i].m1,&s[i].m2,&s[i].m3);
s[i].tot=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].tot/3;
if(s[i].m1<35||s[i].m2<35||s[i].m3<35)
s[i].grade='F';
else
{
if(s[i].avg>=75)
s[i].grade='s';
else if(s[i].avg<75 && s[i].avg>=65)
s[i].grade='a';
else if(s[i].avg<65 && s[i].avg>=50)
s[i].grade='b';
else if(s[i].avg<50 && s[i].avg>=35)
s[i].grade='c';
}
}
printf("\nStudent mark list\n");
printf("Regno\tName\tm1\tm2\tm3\tTot\tAvg\t\tGrade\n");
for(i=0;i<n;i++)
{
printf("%d\t%s\t%d\t%d\t%d\t%d\t%f\t%c",s[i].regno,s[i].name,s[i].m1,s[i].m2,
s[i].m3,s[i].tot,s[i].avg,s[i].grade);
printf("\n");
}
getch();
}

OUTPUT:

Enter the number of students:2


Enter the student regno,name,mark:
1 xxx 34 45 56
2 yyy 67 78 90

Student mark list


Regno Name m1 m2 m3 Tot Avg Grade
1 xxx 34 45 56 135 45.000000 F
2 yyy 67 78 90 235 78.000000 s

You might also like