0% found this document useful (0 votes)
166 views34 pages

C Programs 128

The document contains C code for matrix operations - addition, subtraction and multiplication. It includes functions to input the values of two matrices, perform the various operations and output the results. Input functions take the order and values of two matrices as input. Addition and subtraction functions check if the matrix dimensions are equal before performing the operation element-wise. Multiplication function checks if the number of columns of first matrix is equal to rows of second matrix before multiplying the matrices and storing the result.

Uploaded by

maheswaranapk
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 DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views34 pages

C Programs 128

The document contains C code for matrix operations - addition, subtraction and multiplication. It includes functions to input the values of two matrices, perform the various operations and output the results. Input functions take the order and values of two matrices as input. Addition and subtraction functions check if the matrix dimensions are equal before performing the operation element-wise. Multiplication function checks if the number of columns of first matrix is equal to rows of second matrix before multiplying the matrices and storing the result.

Uploaded by

maheswaranapk
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 DOC, PDF, TXT or read online on Scribd
You are on page 1/ 34

ExNo: 1a RollNo:

Date: Name:

Program for Calculating Simple Interest

#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:

Enter the principal amount


1000
Enter the number of years
6
Enter the rate of interest
3
The simple interest is 1800.000000
ExNo: 1b RollNo:
Date: Name:

Program For Swapping Of Numbers

#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:

Enter two numbers


12
13
The swapped numbers are
a = 13
b = 12
ExNo: 2a RollNo:
Date: Name:

Program To Print Reverse Of An Integer

#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:

Enter the number


17654
Reverse number is 45671
ExNo: 2b RollNo:
Date: Name:

Program For Sum Of Digits

#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:

Enter the number


12456
Sum of digits is 18
ExNo: 2c RollNo:
Date: Name:

Finding A Number Is Palindrome Or Not

#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:

Enter the number


959
959 is a PALIDROME
ExNo: 3a RollNo:
Date: Name:

Program To Find Factorial Of A Number

#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:

Enter the number


6
The factorial is 720
ExNo: 3b RollNo:
Date: Name:

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


ExNo: 3c RollNo:
Date: Name:

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:

Enter two numbers


21
3
1.ADD 2.SUBSTRACT 3.MULTIPLY 4.DIVIDE
Enter the option
4
Division result = 7
ExNo: 4a RollNo:
Date: Name:

Program To Find If A Number Is Odd Or Even

#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:

Enter any number


12
Even
ExNo: 4b RollNo:
Date: Name:

Program To Find The Smallest And The Largest Number

#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:

Program To Find Factorial Using Recursive Fn

#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:

Program To Print GCD

#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:

Enter two numbers


8
16
The GCD of 8 and 16 is 8
ExNo: 6a RollNo:
Date: Name:

Program To Find Average Of N Nos. Using Array

#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:

Enter the limit (<=10)


5
Enter the 5 values
10
12
14
15
14
Sum = 65
Average = 13.000000
ExNo: 6b RollNo:
Date: Name:

Program To Sort An Array

#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:

Program To Search An Element

#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:

Program To Print Addition, Substraction & Multiplication Of Matrix

#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:

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
ExNo: 7b RollNo:
Date: Name:

Sum of diagonal Of a Matrix

#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:

Program To Find Vowels In String

#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:

Enter the text


Kalasalingam university
The text contains 9 vowels
ExNo: 8b RollNo:
Date: Name:

Program For String Handling Functions

#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

Enter 1 for string length


Enter 2 for string concatenation
Enter 3 for string compare
Enter 4 for string copy
Enter 5 for string reverse
2
The concatenated string = kalasalingamuniversity

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:

Enter the value of a and b


45
Values before swapping
45
Values after swapping
54

ExNo: 9b RollNo:
Date: Name:

Sorting the array of numbers

#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:

Enter the limit


3
I=1 x=89 I=2 x=78 I=3 x=90
Reordered list
78 89 90
ExNo: 9c RollNo:
Date: Name:

Dynamic memory allocation

#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();
}

ExNo: 10c RollNo:


Date: Name:

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);
}

printf("Students marks details \n");


printf("Roll\t Name\t Marks1\t Marks2\t Marks3\t Marks4\t Marks5\t Marks6\t Total\t
Average\n");
for(i=0;i<n;i++)
{
printf("%d\t %s\t %d\t %d\t %d\t %d\t %d\t %d\t %f\t
%f\t",stdb[i].roll,stdb[i].name,stdb[i].m1,stdb[i].m2,stdb[i].m3,stdb[i].m4,stdb[i].m5,stdb
[i].m6,stdb[i].total,stdb[i].avg);
printf("\n \n");
}
getch();
}
ExNo: 11a RollNo:
Date: Name:

Average of ‘n’ numbers using files

#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:

Merging of two files


#include<stdio.h>
#include<conio.h>
void main()
{
int x;
FILE *f1, *f2;
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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy