Computer Programing Laboratory. (CPL.)
Computer Programing Laboratory. (CPL.)
PROGRAMING
LABORATORY.
(CPL.)
SUMANTHKUMAR.G.HIREMATH.
CONTENT
1. Design and develop a flowchart or an algorithm that takes three coefficients (a, b, and c) of a
Quadratic equation (ax2+bx+c=0) as input and compute all possible roots. Implement a C
program for the developed flowchart/algorithm and execute the same to output the possible
roots for a given set of coefficients with appropriate messages.
2. Design and develop an algorithm to find the reverse of an integer number NUM and
check whether it is PALINDROME or NOT. Implement a C program for the developed
algorithm that takes an integer number as input and output the reverse of the same
with suitable messages. Ex: Num: 2014, Reverse: 4102, Not a Palindrome.
3a. Design and develop a flowchart to find the square root of a given number N.
Implement a C program for the same and execute for all possible inputs with
appropriate messages. Note: Don’t use library function sqrt(n).
3b. Design and develop a C program to read a year as an input and find whether it is leap year
or not. Also consider end of the centuries.
4. Design and develop an algorithm for evaluating the polynomial f(x) = a4x4 + a3x3 + a2x2
+a1x + a0, for a given value of x and its coefficients using Horner’s method. Implement a C
program for the developed algorithm and execute for different sets of values of coefficients and
x.
6. Develop, implement and execute a C program that reads N integer numbers and arrange them
in ascending order using Bubble Sort technique.
7. Develop, implement and execute a C program that reads two matrices A(mxn ) and B(pxq
) and Compute the product A and B. Read matrix A in row major order and matrix B in
column major order. Print both the input matrices and resultant matrix with suitable headings
and in matrix format. Program must check the compatibility of orders of the matrices for
multiplication. Report appropriate message in case of incompatibility.
10a. Design and develop a C function RightShift(x ,n) that takes two integers x and n as input
and returns value of the integer x rotated to the right by n positions. Assume the integers are
unsigned. Write a C program that invokes this function with different values for x and n and
tabulate the results with suitable headings
10 b. Design and develop a C function isprime(num) that accepts an integer argument and
returns 1 if the argument is prime, a 0 otherwise. Write a C program that invokes this function
to generate prime numbers between the given range.
11. Draw the flowchart and write a recursive C function to find the factorial of a number, n!,
defined by fact(n)=1, if n=0. Otherwise fact(n)=n*fact(n-1). Using this function, write a C
program to compute the binomial coefficient (nCr). Tabulate the results for different values of n
and r with suitable messages.
12. Given two university information files “studentname.txt” and “usn.txt” that contains
students Name and USN respectively. Write a C program to create a new file called “output.txt”
and copy the content of files “studentname.txt” and “usn.txt” into output file in the sequence
shown below. Display the contents of output file “output.txt” on to the screen.
13. Write a C program to maintain a record of “n” student details using an array of structures
with four fields (Roll number, Name, Marks, and Grade). Each field is of an appropriate data
type. Print the marks of the student given student name as input.
14. Write a C program using pointers to compute the sum, mean and standard deviation of all
elements stored in an array of n real numbers.
2
Page
PROGRAM 1
QUADRATIC EQUATION.
Design and develop a flowchart or an algorithm that takes three coefficients (a, b, and c) of a Quadratic equation
(ax2+bx+c=0) as input and compute all possible roots. Implement a C program for the developed
flowchart/algorithm and execute the same to output the possible roots for a given set of coefficients with
appropriate messages.
Program:
#include <stdio.h>
#include<conio.h>
#include <math.h>
void main()
{
float a,b,c;
float d,r,r1,r2;
clrscr();
printf("Enter the three co-efficient :\n");
scanf("%f%f%f",&a,&b,&c);
if (a* b* c == 0)
{
printf("\n Invalid Input ");
}
else
{
d = b * b - 4 * a * c;
r=sqrt(fabs(d));
if (d > 0)
{
r1 = (-b + r) / (2.0*a);
r2 = (-b - r) / (2.0*a);
printf("\n The roots are real and distinct\n");
printf("\n The roots are \n 1) r1=%f\t\t \n 2) r2=%f",r1,r2);
}
else if (d == 0)
{
r1 = r2 = -b/(2.0*a);
printf("\n The roots are real and equal\n");
printf("\n The roots are: \n 1) r1=r2=%f",r1);
}
else
{
r1 = -b / (2.0 * a);
r2 = r / (2.0*a);
printf("\n The roots are real and imaginary\n");
printf("\n The roots are:\n 1) %f +i %f \t\t\n 2) %f -i %f",r1,r2,r1,r2);
}
}
3
getch();
Page
PROGRAM 2
PALINDROME.
Design and develop an algorithm to find the reverse of an integer number NUM and check whether it is
PALINDROME or NOT. Implement a C program for the developed algorithm that takes an integer number as
input and output the reverse of the same with suitable messages. Ex: Num: 2014, Reverse: 4102, Not a Palindrome.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int rem,rev,m,n;
clrscr();
printf("enter the number:\t");
scanf("%d",&n);
rev=0;
m=n;
while(n!=0)
{
rem=n%10;
n=n/10;
rev=rev*10+rem;}
printf("\nreversed number is %d\n",rev);
if(m==rev)
{
printf("number is palindrome");
}
else
{
printf("number is not palindrome");
}
getch();
}
4
Page
PROGRAM 3
A)SQUARE ROOT.
Design and develop a flowchart to find the square root of a given number N. Implement a C program for the same
and execute for all possible inputs with appropriate messages. Note: Don’t use library function sqrt(n).
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
float n,m;
clrscr();
printf("enter the number:\t");
scanf("%f",&n);
m=n;
if(n>0)
{
for(i=0;i<20;i++)
n=(m+n*n)/(2*n);
printf("square root of %f is %f",m,n);
}
else
{
printf("square root cannot be obtained for negative numbers");
}
getch();
}
B)LEAP YEAR.
Design and develop a C program to read a year as an input and find whether it is leap year or not. Also consider
end of the centuries.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int y;
clrscr();
printf("enter year:\t");
scanf("%d",&y);
if((y%4==0)&(y%100!=0)||(y%400==0))
printf("%d is leap year",y);
else
printf("%d is not leap year",y);
getch();
5
}
Page
PROGRAM 4
HORNER’S POLYNOMIAL METHOD.
Design and develop an algorithm for evaluating the polynomial f(x) = a4x4 + a3x3 + a2x2 +a1x + a0, for a given
value of x and its coefficients using Horner’s method. Implement a C program for the developed algorithm and
execute for different sets of values of coefficients and x.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,x;
float a[10],sum;
clrscr();
printf("enter degree:\t");
scanf("%d",&n);
printf("enter the coefficients one by one\n");
for(i=0;i<=n;i++)
{
scanf("%f",&a[i]);
}
printf("enter the value of X:\t");
scanf("%d",&x);
sum=0;
for(i=n;i>0;i--)
{
sum=(sum+a[i])*x;
}
sum+=a[0];
printf("sum=%f",sum);
getch();
}
6
Page
PROGRAM 5
SIN(X) USING TAYLORS SERIES.
Write C Program to compute Sin(x) using Taylor series approximation given by
Sin(x) = x - (x3/3!) + (x5/5!) - (x7/7!) + …….Compare the result with the built- in Library function and print both
the results.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int deg,n,sign;
float acc,num,den,sinx,x,term;
clrscr();
printf("enter degree:\t");
scanf("%d",°);
printf("enter accuracy:\t");
scanf("%f",&acc);
x=(deg*3.142)/180;
sinx=x;
num=x;
den=1;
n=2;
sign=-1;
do
{
num=num*x*x;
den=den*(n)*(n+1);
term=num/den;
term=term*sign;
sinx=sinx+term;
sign=sign*-1;
n=(n+2);
}
while(acc<=(fabs((sinx)-sin(x))));
printf("\nwithout built in function:\n");
printf("sin(%d)=%f.\n",deg,sinx);
printf("with built in function:\n");
printf("sin(%d)=%f.",deg,sin(x));
getch();
}
7
Page
PROGRAM 6
BUBBLE SORT.
Develop, implement and execute a C program that reads N integer numbers and arrange them in
ascending order using Bubble Sort technique.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int temp,i,j,n,a[100];
clrscr();
printf("enter total number of numbers to be sorted:\t");
scanf("%d",&n);
printf("enter numbers to be sorted one by one:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("numbers after sorting are:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
8
Page
PROGRAM 7
MATRIX MULTIPLICATION.
Develop, implement and execute a C program that reads two matrices A(mxn ) and B(pxq ) and Compute the
product A and B. Read matrix A in row major order and matrix B in column major order. Print both the input
matrices and resultant matrix with suitable headings and in matrix format. Program must check the compatibility
of orders of the matrices for multiplication. Report appropriate message in case of incompatibility.
Program:
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int i,j,k,m,n,p,q,a[10][10],b[10][10],c[10][10];
clrscr();
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("invalid order for matrix multiplication");
getch();
exit(0);
}
printf("enter the elements of matrix A(enter row wise):\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the elements of matrix B(enter row wise):\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
9
Page
PROGRAM 8
BINARY SEARCH.
Develop,implement and execute a C program to search a Name in a list of names using Binary Search.
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int start=1, end=15, mid,i,flag=0,value,n;
char name[20][20], nm[15];
clrscr();
printf("enter the number of names(enter less than 9):\t");
scanf("%d",&n);
printf("Enter %d names in alphabetic order: \n",n);
for (i=1; i<=n; i++)
scanf("%s",name[i]);
printf("\n Enter the name to be search:\t");
scanf("%s",nm);
mid = (start+end)/2;
while(strcmp(nm,name[mid])!=0 && start<=end)
{
value = strcmp(nm,name[mid]);
if(value > 0)
{
start = mid + 1;
mid = (start + end) / 2;
}
else
{
end = mid - 1;
mid = (start + end) / 2;
}
}
if(strcmp(nm,name[mid]) == 0)
{
flag = 1;
}
if(flag == 1)
printf("\n The name %s is found at position %d successfully",nm,mid);
else
printf("\n The name %s is not found",nm);
getch();
11
}
Page
PROGRAM 9
A) STRING COPY.
Write and execute a C program that implements string copy operation STRCOPY(str1,str2) that copies
a string str1 to another string str2 without using library function.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50],ch;
int j,con_count, a=0, e=0, i=0, o=0, u=0;
clrscr();
printf("\n Enter the sentence: ");
gets(str);
con_count = 0;
for(j=0; j<strlen(str);j++)
{
ch = tolower(str[j]);
if(ch=='a' ||ch=='e' ||ch=='i' ||ch=='o' ||ch=='u' || ch ==' ' )
{
if(ch=='a')
{
a++;
}
if(ch=='e')
{
e++;
}
if(ch=='i')
{
i++;
}
if(ch=='o')
{
o++;
}
if(ch=='u')
{
u++;
}
}
else
con_count++;
}
printf("\n Frequency of vowel 'a'=%d ",a);
printf("\n Frequency of vowel 'e'=%d ",e);
printf("\n Frequency of vowel 'i'=%d ",i);
printf("\n Frequency of vowel 'o'=%d ",o);
printf("\n Frequency of vowel 'u'=%d ",u);
13
PROGRAM 10
A) RIGHT SHIFT.
Design and develop a C function RightShift(x ,n) that takes two integers x and n as input and returns
value of the integer x rotated to the right by n positions. Assume the integers are unsigned. Write a C
program that invokes this function with different values for x and n and tabulate the results with suitable
headings.
Program:
#include<stdio.h>
#include<conio.h>
return x;
}
Page
B)PRIME NUMBERS.
Design and develop a C function isprime(num) that accepts an integer argument and returns 1 if the
argument is prime, a 0 otherwise. Write a C program that invokes this function to generate prime
numbers between the given range.
Program:
#include<stdio.h>
#include<conio.h>
int isprime(int x)
{
int i,flag=1;
for(i=2;i<=x/2;i++)
{
if(x%i==0);
{
flag=0;
break;
}
}
return(flag);
}
15
Page
PROGRAM 11
FACTORIAL(nCr).
Draw the flowchart and write a recursive C function to find the factorial of a number, n!, defined by
fact(n)=1, if n=0. Otherwise fact(n)=n*fact(n-1). Using this function, write a C program to compute the
binomial coefficient (nCr). Tabulate the results for different values of n and r with suitable messages.
Program:
#include<stdio.h>
#include<conio.h>
#include<process.h>
PROGRAM 12
UNIVERSITY INFORMATION.
Given two university information files “studentname.txt” and “usn.txt” that contains students Name and
USN respectively. Write a C program to create a new file called “output.txt” and copy the content of files
“studentname.txt” and “usn.txt” into output file in the sequence shown below. Display the contents of
output file “output.txt” on to the screen.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
FILE *fp1,*fp2,*fp3;
char usn[20],name[20];
clrscr();
fp1=fopen("studname.txt","r");
if(fp1==NULL)
{
printf("File not found");
exit(0);
}
fp2=fopen("studusn.txt","r");
if(fp2==NULL)
{
printf("File not found");
exit(0);
}
fp3=fopen("output.txt","w");
while(!feof(fp1)&&!feof(fp2))
{
fscanf(fp1,"%s",name);
fscanf(fp2,"%s",usn);
fprintf(fp3,"%s%10s\n",name,usn);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp3=fopen("output.txt","r");
printf("\n......................\n");
printf("Name USN \n");
printf(".......................\n");
while(!feof(fp3))
{
fscanf(fp3,"%s",name);
fscanf(fp3,"%s\n",usn);
printf("%s %10s\n",name,usn);
}
17
fclose(fp3);
getch();
Page
PROGRAM 13
STUDENT DETAILS.
Write a C program to maintain a record of “n” student details using an array of structures with four
fields (Roll number, Name, Marks, and Grade). Each field is of an appropriate data type. Print the marks
of the student given student name as input.
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
struct student
{
char name[50];
int roll;
int marks;
char grade;
};
void main()
{
struct student s[10];
int i,flag=0,n;
char dname[50];
clrscr();
printf("Enter the number of students:\t");
scanf("%d",&n);
printf("Enter information of %d students:\n",n);
for(i=0;i<n;i++)
{
fflush(stdin);
printf("\nEnter the roll number:\t ");
scanf("%d",&s[i].roll);
printf("Enter name:\t\t ");
scanf("%s",s[i].name);
printf("Enter marks:\t\t ");
18
scanf("%d",&s[i].marks);
Page
fflush(stdin);
19
Page
PROGRAM 14
SUM, MEAN, STANDARD DEVIATION.
Write a C program using pointers to compute the sum, mean and standard deviation of all elements
stored in an array of n real numbers.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a[10],*pointer, mean, std, sum=0,sumstd=0;
int n, i;
clrscr();
printf("Enter the no of elements:\t");
scanf("%d",&n);
printf("Enter the array elements:\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
pointer=a;
for(i=0;i<n;i++)
{
sum=sum+*pointer;
pointer++;
}
mean=sum/n;
pointer=a;
for(i=0;i<n;i++)
{
sumstd=sumstd+pow((*pointer-mean),2);
pointer++;
}
std=sqrt(sumstd/n);
printf("\nsum = %f\n",sum);
printf("Mean = %f\n",mean);
printf("standard deviation = %f\n",std);
getch( );
20
}
Page
VIVA QUESTIONS
1) What is an algorithm?
2) What is high level language?
3) What is compiler?
4) What are tokens?
5) What are identifiers?
6) What are keywords? How many keywords is their in C programming language?
7) What is a variable?
8) What are the rules to be followed while declaring a variable?
9) What is a constant?
10) What is a datatype? What are the different dataypes?
11) What are escape sequence characters?
12) List the size and range of basic datatypes.
13) What is the difference between a character and string?
14) What is implicit type conversion and explicit type conversion (type casting)?
15) What is precedence of an operator means?
16) What is the difference between printf() and puts() functions.
17) What is function? What are the advantages of functions?
18) What are the different types of functions?
19) What is a library function?
20) What is calling function and called function?
21) What is the meaning of actual parameter and formal parameter?
22) What is the purpose of switch statement? Explain with syntax.
23) What is loop? List the differences between pre-test and post-test loop.
24) What is the meaning of event controlled loop and counter controlled loop?
25) What are the advantages of loops?
26) What is control statement? What are the various types of control statements available in
C language?
27) Explain for loop with syntax.
28) What is the difference between while and do-while loop?
29) What are unconditional control statements?
30) What is the use of break statement?
31) What is an array? What is the difference between an ordinary variable and an array variable?
32) What are the differences between recursion and iteration?
33) What is a pointer?
34) What is a NULL pointer?
35) What is a Structure? What are the differences between structures and arrays?
36) What is memory leak? Why it should be avoided.
37) Which header file should be included to use functions like malloc() and calloc()?
38) List string.h Library functions in C.
39) What is the purpose of main() function?
40) What is the difference between static and dynamic memory allocation in C?
21
Page