C Programs 128
C Programs 128
Date: Name:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
float p,r,si;
clrscr();
printf("Enter the principal amount \n");
scanf("%f",&p);
printf("enter the number of years \n");
scanf("%d",&n);
printf("enter the rate of interest \n");
scanf("%f",&r);
si=(p*n*r)/100;
printf("the simple interest is %f",si);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter the two numbers \n");
scanf("%d%d",&a,&b);
c=a;
a=b;
b=c;
printf("The swapped numbers are \n");
printf("a = %d \n",a);
printf("b = %d \n",b);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
long a,b,c=0;
clrscr();
printf("Enter the number \n");
scanf("%ld",&a);
while(a>0)
{
b=a%10;
a=a/10;
c=b+c*10;
}
printf("Reverse number is %ld",c);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
long x,y,z=0;
clrscr();
printf("Enter the number \n");
scanf("%ld",&x);
while(x>0)
{
y=x%10;
z=z+y;
x=x/10;
}
printf("Sum of digits is %ld",z);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
long int a=0,b=0,c=0,d=0,e=0;
clrscr();
printf("Enter the number\n");
scanf("%ld",&a);
e=a;
while(a>0)
{
b=a%10;
c=c+b;
a=a/10;
d=b+d*10;
}
if(e==d)
printf("%ld is a PALIDROME",e);
else
printf("%ld is not a PALIDROME",e);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
long i,j,x=1,k=1;
clrscr();
printf("Enter the number\n");
scanf("%ld",&i);
if(i==0)
printf("Factorial is %ld",k);
else
{
for(j=1;j<=i;j++)
{
x=j*x;
}
printf("The factorial is %ld",x);
}
getch();
}
Output:
Sum of series
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10]={1,2,3,4,5,6,7,8,9,10},I,t,s=0;
clrscr();
for(I=0;I<10;I++)
{
t=a[I]*a[I];
s+=t;
}
printf(“the sum of the series is: %d”,s);
getch();
}
Output:
Simple Calculator
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,n;
clrscr();
printf("Enter two numbers \n");
scanf("%d%d",&a,&b);
printf("\n 1.ADD 2.SUBSTRACT 3.MULTIPLY 4.DIVIDE \n");
printf("\n Enter the option\n");
scanf("%d",&n);
switch(n)
{
case 1:
c=a+b;
printf("Sum = %d",c);
break;
case 2:
c=a-b;
printf("Substraction = %d",c);
break;
case 3:
c=a*b;
printf("Multiplication = %d",c);
break;
case 4:
if(b<a)
{
c=a/b;
printf("Division result = %d",c);
}
else
{
printf("Invalid");
}
break;
default:
printf("No such option\n");
}
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void oden(int n)
{
int r;
r=n%2;
if(r==0)
printf("even");
else
printf("odd");
}
void main()
{
int a;
clrscr();
printf("Enter any number\n");
scanf("%d",&a);
oden(a);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
int max(int x,int y)
{
int z;
z=(x>=y)?x:y;
return(z);
}
int min(int m,int n)
{
int p;
p=(m<=n)?m:n;
return(p);
}
void main()
{
int a,b,c,d,e;
clrscr();
printf("Enter 3 numbers\n");
scanf("%d%d%d",&a,&b,&c);
d=max(a,b);
e=min(a,b);
printf("The largest number is= %d\n",max(c,d));
printf("The smallest number is= %d",min(c,e));
getch();
}
Output:
Enter 3 numbers
14
13
8
The largest number is = 14
The smallest number is = 8
ExNo: 5a RollNo:
Date: Name:
#include<stdio.h>
#include<conio.h>
long int factorial(int n);
void main()
{
int n;
clrscr();
printf("Number, n= ");
scanf("%d",&n);
printf("n!= %d\n",factorial(n));
getch();
}
long int factorial(int n)
{
if(n<=1)
return(1);
else
return(n*factorial(n-1));
}
Output:
Number, n = 6
n! = 720
ExNo: 5b RollNo:
Date: Name:
#include<stdio.h>
#include<conio.h>
int gcd(int a,int b)
if(b==0)
return(a);
else
return gcd(b,a%b);
}
void main()
{
int n1,n2;
clrscr();
printf("Enter two numbers\n");
scanf("%d%d",&n1,&n2);
printf("The gcd of %d and %d is %d\n",n1,n2,gcd(n1,n2));
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,sum=0;
float avg;
clrscr();
printf("Enter the limit (<=10)\n");
scanf("%d",&n);
printf("Enter the %d values\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
sum+=a[i];
avg=sum/n;
printf("Sum = %d\n",sum);
printf("Average = %f",avg);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],temp=0,i,j,n;
clrscr();
printf("Enter the limit\n");
scanf("%d",&n);
printf("Enter the %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=i;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("The sorted order = \n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}
Output:
Enter the limit
5
Enter the 5 elements
1
4
7
3
2
The sorted order =
1 2 3 4 7
ExNo: 6c RollNo:
Date: Name:
#include<stdio.h>
#include<conio.h>
#define SIZE 25
void main()
{
int i,j,k,n,search,count,array[SIZE];
count=0;
clrscr();
printf("Enter the limit\n");
scanf("%d",&n);
printf("Enter the elements \n");
for(i=0;i<n;i++)
scanf("%d",&array[i]);
printf("Enter the element to be searched\n");
scanf("%d",&search);
for(i=0;i<n;i++)
{
if(array[i]==search)
{
++count;
}
}
if(count==0)
printf("Element not present\n");
else
printf("%d occurs %d times \n",search,count);
getch();
}
Output:
Enter the limit
5
Enter the elements
4
12
14
15
4
Enter the element to be searched
4
4 occurs 2 times
ExNo: 7a RollNo:
Date: Name:
#include<stdio.h>
#include<conio.h>
int a[5][5],b[5][5],c[5][5],d[5][5],e[5][5];
int i,j,k,m,n,p,q;
void inputa()
{
printf("Enter the order of first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the value of the matrix A\n");
for(i=0;i<m;i++)
{
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
}
}
void inputb()
{
printf("Enter the order of second matrix\n");
scanf("%d%d",&p,&q);
printf("Enter the value of the matrix B\n");
for(i=0;i<p;i++)
{
{
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
}
}
}
void add()
{
if(m==p&&n==q)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
else
printf("Addition not possible\n");
}
void sub()
{
if(m==p&&n==q)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
d[i][j]=a[i][j]-b[i][j];
}
}
}
else
printf("Substraction not possible\n");
}
void mul()
{
if(n==p)
{
printf("multiplication is possible\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
e[i][j]=0;
for(k=0;k<p;k++)
{
e[i][j]=e[i][j]+a[i][k]*b[k][j]
;
}
}
}
}
else
printf("Matrix multiplication not possible\n");
}
void resm()
{
printf("Multiplication result= \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%4d\t",e[i][j]);
}
printf("\n");
}
}
void resadd()
{
printf("Addition result= \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%4d\t",c[i][j]);
printf("\n");
}
}
void resub()
{
printf("Substraction result= \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%4d\t",d[i][j]);
printf("\n");
}
}
void main()
{
clrscr();
inputa();
inputb();
add();
resadd();
sub();
resub();
mul();
resm();
getch();
}
Output:
Addition result =
12 4 6
18 16 21
24 16 12
Substraction result =
-10 0 0
-10 -6 -9
-10 0 6
Multiplication is possible
Multiplication result =
90 48 42
216 111 105
342 174 168
ExNo: 7b RollNo:
Date: Name:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], I, j, d=0;
clrscr();
printf(“the matrix is”);
for(I=0;I<3;I++)
{
printf(“\n”);
for(j=0;j<3;j++)
{
scanf(“%d”,&a[I][j])
}
}
printf(“\n”);
for(I=0;I<3;I++)
{
for(j=0;j<3;j++)
{
if(I==j)
{
d+=a[I][j];
}
}
printf(“the sum of diagonals is %d”,d);
getch();
}
Output:
The matrix is
234
567
898
the sum of diagonals is 16
ExNo: 8a RollNo:
Date: Name:
#include<stdio.h>
#include<conio.h>
void main()
{
int vol=0,i;
char text[100],ch;
clrscr();
printf("Enter the text\n");
scanf("%[^\n]",text);
i=0;
while((ch=tolower(text[i++]))!='\0')
{
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
++vol;
}
printf("The text cointains %d vowels",vol);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20],s2[20],st1[20],st2[20];
int c,l1,l2,t;
clrscr();
printf("Enter string 1\n");
scanf(" %[^\n]",s1);
printf("Enter string 2\n");
scanf(" %[^\n]",s2);
while(c<6)
{
printf("Enter 1 for string length\n");
printf("Enter 2 for string concatenation\n");
printf("Enter 3 for string compare\n");
printf("Enter 4 for string copy\n");
printf("Enter 5 for string reverse\n");
scanf("%d",&c);
switch(c)
{
case 1:
{
l1=strlen(s1);
l2=strlen(s2);
printf("\nlength of string 1=%d\n",l1);
printf("\nlength of string 2=%d\n",l2);
break;
}
case 2:
{
printf("the concatenated string is = %s",strcat(s1,s2));
break;
}
case 3:
{
t=strcmp(s1,s2);
if(t<0)
printf("String 1 is alphabetically above string2");
else
printf("String 2 is alphabetically above string1");
break;
}
case 4:
{
strcpy(st1,s1);
strcpy(st2,s2);
printf("String 1= %s\n",strcpy(s1,s2));
printf("String 2 = %s",strcpy(st2,st1));
break;
}
case 5:
{
printf("The reversed s1=%s\n",strrev(s1));
printf("The reversed s2=%s\n",strrev(s2));
break;
}
default:
printf("Wrong option");
}
getch();
clrscr();
}
getch();
}
Output:
Enter string1
Kalasalingam
Enter string2
University
ExNo: 9a RollNo:
Date: Name:
Swapping of numbers
#include<stdio.h>
#include<conio.h>
int swap(int*,int*);
void main()
{
int a,b;
clrscr();
printf("Enter the value of a and b\n");
scanf("%d%d",&a,&b);
printf("Values before swapping %d %d\n",a,b);
swap(&a,&b);
printf("Values after swapping %d %d\n",a,b);
getch();
}
int swap(int *x,int *y)
{
int temp=0;
temp=*x;
*x=*y;
*y=temp;
return;
}
Output:
ExNo: 9b RollNo:
Date: Name:
#include<stdio.h>
#include<conio.h>
void reorder(int n,int *x);
void main()
{
int i,n,*x;
clrscr();
printf("Enter the limit \n");
scanf("%d",&n);
printf("\n");
x=(int*)malloc(n*sizeof(int));
for(i=0;i<n;++i)
{
printf("i=%d x=",i+1);
scanf("%d",x+i);
}
reorder(n,x);
printf("Reordered list\n");
for(i=0;i<n;i++)
printf("i=%d v=%d\n",i+1,*(x+i));
getch();
}
void reorder(int n,int *x)
{
int i,item,temp;
for(item=0;item<n-1;item++)
for(i=item+1;i<n;i++)
if(*(x+i)<*(x+item))
{
temp=*(x+item);
*(x+item)=*(x+i);
*(x+i)=temp;
}
return;
}
Output:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define NULL 0
void main()
{
char *buffer;
clrscr();
if((buffer=(char*)malloc(10))==NULL)
{
printf("malloc failed\n");
exit(1);
}
strcpy(buffer,"hyderabad");
printf("Buffer contains %s\n",buffer);
if((buffer=(char*)realloc(buffer,15))==NULL)
{
printf("Reallocation failed\n");
exit(1);
}
printf("Buffer size modified\n");
printf("Buffer still contains %s",buffer);
strcpy(buffer,"secundrabad");
printf("\nBuffer contains %s",buffer);
free(buffer);
getch();
}
ExNo: 10a RollNo:
Date: Name:
Employee database
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct emp
{
char name[80];
int basic;
float hr;
float da;
float ta;
float gross;
}emp[10];
void main()
int i,n;
clrscr();
printf("Enter the no. of employees\n");
scanf("%d",&n);
printf("Collecting details\n");
for(i=0;i<n;i++)
{
printf("Enter the name \n");
scanf("%s",emp[i].name);
printf("Enter the basic salary\n");
scanf("%d",&emp[i].basic);
emp[i].hr=((emp[i].basic*10)/100);
emp[i].da=((emp[i].basic*12)/100);
emp[i].ta=((emp[i].basic*12)/100);
emp[i].gross=emp[i].basic+emp[i].ta+emp[i].hr+emp[i].da;
}
printf("Employee salary details \n");
printf("No.\t Name\t Basic\t HR\t DA\t TA\t Gross\n");
for(i=0;i<n;i++)
{
printf("%d %s %d %f %f %f
%f\n",i+1,emp[i].name,emp[i].basic,emp[i].hr,emp[i].da,emp[i].ta,emp[i].gross);
}
getch();
}
Student database
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct student
{
char name[80];
int roll;
int m1,m2,m3,m4,m5,m6;
float total,avg;
}stdb[10];
void main()
{
int i,n;
clrscr();
printf("Enter the no. of students\n");
scanf("%d",&n);
printf("Collecting details\n");
for(i=0;i<n;i++)
{
printf("Enter the name \n");
scanf("%s",stdb[i].name);
printf("Enter the roll no.\n");
scanf("%d",&stdb[i].roll);
printf("Enter the marks in subject1\n");
scanf("%d",&stdb[i].m1);
printf("Enter the marks in subject2\n");
scanf("%d",&stdb[i].m2);
printf("Enter the marks in subject3\n");
scanf("%d",&stdb[i].m3);
printf("Enter the marks in subject4\n");
scanf("%d",&stdb[i].m4);
printf("Enter the marks in subject5\n");
scanf("%d",&stdb[i].m5);
printf("Enter the marks in subject6\n");
scanf("%d",&stdb[i].m6);
stdb[i].total=(stdb[i].m1+stdb[i].m2+stdb[i].m3+stdb[i].m4+stdb[i].m5+stdb[i].m6);
stdb[i].avg=(stdb[i].total/6);
}
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fpin, *fpout;
int val, avg, sum=0;
int count=0;
if((fpin=fopen(“values.dat”,”r”))==NULL)
{
printf(“\n cannot open the designated file \n”);
}
else
{
while(!foef(fpin))
{
fscanf(fpin,”%d”,&val);
sum+=val;
count++;
}
}
avg=sum/count;
if((fpout=fopen(“average.res”,”w”))==NULL)
{
printf(“\n cannot open the designated file \n”);
}
else
{
fprintf(fpout,”the average of numbers of file values.dat is %d \n”,avg);
}
fclose(fpin);
fclose(fpout);
getch();
}
Output:
Values.dat
4
4
4
4
4
4
4
4
4
4
average.res
the average of numbers of file values.dat is 4
ExNo: 11b RollNo:
Date: Name:
Output:
Input1.txt
5
input2.txt
4
output.txt
54