0% found this document useful (0 votes)
31 views12 pages

C Lab Manual

The document outlines a list of 12 laboratory experiments for students. The experiments cover topics like simulation of a simple calculator, solving quadratic equations, electricity bill calculation, binary search, matrix multiplication, and more. Code snippets are provided for some of the experiments.
Copyright
© © All Rights Reserved
0% found this document useful (0 votes)
31 views12 pages

C Lab Manual

The document outlines a list of 12 laboratory experiments for students. The experiments cover topics like simulation of a simple calculator, solving quadratic equations, electricity bill calculation, binary search, matrix multiplication, and more. Code snippets are provided for some of the experiments.
Copyright
© © All Rights Reserved
You are on page 1/ 12

List of Laboratory experiments (2 hours/week per batch/ batch strength 36)

12 lab sessions + 3 repetition class + 1 Lab Assessment

1 Simulation of a Simple Calculator.


2 Compute the roots of a quadratic equation by accepting the coefficients. Print
appropriate messages.

3 An electricity board charges the following rates for the use of electricity: for the
first 200 units 80 paise per unit: for the next 100 units 90 paise per unit: beyond 300
units Rs 1 per unit. All users are charged a minimum of Rs. 100 as meter charge. If
the total amount is more than Rs 400, then an additional surcharge of 15% of total
amount is charged. Write a program to read the name of the user, number of units
consumed and print out the charges.
4 Write a C Program to display the following by reading the number of rows as input,
1
121
12321
1234321
---------------------------
Nth row
5 Implement Binary Search on Integers.
6 Implement Matrix multiplication and validate the rules of multiplication.
7 Compute sin(x)/cos(x) using Taylor series approximation. Compare your result with
the built-in library function. Print both the results with appropriate inferences.
8 Sort the given set of N numbers using Bubble sort.
9 Write functions to implement string operations such as compare, concatenate, and
find string length. Use the parameter passing techniques.
10 Implement structures to read, write and compute average- marks of the students, list
the students scoring above and below the average marks for a class of N students.
11 Develop a program using pointers to compute the sum, mean and standard deviation
of all elements stored in an array of N real numbers.
12 Write a C program to copy a text file to another, read both the input file name and
target file name.
Suggested software’s : gcc compiler, Ubuntu Operating System

1. Simulation of a Simple Calculator.


#include <stdio.h>

int main() {

char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);

switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}

return 0;
}

2. Compute the roots of a quadratic equation by accepting the coefficients. Print


appropriate messages.
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);

discriminant = b * b - 4 * a * c;

// condition for real and different roots


if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf\n the roots are real and different", root1, root2);
}

// condition for real and equal roots


else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf\n the roots are real and same", root1);
}

// if roots are not real


else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi\n the roots are complex and different",
realPart, imagPart, realPart, imagPart);
}

return 0;
}
3. An electricity board charges the following rates for the use of electricity: for the first
200 units 80 paise per unit: for the next 100 units 90 paise per unit: beyond 300 units
Rs 1 per unit. All users are charged a minimum of Rs. 100 as meter charge. If the total
amount is more than Rs 400, then an additional surcharge of 15% of total amount is
charged. Write a program to read the name of the user, number of units consumed and
print out the charges.
#include<stdio.h>
void main ()
{
float unit, total, finalamount;
printf("Enter Total Units:");
scanf ("%f", &unit);
if (unit<=200)
{
total=100+unit*0.80;
}
else if (unit>200 && unit<=300)
{
total=100+160+(unit-200)*0.90;
}
else if (unit >300 )
{
total=100+160+90+(unit-300)*1.0;
}

printf("\nTotal: %.2f", total);


if(total>400)
finalamount= total+(total*0.15);
printf("\n finalamount if it exceeds Rs. 400: %.2f", finalamount);
}

4. Write a C Program to display the following by reading the number of rows as input,
1
121
12321
1234321
---------------------------
Nth row

#include<stdio.h>

int main()
{
int i,j,row;

printf("Enter number of rows: ");


scanf("%d",&row);
for(i=1; i<=row; i++) //loop to track number of rows
{
for(j=1; j<=row-i; j++) //loop to print starting spaces
{
printf(" ");
}

for(j=1; j<=i; j++) // loop to display/print number in ascending order


{
printf("%d ", j);
}

for(j=i-1 ;j>=1; j--) //loop to display numbers in reverse order


{
printf("%d ", j);
}
printf("\n");
}

return 0;
}
Output:
Enter number of rows: 4
1
121
12321
1234321

5. Implement Binary Search on Integers

#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[50],i,n,low, mid, high,key;
printf("\n enter count of total number of elements to an array");
scanf("%d",&n);
printf("\n enter a sorted list");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n enter key element to search");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==key)
{ printf("\n key found at %d location",mid); exit(0);
}
if(key>a[mid])
low=mid+1;
if(key<a[mid])
high=mid-1;
}
printf("\n key element not found");
return 0;
}
Output:
enter count of total number of elements to an array10

enter a sorted list1 2 3 4 5 6 7 8 9 10

enter key element to search9

key found at 9 locations

6. Implement Matrix multiplication and validate the rules of multiplication.


#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m, n, p, q, i, j, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter number of rows and columns of first matrix\n");


scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");

for (i = 0; i < m; i++)


for (j = 0; j < n; j++)
scanf("%d", &first[i][j]);

printf("Enter number of rows and columns of second matrix\n");


scanf("%d%d", &p, &q);

if (n != p)
{
printf("The multiplication isn't possible.\n");
exit(0);
}
else
{
printf("Enter elements of second matrix\n");

for (i = 0; i < p; i++)


for (j = 0; j < q; j++)
scanf("%d", &second[i][j]);

for (i = 0; i < m; i++)


{
for (j = 0; j < q; j++)
{
multiply[i][j]=0;
for (k = 0; k < p; k++)
{
multiply[i][j] = multiply[i][j] + (first[i][k] * second[k][j]);
}
}
}

printf("Product of the matrices:\n");


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

printf("\n");
}
}

return 0;
}Output:
Enter number of rows and columns of first matrix
2 2
Enter elements of first matrix
3 4
8 9
Enter number of rows and columns of second matrix
2 2
Enter elements of second matrix
6 2
7 5
Product of the matrices:
46 26
111 61

7. Compute sin(x)/cos(x) using Taylor series approximation. Compare your result with
the built-in library function. Print both the results with appropriate inferences.
#include<stdio.h>
#include<math.h>
#define PI 3.142
int main()
{
int i, degree;
float x, sum=0,term,nume,deno;
printf("Enter the value of degree");
scanf("%d",&degree);
x = degree * (PI/180); //converting degree into radian
nume = x;
deno = 1;
i=2;
do
{ //calculating the sine value.
term = nume/deno;
nume = -nume*x*x;
deno = deno*i*(i+1);
sum=sum+term;
i=i+2;
} while (fabs(term) >= 0.00001); // Accurate to 4 digits
printf("The sine of %d is %.3f\n", degree, sum);
printf("The sine function of %d is %.3f", degree, sin(x));
return 0;
}

Output:
Enter the value of degree75
The sine of 75 is 0.966
The sine function of 75 is 0.966

8. Sort the given set of N numbers using Bubble sort.


#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[100], n, i, j, swap;
printf("\nEnter number of elements\n");
scanf("%d", &n);
printf("\n Enter %d Numbers:", n);
for(i = 1; i <= n; i++)
scanf("%d", &a[i]);
for(i = 1 ; i <= n ; i++)
{
for(j = 1 ; j <= n-i; j++)
{
if(a[j] > a[j+1])
{
swap=a[j];
a[j]=a[j+1];
a[j+1]=swap;
}
}
}
printf("Sorted Array:\n");
for(i = 1; i <= n; i++)
printf("\t%d", a[i]);
return 0;
}

Output:
Enter number of elements 7
Enter 7 Numbers
9
1
8
3
7
4
2
Sorted Array:
1 2 3 4 7 8 9

9. Write functions to implement string operations such as compare, concatenate, and


find string length. Use the parameter passing techniques.
#include <stdio.h>
#include <string.h>
int main()
{
int length(char str[100]);
int compare(char s1[100], char s2[100]);
void concat(char s1[100], char s2[100]);

int option, result;


char str[100], s1[100], s2[100];
do
{
printf("1.String length \n");
printf("2.string comparision \n");
printf("3.string concatenation \n");
printf("4.quit \n");
printf("enter your choice \n");
scanf("%d", &option);
switch (option)
{
case 1:
printf("enter string \n");
scanf("%s", &str);
result = length(str);
printf("the length of string= %d\n", result);
break;
case 2:
printf("enter 1st string\n");
scanf("%s", &s1);
printf("enter 2nd string\n");
scanf("%s", &s2);
result = compare(s1, s2);
if (result == 0)
{
printf("strings are equal \n");
}
else
{
printf("strings are not equal \n");
break;
}

case 3:
printf("enter two strings\n");
scanf("%s%s", s1, s2);
concat(s1, s2);
printf("result=%s \n", s1);
break;
}
} while (option <= 3);
return 0;
}
int length(char str[100])
{
int i = 0;
while (str[i] != '\0')
i++;
return (i);
}
int compare(char s1[100], char s2[100])
{
int i = 0;
while (s1[i] != '\0')
{
if (s1[i] > s2[i])
return (1);
else if (s1[i] < s2[i])
return (-1);
i++;
}
return 0;
}
void concat(char s1[100], char s2[100])
{
int i, j;
i = 0;
while (s1[i] != '\0')
i++;
for (j = 0; s2[j] != '\0'; i++, j++)
s1[i] = s2[j];
s1[i] = '\0';
}

Output:
1.String length
2.string comparison
3.string concatenation
4.quit
enter your choice
2
enter 1st string
bms
enter 2nd string
it
strings are not equal
1.String length
2.string comparison
3.string concatenation
4.quit
enter your choice

10. Implement structures to read, write and compute average- marks of the students, list
the students scoring above and below the average marks for a class of N students.

#include<stdio.h>
#include<conio.h>

struct student
{
int marks;
}st[10];

void main()
{
int i,n;
float total=0,avgmarks;
printf("\nEnter the number of students in class(<=10):");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter student %d marks :",i+1);
scanf("%d",&st[i].marks);
}
for(i=0;i<n;i++)
{
total = total + st[i].marks;
}
avgmarks=total/n;
printf("\nAverage marks = %.2f",avgmarks);
for(i=0;i<n;i++)
{
if(st[i].marks>=avgmarks)
{
printf("\n student %d marks = %d above average",i+1,st[i].marks);
}
else
{
printf("\n student %d marks = %d below average",i+1,st[i].marks);
}
}
}

Output:
Enter the number of students in class(<=10):5

Enter student 1 marks :50

Enter student 2 marks :32

Enter student 3 marks :25

Enter student 4 marks :48

Enter student 5 marks :38

Average marks = 38.60


student 1 marks = 50 above average
student 2 marks = 32 below average
student 3 marks = 25 below average
student 4 marks = 48 above average
student 5 marks = 38 below average

11. Develop a program using pointers to compute the sum, mean and standard deviation
of all elements stored in an array of N real numbers.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float a[10], *ptr, mean, std, sum=0, sumstd=0;
int n,i;
printf("Enter the no of elements\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+ *ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumstd=sumstd + pow((*ptr - mean),2);
ptr++;
}
std= sqrt(sumstd/n);
printf("Sum=%.3f\t",sum);
printf("Mean=%.3f\t",mean);
printf("Standard deviation=%.3f\t",std);
return 0;
}

Output:
Enter the no of elements
5
Enter the array elements
18394
Sum=25.000 Mean=5.000 Standard deviation=3.033

12. Write a C program to copy a text file to another, read both the input file name and
target file name.
Note: create 2 text files a.txt (put some text in this file) and b.txt (empty file) before compiling the
program
#include <stdio.h>
#include <stdlib.h> // For exit()

int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;

printf("Enter the filename to open for reading \n");


scanf("%s", filename);

// Open one file for reading


fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}

printf("Enter the filename to open for writing \n");


scanf("%s", filename);

// Open another file for writing


fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}

// Read contents from file


c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}

printf("\nContents copied to %s", filename);

fclose(fptr1);
fclose(fptr2);
return 0;
}

OUTPUT:
Gcc program12.c
./a.out
Enter the filename to open for reading a.txt

You might also like