You are on page 1of 32

Computer Programming Laboratory

Sub Code : 21CS116 Credits : 01


Hrs/Week : 0+0+3+0
Course Learning Objectives:
This Course will enable students to
1. Get the idea of how to debug and execute C programs
2. Make use of flowcharts and algorithms to analyze and demonstrate various C programming
concepts.
3. Experiment with different types of low complexity and high complexity programs using C
language

PART A
1. Write a C program to find the roots of a quadratic equation ax2+bx+c=0
2. Write a C program to find the sum of all the digits and occurrence of a digit in the number.
3. Write a C program to find the GCD and LCM of given two numbers using Euclid’s method.
4. Write a C program to print the prime numbers in a given range.
5. Write a C program to find if a given string is a palindrome or not.
6. 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.]
7. Write a C program to read N integers into an array A and find the sum of elements using
pointers.

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

PART B
9. 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.

10. Write a C program to input N integer numbers into a single dimension array, sort them in to
ascending order using selection sort technique, and then to print both the given array and the
sorted array with suitable headings.

11. Write a C program to transpose a matrix of order M x N and find the trace of the resultant
matrix.

12. 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.

13. 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.

Page 1
14. 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.

15. 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

16. Write a C program, to implement a bubble sort technique using function to sort given N
integers in ascending/ descending order as per user’s preference

Course Outcomes:
At the end of the course the student will be able to:
1. Develop programs using the concept of decision making statements and arrays.
2. Reduce the complexity of the programs by making use of functions.
3. Design the programs that can perform operations on matrices.
4. Apply the programming concepts to implement simple algorithms like sorting and searching.
5. Develop and experiment with programs using concepts like pointers, files, structures.

Mapping of COs & POs/PSOs:

COs Program Outcomes / Program Specific Outcomes

PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO PO PO PSO1 PSO2 PSO3
10 11 12
CO1 H H H M M H H H
CO2 H H H M H H H H
CO3 H H L H M H H H H
CO4 H H M H M H H H H
CO5 H H M H M H H H H

L: Slight (Low) M: Moderate (Medium) H: Substantial (High)

PART A : Low Complexity Programs:


1. Write a C program to find the roots of a quadratic equation ax2+bx+c=0

Page 2
#include <stdio.h>
#include <stdlib.h>
int main()
{
float a,b,c,x1,x2,d;
printf("Enter the coeffients for a,b,c\n");
scanf("%f%f%f", &a,&b,&c);

if(a*b*c==0)
{
printf("\nRoots cant be find\n");
exit(0);
}
d=b*b-4*a*c;

if(d==0)
{

x1=x2= -b/(2*a);
printf("\nThe roots are real and equal\n");
printf("\nx1=%f\n x2=%f\n", x1,x2);

}
else if(d>0)
{
x1= (-b + sqrt(d))/(2*a);
x2= (-b - sqrt(d))/(2*a);
printf("\nThe roots are real and distinct\n");
printf(" \nx1=%f\n x2=%f\n", x1,x2);
}
else
{
x1= -b/(2*a);
x2=sqrt(fabs(d))/ (2*a);
printf("\nThe roots are real and imaginary\n");
printf("\n(x1+ix2)= %f+i%f\n",x1,x2);
printf("\n(x1-ix2)= %f-i%f\n", x1,x2);

Page 3
}
return 0;
}

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

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

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

Page 7
Page 8
5.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;
}

Page 9
6. 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("\nthe mean= %f\n the variance=%f\n standard deviation=%f\n",
mean,var,std);
return 0;
}

Page 10
7. 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;
}

Page 11
8. 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(f2);
return 0;
}

Page 12
PART B : High Complexity Programs:
9.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;
}

Page 13
Page 14
10.Write a C program to input N integer numbers into a single dimension
array, sort them in to ascending order using selection sort technique, and then
to print both the given array and the sorted array with suitable headings.
#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;
}

Page 15
Page 16
11.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)

Page 17
{
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;
}

Page 18
Page 19
12.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;

Page 20
for(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];
}
}
}
}

Page 21
Page 22
13.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]);
}

Page 23
void 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;
}

Page 24
}

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

Page 25
14.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 to be searched\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");

Page 26
Page 27
15. Write a C program to enter the information like name, register number,
marks in 6 subjects of N studentsinto an array of structures, 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);

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

Page 29
Page 30
16.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;

Page 31
}

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

**************************************************************

Page 32

You might also like