0% found this document useful (0 votes)
58 views

Simple Programs in C

This document contains 19 C programming examples/exercises to calculate basic mathematical operations and perform conditional checks. The examples cover calculating the sum and product of numbers, simple interest, perimeter and area of shapes, even/odd number checks, leap year checks, number range generation, number conversion to words, and factorial and summation calculations. Each example contains the full source code to implement the given task.

Uploaded by

mani gopal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Simple Programs in C

This document contains 19 C programming examples/exercises to calculate basic mathematical operations and perform conditional checks. The examples cover calculating the sum and product of numbers, simple interest, perimeter and area of shapes, even/odd number checks, leap year checks, number range generation, number conversion to words, and factorial and summation calculations. Each example contains the full source code to implement the given task.

Uploaded by

mani gopal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Output:

Enterany2Numbers:53
Sumof2nos=8

Output:

Enterany3Numbers:243
Productof3nos=24

CPrograms

Ex.No.:1
WriteaCProgramtofindtheSumof2Numbers

#include<stdio.h>
#include<conio.h>
main()
{
inta,b,c;
clrscr();
printf("Enterany2Numbers:");
scanf(%d%d,&a,&b);
c=a+b;
printf("Sumof2nos=%d",c);
getch();
}
Ex.No.:2
WriteaCprogramtofindtheproductof3nos.

#include<stdio.h>
#include<conio.h>
main()
{
inta,b,c,d;
clrscr();
printf("Enterany3Numbers:")
scanf("%d%d%d",&a,&b,&c);
d=a*b*c;
printf("Productof3nos=%d,d);
getch();
}


Output:

EntervaluesofP,NandR:500024
SimpleInterest=400.0000

Output:

EnterLengthandBreadth:24
PerimeterofRectangle=12.0000

Ex.No.:3
WriteaCprogramtocalculateSimpleInterest.

#include<stdio.h>
#include<conio.h>
main()
{
floatp,n,r,si;
clrscr();
printf("EntervaluesofP,NandR:");
scanf("%f%f%f",&p,&n,&r);
si=(p*n*r)/100;
printf("SimpleInterest=%f",si);
getch();
}
Ex.No.:4
WriteaCprogramtofindtheperimeterofarectangle.

#include<stdio.h>
#include<conio.h>
main()
{
floatl,b,p;
printf("EnterLength&Breadth:\n");
scanf("%f%f",&l,&b);
p=2*(l+b);
printf("PerimeterofRectangle=%f",p);
getch();
}

Ex.No.:5

WriteaCprogramtofindtheAreaofTriangle.

#include<stdio.h>
#include<conio.h>
main()
{
floatb,h,a;
clrscr();
printf(EnterBaseandHeight:);
scanf(%f%f,&b,&h);
a=0.5*b*h;
printf(AreaofTriangle=%f,a);
getch();
}

Output:

EnterBaseandHeight:46
AreaofTriangle=12.0000

Output:

EnterRadiusandHeight:62
VolumeofCylinder=226.08

Ex.No.:6
WriteaCprogramtofindtheVolumeofCylinder.

#include<stdio.h>
#include<conio.h>
main()
{
floatr,h,v;
clrscr();
printf(EnterRadiusandHeight:);
scanf(%f%f,&r,&h);
v=3.14*r*r*h;
printf(VolumeofCylinder=%f,v);
getch();
}

Ex.No.:7

Output:

EnterRadius:8
VolumeofSphere=2143.03744

Output:

EnterRollno:11405
Input3Marks:456789
Rollno:11405

TotalMarks:201

Average:67.0000

WriteaCprogramtofindtheVolumeofSphere.

#include<stdio.h>
#include<conio.h>
main()
{
floatr,v;
clrscr();
printf(EnterRadius:);
scanf(%f,&r);
v=1.333*3.14*r*r*r;
printf(VolumeofSphere=%f,v):
getch();
}

Ex.No.:8
WriteaCprogramtoacceptrollno.fromtheuser,acceptmarksfrom3subjects
andcalculatetotalandaverage.

#include<stdio.h>
#include<conio.h>
{
main()
intrno,m1,m2,m3,tot=0;
floatavg;
clrscr();
printf(\nEnterRollno:);
scanf(%d&rno);
printf(\nInput3Marks:);
scanf(%d%d%d,&m1,&m2,&m3);
tot=m1+m2+m3;
avg=tot/3;
printf(\nRollno:%d\n,rno);
printf(\nTotalMarks:%d\n,tot);
printf(\nAverage:%f,avg);
getch();
}

Ex.No.:9

Output:

InputAge:21
MAJOR

Output:

InputAge:20
MAJOR

InputAge:15
MINOR

WriteaCprogramtocheckwhethertheinputAgeisMAJOR.

#include<stdio.h>
#include<conio.h>
main()
{
intage;
clrscr();
printf(InputAge:);
scanf(%d,&age);
if(age>=18)
printf(MAJOR):
getch();
}

Ex.No.:10
WriteaCprogramtocheckwhethertheinputAgeisMAJORorMINOR.

#include<stdio.h>
#include<conio.h>
main()
{
intage;
clrscr();
printf(InputAge:);
scanf(%d,&age);
if(age>=18)
printf(MAJOR):
else
printf(MINOR);
getch();
}

Ex.No.:11

Output:

EnteraNumber:0
GivenNo.isEQUALTOZERO

EnteraNumber:5

POSITIVENumber

EnteraNumber:8

NEGATIVENumber

Output:

EnteraNumber:4
EVENNumber

EnteraNumber:5

ODDNumber

WriteaCprogramtocheckwhetherthegivenno.isequaltoZero,Positive
NumberorNegativeNumber.

#include<stdio.h>
#include<conio.h>
main()
{
intnum;
clrscr();
printf(EnteraNumber:);
scanf(%d,&num);
if(num==0)
printf(GivenNo.isEQUALTOZERO);
elseif(num>0)
printf(POSITIVENumber);
else
printf(NEGATIVENumber);
getch();
}
Ex.No.:12
WriteaCProgramtocheckwhetherthegivenno.isODDorEVEN.

#include<stdio.h>
#include<conio.h>
main()
{
intnum;
clrscr();
printf(EnteraNumber:);
scanf(%d,&num);
if(num%2==0)
printf(EVENNumber);
else
printf(ODDNumber);
getch();
}

Output:

InputYear:2004
LEAPYEAR

InputYear:2005
NotaLEAPYEAR

Output:

EnterNvalue:10
0
2

4
6
8
10

Ex.No.:13
WriteaCprogramtocheckwhethertheinputyearisLEAPyearornot.

#include<stdio.h>
#include<conio.h>
main()
{
intyr;
clrscr();
printf(InputYear:);
scanf(%d,&num);
if(yr%4==0)
printf(LEAPYEAR);
else
printf(NotaLEAPYEAR);
getch();
}
Ex.No.:14
WriteaCProgramtogenerateEVENnosfrom0toN.

#include<stdio.h>
#include<conio.h>
main()
{
intn,i;
clrscr();
printf(EnterNvalue:);
scanf(%d,&n);
for(i=0;i<=n;i+=2)
{
printf(%d\n,i);
}
getch();
}

Output:

EnteraNumber:1

ONE


EnteraNumber:2
TWO

EnteraNumber:3
THREE

EnteraNumber:4
FOUR

EnteraNumber:5
FIVE

EnteraNumber:8
EnteraNo.between15

Ex.No.:15
WriteaCprogramtodisplaythenumberinwords.(UseSwitchStatement)

#include<stdio.h>
#include<conio.h>
main()
{
intnum;
clrscr();
printf(EnteraNumber:);
scanf(%d,&num);
switch(num)
{
case1:
printf(ONE);
break;
case2:
printf(TWO);
break;
case3:
printf(THREE);
break;
case4:
printf(FOUR);
break;
case5:
printf(FIVE);
break;
default:
printf(EnteraNo.between15);
break;
}
getch();
}

Output:

EnterNvalue:10
1
3
5
7
9

Output:

EnterNvalue:4
Sumofnnumbers=10

Ex.No.:16

WriteaCProgramtogenerateODDnosfrom1toN.

#include<stdio.h>
#include<conio.h>
main()
{
inti,n;
clrscr();
printf(EnterNvalue:);
scanf(%d,&n);
for(i=1;i<=n;i+=2)
{
printf(%d\n,i);
}
getch();
}
Ex.No.:17
WriteaCprogramtofindthesumof1+2+3+....N.

#include<stdio.h>
#include<conio.h>
main()
{
intn,i,sum=0;
clrscr();
printf(EnterNvalue:);
scanf(%d,&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf(SumofNnumbers=%d\n,sum);
getch();
}

Ex.No.:18

Output:

EnterNvalue:4
SumofsquaresofNNumbers=30

Output:

EnterNvalue:5
FactorialofNNumbers=120

WriteaCprogramtofindthesumofsquaresofnumbersfrom12+22+32+....N2.

#include<stdio.h>
#include<conio.h>
main()
{
intn,i,sum=0;
clrscr();
printf(EnterNvalue:);
scanf(%d,&n);
for(i=1;i<=n;i++)
{
sum=sum+(i*i);
}
printf(SumofsquaresofNnumbers=%d\n,sum);
getch();
}
Ex.No.:19
WriteaCprogramtofindthesumoffactorialofNnumbers.

#include<stdio.h>
#include<conio.h>
main()
{
intn,i,fact=1;
clrscr();
printf(EnterNvalue:);
scanf(%d,&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(FactorialofNnumbers=%d\n,fact);
getch();
}

Output:

1x7=7

2x7=14

3x7=21

4x7=28
5x7=35
6x7=42
7x7=49

8x7=56

9x7=63

10x7=70

Output:

Whattable:5

Uptowhatnumber:6

1x5=5
2x5=10
3x5=15
4x5=20

5x5=25

6x5=30

Ex.No.:20
WriteaCprogramtoprintthemultiplicationtable7.(usedowhileloop)

#include<stdio.h>
#include<conio.h>
main()
{
inti;
do
{
printf(%dx7=%d\n,i,i*7);
i++;
}while(i<=10);
getch();
}
Ex.No.:21
WriteaCprogramtoprintthemultiplicationtablebyacceptingtablenumber
fromtheuser.(usedowhileloop)

#include<stdio.h>
#include<conio.h>
main()
{
inti,t,n;
printf(\nWhattable:);
scanf(%d,%t);
printf(\nUptoWhatNumber:);
scanf(%d,%n);
do
{
printf(%dx%d=%d\n,i,t,i*t);
i++;
}while(i<=n);
getch();
}

Output:

Enter5numbers:

2345588844

Storedvaluesare

23

45

58

88

44

Output:

EnterNvalue:7

2
3
5
8

13

Ex.No.22
WriteaCProgramtoaccept5nos.fromtheuserandprintthesameusingarray.
#include<stdio.h>
#include<conio.h>
main()
{
inti,a[5];
printf(\nEnter5numbers:);
for(i=0;i<5;i++)
{
scanf(%d,%a[i]);
}
printf(\nStoredvaluesare\n:);
for(i=0;i<5;i++)
{
printf(%d,a[i]);
}
getch();
}
Ex.No.:23
WriteaCprogramtogenerateFibonacciseries.
#include<stdio.h>
#include<conio.h>
main()
{
inti,n,f=0,f1=1,f2=1;
printf(\nEnterNvalue:);
scanf(%d,%n);
printf(%d\n,f1);
printf(%d\n,f2);
for(i=3;i<=n;i++)

{
f=f1+f2;
printf(%d\n,f);
f1=f2;
f2=f;
}
getch();
}


Ex.No.24

Output:

EnterRollNo:11405
Enter5Marks:5456895887

RollNumber=11405

TotalMarks=344

Average=68.80000

WriteaCprogramtoacceptrollno,5marksfromtheuserand
calculateTotalandAverage.

#include<stdio.h>
#include<conio.h>
main()
{
intrno,i,mark[5],tot=0;
floatavg;
printf(\nEnterRollNo:);
scanf(%d,&rno);
printf(\nEnter5Marks:);
for(i=0;i<5;i++)
{
scanf(%d,&mark[i]);
tot=tot+mark[i];
}
avg=tot/3;
printf(\nRollNumber=%d,rno);
printf(\nTotalMarks=%d,tot);
printf(\nAverage=%f,avg);
getch();
}

Output:

EntertheString:software

Numberofvowelsinthestringsoftwareis3

Ex.No.:25

WriteaCprogramtocountthenumberofvowelspresentinaString.

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
inti,j,k;
charstr[15];
printf("\nEntertheString:");
scanf("%s",str);
i=strlen(str);
k=0;
for(j=0;j<=i;j++)
{

if((str[j]=='a'||str[j]=='e'||str[j]=='i'||str[j]=='o'||str[j]=='u'))
k=k+1;
}
printf("\nNumberofvowelsintheString%sis%d",str,k);
getch();
}

WriteaCprogramtofindalltheAdamnumberbetween10to
100.(Anadamnumberisonewhichsatisfiesthefollowing:The
reverseofthesquareofanumberisthesquareofthereverse
ofthatnumber,thatis,ifthenumberis12then122=144,the
reverseis441whichis212).

Output:

Enteranynumberbetween10to100:12

Thesquareof12is144

Thereverseof12is21

Thesquareof12is441

Thus12isanAdamNumber

Enteranynumberbetween10to100:14

Thesquareof14is196

Thereverseof14is4

Thesquareof14is1681

Thus13isnotanAdamNumber

Ex.No.:26

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
intp,m,b,n,rev,a,c,i,z,r=0;
rev=0;
printf("\nEnteranyno.between10to100:");
scanf("%d",&z);
n=z;
c=n*n;
while(n!=0)
{
m=n%10;
r=r*10+m;
n=n/10;
}
printf("\n\nTheSquareof%dis\t%d",z,c);
printf("\n\nThereverseof%dis\t%d",z,r);
p=r*r;
printf("\n\nTheSquareof%dis\t%d",r,p);
while(c!=0)
{
a=c%10;
rev=rev*10+a;
c=c/10;
}
if(rev==p)
printf("\n\nThus%disanAdamNumber",z);
else
printf("\n\nThus%disNotanAdamNunber",z);
getch();
}

Output:

EntertheBinarynumber:10110

TheDecimalnumberis22

Ex.No.:27

WriteaCprogramtoconvertbinaryequivalentnumberintoitsdecimal.

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
intn,deci,i;
deci=0;
i=0;
clrscr();
printf("\nEntertheBinarynumber:");
scanf("%d",&n);
while(n!=0)
{
deci=deci+(n%10)*pow(2,i);
n=n/10;
i=i+1;
}
printf("\nDecimalNumberis%d",deci);
getch();
}

Output:

Enteranynumber:496

Thedivisiblenumber:1

Thedivisiblenumber:2
Thedivisiblenumber:4
Thedivisiblenumber:8
Thedivisiblenumber:16
Thedivisiblenumber:31

Thedivisiblenumber:62

Thedivisiblenumber:124
Thedivisiblenumber:248
TheSumis496

Hence496isaperfectnumber!

Enteranynumber:248
Thedivisiblenumber:1
Thedivisiblenumber:2
Thedivisiblenumber:4
Thedivisiblenumber:8
Thedivisiblenumber:31
Thedivisiblenumber:62
Thedivisiblenumber:124
TheSumis232
Hence232isnotaperfectnumber!

Ex.No.:28
Write a C program to find the perfect numbers between 1 and n terms. (A
perfectnumberisanumberthatisthesumofallitsdivisorsexceptitself.Sixis
the perfect number, since the sum of the divisors of six except itself is
1+2+3=6)

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()

{
intnum,div,i,j,sum=0,r;
clrscr();
printf("\nEnteranyNumber:");
scanf("%d",&num);
i=1;
while(i<num)
{
div=num/i;
r=num%i;
if(r==0)
{
sum=sum+i;
printf("\nTheDivisibleNumber:%d\n",i,div);
}
i=i+1;
}
printf("\nTheSumis%d\n",sum);
if(sum==num)
printf("\nHence%disaperfectnumber!",num);
else
printf("\nHence%disnotaperfectnumber!",num);
getch();
}

Output:

EntertheString:Malayalam

Thegivenstringisapalindrome

EntertheString:bosco

Thegivenstringisnotapalindrome

Ex.No.:29
WriteaCprogramtocheckwhetherthegivenstringispalindromeornot.

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
inti,j,len,k=0;
charstr[20];
clrscr();
printf("EntertheString:");
scanf("%s",str);
len=strlen(str)1;
j=len;
for(i=0;i<=len;i++)
{
if(str[i]!=str[j])
k=1;
j;
}
if(k==0)
printf("\nThegivenstringisapalindrome");
else
printf("\nThegivenstringisnotapalindrome");
getch();
}

Ex.No.:30

Output:

Enterno.ofterms:10
Enter10terms:112548899789250

DescendingOrder
99
88
78
25
11
9
5
4
2
0

WriteaCprogramtoarrangethearrayofnnumbersindescendingorder.
#include<stdio.h>
#include<conio.h>
main()
{
inti,j,temp,n,a[20];
clrscr();
printf("Enterno.ofterms:");
scanf("%d",&n);
printf("Enter%dterms:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nDescendingorder\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}

Output:

Enterthesizeofthematrix:44

Entertheelementsinthematrix

1234
5678
9456
8745

Thesumofthediagonal17

Ex.No.:31

WriteaCprogramtofindthesumofthediagonalelementsofaMatrix.

#include<stdio.h>
#include<conio.h>
main()
{
inta[10][10],c,m,i,j,n;

clrscr();
printf("Enterthesizeofthematrix:");
scanf("%d%d",&m,&n);
if(m==n)
{
printf("\nEntertheelementsinthematrix\n");
for(i=0;i<m;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
c=0;
for(i=0;i<m;i++)
for(j=0;j<m;j++)
{
if(i==j)
c=c+a[i][j];
}
printf("\nTheSumofthediagonal%d",c);
}
else
{
printf("Therowsandcolumnsshouldbeequal");
}
getch();
}


Output:

EnterMatrixA
123
456
789

EnterMatrixB
987
654
321

SumofMatrix
101010
101010
101010

Ex.No.:32

WriteaCprogramtofindtheSumof3x3matrix.

#include<stdio.h>
#include<conio.h>
main()
{
inta[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("\nEnterMatrixA\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnterMatrixb\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}

//SumofMatrix
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\nSumofMatrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}

Output:

Inputrowandcolumnofamatrix:33

EnterMatrixA
3 45
4 69
1 23

TransposeofMatrixA

341
462
593

Ex.No.:33

WriteaCprogramtogettheTransposeofaMatrix.
#include<stdio.h>
#include<conio.h>
main()
{
inta[10][10],b[10][10],i,j,m,n;
clrscr();
printf("\nInputrowandcolumnofAmatrix:");
scanf("%d%d",&n,&m);
printf("\nEnterMatrixA\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
b[i][j]=a[j][i];
printf("\nTransposeofMatrixA\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
getch();
}

Ex.No.:34

Output:

WriteaCprogramtostore10namesinanarrayandprintthemeachin
oneline.
Enterany10names
Andrew
Ashwin
Joseph
Arvind
Stefan
Balaji
Arun
Vijay
Surya
Bosco

Enterednamesare
Andrew
Ashwin
Joseph
Arvind
Stefan
Balaji
Arun
Vijay
Surya
Bosco

#include<stdio.h>
#include<conio.h>
main()
{
chara[20][20];
inttemp,i,j;
clrscr();
printf("\nEnteranyTennames\n");
for(i=0;i<10;i++)
{
temp=0;
for(j=0;temp==0;j++)
{
scanf("%c",&a[i][j]);
if(a[i][j]=='\n')
{
temp=1;
}
}
a[i][j]='\o';
}
printf("\nEnterednamesare.....\n");
for(i=0;i<10;i++)
{
for(j=0;a[i][j]!='\o';j++)
printf("%c",a[i][j]);
}
getch();
}

Output:

Valuesbeforeswapping10and20
Valuesafterswapping20and10

Ex.No.:35
WriteaCprogramtointerchangethevaluesoftwovariablesusing
function.

#include<stdio.h>
#include<conio.h>
voidswap(int,int);
main()
{
intm1=10,m2=20;
clrscr();
printf("\nValuesbeforeswapping:%dand%d\n",m1,m2);
swap(m1,m2);
getch();
}
voidswap(intn1,intn2)
{
inttemp;
temp=n1;
n1=n2;
n2=temp;
printf("\nValuesafterswapping:%dand%d\n",n1,n2);
}

Ex.No.:36

Output:

WriteaCProgramtoacceptRollNumber,Name,Averagemarksand
printthemusingStructure.
EnterRollnumber:11405
EnterName:Bosco
EnterAverageMarks:56.89

Rollnumber:11405
Name:Bosco
AverageMarks:56.889999

#include<stdio.h>
#include<conio.h>
main()
{
structstudent
{
intrno;
charname[25];
floatavg;
};
structstudents;
printf("\nEnterRollnumber:");
scanf("%d",&s.rno);
printf("\nEnterName:");
scanf("%s",&s.name);
printf("\nEnterAverageMarks:");
scanf("%f",&s.avg);
printf("\n\n");
printf("\nRollNumber:%d\n",s.rno);
printf("\nName:%s\n",s.name);
printf("\nAverageMarks:%f\n",s.avg);
getch();
}

You might also like