C Programs Part III
C Programs Part III
Output:
Enter the principal amount
1000
Enter the number of years
6
Enter the rate of interest
3
The simple interest is 1800.000000
printf("b = %d \n",b);
getch();
}
Output:
Enter two numbers
12
13
The swapped numbers are
a = 13
b = 12
Output:
Enter the number
17654
Reverse number is 45671
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:
Enter the number
12456
Sum of digits is 18
Output:
Output:
Enter the number
6
The factorial is 720
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:
The sum of the series is: 385
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:
Output:
Enter two numbers
21
3
1.ADD 2.SUBSTRACT 3.MULTIPLY 4.DIVIDE
Enter the option
4
Division result = 7
Output:
Enter any number
12
Even
Output:
Enter 3 numbers
14
13
8
The largest number is = 14
The smallest number is = 8
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
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:
Enter two numbers
8
16
The GCD of 8 and 16 is 8
Output:
Enter the limit (<=10)
5
Enter the 5 values
10
12
14
15
14
Sum = 65
Average = 13.000000
{
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
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
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:
Enter the order of first matrix
3
3
Enter the value of the matrix A
1 2 3
4 5 6
7 8 9
Enter the order of second matrix
3
3
Enter the value of the matrix B
11 2 3
14 11 15
17 8 3
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
Output:
The matrix is
234
567
898
the sum of diagonals is 16
Output:
Enter the text
Kalasalingam university
The text contains 9 vowels
Output:
Enter string1
Kalasalingam
Enter string2
University
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:
Enter the value of a and b
45
Values before swapping
45
Values after swapping
54
Output:
Enter the limit
3
I=1 x=89 I=2 x=78 I=3 x=90
Reordered list
78 89 90
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();
}
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++)
{
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
F1=fopen(“output.txt”,”w”);
F2=fopen(“input1.txt”,”r”);
While(!foef(f2))
{
fscanf(f2,”%d”,&x);
fprintf(f1,”%d”,x);
}
fclose(f2);
f2=fopen(“input2.txt”,”r”);
{
while(!foef(f2))
{
fscanf(f2,”%d”,&x);
fprintf(f1,”%d”,x);
}
fclose(f2);
fclose(f1);
getch();
}
Output:
Input1.txt
5
input2.txt
4
output.txt
54