C Lab Manual
C Lab Manual
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
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;
}
discriminant = b * b - 4 * a * c;
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;
}
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;
return 0;
}
Output:
Enter number of rows: 4
1
121
12321
1234321
#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
if (n != p)
{
printf("The multiplication isn't possible.\n");
exit(0);
}
else
{
printf("Enter elements of second matrix\n");
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",°ree);
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
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
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
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;
fclose(fptr1);
fclose(fptr2);
return 0;
}
OUTPUT:
Gcc program12.c
./a.out
Enter the filename to open for reading a.txt