R2021 Programming in C Lab Manual For Colleges
R2021 Programming in C Lab Manual For Colleges
Program No: 1A
File Name:
SOLVING QUADRATIC EQUATION
Ex. No:
Date: ___________
Aim:
To write a C program to find the roots of a quadratic equation.
Algorithm:
1. Start
2. Enter three coefficients a,b,c.
3. Calculate d = b*b – 4 * a a.
4. Check if d = 0 then roots are equal, and calculate
root1 = root2 = -b/(2.0 * a)
5. If d> 0 then roots are real and distinct. Calculate root1 = -b(2.0*a)
root1 = (-b + sqrt (d))/(2*a)
root2 = (-b – sqrt (d))/(2*a)
6. If d<0 then roots are imaginary root contains real and imaginary parts.
Calculate realp = -b(2*a),
imagp = sqrt(d)/(2*a) and
root1 = real p+iimgp
root2 = real p-iimgp.
7. Display root1, root2.
8. Stop.
Program:
#include<stdio.h>
#include<math.h>
int main()
{
float root1,root2,realp,imgp,a,b,c,d;
clrscr()
scanf(“%f%f%f”,&a,&b,&c);
d=b*b-4*a*c;
if(d= =0)
{
printf(“roots are equal\n”);
root1=root2=-b/(2*a);
printf(“root1=root2=%f\n”, root1);
}
else if(d>0)
{
printf(“roots are real and distinct\n”);
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
printf(“root1=%f\n”,root1);
printf(“root2=%f\n”,root2);
}
else
{
printf(“roots are imaginary\n”);
realp=-b/(2*a);
imgp=sqrt((-d))/(2*a);
printf(“root1=%f+i %f\n”,realp,imgp);
printf(“root2=%f- i %f\n”,realp,imgp);
getch( );
Output:
Program No: 1B
File Name: FINDING BIGGEST AMOUNG TWO NUMBERS
Ex. No: USING TERNARY OPERATOR
Date: ___________
Aim:
To write a c program to find biggest among two numbers using ternary
operator.
Algorithm:
1. Start.
2. Read two numbers A and B.
3. Check c = (A>B)? A:B. That is if A is bigger than B then C=A otherwise
C=B.
4. Print bigger value as C.
5. Stop.
Program:
#include<stdio.h>
#include<conio.h>
void main()
int A,B,C;
clrscr();
scanf(“%d”,&A);
scanf(“%d”,&B);
C=(A>B) ? A : B;
getch();
Output:
1. Start.
2. Read centigrade value C.
3. Calculate F = C* (9/5)+32
4. Print Fahrenheit value F.
5. Stop.
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
float c,f;
clrscr();
printf(“CONVERSION OF CENTIGRADE TO FARENHIT\n”);
scanf(“%f”,&c);
printf(“CENTIGRADE VALUE: %f\n”,c);
printf(“\n FORMULA USED\n”);
printf(“\n F = C*(9/5) + 32\n”);
f=c*9/5+32;
printf(“\n FARENHEIT VALUE:%\n”,f);
getch();
}
Output:
Program No: 2A
File Name:
CHECKING LEAP YEAR OR NOT
Ex. No:
Date: ___________
Aim:
Algorithm:
1. Start.
2. Read year.
3. Check whether year is divisible by 4 and 400 and not divisible by 100.
4. If the condition is true then print year is leap year. Otherwise print year is not
leap year.
5. Stop.
Program:
#include<stdio.h>
#include<conio.h>
void main()
int i, year;
clrscr();
scanf(“%d”,&year);
if(((year%4==0)&&(year%100!=0))||(year%400==0))
else
getch();
Output:
Program No: 2B
File Name:
Ex. No: BIGGEST AMONG TWO NUMBERS USING
Date: ___________ GOTO
Aim:
To write a C program to find biggest among two numbers using goto.
Algorithm:
1. Start.
2. Read two numbers a, b.
3. If a>b then go to greater. Otherwise print B is greater than A.
4. In greater section print A is greater than B.
5. Stop.
Program:
#include<stdio.h>
#include<conio.h>
void main()
inta,b;
clrscr();
scanf(“%d %d”,&a,&b);
if(a>b)
goto greater;
else
exit(0);
greater:
getch();
Output:
45
12
A is greater than B
45
97
B is greater than A
Program No: 2C
File Name:
Ex. No: SIMULATING SIMPLE CALCULATOR
Date: ___________
Aim:
Algorithm:
1. Start
2. Display the menu
3. Read the choice
4. If the choice is 1, then Read two numbers. Add two numbers, and print the
result.
5. If the choice is 2, then read two numbers, subtract a-b and print the result.
6. If the choice is 3 then read two numbers, multiply two numbers and print the
result.
7. If the choice is 4 then read two numbers, divide a/b and print the result.
8. If the choice is 5, then exit.
Program:
#include<stdio.h>
#include<conio.h>
void main( )
char op;
clrscr( );
switch (op)
case ‘+’:
break;
case ‘-’:
break;
case ‘*’:
break;
case ‘/’:
if (num2 = = 0)
else
break;
default:
getch( );
Output:
65-98
65.00-98.00=-33.00
65+76
65.00+76.00=141.00
7*7
7*7=49
Program No: 2D
File Name:
PRINTING NUMBER DIGITS
Ex. No:
Date: ___________
Aim:
To write a c program to print number digits.
Algorithm:
1. Start.
2. Read n
3. If n is 1 then print “ONE”,
if n is 2 then print “TWO”,
Similarly print till number 9 and numbers 0.
4. Stop.
Program:
#include<stdio.h>
#include<conio.h>
void main()
int n;
clrscr();
scanf(“%d”,&n);
switch(n)
case 1:
printf(“ONE”);
break;
case 2:
printf(“TWO”);
break;
case 3:
printf(“THREE”);
break;
case 4:
printf(“FOUR”);
break;
case 5:
printf(“FIVE”);
break;
case 6:
printf(“SIX”);
break;
case 7:
printf(“SEVEN”);
break;
case 8:
printf(“EIGHT”);
break;
case 9:
printf(“NINE”);
break;
case 0:
printf(“ZERO”);
break;
default:
printf(“Invalid number”);
break;
getch();
Output:
THREE
Enter the value of N:4
FOUR
Enter the value of N:5
FIVE
Enter the value of N:6
SIX
Enter the value of N:7
SEVEN
Enter the value of N:8
EIGHT
Enter the value of N:9
NINE
Enter the value of N:0
ZERO
Program No: 2E
File Name:
Ex. No: ILLUSTRATION OF CONTINUE
Date: ___________ STATEMENT
Aim:
To write a C program to print numbers to illustrate continue statement.
Algorithm:
1. Start
2. Assign j = 10.
3. For (i=0; i<=j; i++)
if i==j then continue
otherwise print i value
4. Stop.
Program:
#include<stdio.h>
#include<conio.h>
void main()
inti;
int j=10;
clrscr();
for(i=0;i<=j;i++)
if(i==5)
continue;
printf(“Hello %d\n”,i);
getch();
Output:
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10
Aim:
To write a c program to evaluate the series.
Algorithm:
1. Start
2. Read n, assign sum = 0
3. for i = 1 to n
calculate sum = sum + 1/(i*i*i)
4. print sum
5. Stop.
Program:
#include<stdio.h>
void main()
intn,i,sum=0;
clrscr();
scanf(“%d”,&n);
for(i=1;i<=n;i++)
sum=sum+i*i*i;
printf(“Sum = %d”,sum);
getch();
Output:
Enter n 5
Sum = 225
Aim:
Algorithm:
1. Start
2. Read num
3. Assign f1=0, f2=1.
4. Set loop for I and for all i<num value and calculate f3=f1+f2.
5. Display f1
6. Assign f1=f2 and f2=f3
7. Stop.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,num;
int f1=0,f2=1,f3;
clrscr();
printf(“Enter how many numbers”);
scanf(“%d”,&num);
for(i=1;i<=num;i++)
{
f3=f1+f2;
printf(“%5d”,f1);
f1=f2;
f2=f3;
}
getch();
}
Output:
Enter how many numbers 7
0 1 1 2 3 5 8
Aim:
Algorithm:
1. Start.
2. Read n.
4. Calculate
r=n%10
rev=rev*10+r
n=n/10
9. Stop.
Program:
#include<stdio.h>
#include<conio.h>
void main()
int n,r,rev,temp;
rev=0;
clrscr();
scanf(“%d”,&n);
temp=n;
while(n>0)
r=n%10;
rev=rev*10+r;
n=n/10;
if(temp==rev)
printf(“It is palindrome”);
else
getch();
Output:
It is not palindrome
It is palindrome
Program No: 3D
File Name: ILLUSTRATION OF DO-WHILE
Ex. No: PRINTING NUMBERS
Date: ___________
Aim:
To write a C program to print numbers using do-while loop.
Algorithm:
1. Start
2. Assign digit = 0
3. Using do-while loop
Print the digit and increment i. If digit <=9
4. Stop.
Program:
#include<stdio.h>
#include<conio.h>
void main()
int digit=0;
clrscr();
do
printf(“%d \t”,digit);
++digit;
while(digit<=9);
getch();
Output:
0 1 2 3 4 5 6 7 8 9
Program No: 4A
File Name:
COMPUTING MEAN VALUE OF N NUMBERS
Ex. No:
Date: ___________
Aim:
To write a C program to compute the mean value of N numbers.
Algorithm:
1. Start
2. Read number of elements n, and n numbers in an array.
3. Assign sum=0.
4. Read one element at a time from the array and add it to the sum.
5. Calculate mean = sum/n
6. Print mean
7. Stop.
Program:
#include<stdio.h>
#include<math.h>
void main()
int n,i;
float A[20],sum=0.0,mean;
clrscr();
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%f",&A[i]);
}
for(i=0;i<n;i++)
{
sum=sum+A[i];
}
mean=sum/n;
printf("Arithmetic mean=%f\n",mean);
getch();
}
Output:
Enter number of elements 6
Enter the elements 8
3
9
4
5
6
Arithmetic mean=5.833333
Program No: 4B
File Name:
Ex. No: ADDITION OF TWO MATRICES
Date: ___________
Aim:
Algorithm:
1. Start.
6. Add the element of A and B in column wise and store the result in
sum matrix.
8. Stop.
Program:
#include<stdio.h>
void main()
int A[3][3],B[3][3],sum[3][3],i,j;
clrscr();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(“%d”,&B[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
sum[i][j]=A[i][j]+B[i][j];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf(“%3d”,sum[i][j]);
printf(“\n”);
}
Output:
Enter the elements of matrix A
101
111
010
Enter the elements of matrix B
011
100
111
sum of two matrix
112
211
121
Program No: 4C
File Name:
MULTIPLICATION OF TWO MATRICES
Ex. No:
Date: ___________
Aim:
Algorithm:
1. Start.
11. Multiply the A and B matrix and store the elements in the C matrix.
13. Stop.
Program:
#include <stdio.h>
void main()
intA[10][10],B[10][10],C[20][20];
int i,j,k,m,n,p,q;
clrscr();
scanf(“%d%d”, &m,&n);
scanf(“%d%d”, &p,&q);
if(n!=p)
getch();
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&A[i][j]);
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf(“%d”,&B[i][j]);
for(i=0;i<m;i++)
for(j=0;j<q;j++)
for(k=0;k<n;k++)
C[i][j]=C[i][j]+A[i][k]*B[k][j];
for(i=0;i<m;i++)
for(j=0;j<q;j++)
printf(%3d”,C[i][j]);
printf(“\n”);
getch();
Output:
30 24 18
84 69 54
138 114 90
Program No: 4D
File Name: LINEAR SEARCH
Ex. No:
Date: ___________
Aim:
To write a C program to perform linear search.
Algorithm:
1. Start
2. Read n and n numbers in array A.
3. Read the key element to be searched
4. Assign sound = 0
5. Access the elements one at a time and compare it with the key.
6. If both are equal then print key found and increment sound variable.
7. After checking all the elements check if found=0 then print key not found.
8. Stop.
Program:
#include<stdio.h>
void main()
{
int n,A[20],key,i,found=0;
clrscr();
scanf(“%d”,&n);
printf(“Enter %d numbers”,n);
for(i=0;i<n;i++)
scanf(“%d”,&A[i]);
scanf(“%d”,&key);
for(i=0;i<n;i++)
if(key= =A[i])
printf(“key found”);
found++;
getch();
Output:
Enter 6 numbers1
key found
Program No: 5
File Name:
SORTING NAMES IN ASCENDING ORDER
Ex. No:
Date: ___________
Aim:
To write a C program to sort the names in alphabetical order using string
function.
Algorithm:
1. Start
2. Enter number of names.
3. Enter the names.
4. Set two loops and compare every two strings.
5. If strcmp function return greater than zero value then swap two strings. Using
strcpy function and use temporary variable
6. Repeat step 5 until all the strings are compared
7. Print the names in sorted order
8. Stop.
Program:
#include<stdio.h>
#include<string.h>
void main()
char names[10][15],temp[25];
int i,j,n;
clrscr();
scanf(“%d”,&n);
for(i=0;i<n;i++)
scanf(“%s”,names[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(strcmp(names[i],names[j])>0)
strcpy(temp,names[i]);
strcpy(names[i],names[j]);
strcpy(names[j],temp);
for(i=0;i<n;i++)
printf(“\n%s”,names[i]);
getch();
Output:
Program No: 6A
File Name: FINDING MAX OF THREE NUMBERS USING
Ex. No: FUNCTION
Date: ___________
Aim:
Algorithm:
1. Start
2. Read a,b,c
5. Stop.
Max function:
1. Start
4. Return max.
5. Stop.
Program:
#include<stdio.h>
int max(int,int,int);
void main()
int a,b,c,d;
clrscr();
scanf(“%d %d %d”,&a,&b,&c);
d=max(a,b,c);
getch();
}
int max(int a, int b,int c)
{
int max;
if(a>b&&a>c)
max=a;
else if(b>c)
max=b;
else
max=c;
return(max);
}
Output:
Enter three integer values:
56
28
79
Maximum is: 79
Program No: 6B
File Name: SWAPPING TWO NUMBERS USING PASS BY
Ex. No: VALUE
Date: ___________
Aim:
Aim:
To write a C program swapping two number using pass by value.
Algorithm:
1. Start
2. Call swap function and pass a,b as arguments.
3. Calculate
x=x+y
y=x-y
x=x-y
4. Display the swapped numbers
5. Stop.
Program:
#include<stdio.h>
void swap(int,int);
void main()
{
int a=10,b=20;
clrscr();
x=x+y;
y=x-y;
x=x-y;
return;
Output:
Program No: 6C
File Name: SWAPPING TWO NUMBERS USING PASS BY
Ex. No: REFERENCE
Date: ___________
Aim:
Algorithm:
1. Start
3. Calculate
*x=*x+*y
*y=*x-*y
*x=*x-*y
5. Stop.
Program:
#include<stdio.h>
int swap(int*,int*);
void main()
int a=10,b=20;
clrscr();
printf(“Before swap values are %d %d\n”,a,b);
swap(&a,&b);
printf(“After swap values are %d %d\n”,a,b);
getch();
}
int swap(int*x,int*y)
{
*x=*x+*y;
*y=*x-*y;
*x=*x-*y;
printf(“In swap function values are %d %d\n”,*x,*y);
return;
Output:
Program No: 6D
File Name:
Ex. No:
SORTING NUMBERS IN ASCENDING ORDER
Date: ___________ USING FUNCTION
Aim:
Algorithm:
1. Start
6. Stop.
Sort function:
1. Start
3. If the first element is greater than the second element then interchange the elements.
5. Stop.
Program:
#include<stdio.h>
#include<conio.h>
int sort(int[],int);
void main()
{
int n,i,j,a[50];
clrscr();
printf("Enter total number of items:");
scanf("%d",&n);
printf("\n Enter the elements one by one:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,n);
printf("\n The numbers after sorting is:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
{
if(x[i]>x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
return 0; }
Program No: 7
File Name: FACTORIAL OF GIVEN NUMBER USING
Ex. No: RECURSIVE FUNCTION
Date: ___________
Aim:
Algorithm:
1. Start
5. Stop.
Recursive Function:
2. Assign fact=1
Program:
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
int n;
clrscr();
scanf(“%d”,&n);
printf(“factorial of %d = %d”,n,fact(n));
getch();
int fact(int x)
int i;
if(x == 1)
return(1);
else
i = x*fact(x-1);
return(i);
Output:
factorial of 5 = 120
Program No: 8A
File Name: POINTERS TO FUNCTIONS
Ex. No:
Date: ___________ CALCULATING AREA OF A TRIANGLE
Aim:
Algorithm:
1. Start
2. Read base, height
3. Calculate area=0.5*base*height
4. Print area
5. Stop
Program:
#include<stdio.h>
void read(float *b, float *h);
void calculate_area (float *b, float *h),
float *a;
int main()
{
float base, height, area;
read(&base, &height);
calculate_area(&base, &height, &area);
printf(“\n Area of the triangle with base %. If and height % . If = % . 2f”, base,
height, area);
return 0;
}
void read(float *b, float *h)
{
printf(“\n Enter the base of the triangle:”);
scanf(“%f”, b);
printf(“\n Enter the height of the triangle: “);
scanf(“%f”, h);
}
Output:
Program No: 8B
File Name: POINTERS & ARRAYS
Ex. No: FINDING MEAN OF N NUMBERS USING
Date: ___________ ARRAYS
Aim:
Finding mean of n numbers using arrays.
Algorithm:
1. Start
2. Assign mean=0.0
3. Read numbers of elements
4. Read n elements is array
5. Add all the elements in the array and divide the sum by total number of
elements to find the mean
6. Print sum and mean
7. Stop.
Program:
#include<stdio.h>
int main()
{
int i, n, arr[20], sum =0;
int *pn = &n, *parr = arr, *psum = ∑
float mean = 0.0, *pmean = &mean;
printf (“\n Enter the number of elements:”);
scanf (“%d”, pn);
Program No: 9A
File Name: NESTED STRUCTURES:PRINTING
Ex. No: EMPLOYEE PERSONAL DETAILS
Date: ___________
Aim:
Algorithm:
1. Start
2. Struct employee
e_code[4]=char
e_name[20]=char
e_bp=float
struct
e_da=float
e_hra=float
allo
e_pf=float
end struct.
3. Read the employee code, name, basic pay, da, hra, pf.
4. Print the employee code, name, basic pay, da, hra, pf.
5. Calculate gross
salary=emp1.e_bp+emp1.allo.e_da+emp1.allo.e_hra+emp1.e_pf.
6. Print the gross salary. Stop.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
struct employee
{
char e_code[4];
char e_name[20];
float e_bp;
struct
{
float e_da;
float e_hra;
}
allo;
float e_pf;
};
struct employee emp1;
float gross;
printf(“\nEnter the code:”);
gets(emp1.e_code);
printf(“\nEnter the name:”);
gets(emp1.e_name);
printf(“\nEnter the basic pay:”);
scanf(“%f”,&emp1.e_bp);
printf(“\nEnter the dearness allowance:”);
scanf(“%f”,&emp1.allo.e_da);
printf(“\nEnter the house rent allowance:”);
scanf(%f”,&emp1.allo.e_hra);
printf(“\nEnter the provident fund:”);
scanf(“%f”,&emp1.e_pf);
printf(“\nCode :”);
puts(emp1.e_code);
printf(“\nName :”);
puts(emp1.e_name);
printf(“\nBasicpay :%8.2f”,emp1.e_bp);
printf(“\nDearness allowance :%8.2f”,emp1.allo.e_da);
printf(“\nHouserent allowance:%8.2f”,emp1.allo.e_hra);
printf(“\nProvidentfund :%8.2f”,emp1.e_pf);
gross=emp1.e_bp+emp1.allo.e_da+emp1.allo.e_hra+emp1.e_pf;
printf(“\nNetpay :%8.2f”,gross);
}
Output:
Program No: 9B
File Name: POINTERS TO STRUCTURES: PRINTING
Ex. No: STUDENT DETAILS
Date: ___________
Aim:
To write a C program to print student details using pointers and structures.
Algorithm:
1. Start
2. Declare student structure
3. Read student roll number, student name, branch, marks.
4. Print student roll number, student name, branch, marks.
5. Stop.
Program:
#include<stdio.h>
void main() {
struct
{
int rollno;
char name[30];
char branch[4];
int marks;
}
*stud;
Program No:9C
File Name: ARRAY OF STRUCTURES:CALCULATING
Ex. No: STUDENT MARK DETAILS
Date: ___________
Aim:
Algorithm:
1. Start
2. Declare the structure with members.
3. Initialize the marks of the students
4. Calculate the subject total by adding student
[i].sub1+student[i].sub2+student[i].sub3.
5. Print the total marks of the students
6. Stop.
Program:
#include<stdio.h>
#include<conio.h>
struct marks
{
int sub1;
int sub2;
int sub3;
int total;
};
void main()
int i;
struct marks student[3] = { {45, 67, 81, 0},{75, 53, 69, 0},
printf(“TOTAL MARKS\n\n”);
Output:
TOTAL MARKS
student[1]: 193
student[2]: 197
student[3]: 164
Aim:
To write a C program to read and write a file.
Algorithm:
1. Start
2. Create a file pointer
3. Read the file name to be opened.
4. Open the file with write mode.
5. Write the data
6. Open the file with read mode
7. Print the data
8. Stop.
Program:
#include <stdio.h>
#define SIZE 50
int main()
FILE *fptr;
gets(s1);
fputs(s1, fptr);
fclose(fptr);
printf(“\nYou entered:”);
puts(s2);
Aim:
To write a C program to count number of characters and number of lines in a
file using file pointer.
Algorithm:
1. Start
2. Create file pointers
3. Enter the file name
4. Open the file with read mode
5. Till the end of file reached read one character at a time
(a) If it is newline character ‘\n’, then increment no_of_lines count
value by one.
(b) if it is a character then increment no_of_characters count value by
one.
6. Print no_of_lines and no_of_characters values
7. Stop.
Program:
#include <stdio.h>
#include <string.h>
main ()
{
FILE *fp;
Output:
Aim:
Algorithm:
1. Start
6. Read first file character by character and write the characters in the second
file till end of file reached.
8. Stop.
Program:
#include <stdio.h>
#include <conio.h>
main ()
str[30];
clrscr();
gets(filename1);
fflush(stdin);
gets(filename2);
fflush(stdin);
if((fp1=fopen(filename1, “r”))==0)
exit(1);
if((fp2=fopen(filename2, “w”))==0)
exit(1);
while((fgets(str, sizeof(str),
fclose(fp1);
fclose(fp2);
getch();
return 0;
Output:
a.txt
b.txt
FILE COPIED
Aim:
Algorithm:
1. Start
5. From the file pointed by fp read a record of the specified record starting from
the beginning of the file
7. Stop.
Program:
#include <stdio.h>
#include <conio.h>
main ()
int emp_code;
char name[20];
int hra;
int da;
int ta;
};
FILE *fp;
struct employee e;
if(fp==NULL)
exit(1);
read: ”);
scanf(“%d”, &rec_no);
if(rec_no >= 0)
if (result == 1)
else
fclose(fp);
getch();
return 0;
Output:
Employee CODE: 06
Name : Tanya
Aim:
Algorithm:
1. Start
2. Define pi as 3.1415
4. Read radius
6. Print area
7. Stop.
Program:
#include <stdio.h>
#define PI 3. 1415
#definecircleArea(r) (PI*r*r)
int main()
scanf(“%f”, &radius);
area = circleArea(radius);
return();
Output:
Area = 113.094002