0% found this document useful (0 votes)
80 views25 pages

CPP Lab Manual Final

The document contains 11 C programming problems and their solutions. The problems cover topics like finding the sum and occurrence of a digit in a number, calculating the GCD and LCM of two numbers, finding prime numbers in a range, calculating statistics of real numbers in an array, performing binary and linear searches, matrix operations like transpose and multiplication, sorting arrays using bubble sort, and storing and calculating statistics of student data in structures.

Uploaded by

Chinmayee Bhat
Copyright
© © All Rights Reserved
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)
80 views25 pages

CPP Lab Manual Final

The document contains 11 C programming problems and their solutions. The problems cover topics like finding the sum and occurrence of a digit in a number, calculating the GCD and LCM of two numbers, finding prime numbers in a range, calculating statistics of real numbers in an array, performing binary and linear searches, matrix operations like transpose and multiplication, sorting arrays using bubble sort, and storing and calculating statistics of student data in structures.

Uploaded by

Chinmayee Bhat
Copyright
© © All Rights Reserved
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

2.

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()
{

int num, digit, rem,sum=0,temp,


count=0; printf("Enter the
Number\n"); scanf("%d",&num);

printf("Enter the digit to be find in the Number\n");


scanf("%d", &digit);
temp=num;

while(num!=0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
if(rem==digit)

count++;
}

printf("The Sum of all the digits of %d is %d\n", temp,sum); printf("The digit


%d is occurred for %d times\n", digit, count); return 0;}

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

4. Write a C program to print the prime numbers in a given range.

#include <stdio.h>

#include <stdlib.h>
int main()
{
int sr,er,flag,i,j, c=0;

printf("enter the starting and ending


range\n"); scanf("%d%d",&sr,&er);
printf("prime numbers are\n");
for(i=sr;i<=er;i++)
{
flag=0;
for(j=2;j<=(i/2);j++)
{

if(i%j==0)
{
flag=1;
break;
} }
if(flag==0)
{

c++;
printf("%d\t",i);
}
}
if(c==0)

printf("\n\n NULL\n\nThere is no prime number with in the given


range\n"); else

printf("\n\nThere are %d prime numbers within the given range\n",


c); return 0;}

5.Write a C program to input N real numbers in 1-D array. Compute


mean, variance and Standard Deviation. Mean= sum/N, Variance = Σ (Xi-
mean)2/N, STD Deviation= √variance.

#include <stdio.h>
#include <stdlib.h>

int main()
{
float a[10],sum=0,sumv=0,mean,var,std;

int i,n;

printf("Enter the number\n");

scanf("%d",&n);

printf("Enter the numbers\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);

printf("\n The mean= %f \n the variance=%f \n standard deviation=%f


\n", mean,var,std);

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

printf("Enter the key element to search\n");


scanf("%d",&key);
l=0;

h=n-1;
while(l<=h)
{
mid=(l+h)/2;
if(a[mid]==key)
{

printf("The key if found at the position


%d",mid+1); exit(0);

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

printf("The entered matrix is\n");


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}

for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[j][i]=a[i][j];

printf("The transpose of the matrix is\n");


for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",b[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]);

printf("Enter the key element \n");


scanf("%d",&key);

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)
{

printf("The key element is found at position %d",i+1);


exit(0);

printf("The Key element is not found in the list\n");

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

printf("Enter Your Choice\n");

printf("1. Ascending order sorting\n 2. Descending order


sorting\n"); scanf("%d",&ch);

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;

void ascending(int b[30] , int n)


{
int i,j,temp;
for(i=1;i<n;i++)
for(j=0;j<n-i-1;j++)

if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}

void descending(int b[30],int n)


{
int i,j,temp;
for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(b[j]<b[j+1])

{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}

10. Write a C program using functions to read two matrices A (M x N) and B


(P x Q) and to compute the product of A and B if the matrices are compatible
for multiplication.

#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("Enter the order of matrix A\n");


scanf("%d%d",&m,&n);
printf("Enter the order of matrix B\n");
scanf("%d%d",&p,&q);
if(n!=p)
{

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

printf("The resultant matrix is\n");


display(c,m,q);
return 0;
}

void read(int x[10][10], int r, int c)


{

int i,j;
(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&x[i][j]);
}

void display(int x[10][10], int row, int col)


{
int i,j;
for(i=0;i<row;i++)
{

for(j=0;j<col;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
}

void multiply(int a[10][10],int b[10][10],int c[10][10],int m,int n,int


q){

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

60-79 First Class

40-59 Second Class

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

printf("PLEASE ENTER %d STUDENT DETAILS\n",n);

for(i=0;i<n;i++)
{

printf("Enter the details of student


%d\n",i+1); printf("Enter the name\n");
scanf("%s",&s[i].name); printf("Enter the
ID\n");

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

printf("\nThe sum is %d\n",


sum); return 0;

13. Write a C program to find if a given string is a palindrome or not.

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

printf("The given string is a


palindrome\n"); else

printf("The given message is not a palindrome


\n"); return 0;

14. Write a C program to copy contents of one file to another file.

#include <stdio.h>

#include <stdlib.h>

int main()
{
FILE *f1,*f2;
char c;

printf("PLEASE ENTER YOUR INFORMATION\n\n\n\n");


f1=fopen("INPUT", "w");
while((c=getchar())!=EOF)
putc(c,f1);

fclose(f1);

f1=fopen("INPUT", "r");
f2=fopen("OUTPUT","w");
while((c=getc(f1))!=EOF)
putc(c,f2);

fclose(f1);

fclose(f2);

printf("The stored information in file


are\n");
f2=fopen("OUTPUT","r");

while((c=getc(f2))!=EOF)
putchar(c);

fclose(f
2);
return
0;
}

15..Write a C program using functions readmat ( ), rowsum ( ), colsum ( ),


totsum ( ) and printmat( ) to read the values into a two dimensional array A,
find the sum of all the elements of a row, sum of all the elements of a column,
find the total sum of all the elements of the two dimensional array A and
print the results.

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

printf("Enter the %d x %d matrix\n",


m,n); readmat();

printf("The Entered matrix is\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];
}

printf("the sum of elements of column %d is %d\n", i+1,sum);


sum=0;
}

void rowsum()
{
int sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum=sum+a[i][j];
}

printf("the sum of elements of row %d is %d\n", i+1,sum);


sum=0;
}

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

You might also like