CPP Lab Manual Final
CPP Lab Manual Final
Write a C program to find the sum of all the digits and occurrence of a
digit in the number.
#include <stdio.h>
#include <stdlib.h>
int main()
{
while(num!=0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
if(rem==digit)
count++;
}
3 .Write a C program to find the GCD and LCM of given two numbers
using
Euclid’s method.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m,n,temp1,temp2,rem,gcd,lcm;
printf("Enter two integers m and n \n");
scanf("%d%d", &m, &n);
temp1=m;
temp2=n;
while(n!=0)
{
rem=m % n;
m=n;
n=rem;
}
gcd = m;
lcm = (temp1*temp2) / gcd;
printf("The gcd of %d and %d is = %d\n", temp1, temp2,
gcd); printf("The lcm of %d and %d is = %d", temp1,
temp2, lcm);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int sr,er,flag,i,j, c=0;
if(i%j==0)
{
flag=1;
break;
} }
if(flag==0)
{
c++;
printf("%d\t",i);
}
}
if(c==0)
#include <stdio.h>
#include <stdlib.h>
int main()
{
float a[10],sum=0,sumv=0,mean,var,std;
int i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%f",&a[i]);
sum=sum+a[i];
mean=sum/n;
for(i=0;i<n;i++)
sumv=sumv+pow(a[i]-mean,2); //sumv=sumv+(a[i]-mean)*(a[i]-mean);
printf("sumv=%f\n",sumv);
var=sumv/n;
std=sqrt(var);
return 0;
}
6.Write a C program to perform a binary search for a given key integer in a
single dimensional array of numbers in ascending order and report success
or failure in the form of a suitable message.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[20],n,mid,l,h,i,key;
printf("Enter the value for n\n");
scanf("%d",&n);
printf("enter %d elements in Ascending order \n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
h=n-1;
while(l<=h)
{
mid=(l+h)/2;
if(a[mid]==key)
{
}
if(key>a[mid])
l=mid+1;
else
h=mid-1;
}
printf("The key is not found\n");
return 0;
}
7.Write a C program to transpose a matrix of order M x N and find the
trace of the resultant matrix.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,m,n,a[10][10],b[10][10],trace=0;
printf("Enter the size of the matrix\n");
scanf("%d%d",&m,&n);
printf("Enter %d x %d matrix\n",m,n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[j][i]=a[i][j];
}
printf("\n");
}
if(m==n)
{
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(i==j)
trace=trace+b[i][j];
}
}
printf("The trace is %d\n",trace);
}
else
printf(" cant perform trace because its not a square matrix\n");
return 0;
}
8. Write a C program to perform a linear search for a given key integer in a
single dimensional array of numbers and report success or failure in the
form of a suitable message using functions.
#include <stdio.h>
#include <stdlib.h>
void linsearch(int b[100],int n, int key);
int main()
{
int i,n,a[100],key;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
linsearch (a,n,key);
return 0;
}
void linsearch(int b[100],int n,int key)
{
int i;
for(i=0;i<n;i++)
{
if(b[i]==key)
{
}
9. Write a C language, a Bubble sort program using function to sort given N
integers in ascending / descending order as per user’s preference.
#include <stdio.h>
#include <stdlib.h>
void ascending(int b[50],int n);
void descending(int b[50],int n);
int main()
{
int n,i,j,a[100],ch;
printf("Enter the value of N\n");
scanf("%d",&n);
printf("enter the %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
if(ch==1)
ascending(a,n);
else
descending(a,n);
printf("The sorted array is\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("Thank you\n");
return 0;
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
#include <stdio.h>
#include <stdlib.h>
void read( int x[10][10], int r, int c);
void display(int x[10][10], int row, int col);
void multiply(int a[10][10],int b[10][10],int c[10][10],int r,int col,int p);
int main()
{
int a[10][10],b[10][10],c[10][10],m,n,p,q,i,j,k;
printf("not possible\n");
exit(0);
}
printf("Enter the matrix A\n");
read(a,m,n);
printf("Enter the matrix B\n");
read(b,p,q);
printf("Entered matrix A is\n");
display(a,m,n);
printf("entered matrix b is\n");
display(b,p,q);
multiply(a,b,c,m,n,q);
int i,j;
(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&x[i][j]);
}
for(j=0;j<col;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
}
int i,j,k;
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
}
11.
.Write a C program to enter the information like name, register number, marks in 6 subjects of N
students into an array of structures, and find the average & display grade based on average for each
student.
Average Grade
80-100 Distinction
<40 Fail
#include <stdio.h>
#include <stdlib.h>
struct student
{
char name[100];
int id,marks[6];
float avg;
};
int main()
{
struct student s[100];
int i,j,n,sum=0;
printf("Enter the number of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&s[i].id);
printf("enter the marks in 6 subjects\n");
for(j=0;j<6;j++)
{
printf("subject %d\n",j+1);
scanf("%d",&s[i].marks[j]);
}
sum=0;
for(j=0;j<6;j++)
sum=sum+s[i].marks[j];
s[i].avg= sum/6;
printf("The average marks of student %d is %f\n",
i+1,s[i].avg); if(s[i].avg>=80 && s[i].avg<=100)
printf("grade is distinction\n");
else if(s[i].avg>=60 && s[i].avg<=79)
printf("The grade is first class\n");
else if(s[i].avg>=40 && s[i].avg<=59)
printf("The grade is second class\n");
else
printf("FAIL\n");
return 0;
}
12. Write a C program to read N integers into an array A and find the sum
of elements using pointers
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[100],n,I,sum=0;
int *p;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter the numbers\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
p=a;
for(i=0;i<n;i++)
{
sum=sum+*p;
p=p+1;
}
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char str1[100],str2[100];
int i,n;
printf("Enter the string1\n");
gets(str1);
n=strlen(str1);
for(i=0;i<n;i++)
str2[n-i-1]=str1[i];
str2[i]='\0';
if(strcmp(str1,str2)==0)
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *f1,*f2;
char c;
fclose(f1);
f1=fopen("INPUT", "r");
f2=fopen("OUTPUT","w");
while((c=getc(f1))!=EOF)
putc(c,f2);
fclose(f1);
fclose(f2);
while((c=getc(f2))!=EOF)
putchar(c);
fclose(f
2);
return
0;
}
#include <stdio.h>
#include <stdlib.h>
int m,n,i,j,a[10][10];
void readmat();
void colsum();
void totalsum();
void rowsum();
void printmat();
int main()
{
printf("Enter the order of the matrix\n");
scanf("%d%d",&m,&n);
printmat();
rowsum();
colsum();
totsum();
return 0;
}
void readmat()
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
oid printmat()
{int i,j; for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
void colsum()
{
int sum=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
sum=sum+a[j][i];
}
void rowsum()
{
int sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum=sum+a[i][j];
}
void totsum()
{
int sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum=sum+a[i][j];
}
}
printf("the sum of all elements in matrix is %d\n",sum);
}
16.SELECTION SORT:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[50],n,temp,i,j,pos;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter the numbers\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Entered values are\n");
for(i=0;i<n;i++)
printf("%d\t", a[i]);
for(i=0;i<n-1;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
If(a[j]<a[pos])
pos=j;
}
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
printf("\nThe sorted values are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
return 0;
}