Pps Code Tantra
Pps Code Tantra
sNo=3&qId=585e1b8e0cf2f069ef155995&bd=AY3RFZHVEQg%3D%3D&li…
S.No: 3 Exp. Name: Write a C program to print Hello World! Date: 2022-05-31
Aim:
Page No:
Write a program to print Hello World! on the console.
ID: 21331A0211
Source Code:
Program101.c
#include<stdio.h>
void main()
{
printf("Hello World!\n");
}
Test Case - 1
User Output
Hello World!
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=3&qId=585e1b8e0cf2f069ef155995&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 1/1
24/08/2022, 22:18 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=4&qId=5a712069015de9062c85ac9c&bd=AY3RFZHVEQg%3D%3D…
S.No: 4 Exp. Name: Write a C program to print the Digit at ones place of a given number Date: 2022-05-31
Aim:
Page No:
Write a program to print the digit at ones place of a given number.
At the time of execution, the program should print the message on the console as:
ID: 21331A0211
Enter a number :
Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
DigitAtOnesPlace.c
#include<stdio.h>
void main()
{
int n,a;
printf("Enter a number : ");
scanf("%d",&a);
n=a%10;
printf("The digit at ones place of %d : %d\n",a,n);
}
2021-2025-EEE-A
Test Case - 1
User Output
Enter a number : 1993
The digit at ones place of 1993 : 3
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
Enter a number : 1895
The digit at ones place of 1895 : 5
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=4&qId=5a712069015de9062c85ac9c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/1
24/08/2022, 22:19 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=5&qId=585e21db0cf2f069ef1563f7&bd=AY3RFZHVEQg%3D%3D&li…
Page No:
Aim:
Write a program to read two integer values and to print the results of various arithmetic operations on them.
ID: 21331A0211
During execution, the program should print the following message on the console:
24 + 15 = 39
24 - 15 = 9
24 * 15 = 360
24 / 15 = 1
24 % 15 = 9
Note: Use the printf() function with a newline character ( \n ) at the end for every line being printed.
Source Code:
Program304.c
#include<stdio.h>
void main()
{
int a,b;
printf("Enter two integers : ");
scanf("%d%d",&a,&b);
printf("%d + %d = %d\n",a,b,a+b);
printf("%d - %d = %d\n",a,b,a-b);
2021-2025-EEE-A
printf("%d * %d = %d\n",a,b,a*b);
printf("%d / %d = %d\n",a,b,a/b);
printf("%d %% %d = %d\n",a,b,a%b);
}
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter two integers : 12 -6
12 + -6 = 6
12 - -6 = 18
12 * -6 = -72
12 / -6 = -2
12 % -6 = 0
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=5&qId=585e21db0cf2f069ef1563f7&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 1/2
24/08/2022, 22:19 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=6&qId=585e22500cf2f069ef156471&bd=AY3RFZHVEQg%3D%3D&li…
Exp. Name: Write a C program to Swap Two numbers with out using a Third
S.No: 6 Date: 2022-07-01
variable
Page No:
Aim:
Write a program to swap two integer values without using a third variable.
ID: 21331A0211
During execution, the program should print the following message on the console:
Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program311.c
#include<stdio.h>
void main()
{
int a,b;
printf("Enter two integers: ",a,b);
scanf("%d%d" ,&a,&b);
printf("Before swapping a = %d, b = %d\n",a,b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping a = %d, b = %d\n",a,b);
}
2021-2025-EEE-A
Execution Results - All test cases have succeeded!
Test Case - 1
MVGR College of Engineering (Autonomous)
User Output
Enter two integers: 23 67
Before swapping a = 23, b = 67
After swapping a = 67, b = 23
Test Case - 2
User Output
Enter two integers: 99 89
Before swapping a = 99, b = 89
After swapping a = 89, b = 99
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=6&qId=585e22500cf2f069ef156471&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 1/2
24/08/2022, 22:20 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=7&qId=5b86a9cb64bac1106118f187&bd=AY3RFZHVEQg%3D%3D&…
S.No: 7 Exp. Name: Write a C program to find Sum and Average of three numbers Date: 2022-07-01
Aim:
Page No:
Write a program to find the sum and average of the three given integers.
During execution, the program should print the message on the console as:
ID: 21331A0211
Enter three integers :
Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program314.c
#include<stdio.h>
void main()
{
int a,b,c,sum;
float avg;
printf("Enter three integers : ");
scanf("%d%d%d",&a,&b,&c);
sum = a+b+c;
avg = sum/3.0;
printf("Sum of %d, %d and %d : %d\n",a,b,c,sum);
printf("Average of %d, %d and %d : %f\n",a,b,c,avg);
}
2021-2025-EEE-A
Execution Results - All test cases have succeeded!
Test Case - 1
MVGR College of Engineering (Autonomous)
User Output
Enter three integers : 121 34 56
Sum of 121, 34 and 56 : 211
Average of 121, 34 and 56 : 70.333336
Test Case - 2
User Output
Enter three integers : 5 8 3
Sum of 5, 8 and 3 : 16
Average of 5, 8 and 3 : 5.333333
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=7&qId=5b86a9cb64bac1106118f187&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 22:20 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=8&qId=585e22010cf2f069ef156414&bd=AY3RFZHVEQg%3D%3D&li…
Page No:
Aim:
Write a program to convert the given temperature from fahrenheit(F) to celsius(C) .
ID: 21331A0211
During execution, the program should print the following message on the console:
Enter F :
Enter F : 32.45
C = 0.250000
Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program306.c
#include<stdio.h>
void main()
{
float farenheit,celsius;
printf("Enter F : ");
scanf("%f",&farenheit);
celsius = (5.0/9.0) *(farenheit - 32);
printf("C = %f\n",celsius);
}
2021-2025-EEE-A
Execution Results - All test cases have succeeded!
Test Case - 1
MVGR College of Engineering (Autonomous)
User Output
Enter F : 80.779
C = 27.099443
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=8&qId=585e22010cf2f069ef156414&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 1/1
24/08/2022, 22:21 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=9&qId=585e21ed0cf2f069ef156403&bd=AY3RFZHVEQg%3D%3D&li…
Exp. Name: Write a C program to convert the given temperature from Celsius to
S.No: 9 Date: 2022-07-01
Fahrenheit
Page No:
Aim:
Write a program to convert the given temperature from celsius(C) to fahrenheit(F) .
ID: 21331A0211
During execution, the program should print the following message on the console:
enter C :
enter C : 1.23
F = 34.214001
Note: Use the printf() function with a newline character ( \n ) at the end.
Program305.c
#include<stdio.h>
void main()
{
float celsius,farenheit;
printf("enter C : ");
scanf("%f",&celsius);
farenheit = celsius * (9.0/5.0)+ 32.0;
printf("F = %f\n",farenheit);
Test Case - 1
MVGR College of Engineering (Autonomous)
User Output
enter C : 27.1
F = 80.779999
Test Case - 2
User Output
enter C : 1.23
F = 34.214001
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=9&qId=585e21ed0cf2f069ef156403&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 1/1
24/08/2022, 22:21 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=10&qId=5b9faeef64bac14c8a2928a7&bd=AY3RFZHVEQg%3D%3D…
S.No: 10 Exp. Name: Write a program to calculate Simple interest and Compound interest Date: 2022-07-01
Aim:
Page No:
Write a program to calculate the simple interest and compound interest by reading principal
amount, rate of interest and time.
ID: 21331A0211
During execution, the program should print the following message on the console:
Note: Use the printf() function and ensure that the character '\n' is printed at the end of the result.
The formula to find simple interest is simpleInterest = (principal * rate * time) / 100 .
Note: Use float data type for all the involved variables.
Source Code:
Program315.c
#include<stdio.h>
#include<math.h>
void main()
{
float amount,roi,time,si,ci;
2021-2025-EEE-A
printf("Enter principle amount, rate of interest, time : ");
scanf("%f%f%f",&amount,&roi,&time);
si = (amount * roi * time) / 100.0;
ci = amount * pow(1 + (roi / 100), time) - amount;
printf("Simple interest = %f\n",si);
printf("Compound interest = %f\n",ci);
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter principle amount, rate of interest, time : 5000 7 5
Simple interest = 1750.000000
Compound interest = 2012.760376
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=10&qId=5b9faeef64bac14c8a2928a7&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
24/08/2022, 22:22 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=11&qId=5d4c063e4950694bfa6e1275&bd=AMjI0X2N0X2No&lid=5d4…
S.No: 11 Exp. Name: Write a program to find the Square root of a given number Date: 2022-07-01
Aim:
Page No:
Write a program to find the square root of a given number.
During execution, the program should print the message on the console as:
ID: 21331A0211
Enter a number :
Enter a number : 2
Note - 2: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
SquareRoot.c
#include<stdio.h>
void main()
{
double a,n;
printf("Enter a number : ");
scanf("%lf",&a);
n = sqrt(a);
2021-2025-EEE-A
printf("The square root is : %lf\n",n);
}
Test Case - 1
User Output
Enter a number : 25
The square root is : 5.000000
Test Case - 2
User Output
Enter a number : 2
The square root is : 1.414214
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=11&qId=5d4c063e4950694bfa6e1275&bd=AMjI0X2N0X2No&lid=5d4bebb54950694bfa6d… 1/2
24/08/2022, 22:22 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=12&qId=5b62cdc964bac10dd7188469&bd=AY3RFZHVEQg%3D%3…
S.No: 12 Exp. Name: Write a C program to find Area of a Triangle using Heron's formula Date: 2022-07-01
Aim:
Page No:
Write a program to find the area of a triangle using Heron's formula.
During execution, the program should print the following message on the console:
ID: 21331A0211
enter sides :
area : 2.485477
Note: Use the printf() function with a newline character ( \n ) at the end.
The area of a triangle is given by Area = √ p(p − a)(p − b)(p − c) , where p is half of the perimeter, or
(a + b + c) / 2 . Let a,b,c be the lengths of the sides of the given triangle.
Source Code:
Program313.c
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,area,p;
printf("enter sides : ");
scanf("%f%f%f",&a,&b,&c);
p = (a + b + c) / 2.0;
area = sqrt(p * (p - a) * (p - b) * (p - c));
printf("area : %f\n",area);
}
2021-2025-EEE-A
Execution Results - All test cases have succeeded!
Test Case - 1
MVGR College of Engineering (Autonomous)
User Output
enter sides : 2.3 2.4 2.5
area : 2.485477
Test Case - 2
User Output
enter sides : 5 4 6.7
area : 9.952600
Test Case - 3
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=12&qId=5b62cdc964bac10dd7188469&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950… 1/2
24/08/2022, 22:23 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=13&qId=5d4c043e4950694bfa6e11b0&bd=AY3RFZHVEQg%3D%3D…
S.No: 13 Exp. Name: Write a C program to find the Distance Travelled by an Object Date: 2022-07-22
Aim:
Page No:
Write a program to find the distance travelled by an object.
ID: 21331A0211
Enter the acceleration value : 2.5
Enter the initial velocity : 5.7
Enter the time taken : 20
Distance travelled : 614.000000
Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
DistanceTravelled.c
#include<stdio.h>
void main()
{
float u,a,d;
int t;
printf("Enter the acceleration value : ");
scanf("%f",&a);
printf("Enter the initial velocity : ");
scanf("%f",&u);
printf("Enter the time taken : ");
scanf("%d",&t);
d = u * t + ( a * t * t)/2;
printf("Distance travelled : %f\n",d);
}
2021-2025-EEE-A
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
Enter the acceleration value : 5
Enter the initial velocity : 0
Enter the time taken : 10
Distance travelled : 250.000000
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=13&qId=5d4c043e4950694bfa6e11b0&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
24/08/2022, 22:23 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=14&qId=5d343d2ae9524009292df0ad&bd=AY3RFZHVEQg%3D%3D…
Exp. Name: Write a Program that prints the results of all the Operators available in
S.No: 14 Date: 2022-07-28
C
Page No:
Aim:
Write a simple program that prints the results of all the operators available in C (including pre / post increment,
bitwise and/or/not, etc.).
ID: 21331A0211
Read required operand values from standard input.
At the time of execution, the program should print the message on the console as:
Addition of a and b : 20
Substraction of a and b : 8
Multiplication of a and b : 84
Remainder of a and b : 2
Division of a and b : 2
Logical AND result : 1
Logical OR and NOT result : 1
Bitwise AND : 6
Bitwise OR : 14
Bitwise NOT : 0
Bitwise complement : -15
Bitwise XOR : 8
Bitwise shift right : 3
Bitwise shift left : 48
Integer size : 4, Floating point size : 4
Conditional expression : 200
Preincrement : 15
2021-2025-EEE-A
Postincrement : 6
Predecrement : 14
Postdecrement : 7
AllOperators.c
#include <stdio.h>
void main() {
int a, b, result, result2;
printf("Enter a and b values : ");
scanf("%d %d", &a, &b);
printf("Addition of a and b : %d\n",a+b ); // Addition of a and b
printf("Substraction of a and b : %d\n",a-b ); // Subtract b from a
printf("Multiplication of a and b : %d\n",a*b ); // Multiply a and b
printf("Remainder of a and b : %d\n",a%b ); // Numerator is a and denominator is b
printf("Division of a and b : %d\n",a/b ); // Numerator is a and denominator is b
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=14&qId=5d343d2ae9524009292df0ad&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/3
24/08/2022, 22:23 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=14&qId=5d343d2ae9524009292df0ad&bd=AY3RFZHVEQg%3D%3D…
result =(a>0)&&(b<=10) ; // The condition is a is greater than 0 and b is less than
printf("Logical AND result : %d\n", result);
result2 = (a==b)||(b!=0); // The condition is a is equal to (Comparision) b or b is
printf("Logical OR and NOT result : %d\n",result2);
Page No:
printf("Bitwise AND : %d\n",a&b ); // Bitwise and of a and b
printf("Bitwise OR : %d\n",a|b); // Bitwise or of a and b
printf("Bitwise NOT : %d\n",!a ); // Logical not of a
printf("Bitwise complement : %d\n",~a ); // Complement of a
ID: 21331A0211
printf("Bitwise XOR : %d\n",a^b); // Bitwise xor of a and b
printf("Bitwise shift right : %d\n",a>>2 ); // Right shift a by 2 times
printf("Bitwise shift left : %d\n",b<<3 ); // Left shift b by 3 times
printf("Integer size : %d, Floating point size : %d\n",sizeof (int), sizeof(float));
// Sizes of int and float
printf("Conditional expression : %d\n",((a-b)>50?100:200));
printf("Preincrement : %d\n",++a); // Pre increment a
printf("Postincrement : %d\n",b++); // Post increment b
printf("Predecrement : %d\n",--a); // Pre decrement a
printf("Postdecrement : %d\n",b--); // Post decrement b
}
Test Case - 1
User Output
Enter a and b values : 14 6
Addition of a and b : 20
Substraction of a and b : 8
Multiplication of a and b : 84
Remainder of a and b : 2
Division of a and b : 2
Logical AND result : 1
Logical OR and NOT result : 1
Bitwise AND : 6
2021-2025-EEE-A
Bitwise OR : 14
Bitwise NOT : 0
Bitwise complement : -15
Bitwise XOR : 8
Bitwise shift right : 3
Bitwise shift left : 48
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
Enter a and b values : 8 5
Addition of a and b : 13
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=14&qId=5d343d2ae9524009292df0ad&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/3
24/08/2022, 22:24 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=15&qId=585e22420cf2f069ef156466&bd=AY3RFZHVEQg%3D%3D…
Exp. Name: Write a C program to find the Largest and Smallest of Three numbers
S.No: 15 Date: 2022-07-22
using Ternary operator
Page No:
Aim:
Write a program to find the largest and smallest of the three given integers.
ID: 21331A0211
During execution, the program should print the following message on the console:
Enter 3 integers :
999 is largest
9 is smallest
Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program310.c
#include<stdio.h>
void main()
{
int a,b,c,largest,smallest;
printf("Enter 3 integers: ");
scanf("%d%d%d",&a,&b,&c);
largest = a > b ? (a > c ? a : c) : (b > c ? b : c);
smallest = a < b ? (a < c ? a : c) : (b < c ? b : c);
printf("%d is largest\n",largest);
printf("%d is smallest\n",smallest);
}
Test Case - 1
MVGR College of Engineering (Autonomous)
User Output
Enter 3 integers: 34 56 48
56 is largest
34 is smallest
Test Case - 2
User Output
Enter 3 integers: 56 34 12
56 is largest
12 is smallest
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=15&qId=585e22420cf2f069ef156466&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 22:24 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=16&qId=5d4c0aa34950694bfa6e13a4&bd=AY3RFZHVEQg%3D%3D…
S.No: 16 Exp. Name: Write a C program to Total and Average of 5 subjects marks Date: 2022-07-22
Aim:
Page No:
Write a program to take marks of 5 subjects in integers, and find the total , average in float.
ID: 21331A0211
Enter 5 subjects marks : 55 56 57 54 55
Total marks : 277.000000
Average marks : 55.400002
Note: Use the printf() function with a newline character ( \n ) to print the output at the end.
Source Code:
TotalAndAvg.c
#include<stdio.h>
void main()
{
int a,b,c,d,e;
float sum,avg;
printf("Enter 5 subjects marks : ");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
sum = a+b+c+d+e;
avg = sum/5.0;
printf("Total marks : %f\n",sum);
printf("Average marks : %f\n",avg);
}
Test Case - 1
2021-2025-EEE-A
User Output
Enter 5 subjects marks : 45 67 89 57 49
Total marks : 307.000000
Average marks : 61.400002
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
Enter 5 subjects marks : 55 56 57 54 55
Total marks : 277.000000
Average marks : 55.400002
Test Case - 3
User Output
Enter 5 subjects marks : 90 97 95 92 91
Total marks : 465.000000
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=16&qId=5d4c0aa34950694bfa6e13a4&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
24/08/2022, 22:25 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=17&qId=5d4c19734950694bfa6e1973&bd=AY3RFZHVEQg%3D%3D…
S.No: 17 Exp. Name: Write a Program to find the Max and Min of Four numbers Date: 2022-07-22
Aim:
Page No:
Write a program to find the max and min of four numbers.
ID: 21331A0211
Enter 4 numbers : 9 8 5 2
Max value : 9
Min value : 2
Note: Use the printf() function with a newline character ( \n ) to print the output at the end.
Source Code:
MinandMaxOf4.c
#include<stdio.h>
int main()
{
int a,b,c,d,min,max;
printf("Enter 4 numbers : ");
scanf("%d%d%d%d",&a,&b,&c,&d);
max=min=a;
if (b>max)
max=b;
if (b<min)
min=b;
if (c>max)
max=c;
else if (c<min)
min=c;
if (d>max)
max=d;
else if (d<min)
2021-2025-EEE-A
min=d;
printf("Max value : %d\nMin value : %d\n",max,min);
return 0;
}
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter 4 numbers : 9 8 5 2
Max value : 9
Min value : 2
Test Case - 2
User Output
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=17&qId=5d4c19734950694bfa6e1973&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
24/08/2022, 22:25 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=18&qId=5b9f3ccf64bac14c8a1f07a6&bd=AY3RFZHVEQg%3D%3D&…
S.No: 18 Exp. Name: Write a C Program to find out the Electricity Charges of a Customer Date: 2022-07-22
Aim:
Page No:
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
ID: 21331A0211
beyond 300 units Rs. 1 per unit.
If the total amount is more than Rs. 400, then an additional surcharge of 15% of total amount is charged.
Write a C program to read the name of the user, number of units consumed and print out the charges.
At the time of execution, the program should print the following messages one by one on the console as:
Hint: Please convert charges per unit into float datatype [(ex: 80 paise as 0.8f or (float) 0.8)] before
multiplying with units for calculating the amount.
Source Code:
2021-2025-EEE-A
Program424.c
#include <stdio.h>
void main() {
char name[25];
int units;
// start writing your code below this.
MVGR College of Engineering (Autonomous)
// Use the above variables to read values using scanf from the user
float charge, surcharge = 0, amount, totalAmount;
printf("Enter the customer name : ");
scanf("%s", &name);
printf("Enter the units consumed by customer : ");
scanf("%d",&units);
if (units <= 200)
charge = 0.80;
else if (units > 200 && units <= 300)
charge = 0.90;
else if (units > 300)
charge = 1.00;
amount = units * charge;
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=18&qId=5b9f3ccf64bac14c8a1f07a6&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/3
24/08/2022, 22:25 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=18&qId=5b9f3ccf64bac14c8a1f07a6&bd=AY3RFZHVEQg%3D%3D&…
if ( amount > 400)
surcharge = amount * 15 / 100.0;
totalAmount = amount + surcharge;
if (amount < 100)
Page No:
totalAmount = 100;
printf("Customer name : %s\n", name);
printf("Units consumed : %d\n",units);
printf("Amount charged : %f\n",amount);
ID: 21331A0211
printf("Surcharges : %f\n",surcharge);
printf("Total amount to be paid by the customer : %f\n",totalAmount);
}
Test Case - 1
User Output
Enter the customer name : Ganga
Enter the units consumed by customer : 199
Customer name : Ganga
Units consumed : 199
Amount charged : 159.199997
Surcharges : 0.000000
Total amount to be paid by the customer : 159.199997
Test Case - 2
User Output
Enter the customer name : Saraswathi
Enter the units consumed by customer : 89
Customer name : Saraswathi
Units consumed : 89
Amount charged : 71.200005
2021-2025-EEE-A
Surcharges : 0.000000
Total amount to be paid by the customer : 100.000000
Test Case - 3
User Output
MVGR College of Engineering (Autonomous)
Test Case - 4
User Output
Enter the customer name : Govinda
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=18&qId=5b9f3ccf64bac14c8a1f07a6&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/3
24/08/2022, 22:26 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=19&qId=59dde2280cf2e65520534198&bd=AY3RFZHVEQg%3D%3D…
S.No: 19 Exp. Name: Write a C program to find all Roots of a Quadratic equation Date: 2022-07-28
Aim:
Page No:
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 computes all possible roots.
ID: 21331A0211
An equation is quadratic only if a is non zero.
If a is zero and b is non zero in the above equation then it becomes a linear equation (bx + c = 0).
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.
At the time of execution, the program should print the message on the console as:
Invalid coefficients
Enter valid inputs
Linear equation
Root = -4.000000
2021-2025-EEE-A
If the input is given as 1 6 9 then the result should be:
Note - 1: Do use the printf() function with a newline character ( \n ) at the end.
Note - 2: Use fabs() funtion (fabs(determinant)) when the roots are real and imaginary.
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=19&qId=59dde2280cf2e65520534198&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/3
24/08/2022, 22:26 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=19&qId=59dde2280cf2e65520534198&bd=AY3RFZHVEQg%3D%3D…
Program419.c
#include<stdio.h>
Page No:
#include<math.h>
void main()
{
float a,b,c,det,r1,r2,imag,real;
ID: 21331A0211
printf("Enter coefficients a, b and c : ");
scanf("%f%f%f",&a,&b,&c);
if ((a==0)&&(b==0))
{
printf("Invalid coefficients\n");
printf("Enter valid inputs\n");
}
else if (a==0)
{
printf("Linear equation\n");
printf("Root = %f\n", -c/b);
}
else
{
det=(b*b)-(4*a*c);
if(det>0)
{
r1=(-b+sqrt(det))/(2*a);
r2=(-b-sqrt(det))/(2*a);
printf("The roots are real and distinct\n");
printf("root1 = %f and root2 = %f\n",r1,r2);
}
else if(det==0)
{
r1=r2=-b/(2*a);
printf("The roots are real and equal\n");
printf("root1 = root2 = %f\n",r1);
}
else
2021-2025-EEE-A
{
real= -b/(2*a);
imag= sqrt(fabs(det))/(2*a);
printf("The roots are real and imaginary\n");
printf("root1 = %f+i%f\n",real,imag);
printf("root2 = %f-i%f\n",real,imag);
MVGR College of Engineering (Autonomous)
}
}
}
Test Case - 1
User Output
Enter coefficients a, b and c : 2 6 4
The roots are real and distinct
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=19&qId=59dde2280cf2e65520534198&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/3
24/08/2022, 22:26 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=19&qId=59dde2280cf2e65520534198&bd=AY3RFZHVEQg%3D%3D…
Test Case - 1
root1 = -1.000000 and root2 = -2.000000
Page No:
Test Case - 2
User Output
ID: 21331A0211
Enter coefficients a, b and c : 0 0 0
Invalid coefficients
Enter valid inputs
Test Case - 3
User Output
Enter coefficients a, b and c : 0 2 8
Linear equation
Root = -4.000000
Test Case - 4
User Output
Enter coefficients a, b and c : 1 6 9
The roots are real and equal
root1 = root2 = -3.000000
Test Case - 5
User Output
Enter coefficients a, b and c : 1 -5 3
The roots are real and distinct
root1 = 4.302776 and root2 = 0.697224
Test Case - 6
2021-2025-EEE-A
User Output
Enter coefficients a, b and c : 1 4 7
The roots are real and imaginary
root1 = -2.000000+i1.732051
root2 = -2.000000-i1.732051
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=19&qId=59dde2280cf2e65520534198&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 3/3
24/08/2022, 22:27 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=20&qId=585e3e7a0cf2f069ef156bd4&bd=AY3RFZHVEQg%3D%3D…
Page No:
Aim:
Write a program to read two integer values and an arithmetic operator, depending on the operator perform
different arithmetic operations .
ID: 21331A0211
If integer values 2 and 3 are given with operator +, then the output should be 2 + 3 = 5 .
If integer values 6 and 3 are given with operator /, then the output should be 6 / 3 = 2 .
If other than arithmetic operator is given, then display "Error! Operator is not correct".
At the time of execution, the program should print the message on the console as:
Next, the program should print the message on the console as:
12 + 10 = 22
2021-2025-EEE-A
then the output should be
Note-2: Space before %c removes any white space (blanks, tabs, or newlines). It means %c without
space will read white sapce like new line(\n), spaces(‘ ‘) or tabs(\t). By adding space before %c , we are
skipping this and reading only the char given.
Source Code:
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=20&qId=585e3e7a0cf2f069ef156bd4&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/3
24/08/2022, 22:27 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=20&qId=585e3e7a0cf2f069ef156bd4&bd=AY3RFZHVEQg%3D%3D…
Program406.c
#include<stdio.h>
Page No:
void main()
{
int a,b;
char op;
ID: 21331A0211
printf("Enter two integer values : ");
scanf("%d%d",&a,&b);
printf("Enter an arithmetic operator : ");
scanf(" %c",&op);
switch (op)
{
case '+':printf("%d + %d = %d\n",a,b,a+b);
break;
case '-':printf("%d - %d = %d\n",a,b,a-b);
break;
case '*':printf("%d * %d = %d\n",a,b,a*b);
break;
case '/':
if(b!=0)
printf("%d / %d = %d\n",a,b,a/b);
else
printf("Division is not possible! Divide by zero error\n");
break;
case '%':
if(b!=0)
printf("%d %% %d = %d\n",a,b,a%b);
else
printf("Modulo division is not possible! Divide by zero error\n");
break;
default: printf("Error! Operator is not correct\n");
}
}
2021-2025-EEE-A
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
Enter two integer values : 6 9
Enter an arithmetic operator : *
6 * 9 = 54
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=20&qId=585e3e7a0cf2f069ef156bd4&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/3
24/08/2022, 22:27 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=21&qId=585e3e4c0cf2f069ef156bc6&bd=AY3RFZHVEQg%3D%3D&…
S.No: 21 Exp. Name: Write a C program to find whether the given year is a Leap year or not Date: 2022-07-26
Aim:
Page No:
Write a program to check whether the given year is leap year or not.
Use if-else statement and print "__ is a leap year" if year is divisible by 4 or 400. Otherwise, print
ID: 21331A0211
"__ is not a leap year" .
At the time of execution, the program should print the message on the console as:
Enter a year :
Program405.c
#include<stdio.h>
int main()
{
int yr;
printf("Enter a year : ");
scanf("%d",&yr);
if(yr % 4 == 0 && yr % 100 == 0 && yr % 400 ==0)
printf("%d is a leap year\n",yr);
else if(yr %4 ==0 && yr % 100 != 0)
printf("%d is a leap year\n",yr);
else
2021-2025-EEE-A
printf("%d is not a leap year\n",yr);
return 0;
}
Test Case - 1
User Output
Enter a year : 2004
2004 is a leap year
Test Case - 2
User Output
Enter a year : 1900
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=21&qId=585e3e4c0cf2f069ef156bc6&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 22:28 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=22&qId=585e3ec70cf2f069ef156bf8&bd=AY3RFZHVEQg%3D%3D&l…
S.No: 22 Exp. Name: Write a C program to find the Factorial of a given number Date: 2022-07-28
Aim:
Page No:
Write a program to find the factorial of a given number.
At the time of execution, the program should print the message on the console as:
ID: 21331A0211
Enter an integer :
Enter an integer : 5
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program408.c
#include<stdio.h>
void main()
{
int num,fact=1;
printf("Enter an integer : ");
scanf("%d",&num);
printf("Factorial of given number %d = ",num);
while (num!=0)
{
fact = fact * num;
num = num -1 ;
}
printf("%d\n",fact);
}
2021-2025-EEE-A
Execution Results - All test cases have succeeded!
Test Case - 1
MVGR College of Engineering (Autonomous)
User Output
Enter an integer : 3
Factorial of given number 3 = 6
Test Case - 2
User Output
Enter an integer : 5
Factorial of given number 5 = 120
Test Case - 3
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=22&qId=585e3ec70cf2f069ef156bf8&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 22:28 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=23&qId=585e3eea0cf2f069ef156c06&bd=AY3RFZHVEQg%3D%3D&…
S.No: 23 Exp. Name: Write a C program to find whether the given number is Prime or not Date: 2022-07-26
Aim:
Page No:
Write a sample program to check whether the given number is a prime number or not.
[Hint: A prime number is a positive integer which is greater than 1 and divisible by 1 and itself only. A few
ID: 21331A0211
prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, etc.]
At the time of execution, the program should print the message on the console as:
Similarly,If the input is given as 9 then the output should be "The given number 9 is not a prime number".
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program409.c
#include<stdio.h>
void main()
{
int n,i,f=0;
printf("Enter any number : ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n % i == 0)
2021-2025-EEE-A
{
f = 1;
break;
}
}
if(f == 0)
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter any number : 11
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=23&qId=585e3eea0cf2f069ef156c06&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 22:28 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=23&qId=585e3eea0cf2f069ef156c06&bd=AY3RFZHVEQg%3D%3D&…
Test Case - 1
The given number 11 is a prime number
Page No:
Test Case - 2
User Output
ID: 21331A0211
Enter any number : 37
The given number 37 is a prime number
Test Case - 3
User Output
Enter any number : 21
The given number 21 is not a prime number
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=23&qId=585e3eea0cf2f069ef156c06&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
24/08/2022, 22:29 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=24&qId=5b9f423664bac14c8a1f2335&bd=AY3RFZHVEQg%3D%3D…
Aim:
Page No:
Develop a Program to compute Sine(x) using Taylor series approximation. Compare your result with the
built- in Library function.
ID: 21331A0211
Print both the results with appropriate messages.
For example,
When n = 4 , Taylor series considered should be Sin(x) = x - (x3/3!) + (x5/5!) - (x7/7!) [4 Terms].
When n = 5 , Taylor series considered should be Sin(x) = x - (x3/3!) + (x5/5!) - (x7/7!) + (x9/9!) [5
Terms]
Note: The input n refers to the number of terms in Taylor approximation series to be considered.
At the time of execution, the program should print the following messages one by one on the console as:
2021-2025-EEE-A
Note: Consider the value of PI as 3.14 .
Source Code:
LoopExample9.c
MVGR College of Engineering (Autonomous)
#include <stdio.h>
#include <math.h>
void main() {
// decalre variables here
int n,i;
float degrees,radians,sum,term;
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=24&qId=5b9f423664bac14c8a1f2335&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
24/08/2022, 22:29 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=24&qId=5b9f423664bac14c8a1f2335&bd=AY3RFZHVEQg%3D%3D…
// write code here
radians = (degrees * 3.14) / 180;
term = sum = radians;
for (i = 1; i < n; i++) {
Page No:
term = ((-term) * (radians * radians)) / ((2 * i) * (2 * i + 1));
sum = sum + term;
}
ID: 21331A0211
printf("The sine value sin(%f) = %f\n",degrees,sum);
printf("The sine value using built-in sin(%f) = %f\n", degrees,sin(radians));
}
Test Case - 1
User Output
Enter the value of n : 5
Enter degrees : 90
The sine value sin(90.000000) = 1.000003
The sine value using built-in sin(90.000000) = 1.000000
Test Case - 2
User Output
Enter the value of n : 4
Enter degrees : 45
The sine value sin(45.000000) = 0.706825
The sine value using built-in sin(45.000000) = 0.706825
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=24&qId=5b9f423664bac14c8a1f2335&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
24/08/2022, 22:29 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=25&qId=5d5116cce3fffe0a91cf5057&bd=AY3RFZHVEQg%3D%3D&li…
Aim:
Page No:
Develop a program to compute Cosine(x) using Taylor series approximation. Compare your result with the
built- in library function.
ID: 21331A0211
Print both the results with appropriate messages.
For example,
When n = 4 , Taylor series considered should be Cos(x) = 1 - (x2/2!) + (x4/4!) - (x6/6!) [4 Terms].
When n = 5 , Taylor series considered should be Cos(x) = 1 - (x2/2!) + (x4/4!) - (x6/6!) + (x8/8!) [5
Terms]
Note: The input n refers to the number of terms in Taylor approximation series to be considered.
At the time of execution, the program should print the following messages one by one on the console as:
2021-2025-EEE-A
Note: Consider the value of PI as 3.14 .
Source Code:
CosineTaylorSeries.c
MVGR College of Engineering (Autonomous)
#include<stdio.h>
#include<math.h>
void main()
{
int n,i;
float degrees,radians,sum,term;
printf("Enter the value of n : ");
scanf("%d",&n);
printf("Enter degrees : ");
scanf("%f",°rees);
radians = (degrees*3.14)/180;
term = sum = 1;
for(i=1;i<n;i++)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=25&qId=5d5116cce3fffe0a91cf5057&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 1/2
24/08/2022, 22:29 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=25&qId=5d5116cce3fffe0a91cf5057&bd=AY3RFZHVEQg%3D%3D&li…
{
term=((-term)*(radians*radians))/((2*i)*(2*i-1));
sum +=term;
}
Page No:
printf("The cosine value cos(%f) = %f\n",degrees,sum);
printf("The cosine value using built-in cos(%f) = %f\n",degrees,cos(radians));
}
ID: 21331A0211
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Enter the value of n : 5
Enter degrees : 45
The cosine value cos(45.000000) = 0.707388
The cosine value using built-in cos(45.000000) = 0.707388
Test Case - 2
User Output
Enter the value of n : 3
Enter degrees : 120
The cosine value cos(120.000000) = -0.390926
The cosine value using built-in cos(120.000000) = -0.499080
Test Case - 3
User Output
Enter the value of n : 10
Enter degrees : 30
The cosine value cos(30.000000) = 0.866158
The cosine value using built-in cos(30.000000) = 0.866158
User Output
Enter the value of n : 8
MVGR College of Engineering (Autonomous)
Test Case - 5
User Output
Enter the value of n : 4
Enter degrees : 45
The cosine value cos(45.000000) = 0.707385
The cosine value using built-in cos(45.000000) = 0.707388
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=25&qId=5d5116cce3fffe0a91cf5057&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 2/2
24/08/2022, 22:29 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=26&qId=5b63e31a64bac10dd71b5b20&bd=AY3RFZHVEQg%3D%3…
Page No:
Aim:
Write a program to find the reverse of an integer number and check whether it is Palindrome or not.
ID: 21331A0211
At the time of execution, the program should print the message on the console as:
Enter an integer :
Source Code:
Program421.c
#include<stdio.h>
void main()
{
int n,temp,digit,reverse=0;
printf("Enter an integer : ");
scanf("%d",&n);
temp = n;
while(n!=0)
2021-2025-EEE-A
{
digit = n % 10;
reverse = reverse*10 + digit;
n = n/10;
}
printf("The reverse of a given number : %d\n",reverse);
MVGR College of Engineering (Autonomous)
if (temp==reverse)
{
printf("%d is a palindrome\n",temp);
}
else
{
printf("%d is not a palindrome\n",temp);
}
}
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=26&qId=5b63e31a64bac10dd71b5b20&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950… 1/2
24/08/2022, 22:29 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=26&qId=5b63e31a64bac10dd71b5b20&bd=AY3RFZHVEQg%3D%3…
Test Case - 1
User Output
Page No:
Enter an integer : 2017
The reverse of a given number : 7102
2017 is not a palindrome
ID: 21331A0211
Test Case - 2
User Output
Enter an integer : 1221
The reverse of a given number : 1221
1221 is a palindrome
Test Case - 3
User Output
Enter an integer : 12321
The reverse of a given number : 12321
12321 is a palindrome
Test Case - 4
User Output
Enter an integer : 18771
The reverse of a given number : 17781
18771 is not a palindrome
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=26&qId=5b63e31a64bac10dd71b5b20&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950… 2/2
24/08/2022, 22:30 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=27&qId=585e3e990cf2f069ef156bdc&bd=AY3RFZHVEQg%3D%3D&…
S.No: 27 Exp. Name: Write a C program to print the Pyramid with numbers Date: 2022-07-28
Aim:
Page No:
Write a program to print a pyramid of numbers separated by spaces for the given number of rows.
At the time of execution, the program should print the message on the console as:
ID: 21331A0211
Enter number of rows :
1
1 2
1 2 3
Source Code:
PyramidDemo15.c
#include <stdio.h>
void main() {
int n, i, j, s;
printf("Enter number of rows : ");
scanf("%d", &n);
// Fill the missing code
for (i=1;i<=n;i++)
{
for (s=1;s<=n-i;s++)
{
printf(" ");
}
for(j=1;j<=i;j++)
2021-2025-EEE-A
{
printf("%d ", j);
}
printf("\n");
}
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter number of rows : 3
1
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=27&qId=585e3e990cf2f069ef156bdc&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 22:30 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=27&qId=585e3e990cf2f069ef156bdc&bd=AY3RFZHVEQg%3D%3D&…
Test Case - 1
1 2
1 2 3
Page No:
Test Case - 2
User Output
ID: 21331A0211
Enter number of rows : 6
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
Test Case - 3
User Output
Enter number of rows : 8
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=27&qId=585e3e990cf2f069ef156bdc&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
24/08/2022, 22:30 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=28&qId=585e43b00cf2f069ef156d9f&bd=AY3RFZHVEQg%3D%3D&l…
Exp. Name: Write a C program to find the Largest and Smallest elements of the
S.No: 28 Date: 2022-07-28
array
Page No:
Aim:
Write a program to read an array of integers (with max size 10) and print the largest and the smallest of
the given numbers.
ID: 21331A0211
At the time of execution, the program should print the message on the console as:
Next, the program should print the messages one by one on the console as:
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
2021-2025-EEE-A
ArrayAccessDemo10.c
#include<stdio.h>
void main()
{
int array[10],i,n,largest,smallest;
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=28&qId=585e43b00cf2f069ef156d9f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 22:30 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=28&qId=585e43b00cf2f069ef156d9f&bd=AY3RFZHVEQg%3D%3D&l…
largest=array[i];
}
if (array[i]<smallest)
{
Page No:
smallest=array[i];
}
}
printf("The largest element of the array = %d\n",largest);
ID: 21331A0211
printf("The smallest element of the array = %d\n",smallest);
}
Test Case - 1
User Output
Enter how many values you want to read : 6
Enter the value of a[0] : 1
Enter the value of a[1] : 5
Enter the value of a[2] : 9
Enter the value of a[3] : 6
Enter the value of a[4] : 4
Enter the value of a[5] : 2
The largest element of the array = 9
The smallest element of the array = 1
Test Case - 2
User Output
Enter how many values you want to read : 4
Enter the value of a[0] : -23
Enter the value of a[1] : -45
Enter the value of a[2] : -12
2021-2025-EEE-A
Enter the value of a[3] : -29
The largest element of the array = -12
The smallest element of the array = -45
Test Case - 3
MVGR College of Engineering (Autonomous)
User Output
Enter how many values you want to read : 7
Enter the value of a[0] : 24
Enter the value of a[1] : 567
Enter the value of a[2] : 456
Enter the value of a[3] : 345
Enter the value of a[4] : 890
Enter the value of a[5] : 21
Enter the value of a[6] : 378
The largest element of the array = 890
The smallest element of the array = 21
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=28&qId=585e43b00cf2f069ef156d9f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
24/08/2022, 22:31 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=29&qId=585e4ac40cf2f069ef15727c&bd=AY3RFZHVEQg%3D%3D&…
S.No: 29 Exp. Name: Write a C program to Search an element using Linear Search process Date: 2022-07-28
Aim:
Page No:
Write a program to search a key element with in the given array of elements using linear search
process.
ID: 21331A0211
At the time of execution, the program should print the message on the console as:
Enter value of n :
Enter value of n : 3
Next, the program should print the messages one by one on the console as:
Next, the program should print the message on the console as:
2021-2025-EEE-A
Similarly if the key element is given as 25 for the above one dimensional array elements then the program
should print the output as "The Key element 25 is not found in the array".
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
MVGR College of Engineering (Autonomous)
Program509.c
#include<stdio.h>
void main()
{
int i,j,n,a[10],key,flag,index;
printf("Enter value of n : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element for a[%d] : ",i);
scanf("%d",&a[i]);
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=29&qId=585e4ac40cf2f069ef15727c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/3
24/08/2022, 22:31 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=29&qId=585e4ac40cf2f069ef15727c&bd=AY3RFZHVEQg%3D%3D&…
}
printf("Enter key element : ");scanf("%d",&key);
for (i=0;i<n;i++)
{
Page No:
if (a[i]==key)
{
flag = 1;
index = i;
ID: 21331A0211
break;
}
else
{
flag = 0;
}
}
if (flag == 1)
{
printf("The key element %d is found at the position %d\n",key,index);
}
else
{
printf("The key element %d is not found in the array\n",key);
}
}
Test Case - 1
User Output
Enter value of n : 5
Enter element for a[0] : 45
Enter element for a[1] : 67
Enter element for a[2] : 35
2021-2025-EEE-A
Enter element for a[3] : 28
Enter element for a[4] : 16
Enter key element : 28
The key element 28 is found at the position 3
Test Case - 2
MVGR College of Engineering (Autonomous)
User Output
Enter value of n : 5
Enter element for a[0] : 2
Enter element for a[1] : 7
Enter element for a[2] : 5
Enter element for a[3] : 1
Enter element for a[4] : 4
Enter key element : 2
The key element 2 is found at the position 0
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=29&qId=585e4ac40cf2f069ef15727c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/3
24/08/2022, 21:43 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=30&qId=585e415a0cf2f069ef156ce6&bd=AY3RFZHVEQg%3D%3D&…
S.No: 30 Exp. Name: Write a C program to display the elements of an Array in reverse order Date: 2022-08-19
Aim:
Page No:
Write a program to print the given integer elements of an array (with max size 10) in reverse order .
At the time of execution, the program should print the message on the console as:
ID: 21331A0211
Enter size of the array :
Next, the program should print the message on the console as:
[Hint: First read an integers from standard input into the array and then use a loop to iterate on that array in the
reverse order (meaning starting from the last element till the first) to print the elements.]
ArrayAccessDemo1.c
#include <stdio.h>
void main() {
int arr[10], i, n;
2021-2025-EEE-A
printf("Enter size of the array : ");
scanf("%d", &n );
printf("Enter array elements : ");
// Fill the missing code
for(i=0;i<n;i++) {
scanf("%d",&arr[i]);
}
MVGR College of Engineering (Autonomous)
Test Case - 1
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=30&qId=585e415a0cf2f069ef156ce6&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 21:43 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=30&qId=585e415a0cf2f069ef156ce6&bd=AY3RFZHVEQg%3D%3D&…
Test Case - 1
User Output
Page No:
Enter size of the array : 3
Enter array elements : 10 20 30
Array elements in reverse order : 30 20 10
ID: 21331A0211
Test Case - 2
User Output
Enter size of the array : 6
Enter array elements : 11 88 66 22 33 44
Array elements in reverse order : 44 33 22 66 88 11
Test Case - 3
User Output
Enter size of the array : 4
Enter array elements : 123 345 675 845
Array elements in reverse order : 845 675 345 123
Test Case - 4
User Output
Enter size of the array : 3
Enter array elements : -23 -45 -99
Array elements in reverse order : -99 -45 -23
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=30&qId=585e415a0cf2f069ef156ce6&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
24/08/2022, 21:43 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=31&qId=5d524824e3fffe0a91cf9399&bd=AY3RFZHVEQg%3D%3D&l…
S.No: 31 Exp. Name: Write a Program to find 2’s complement of a given Binary number Date: 2022-08-19
Aim:
Page No:
Write a C program to find 2’s complement of a given binary number.
ID: 21331A0211
Enter the size of binary number : 5
Enter 5 bit binary number : 1 0 0 1 0
2's complement is : 0 1 1 1 0
Source Code:
TwosComplement.c
#include <stdio.h>
void main() {
int n,a[20],i,flag=0;
printf("Enter the size of binary number : ");
scanf("%d",&n);
printf("Enter %d bit binary number : ",n);
for (i=0;i<n;i++) {
scanf("%d",&a[i]);
}
for(i=n-1;i>=0;i--) {
if (a[i] == 1 && flag == 0) {
flag = 1;
continue;
}
if (a[i] == 0 && flag == 0) {
continue;
}
2021-2025-EEE-A
if (a[i] == 1)
a[i] = 0;
else
a[i] = 1;
}
printf("2's complement is : ");
for(i=0;i<n;i++) {
MVGR College of Engineering (Autonomous)
printf("%d ",a[i]);
}
printf("\n");
}
Test Case - 1
User Output
Enter the size of binary number : 6
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=31&qId=5d524824e3fffe0a91cf9399&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 21:43 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=31&qId=5d524824e3fffe0a91cf9399&bd=AY3RFZHVEQg%3D%3D&l…
Test Case - 1
Enter 6 bit binary number : 1 0 0 0 1 1
2's complement is : 0 1 1 1 0 1
Page No:
Test Case - 2
User Output
ID: 21331A0211
Enter the size of binary number : 10
Enter 10 bit binary number : 1 1 1 1 1 1 1 1 1 0
2's complement is : 0 0 0 0 0 0 0 0 1 0
Test Case - 3
User Output
Enter the size of binary number : 4
Enter 4 bit binary number : 1 1 1 1
2's complement is : 0 0 0 1
Test Case - 4
User Output
Enter the size of binary number : 8
Enter 8 bit binary number : 1 0 1 0 1 0 1 0
2's complement is : 0 1 0 1 0 1 1 0
Test Case - 5
User Output
Enter the size of binary number : 15
Enter 15 bit binary number : 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1
2's complement is : 0 0 0 1 1 1 0 0 0 1 1 1 0 0 1
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=31&qId=5d524824e3fffe0a91cf9399&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
24/08/2022, 21:44 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=32&qId=5d524de9e3fffe0a91cf9a58&bd=AY3RFZHVEQg%3D%3D&l…
S.No: 32 Exp. Name: Write a Program to Eliminate Duplicate elements in an array Date: 2022-08-19
Aim:
Page No:
Write a C program to eliminate duplicate elements in an array.
ID: 21331A0211
Enter array size : 9
Enter 9 array elements : 1 2 2 3 4 4 4 5 5
After eliminating duplicates, the array is : 1 2 3 4 5
Source Code:
EliminateDuplicates.c
#include<stdio.h>
void main()
{
int a[20],i,j,k,n;
printf("Enter array size : ");
scanf("%d",&n);
printf("Enter %d array elements : ",n);
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for (i=0;i<n;i++)
{
for (j=i+1;j<n; )
{
if (a[j] == a[i]){
for(k=j;k<n;k++)
2021-2025-EEE-A
{
a[k]=a[k+1];
}
n--;
}
else
{
MVGR College of Engineering (Autonomous)
j++;
}
}
}
printf("After eliminating duplicates, the array is : ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=32&qId=5d524de9e3fffe0a91cf9a58&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 21:44 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=32&qId=5d524de9e3fffe0a91cf9a58&bd=AY3RFZHVEQg%3D%3D&l…
Test Case - 1
Page No:
User Output
Enter array size : 5
ID: 21331A0211
Enter 5 array elements : 1 2 1 2 3
After eliminating duplicates, the array is : 1 2 3
Test Case - 2
User Output
Enter array size : 5
Enter 5 array elements : 11 13 11 12 13
After eliminating duplicates, the array is : 11 13 12
Test Case - 3
User Output
Enter array size : 10
Enter 10 array elements : 1 5 1 2 3 9 2 5 8 6
After eliminating duplicates, the array is : 1 5 2 3 9 8 6
Test Case - 4
User Output
Enter array size : 5
Enter 5 array elements : 10 20 30 20 10
After eliminating duplicates, the array is : 10 20 30
Test Case - 5
User Output
2021-2025-EEE-A
Enter array size : 4
Enter 4 array elements : 2 2 2 2
After eliminating duplicates, the array is : 2
Test Case - 6
MVGR College of Engineering (Autonomous)
User Output
Enter array size : 3
Enter 3 array elements : 10 50 60
After eliminating duplicates, the array is : 10 50 60
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=32&qId=5d524de9e3fffe0a91cf9a58&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
24/08/2022, 21:44 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=33&qId=585e4afa0cf2f069ef1572d0&bd=AY3RFZHVEQg%3D%3D&l…
Exp. Name: Write a C program to find the Addition of Two matrices by checking
S.No: 33 Date: 2022-08-23
compatibility
Page No:
Aim:
Write a program to find the addition of two matrices .
ID: 21331A0211
At the time of execution, the program should print the message on the console as:
Next, the program should print the message on the console as:
Next, the program should print the message on the console as:
Next, the program should print the message on the console as:
2021-2025-EEE-A
then the program should print the result as:
1 2
3 4
Addition of two matrices is
2 4
6 8
Source Code:
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=33&qId=585e4afa0cf2f069ef1572d0&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/4
24/08/2022, 21:44 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=33&qId=585e4afa0cf2f069ef1572d0&bd=AY3RFZHVEQg%3D%3D&l…
Program511.c
#include<stdio.h>
Page No:
void main()
{
int i,j,m,n,p,q,a[5][5],b[5][5],c[5][5];
printf("Enter the row & column sizes of matrix-1 : ");
ID: 21331A0211
scanf("%d%d",&m,&n);
printf("Enter matrix-1 %d elements : ",m*n);
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the row & column sizes of matrix-2 : ");
scanf("%d%d",&p,&q);
printf("Enter matrix-2 %d elements : ",p*q);
for (i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("The given matrix-1 is\n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("The given matrix-2 is\n");
2021-2025-EEE-A
for (i=0;i<p;i++)
{
for (j=0;j<q;j++)
{
printf("%d ",b[i][j]);
}
MVGR College of Engineering (Autonomous)
printf("\n");
}
if(m==p&&n==q)
{
printf("Addition of two matrices is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d ",c[i][j]);
}
printf("\n");
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=33&qId=585e4afa0cf2f069ef1572d0&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/4
24/08/2022, 21:44 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=33&qId=585e4afa0cf2f069ef1572d0&bd=AY3RFZHVEQg%3D%3D&l…
}
}
else
{
Page No:
printf("Addition is not possible\n");
}
}
ID: 21331A0211
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Enter the row & column sizes of matrix-1 : 2 2
Enter matrix-1 4 elements : 11 22 33 44
Enter the row & column sizes of matrix-2 : 2 2
Enter matrix-2 4 elements : 22 33 44 55
The given matrix-1 is
11 22
33 44
The given matrix-2 is
22 33
44 55
Addition of two matrices is
33 55
77 99
Test Case - 2
User Output
Enter the row & column sizes of matrix-1 : 2 3
Enter matrix-1 6 elements : 1 2 3 4 5 6
Enter the row & column sizes of matrix-2 : 3 2
2021-2025-EEE-A
Enter matrix-2 6 elements : 1 3 4 5 6 7
The given matrix-1 is
1 2 3
4 5 6
The given matrix-2 is
1 3
MVGR College of Engineering (Autonomous)
4 5
6 7
Addition is not possible
Test Case - 3
User Output
Enter the row & column sizes of matrix-1 : 3 3
Enter matrix-1 9 elements : 1 2 3 4 5 6 7 8 9
Enter the row & column sizes of matrix-2 : 3 3
Enter matrix-2 9 elements : 1 3 2 4 6 5 8 7 9
The given matrix-1 is
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=33&qId=585e4afa0cf2f069ef1572d0&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 3/4
24/08/2022, 21:45 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=34&qId=585e4b390cf2f069ef15731a&bd=AY3RFZHVEQg%3D%3D…
Page No:
Aim:
Write a program to find the multiplication of two matrices .
ID: 21331A0211
At the time of execution, the program should print the message on the console as:
Next, the program should print the message on the console as:
Next, the program should print the message on the console as:
Next, the program should print the message on the console as:
2021-2025-EEE-A
then the program should print the result as:
4 5
6 7
8 9
Multiplication of two matrices is
40 46
94 109
Source Code:
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=34&qId=585e4b390cf2f069ef15731a&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/4
24/08/2022, 21:45 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=34&qId=585e4b390cf2f069ef15731a&bd=AY3RFZHVEQg%3D%3D…
Program513.c
#include<stdio.h>
Page No:
void main()
{
int i,j,k,m,n,p,q,a[5][5],b[5][5],c[5][5];
printf("Enter the row & column sizes of matrix-1 : ");
ID: 21331A0211
scanf("%d%d",&m,&n);
printf("Enter matrix-1 %d elements : ",m*n);
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the row & column sizes of matrix-2 : ");
scanf("%d%d",&p,&q);
printf("Enter matrix-2 %d elements : ",p*q);
for(i=0;i<p;i++)
{
for (j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("The given matrix-1 is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("The given matrix-2 is\n");
2021-2025-EEE-A
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d ",b[i][j]);
}
MVGR College of Engineering (Autonomous)
printf("\n");
}
if (n==p)
{
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];
}
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=34&qId=585e4b390cf2f069ef15731a&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/4
24/08/2022, 21:45 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=34&qId=585e4b390cf2f069ef15731a&bd=AY3RFZHVEQg%3D%3D…
}
}
printf("Multiplication of two matrices is\n");
for(i=0;i<m;i++)
Page No:
{
for(j=0;j<q;j++)
{
printf("%d ",c[i][j]);
ID: 21331A0211
}
printf("\n");
}
}
else
{
printf("Multiplication is not possible\n");
}
}
Test Case - 1
User Output
Enter the row & column sizes of matrix-1 : 2 2
Enter matrix-1 4 elements : 11 33 22 44
Enter the row & column sizes of matrix-2 : 2 2
Enter matrix-2 4 elements : 11 33 44 22
The given matrix-1 is
11 33
22 44
The given matrix-2 is
11 33
44 22
Multiplication of two matrices is
2021-2025-EEE-A
1573 1089
2178 1694
Test Case - 2
User Output
MVGR College of Engineering (Autonomous)
Exp. Name: Write a C program to Sort the given elements in Ascending order
S.No: 35 Date: 2022-08-23
using Bubble Sort
Page No:
Aim:
Write a program to sort ( Ascending order ) the given elements using bubble sort technique .
ID: 21331A0211
At the time of execution, the program should print the message on the console as:
Enter value of n :
Enter value of n : 3
Next, the program should print the messages one by one on the console as:
2021-2025-EEE-A
Note: Do use the printf() function with a newline character ( \n ).
Source Code:
Program504.c
#include<stdio.h>
MVGR College of Engineering (Autonomous)
void main()
{
int i,j,a[10],n,temp;
printf("Enter value of n : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element for a[%d] : ",i);
scanf("%d",&a[i]);
}
printf("Before sorting the elements in the array are\n");
for (i=0;i<n;i++)
{
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=35&qId=585e49d80cf2f069ef15718f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/4
24/08/2022, 21:46 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=35&qId=585e49d80cf2f069ef15718f&bd=AY3RFZHVEQg%3D%3D&l…
printf("Value of a[%d] = %d\n",i,a[i]);
}
for (i=0;i<n-1;i++)
{
Page No:
for (j=0;j<n-i-1;j++)
{
if (a[j]>a[j+1])
{
ID: 21331A0211
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("After sorting the elements in the array are\n");
for(i=0;i<n;i++)
{
printf("Value of a[%d] = %d\n",i,a[i]);
}
}
Test Case - 1
User Output
Enter value of n : 5
Enter element for a[0] : 2
Enter element for a[1] : 7
Enter element for a[2] : 6
Enter element for a[3] : 4
Enter element for a[4] : 1
Before sorting the elements in the array are
Value of a[0] = 2
2021-2025-EEE-A
Value of a[1] = 7
Value of a[2] = 6
Value of a[3] = 4
Value of a[4] = 1
After sorting the elements in the array are
Value of a[0] = 1
MVGR College of Engineering (Autonomous)
Value of a[1] = 2
Value of a[2] = 4
Value of a[3] = 6
Value of a[4] = 7
Test Case - 2
User Output
Enter value of n : 4
Enter element for a[0] : 28
Enter element for a[1] : 34
Enter element for a[2] : 26
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=35&qId=585e49d80cf2f069ef15718f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/4
24/08/2022, 21:46 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=36&qId=5865f4650cf2bd9bdd2be71d&bd=AY3RFZHVEQg%3D%3D…
Exp. Name: Write a C program to Concatenate two given strings without using
S.No: 36 Date: 2022-08-23
string library functions
Page No:
Aim:
Write a program to concatenate two given strings without using string library functions.
ID: 21331A0211
At the time of execution, the program should print the message on the console as:
string1 :
string1 : ILove
Next, the program should print the message on the console as:
string2 :
string2 : Coding
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program605.c
#include<stdio.h>
void main()
{
int i,j;
char str1[20],str2[20],str3[20];
2021-2025-EEE-A
printf("string1 : ");
scanf("%s", str1);
printf("string2 : ");
scanf("%s", str2);
for (i=0; str1[i] != '\0';i++)
{
str3[i]=str1[i];
MVGR College of Engineering (Autonomous)
}
for (j=0; str2[j] != '\0';j++)
{
str3[i]=str2[j];
i++;
}
str3[i] = '\0';
printf("concatenated string = %s\n",str3);
}
Test Case - 1
User Output
Page No:
string1 : ILove
string2 : Coding
concatenated string = ILoveCoding
ID: 21331A0211
Test Case - 2
User Output
string1 : 1234
string2 : 567
concatenated string = 1234567
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=36&qId=5865f4650cf2bd9bdd2be71d&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
24/08/2022, 21:46 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=37&qId=5866094d0cf2bd9bdd2c0b07&bd=AY3RFZHVEQg%3D%3D…
Exp. Name: Write a C program to Reverse the given string without using the
S.No: 37 Date: 2022-08-23
Library Functions
Page No:
Aim:
Write a program to reverse the given string without using the library functions.
ID: 21331A0211
At the time of execution, the program should print the message on the console as:
Enter a string :
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program609.c
#include<stdio.h>
void main()
{
int i,j,temp;
char ch[20];
printf("Enter a string : ");
scanf("%s",ch);
for (i=0;ch[i] != '\0';i++)
{
}
for (j=0;j<i;j++)
{
temp = ch[i-1];
2021-2025-EEE-A
ch[i-1] = ch[j];
ch[j] = temp;
i--;
}
printf("Reverse string : %s\n",ch);
}
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter a string : Dallas
Reverse string : sallaD
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=37&qId=5866094d0cf2bd9bdd2c0b07&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/1
24/08/2022, 21:47 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=38&qId=5885d3240cf2a7e7e1393e5c&bd=AY3RFZHVEQg%3D%3D…
Aim:
Page No:
Write a C program to compute the binomial coefficient ncr .
ID: 21331A0211
n! = n * (n - 1)!; if n > 0 (Recursive criteria)
n! = 1; if n = 0 (Base criteria)
At the time of execution, the program should print the message on the console as:
Source Code:
FindingNcr.c
#include <stdio.h>
#include "RecursiveFactorial.c"
void main() {
long int n, r;
printf("Enter the value of n and r : ");
scanf("%ld%ld", &n, &r);
printf("The value of %ldc%ld = %ld\n", n, r, FindNcr(n, r));
}
2021-2025-EEE-A
RecursiveFactorial.c
return n * factorial(n-1);
}
}
long int FindNcr(long int n, long int r) {
long result;
result = factorial(n) / (factorial(r) * factorial(n-r));
return result;
}
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=38&qId=5885d3240cf2a7e7e1393e5c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
24/08/2022, 21:47 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=38&qId=5885d3240cf2a7e7e1393e5c&bd=AY3RFZHVEQg%3D%3D…
Test Case - 1
User Output
Page No:
Enter the value of n and r : 6 2
The value of 6c2 = 15
Test Case - 2
ID: 21331A0211
User Output
Enter the value of n and r : 5 3
The value of 5c3 = 10
Test Case - 3
User Output
Enter the value of n and r : 15 6
The value of 15c6 = 5005
Test Case - 4
User Output
Enter the value of n and r : 18 10
The value of 18c10 = 43758
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=38&qId=5885d3240cf2a7e7e1393e5c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
24/08/2022, 21:47 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=39&qId=5d5144d7e3fffe0a91cf5f9f&bd=AY3RFZHVEQg%3D%3D&li…
S.No: 39 Exp. Name: Write a Program to find the Length of a String Date: 2022-08-23
Aim:
Page No:
Write a C program to find the length of a given string.
ID: 21331A0211
Enter the string : CodeTantra
Length of CodeTantra : 10
Source Code:
StrLength.c
#include <stdio.h>
#include "StrLength1.c"
void main() {
char str[30];
printf("Enter the string : ");
scanf("%s", str);
printf("Length of %s : %d\n", str, myStrLen(str));
}
StrLength1.c
2021-2025-EEE-A
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
Enter the string : IndoUsUk
Length of IndoUsUk : 8
Test Case - 3
User Output
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=39&qId=5d5144d7e3fffe0a91cf5f9f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950694… 1/2
24/08/2022, 21:48 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=40&qId=5d51484ce3fffe0a91cf609c&bd=AY3RFZHVEQg%3D%3D&l…
Exp. Name: Write a C program to find the Transpose of a given Matrix using
S.No: 40 Date: 2022-08-23
Functions
Page No:
Aim:
Write a program to find the transpose of a given matrix using functions.
ID: 21331A0211
Sample Input and Output:
Source Code:
TransposeFunction.c
#include <stdio.h>
#include "TransposeFunction1.c"
void main() {
int a[5][5], b[5][5], m, n, p, q;
printf("Enter the size of matrix : ");
scanf("%d%d", &m, &n);
read(a, m, n);
printf("The given matrix is\n");
display(a, m, n);
transposeMatrix(a, b, m, n);
printf("The transpose matrix is\n");
display(b, n, m);
}
TransposeFunction1.c 2021-2025-EEE-A
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=40&qId=5d51484ce3fffe0a91cf609c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/3
24/08/2022, 21:48 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=40&qId=5d51484ce3fffe0a91cf609c&bd=AY3RFZHVEQg%3D%3D&l…
{
for(j=0;j<n;j++)
{
printf("%d ",y[i][j]);
Page No:
}
printf("\n");
}
}
ID: 21331A0211
void transposeMatrix(int a[5][5], int b[5][5], int m, int n) {
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[j][i]=a[i][j];
}
}
Test Case - 1
User Output
Enter the size of matrix : 2 2
Enter 4 elements : 1 2 3 4
The given matrix is
1 2
3 4
The transpose matrix is
1 3
2 4
2021-2025-EEE-A
Test Case - 2
User Output
Enter the size of matrix : 2 3
Enter 6 elements : 22 33 44 55 66 77
MVGR College of Engineering (Autonomous)
Test Case - 3
User Output
Enter the size of matrix : 3 3
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=40&qId=5d51484ce3fffe0a91cf609c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/3
24/08/2022, 21:48 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=41&qId=5885f6980cf2a7e7e1396286&bd=AY3RFZHVEQg%3D%3D…
Exp. Name: Write a C program to display the Fibonacci series up to the given
S.No: 41 Date: 2022-08-23
number of terms using Recursion
Page No:
Aim:
Write a program to display the fibonacci series up to the given number of terms using recursion process.
ID: 21331A0211
The fibonacci series is 0 1 1 2 3 5 8 13 21 34...... .
At the time of execution, the program should print the message on the console as:
Enter value of n :
Enter value of n : 6
Program908.c
#include <stdio.h>
#include "Program908a.c"
void main() {
int n, i;
printf("Enter value of n : ");
scanf("%d", &n);
printf("The fibonacci series of %d terms are : ", n);
for (i = 0; i < n; i++) {
printf(" %d ", fib(i));
}
}
Program908a.c 2021-2025-EEE-A
int fib(int n)
{
MVGR College of Engineering (Autonomous)
if(n==0)
{
return 0;
}
else if(n==1)
{
return 1;
} else {
return fib(n-1)+fib(n-2);
}
}
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=41&qId=5885f6980cf2a7e7e1396286&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
24/08/2022, 21:48 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=41&qId=5885f6980cf2a7e7e1396286&bd=AY3RFZHVEQg%3D%3D…
Test Case - 1
Page No:
User Output
Enter value of n : 4
ID: 21331A0211
The fibonacci series of 4 terms are : 0 1 1 2
Test Case - 2
User Output
Enter value of n : 8
The fibonacci series of 8 terms are : 0 1 1 2 3 5 8 13
Test Case - 3
User Output
Enter value of n : 14
The fibonacci series of 14 terms are : 0 1 1 2 3 5 8 13 21 34 55 89 144 233
Test Case - 4
User Output
Enter value of n : 3
The fibonacci series of 3 terms are : 0 1 1
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=41&qId=5885f6980cf2a7e7e1396286&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
24/08/2022, 21:49 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=42&qId=5885ef250cf2a7e7e139624b&bd=AY3RFZHVEQg%3D%3D…
S.No: 42 Exp. Name: Write a C program to find the LCM of two numbers using Recursion Date: 2022-08-23
Aim:
Page No:
Write a program to find the lcm (Least Common Multiple) of a given two numbers using recursion process.
The least common multiple ( lcm ) of two or more integers, is the smallest positive integer that is divisible by
ID: 21331A0211
both a and b.
At the time of execution, the program should print the message on the console as:
Note: Write the function lcm() and recursive function gcd() in Program907a.c .
Source Code:
Program907.c
#include <stdio.h>
#include "Program907a.c"
void main() {
int a, b;
printf("Enter two integer values : ");
scanf("%d %d", &a, &b);
printf("The lcm of two numbers %d and %d = %d\n", a, b, lcm(a, b));
}
Program907a.c
2021-2025-EEE-A
int a,b;
int gcd(a,b)
{
if (a%b==0)
{
MVGR College of Engineering (Autonomous)
return b;
}
else {
return gcd (b,a%b);
}
}
int lcm(a,b)
{
return(a*b)/gcd(a,b);
}
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=42&qId=5885ef250cf2a7e7e139624b&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
24/08/2022, 21:49 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=42&qId=5885ef250cf2a7e7e139624b&bd=AY3RFZHVEQg%3D%3D…
Test Case - 1
Page No:
User Output
Enter two integer values : 34 24
ID: 21331A0211
The lcm of two numbers 34 and 24 = 408
Test Case - 2
User Output
Enter two integer values : 6 9
The lcm of two numbers 6 and 9 = 18
Test Case - 3
User Output
Enter two integer values : 345 467
The lcm of two numbers 345 and 467 = 161115
Test Case - 4
User Output
Enter two integer values : 100 88
The lcm of two numbers 100 and 88 = 2200
Test Case - 5
User Output
Enter two integer values : 123 420
The lcm of two numbers 123 and 420 = 17220
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=42&qId=5885ef250cf2a7e7e139624b&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
24/08/2022, 21:49 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=43&qId=5885df2f0cf2a7e7e1395d20&bd=AY3RFZHVEQg%3D%3D…
Exp. Name: Write a C program to find the Factorial of a given number using
S.No: 43 Date: 2022-08-23
Recursion
Page No:
Aim:
Write a program to find the factorial of a given number using recursion process.
ID: 21331A0211
At the time of execution, the program should print the message on the console as:
Enter an integer :
Enter an integer : 6
Factorial of 6 is : 720
Source Code:
Program901.c
#include <stdio.h>
#include "Program901a.c"
void main() {
long int n;
printf("Enter an integer : ");
scanf("%ld", &n);
printf("Factorial of %ld is : %ld\n", n ,factorial(n));
}
Program901a.c
2021-2025-EEE-A
long int factorial(long int n)
{
if (n<=1)
{
return 1;
}
else
MVGR College of Engineering (Autonomous)
{
return n*factorial(n-1);
}
}
Test Case - 1
User Output
Enter an integer : 5
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=43&qId=5885df2f0cf2a7e7e1395d20&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 21:49 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=43&qId=5885df2f0cf2a7e7e1395d20&bd=AY3RFZHVEQg%3D%3D…
Test Case - 1
Factorial of 5 is : 120
Page No:
Test Case - 2
User Output
ID: 21331A0211
Enter an integer : 7
Factorial of 7 is : 5040
Test Case - 3
User Output
Enter an integer : 4
Factorial of 4 is : 24
Test Case - 4
User Output
Enter an integer : 8
Factorial of 8 is : 40320
Test Case - 5
User Output
Enter an integer : 0
Factorial of 0 is : 1
Test Case - 6
User Output
Enter an integer : 9
Factorial of 9 is : 362880
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=43&qId=5885df2f0cf2a7e7e1395d20&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
24/08/2022, 21:50 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=44&qId=5d515567e3fffe0a91cf64bc&bd=AY3RFZHVEQg%3D%3D&l…
S.No: 44 Exp. Name: Write a program to find the Sum of Series using Recursion Date: 2022-08-23
Aim:
Page No:
Write a program to find the sum of series 11 + 22 + 33 + ....... using recursion process.
At the time of execution, the program should print the message on the console as:
ID: 21331A0211
Enter value of n :
Enter value of n : 3
Source Code:
SumOfSeries.c
#include <stdio.h>
#include "SumOfSeries1.c"
void main() {
long int n;
printf("Enter value of n : ");
scanf("%li", &n);
printf("Sum of the series = %li\n", sumOfSeries(n));
}
SumOfSeries1.c
#include<math.h>
long int sumOfSeries(int n)
2021-2025-EEE-A
{
if (n==1)
{
return 1;
}
else
{
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter value of n : 4
Sum of the series = 288
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=44&qId=5d515567e3fffe0a91cf64bc&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 21:51 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=45&qId=5d51591de3fffe0a91cf6622&bd=AY3RFZHVEQg%3D%3D&l…
S.No: 45 Exp. Name: Write a program to implement Ackermann function using Recursion Date: 2022-08-23
Aim:
Page No:
Write a program to implement Ackermann function (https://en.wikipedia.org/wiki/Ackermann_function) using
recursion process.
ID: 21331A0211
At the time of execution, the program should print the message on the console as:
A(2, 1) = 5
Source Code:
AckermannFunction.c
#include <stdio.h>
#include "AckermannFunction1.c"
void main() {
long long int m, n;
printf("Enter two numbers : ");
scanf("%lli %lli", &m, &n);
printf("A(%lli, %lli) = %lli\n", m, n, ackermannFun(m, n));
}
AckermannFunction1.c
2021-2025-EEE-A
#include<math.h>
long long int ackermannFun(long long int m, long long int n)
{
if (m == 0)
return n + 1;
else if (n==0)
return ackermannFun(m-1,1);
MVGR College of Engineering (Autonomous)
else
return ackermannFun(m-1,ackermannFun(m,n-1));
}
Test Case - 1
User Output
Enter two numbers : 0 1
A(0, 1) = 2
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=45&qId=5d51591de3fffe0a91cf6622&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/3
24/08/2022, 21:51 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=46&qId=588b2f5f0cf2a7e7e13aa189&bd=AY3RFZHVEQg%3D%3D…
Page No:
Aim:
Write a program to swap two values by using call by address method.
ID: 21331A0211
At the time of execution, the program should print the message on the console as:
Note: Write the function swap() in Program1002a.c and do use the printf() function with a newline character
( \n ).
Source Code:
Program1002.c
#include <stdio.h>
#include "Program1002a.c"
void main() {
int a, b;
printf("Enter two integer values : ");
scanf("%d %d", &a, &b);
printf("Before swapping in main : a = %d b = %d\n", a, b);
swap(&a, &b);
printf("After swapping in main : a = %d b = %d\n", a, b);
}
Program1002a.c 2021-2025-EEE-A
int temp;
temp=*p;
*p=*q;
*q=temp;
printf("After swapping in swap : *p = %d *q = %d\n", *p, *q);
}
Test Case - 1
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=46&qId=588b2f5f0cf2a7e7e13aa189&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 21:51 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=46&qId=588b2f5f0cf2a7e7e13aa189&bd=AY3RFZHVEQg%3D%3D…
Test Case - 1
User Output
Page No:
Enter two integer values : 121 131
Before swapping in main : a = 121 b = 131
After swapping in swap : *p = 131 *q = 121
After swapping in main : a = 131 b = 121
ID: 21331A0211
Test Case - 2
User Output
Enter two integer values : 555 999
Before swapping in main : a = 555 b = 999
After swapping in swap : *p = 999 *q = 555
After swapping in main : a = 999 b = 555
Test Case - 3
User Output
Enter two integer values : 1001 101
Before swapping in main : a = 1001 b = 101
After swapping in swap : *p = 101 *q = 1001
After swapping in main : a = 101 b = 1001
Test Case - 4
User Output
Enter two integer values : 9999 2999
Before swapping in main : a = 9999 b = 2999
After swapping in swap : *p = 2999 *q = 9999
After swapping in main : a = 2999 b = 9999
Test Case - 5
2021-2025-EEE-A
User Output
Enter two integer values : 10101 11010
Before swapping in main : a = 10101 b = 11010
After swapping in swap : *p = 11010 *q = 10101
After swapping in main : a = 11010 b = 10101
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=46&qId=588b2f5f0cf2a7e7e13aa189&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
24/08/2022, 21:52 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=47&qId=588d951b0cf2a7e7e13ad8f1&bd=AY3RFZHVEQg%3D%3D…
Aim:
Page No:
Dangling pointer is a pointer which points to a memory that has been freed or deallocated. Such a
pointer still points to the memory location of the deallocated memory, accessing such memory might cause
segmentation fault error.
ID: 21331A0211
In simple terms, a pointer pointing to a non-existing memory location is called a dangling pointer.
void main() {
int *p = (int *) malloc(sizeof(int)); // allocating heap memory
....
free(p); // after deallocating the heap memory, *p now becomes a dangling
pointer
*p = 12; // trying to access non-existing memory location can result in error
}
void main() {
int *q;
int *p = (int *) malloc(sizeof(int)); // allocating heap memory
q = p; // store the address containing in p to q i.e., p and q are referring the
same heap memory
free(q); // after deallocating the heap memory through q
*p = 12; // trying to access non-existing memory location through p which is also
a dangling pointer, can result in an error
}
The below code gives an error message as it is referencing dangling pointer, Correct the code and print the
given value on the console.
Source Code:
2021-2025-EEE-A
DanglingPointer.c
#include <stdio.h>
#include <stdlib.h>
void main() {
int *p, *q;
MVGR College of Engineering (Autonomous)
p = (int *) malloc(sizeof(int));
*p = 99;
q = p;
printf("The given value = %d\n", *p);
free(q);
}
Test Case - 1
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=47&qId=588d951b0cf2a7e7e13ad8f1&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
24/08/2022, 21:52 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=47&qId=588d951b0cf2a7e7e13ad8f1&bd=AY3RFZHVEQg%3D%3D…
Test Case - 1
User Output
Page No:
The given value = 99
ID: 21331A0211
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=47&qId=588d951b0cf2a7e7e13ad8f1&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
24/08/2022, 21:52 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=48&qId=5d516882e3fffe0a91cf6a87&bd=AY3RFZHVEQg%3D%3D&l…
S.No: 48 Exp. Name: Write a C program to Copy one String into another using Pointers Date: 2022-08-23
Aim:
Page No:
Write a C program to copy one string into another using pointers.
ID: 21331A0211
Enter source string : Robotic Tool
Target string : Robotic Tool
Source Code:
CopyStringPointers.c
#include <stdio.h>
#include "CopyStringPointers1.c"
void main() {
char source[100], target[100];
printf("Enter source string : ");
gets(source);
copyString(target, source);
printf("Target string : %s\n", target);
}
CopyStringPointers1.c
2021-2025-EEE-A
Execution Results - All test cases have succeeded!
Test Case - 1
MVGR College of Engineering (Autonomous)
User Output
Enter source string : CodeTantra
Target string : CodeTantra
Test Case - 2
User Output
Enter source string : Robotic Tool
Target string : Robotic Tool
Test Case - 3
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=48&qId=5d516882e3fffe0a91cf6a87&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 21:53 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=49&qId=5d517283e3fffe0a91cf6cd9&bd=AY3RFZHVEQg%3D%3D&l…
Page No:
Aim:
Write a C program to find number of lowercase , uppercase , digits and other characters using
pointers.
ID: 21331A0211
Sample Input and Output:
Source Code:
CountCharDigitOthers.c
#include <stdio.h>
#include "CountCharDigitOthers1.c"
void main() {
char str[80];
int upperCount = 0, lowerCount = 0, digitCount = 0, otherCount = 0;
printf("Enter a string : ");
gets(str);
countCharDigitOthers(str, &upperCount, &lowerCount, &digitCount, &otherCount);
printf("Number of uppercase letters = %d\n", upperCount);
printf("Number of lowercase letters = %d\n", lowerCount);
printf("Number of digits = %d\n", digitCount);
printf("Number of other characters = %d\n", otherCount);
}
CountCharDigitOthers1.c
2021-2025-EEE-A
void countCharDigitOthers(char *str,int *upperCount,int *lowerCount,int *digitCount,int
*otherCount) {
int count = 0;
while (*str) {
if (*str >= 'A' && *str <= 'Z') {
MVGR College of Engineering (Autonomous)
*upperCount = *upperCount + 1;
} else if (*str >= 'a' && *str <= 'z') {
*lowerCount = *lowerCount + 1;
} else if (*str >= '0' && *str <= '9') {
*digitCount = *digitCount +1;
} else {
*otherCount = *otherCount + 1;
}
str++;
}
}
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=49&qId=5d517283e3fffe0a91cf6cd9&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 21:53 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=49&qId=5d517283e3fffe0a91cf6cd9&bd=AY3RFZHVEQg%3D%3D&l…
Test Case - 1
Page No:
User Output
Enter a string : CodeTantra123&*@987.
ID: 21331A0211
Number of uppercase letters = 2
Number of lowercase letters = 8
Number of digits = 6
Number of other characters = 4
Test Case - 2
User Output
Enter a string : Indo Pak 125 143 *.$
Number of uppercase letters = 2
Number of lowercase letters = 5
Number of digits = 6
Number of other characters = 7
Test Case - 3
User Output
Enter a string : 12345
Number of uppercase letters = 0
Number of lowercase letters = 0
Number of digits = 5
Number of other characters = 0
Test Case - 4
User Output
Enter a string : USA@
2021-2025-EEE-A
Number of uppercase letters = 3
Number of lowercase letters = 0
Number of digits = 0
Number of other characters = 1
MVGR College of Engineering (Autonomous)
Test Case - 5
User Output
Enter a string : Wellington@NZ I will Stay Here
Number of uppercase letters = 6
Number of lowercase letters = 19
Number of digits = 0
Number of other characters = 5
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=49&qId=5d517283e3fffe0a91cf6cd9&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
24/08/2022, 21:53 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=50&qId=59dcaa3a0cf2e6552052c9b8&bd=AY3RFZHVEQg%3D%3D…
Exp. Name: Write a C program to find Sum of array elements by allocating memory
S.No: 50 Date: 2022-08-24
using malloc() function
Page No:
Aim:
Write a program to find the sum of n elements by allocating memory by using malloc() function.
ID: 21331A0211
At the time of execution, the program should print the message on the console as:
Enter n value :
Enter n value : 4
Next, the program should print the message on the console as:
Enter 4 values :
Enter 4 values : 1 5 4 2
Source Code:
SumOfArray1.c
#include <stdio.h>
#include <stdlib.h>
#include "UsingMalloc.c"
void main() {
int *p, n, i;
2021-2025-EEE-A
printf("Enter n value : ");
scanf("%d", &n);
p = allocateMemory(n);
printf("Enter %d values : ", n);
read(p, n);
printf("The sum of given array elements : %d\n", sum(p, n));
}
MVGR College of Engineering (Autonomous)
UsingMalloc.c
int* allocateMemory(int n)
{
return (int* )malloc(n * sizeof(int));
}
void read(int *p,int n)
{
int i;
for (i=0;i<n;i++)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=50&qId=59dcaa3a0cf2e6552052c9b8&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
24/08/2022, 21:53 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=50&qId=59dcaa3a0cf2e6552052c9b8&bd=AY3RFZHVEQg%3D%3D…
{
scanf("%d",p+i);
}
}
Page No:
int sum(int *p, int n)
{
int i,total = 0;
for (i=0; i<n;i++)
ID: 21331A0211
{
total=total+ *(p+i);
}
return total;
}
Test Case - 1
User Output
Enter n value : 4
Enter 4 values : 1 4 5 2
The sum of given array elements : 12
Test Case - 2
User Output
Enter n value : 3
Enter 3 values : 10 20 30
The sum of given array elements : 60
Test Case - 3
User Output
2021-2025-EEE-A
Enter n value : 4
Enter 4 values : -5 -6 -4 -2
The sum of given array elements : -17 MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=50&qId=59dcaa3a0cf2e6552052c9b8&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
24/08/2022, 21:54 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=51&qId=5884ae730cf2a7e7e138f185&bd=AY3RFZHVEQg%3D%3D…
S.No: 51 Exp. Name: Write a Program to find Average Marks of the Class using Structures Date: 2022-08-23
Aim:
Page No:
Implement structures to read, write and compute average marks for a class of N students. Also identify
students scoring above and below the average marks
ID: 21331A0211
Sample Input and Output for the program are given below:
Program1110.c
#include <stdio.h>
MVGR College of Engineering (Autonomous)
struct student {
int roll;
char name[50];
int marks;
};
void main() {
struct student s[10];
int i, n, sum = 0;
float average = 0;
printf("Enter the number of students : ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter the details of student - %d\n", i+1);
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=51&qId=5884ae730cf2a7e7e138f185&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/3
24/08/2022, 21:54 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=51&qId=5884ae730cf2a7e7e138f185&bd=AY3RFZHVEQg%3D%3D…
printf("Enter the roll number : ");
scanf("%d",&s[i].roll); // Correct the code
printf("Enter the name : ");
scanf("%s",s[i].name); // Correct the code
Page No:
printf("Enter total marks : ");
scanf("%d",&s[i].marks); // Correct the code
}
for (i=0;i<n;i++){ // Complete the code
ID: 21331A0211
sum = sum + s[i].marks; // Complete the code
}
average = (float)sum/n; // Complete the code
printf("Class average : %f\n", average);
printf("RollNo\tStudentName\tTotalMarks\tAboveAverage(Y/N)\n");
for (i=0;i<n;i++) { // Complete the code
printf("%4d",s[i].roll); // Complete the code
printf("%15s",s[i].name); // Complete the code
printf("%15d",s[i].marks); // Complete the code
if (s[i].marks>=average) // Complete the code
printf("\tYes");
else
printf("\tNo");
printf("\n");
}
}
Test Case - 1
User Output
Enter the number of students : 3
Enter the details of student - 1 101
Enter the roll number : 101
Enter the name : Ganga
2021-2025-EEE-A
Enter total marks : 78
Enter the details of student - 2 102
Enter the roll number : 102
Enter the name : Saraswathi
Enter total marks : 83
Enter the details of student - 3 103
MVGR College of Engineering (Autonomous)
Test Case - 2
User Output
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=51&qId=5884ae730cf2a7e7e138f185&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/3
24/08/2022, 21:55 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=53&qId=5d5290b7e3fffe0a91d0016c&bd=AY3RFZHVEQg%3D%3D&…
Exp. Name: Write a C program to find Total Marks of a Student using Command-
S.No: 53 Date: 2022-08-23
line arguments
Page No:
Aim:
Write a C program to read student name and 3 subjects marks from the command line and display the student
details along with total.
ID: 21331A0211
Sample Input and Output - 1:
Hint : atoi() is a library function that converts string to integer. When program gets the input from command
line, string values transfer in the program, we have to convert them to integers. atoi() is used to return the
integer of the string arguments.
Source Code:
TotalMarksArgs.c
#include<stdio.h>
int main(int argc,char *argv[])
{
if (argc !=5)
2021-2025-EEE-A
{
printf("Arguments passed through command line are not equal to 4\n");
return 1;
}
printf("Student name : %s\n",argv[1]);
printf("Subject-1 marks : %s\n",argv[2]);
MVGR College of Engineering (Autonomous)
Test Case - 1
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=53&qId=5d5290b7e3fffe0a91d0016c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 21:55 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=53&qId=5d5290b7e3fffe0a91d0016c&bd=AY3RFZHVEQg%3D%3D&…
Test Case - 1
User Output
Page No:
Student name : Sachin
Subject-1 marks : 67
Subject-1 marks : 89
Subject-1 marks : 58
ID: 21331A0211
Total marks : 214
Test Case - 2
User Output
Arguments passed through command line are not equal to 4
Test Case - 3
User Output
Student name : Kohli
Subject-1 marks : 78
Subject-1 marks : 98
Subject-1 marks : 83
Total marks : 259
Test Case - 4
User Output
Student name : Taylor
Subject-1 marks : 45
Subject-1 marks : 45
Subject-1 marks : 45
Total marks : 135
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=53&qId=5d5290b7e3fffe0a91d0016c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
24/08/2022, 21:55 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=54&qId=5d529c1de3fffe0a91d00a2c&bd=AY3RFZHVEQg%3D%3D&…
Aim:
Page No:
Write a C program to implement realloc() .
The process is
ID: 21331A0211
1. Allocate memory of an array with size 2 by using malloc()
2. Assign the values 10 and 20 to the array
3. Reallocate the size of the array to 3 by using realloc()
4. Assign the value 30 to the newly allocated block
5. Display all the 3 values
Source Code:
ProgramOnRealloc.c
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int) * 2);
int i;
int *ptr_new;
*ptr = 10;
*(ptr + 1) = 20;
ptr_new = (int *)realloc(ptr, sizeof(int)*3);
*(ptr_new + 2) = 30;
for (i = 0; i < 3; i++)
printf("%d ", *(ptr_new + i));
}
Test Case - 1
2021-2025-EEE-A
User Output
10 20 30
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=54&qId=5d529c1de3fffe0a91d00a2c&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/1
24/08/2022, 21:55 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=55&qId=5d52a055e3fffe0a91d00b8e&bd=AY3RFZHVEQg%3D%3D…
S.No: 55 Exp. Name: Write a C program to demonstrate about Bit-fields Date: 2022-08-24
Aim:
Page No:
Write a C program to read and print a date using dd/mm/yyyy format using bit-fields and differentiate the
same without using bit-fields.
ID: 21331A0211
The process is
1. Create a structure date with bit fields
2. Assign the day, month and year to the members
3. Display the structure member values along with size of the structure
Source Code:
ProgramOnBitFields.c
#include <stdio.h>
struct date {
unsigned int day : 5;
unsigned int month : 4;
unsigned int year;
};
struct date1 {
unsigned int day;
unsigned int month;
unsigned int year;
};
void main() {
struct date d;
struct date1 d1;
printf("Enter a date : ");
scanf("%d%d%d", &d1.day, &d1.month, &d1.year);
d.day =d1.day;
d.month = d1.month;
d.year = d1.year;
printf("The size of the structure without bit-fields : %zu\n",sizeof(struct date1)
2021-2025-EEE-A
); // Fill in the missing code
printf("Date stored without bit-fields : %d/%d/%d\n",d1.day,d1.month,d1.year ); // F
ill in the missing code
printf("The size of the structure uses bit-fields : %zu\n",sizeof(struct date) ); //
Fill in the missing code
printf("Date stored in bit-fields : %d/%d/%d\n",d.day,d.month,d.year ); // Fill in t
he missing code
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter a date : 23 8 1998
The size of the structure without bit-fields : 12
Date stored without bit-fields : 23/8/1998
The size of the structure uses bit-fields : 8
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=55&qId=5d52a055e3fffe0a91d00b8e&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 21:55 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=55&qId=5d52a055e3fffe0a91d00b8e&bd=AY3RFZHVEQg%3D%3D…
Test Case - 1
Date stored in bit-fields : 23/8/1998
Page No:
Test Case - 2
User Output
ID: 21331A0211
Enter a date : 29 02 2016
The size of the structure without bit-fields : 12
Date stored without bit-fields : 29/2/2016
The size of the structure uses bit-fields : 8
Date stored in bit-fields : 29/2/2016
Test Case - 3
User Output
Enter a date : 1 1 2020
The size of the structure without bit-fields : 12
Date stored without bit-fields : 1/1/2020
The size of the structure uses bit-fields : 8
Date stored in bit-fields : 1/1/2020
Test Case - 4
User Output
Enter a date : 31 12 2018
The size of the structure without bit-fields : 12
Date stored without bit-fields : 31/12/2018
The size of the structure uses bit-fields : 8
Date stored in bit-fields : 31/12/2018
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=55&qId=5d52a055e3fffe0a91d00b8e&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
24/08/2022, 21:56 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=56&qId=58be7ffd0cf26ce633b573be&bd=AY3RFZHVEQg%3D%3D&…
S.No: 56 Exp. Name: Write a C program to Create a Singly Linked List Date: 2022-08-24
Aim:
Page No:
The addNodes() function creates a new list and adds elements to the list until delimiter -1 is occurred.
ID: 21331A0211
Step-1: Allocate memory to the node temp .
Step-2: Store an integer value into data field of node temp .
Step-3: If the first is referenced to NULL then assign temp to first node,
otherwise traverse the list up to the last node (meaning the next field of a
node contains Null )
and then assign the temp node to next field of the lastNode .
Step-4: Finally return the first node.
The traverseList() function traverses and prints all the elements of the list.
Fill in the missing code in the below functions addNodes(NODE first, int x) and
traverseList(NODE first) in the file CreateAndAddNodes.c .
Source Code:
SingleLL1.c
#include<stdio.h>
#include<stdlib.h>
#include "CreateAndAddNodes.c"
2021-2025-EEE-A
void main() {
NODE first = NULL;
int x;
printf("Enter elements up to -1 : ");
scanf("%d", &x);
MVGR College of Engineering (Autonomous)
while (x != -1) {
first = addNodes(first, x);
scanf("%d", &x);
}
if (first == NULL) {
printf("Single Linked List is empty\n");
} else {
printf("The elements in SLL are : ");
traverseList(first);
}
}
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=56&qId=58be7ffd0cf26ce633b573be&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/3
24/08/2022, 21:56 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=56&qId=58be7ffd0cf26ce633b573be&bd=AY3RFZHVEQg%3D%3D&…
CreateAndAddNodes.c
Page No:
struct node {
int data;
struct node *next;
};
ID: 21331A0211
typedef struct node *NODE;
NODE createNode() {
NODE temp;
temp = (NODE) malloc(sizeof(struct node));
temp -> next = NULL;
return temp;
}
NODE addNodes(NODE first, int x) {
NODE temp, lastNode = first;
temp = createNode();
temp -> data = x;
if (first == NULL)
{
first = temp;
}
else
{
while (lastNode -> next != NULL)
{
lastNode = lastNode -> next;
}
lastNode -> next = temp;
}
return first;
}
void traverseList(NODE first) {
NODE temp = first;
while (temp !=NULL)
{
2021-2025-EEE-A
printf("%d --> ",temp -> data);
temp = temp -> next;
}
printf("NULL\n");
}
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter elements up to -1 : 9 18 27 36 45 -1
The elements in SLL are : 9 --> 18 --> 27 --> 36 --> 45 --> NULL
Test Case - 2
User Output
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=56&qId=58be7ffd0cf26ce633b573be&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/3
24/08/2022, 21:57 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=57&qId=5d52b416e3fffe0a91d0127f&bd=AY3RFZHVEQg%3D%3D&l…
Page No:
Aim:
Write a C program to demonstrate the differences between structures and unions .
ID: 21331A0211
The process is
1. Create a structure student-1 with members rollno, m1, m2, m3, total of int type and avg of float type
2. Read rollno, m1, m2 and m3 of student-1
3. Find and display total and average marks of student-1
4. Display the size of struct student-1
5. Create a union student-2 with members rollno, m1, m2, m3, total of int type and avg of float type
6. Read rollno, m1, m2 and m3 of student-2
7. Find and display total and average marks of student-2
8. Display the size of union student-2
Source Code:
StructureAndUnion.c
#include<stdio.h>
struct student1
{
2021-2025-EEE-A
int rollno;
int m1,m2,m3,total;
float avg;
};
union student2
{
MVGR College of Engineering (Autonomous)
int rollno;
int m1,m2,m3,total;
float avg;
};
void main()
{
union student2 u;
struct student1 s;
int sum=0;
printf("Enter rollno and 3 subjects marks of student - 1 : ");
scanf("%d%d%d%d",&s.rollno,&s.m1,&s.m2,&s.m3);
s.total = s.m1 + s.m2 + s.m3;
s.avg = s.total/3.0;
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=57&qId=5d52b416e3fffe0a91d0127f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/3
24/08/2022, 21:57 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=57&qId=5d52b416e3fffe0a91d0127f&bd=AY3RFZHVEQg%3D%3D&l…
printf("Total and average marks of student - 1 : %d %f\n",s.total,s.avg);
printf("Size of struct student - 1 : %zu\n",sizeof(struct student1));
printf("Enter rollno of student - 2 : ");
scanf("%d",&u.rollno);
Page No:
printf("Enter first subject marks of student - 2 : ");
scanf("%d",&u.m1);
sum = u.m1;
printf("Enter second subject marks of student - 2 : ");
ID: 21331A0211
scanf("%d",&u.m2);
sum = sum + u.m2;
printf("Enter third subject marks of student - 2 : ");
scanf("%d",&u.m3);
sum = sum + u.m3;
u.total = sum;
printf("Total marks of student - 2 : %d\n",u.total);
u.avg = u.total/3.0;
printf("Average marks of student - 2 : %f\n",u.avg);
printf("Size of union student - 2 : %zu\n",sizeof(union student2));
}
Test Case - 1
User Output
Enter rollno and 3 subjects marks of student - 1 : 101 76 58 67
Total and average marks of student - 1 : 201 67.000000 102
Size of struct student - 1 : 24 102
Enter rollno of student - 2 : 102
Enter first subject marks of student - 2 : 76
Enter second subject marks of student - 2 : 87
Enter third subject marks of student - 2 : 69
Total marks of student - 2 : 232
Average marks of student - 2 : 77.333336
2021-2025-EEE-A
Size of union student - 2 : 4
Test Case - 2
User Output
MVGR College of Engineering (Autonomous)
Test Case - 3
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=57&qId=5d52b416e3fffe0a91d0127f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/3
24/08/2022, 21:57 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=58&qId=59dccf730cf2e6552052e11a&bd=AY3RFZHVEQg%3D%3D…
Exp. Name: Write a C program to Open a File and to Print its contents on the
S.No: 58 Date: 2022-08-23
screen
Page No:
Aim:
Follow the instructions given below to write a program to open a file and to print its contents on the
screen.
ID: 21331A0211
Open a new file " SampleText1.txt " in write mode
Write the content in the file
Close the file
Open the same file in read mode
Read the content from file and print them on the screen
Close the file
Source Code:
Program1501.c
#include <stdio.h>
void main() {
FILE *fp;
char ch;
fp = fopen("SampleText1.txt", "w"); // Open a new file in write mode
printf("Enter the text with @ at end : ");
while ((ch = getchar()) != '@') { // Repeat loop till read @ at the end
putc(ch, fp); // Put read character onto the file
}
putc(ch, fp); // Put delimiter @ at the end on the file
fclose(fp); // Close the file
fp = fopen("SampleText1.txt", "r"); // Open the existed file in read mode
printf("Given message is : ");
while ((ch = getc(fp)) != '@') { // Repeat loop till get @ at the end of existed fil
e
putchar(ch); // Put the character on the screen
}
printf("\n");
fclose(fp); // Close the file
2021-2025-EEE-A
}
Test Case - 1
User Output
Enter the text with @ at end : CodeTantra is a
Startup Company recognized by Government of Startup Company recognized by
India@ Government of India@
Given message is : CodeTantra is a
Startup Company recognized by Government of India
Test Case - 2
User Output
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=58&qId=59dccf730cf2e6552052e11a&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 21:57 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=59&qId=58b7b90d0cf2e7f49a8c3d3e&bd=AY3RFZHVEQg%3D%3D…
Exp. Name: Write a C program to write and read text into a binary file using fread()
S.No: 59 Date: 2022-08-24
and fwrite()
Page No:
Aim:
Write a C program to write and read text into a binary file using fread() and fwrite().
ID: 21331A0211
The program is to write a structure containing student roll number, name, marks into a file and read them to
print on the standard output device.
Source Code:
FilesStructureDemo1.c
#include<stdio.h>
struct student {
int roll;
char name[25];
float marks;
};
void main() {
FILE *fp;
char ch;
struct student s;
fp = fopen("student-information.txt","w"); // Complete the statement
do {
printf("Enter Roll number : ");
scanf("%d",&s.roll); // Complete the statement
printf("Enter Name : ");
scanf("%s",s.name); // Complete the statement
printf("Enter Marks : ");
scanf("%f",&s.marks); // Complete the statement
fwrite(&s, sizeof(s), 1, fp); // Complete the statement
printf("Do you want to add another data (y/n) : ");
scanf(" %c", &ch);
}while (ch == 'y' || ch == 'y'); // Complete the condition
2021-2025-EEE-A
printf("Data written successfully...\n");
fclose(fp);
fp = fopen("student-information.txt","r"); // Complete the statement
printf("\t Roll \t Name \t Marks\n");
while (fread(&s, sizeof(s), 1, fp) > 0) { // Complete the condition
printf("\t %d \t %s \t %f\n",s.roll,s.name,s.marks); // complete the statement
MVGR College of Engineering (Autonomous)
}
fclose(fp);
}
Test Case - 1
User Output
Enter Roll number : 501
Enter Name : Ganga
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=59&qId=58b7b90d0cf2e7f49a8c3d3e&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
24/08/2022, 21:57 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=59&qId=58b7b90d0cf2e7f49a8c3d3e&bd=AY3RFZHVEQg%3D%3D…
Test Case - 1
Enter Marks : 92
Do you want to add another data (y/n) : y
Page No:
Enter Roll number : 502
Enter Name : Smith
Enter Marks : 62
ID: 21331A0211
Do you want to add another data (y/n) : n
Data written successfully...
Roll Name Marks
501 Ganga 92.000000
502 Smith 62.000000
Test Case - 2
User Output
Enter Roll number : 2001
Enter Name : Federer
Enter Marks : 89
Do you want to add another data (y/n) : y
Enter Roll number : 2001
Enter Name : Kohli
Enter Marks : 95
Do you want to add another data (y/n) : y
Enter Roll number : 2003
Enter Name : Srikant
Enter Marks : 73
Do you want to add another data (y/n) : n
Data written successfully...
Roll Name Marks
2001 Federer 89.000000
2001 Kohli 95.000000
2003 Srikant 73.000000
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=59&qId=58b7b90d0cf2e7f49a8c3d3e&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
24/08/2022, 21:58 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=60&qId=59dcd1570cf2e6552052e22a&bd=AY3RFZHVEQg%3D%3D…
S.No: 60 Exp. Name: Write a C program to Copy contents of one File into another File Date: 2022-08-24
Aim:
Page No:
Write a program to copy contents of one file into another file. Follow the instructions given below to write a
program to copy the contents of one file to another file:
Open a new file " SampleTextFile1.txt " in write mode
ID: 21331A0211
Write the content onto the file
Close the file
Open an existing file " SampleTextFile1.txt " in read mode
Open a new file " SampleTextFile2.txt " in write mode
Copy the content from existing file to new file
Close the files
Open the copied file in read mode
Read the text from file and print on the screen
Close the file
Source Code:
Program1502.c
#include<stdio.h>
void main()
{
FILE *fp, *fp1, *fp2;
char ch;
fp = fopen("SampleTextFile1.txt", "w");
printf("Enter the text with @ at end : ");
while ((ch = getchar()) != '@')
{
putc(ch, fp);
}
putc(ch, fp);
fclose(fp);
fp1 = fopen("SampleTextFile1.txt", "r");
fp2 = fopen("SampleTextFile2.txt", "w");
2021-2025-EEE-A
while ((ch = getc(fp1)) != '@') {
putc(ch, fp2);
}
putc(ch, fp2);
fclose(fp1);
fclose(fp2);
fp2 = fopen("SampleTextFile2.txt", "r");
MVGR College of Engineering (Autonomous)
Test Case - 1
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=60&qId=59dcd1570cf2e6552052e22a&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 1/2
24/08/2022, 21:58 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=60&qId=59dcd1570cf2e6552052e22a&bd=AY3RFZHVEQg%3D%3D…
Test Case - 1
User Output
Page No:
Enter the text with @ at end : CodeTantra started in the year 2014@
Copied text is : CodeTantra started in the year 2014
Test Case - 2
ID: 21331A0211
User Output
Enter the text with @ at end : CodeTantra received
best Startup award from Hysea in 2016@ best Startup award from Hysea in
2016@
Copied text is : CodeTantra received
best Startup award from Hysea in 2016
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=60&qId=59dcd1570cf2e6552052e22a&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb549506… 2/2
24/08/2022, 21:58 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=61&qId=5d52bb1ce3fffe0a91d0164f&bd=AY3RFZHVEQg%3D%3D&l…
Exp. Name: Write a C program to Merge two Files and stores their contents in
S.No: 61 Date: 2022-08-24
another File using Command-Line arguments
Page No:
Aim:
Write a program to merge two files and stores their contents in another file using command-line arguments.
Open a new file specified in argv[1] in write mode
ID: 21331A0211
Write the content onto the file
Close the file
Open another new file specified in argv[2] in write mode
Write the content onto the file
Close the file
Open first existing file specified in argv[1] in read mode
Open a new file specified in argv[3] in write mode
Copy the content from first existing file to new file
Close the first existing file
Open another existing file specified in argv[2] in read mode
Copy its content from existing file to new file
Close that existing file
Close the merged file
Source Code:
MergeFilesArgs.c
#include <stdio.h>
void main(int argc, char* argv[]) {
FILE *fp1, *fp2, *fp3;
char ch;
fp1 = fopen(argv[1], "w"); // Open file in corresponding mode
printf("Enter the text with @ at end for file-1 :\n");
while ((ch = getchar()) != '@') { // Write the condition
putc(ch, fp1);
}
putc(ch, fp1);
fclose(fp1);
fp2 = fopen(argv[2], "w"); // Open file in corresponding mode
2021-2025-EEE-A
printf("Enter the text with @ at end for file-2 :\n");
while ((ch = getchar()) != '@') { // Write the condition
putc(ch, fp2);
}
putc(ch, fp2);
fclose(fp2);
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=61&qId=5d52bb1ce3fffe0a91d0164f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 1/2
24/08/2022, 21:58 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=61&qId=5d52bb1ce3fffe0a91d0164f&bd=AY3RFZHVEQg%3D%3D&l…
fclose(fp2);
fclose(fp3);
fp3 = fopen(argv[3], "r"); // Open the merged file in read mode
printf("Merged text is : ");
Page No:
while ((ch = getc(fp3)) != '@') { // Repeat loop till get @ at the end of merged fil
e
putchar(ch);
}
ID: 21331A0211
printf("\n");
fclose(fp3); // Close the merged file
}
Test Case - 1
User Output
Enter the text with @ at end for file-1 : This is CodeTantra
They implemented automatic robotic tool@ They implemented
automatic robotic
tool@
Enter the text with @ at end for file-2 : Started the company in
2014@ 2014@
Merged text is : This is CodeTantra
They implemented automatic robotic tool
Started the company in
2014
Test Case - 2
User Output
Enter the text with @ at end for file-1 : Best
Fair Fair
2021-2025-EEE-A
Awesome@ Awesome@
Enter the text with @ at end for file-2 : False@
Merged text is : Best
Fair
Awesome
False
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=61&qId=5d52bb1ce3fffe0a91d0164f&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb5495069… 2/2
24/08/2022, 21:59 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=62&qId=5c62354364bac12393881510&bd=AY3RFZHVEQg%3D%3…
Exp. Name: Write a C program to Count number of Characters, Words and Lines of
S.No: 62 Date: 2022-08-24
a given File
Page No:
Aim:
Write a program to count number of characters, words and lines of given text file.
open a new file " DemoTextFile2.txt " in write mode
ID: 21331A0211
write the content onto the file
close the file
open the same file in read mode
read the text from file and find the characters, words and lines count
print the counts of characters, words and lines
close the file
Source Code:
Program1508.c
#include <stdio.h>
void main() {
FILE *fp;
char ch;
int charCount = 0, wordCount = 0, lineCount = 0;
fp = fopen("DemoTextFile2.txt", "w"); // Open a new file in write mode
printf("Enter the text with @ at end : ");
while ((ch = getchar()) != '@') { // Repeat loop till read @ at the end
putc(ch, fp); // Put read character onto the file
}
putc(ch, fp); // Put delimiter @ at the end on the file
fclose(fp); // Close the file
fp = fopen("DemoTextFile2.txt", "r"); // Open the existing file in read mode
do {
if ((ch == ' ')|| (ch == '\n') || (ch == '@'))// Write the condition to count wor
ds
wordCount++;
else
charCount++;
2021-2025-EEE-A
if (ch == '\n' || ch == '@') // Write the condition to count lines
lineCount++;
} while ((ch = getc(fp)) != '@'); // Repeat loop till read @ at the end
fclose(fp);
printf("Total characters : %d\n", charCount);
printf("Total words : %d\n", wordCount);
MVGR College of Engineering (Autonomous)
Test Case - 1
User Output
Enter the text with @ at end : Arise! Awake!
and stop not until and stop not until
the goal is reached@ the goal is reached@
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=62&qId=5c62354364bac12393881510&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950… 1/2
24/08/2022, 21:59 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=62&qId=5c62354364bac12393881510&bd=AY3RFZHVEQg%3D%3…
Test Case - 1
Total characters : 43
Total words : 10
Page No:
Total lines : 3
Test Case - 2
ID: 21331A0211
User Output
Enter the text with @ at end : Believe in your self
and the world will be and the world will be
at your feet@ at your feet@
Total characters : 44
Total words : 12
Total lines : 3
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=62&qId=5c62354364bac12393881510&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950… 2/2
24/08/2022, 21:59 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=63&qId=5c514be164bac114aabade51&bd=AY3RFZHVEQg%3D%3…
Exp. Name: Write a C program to Copy last n characters from one File to another
S.No: 63 Date: 2022-08-23
File
Page No:
Aim:
Write a program to copy last n characters from file-1 to file-2.
open a new file " TestDataFile1.txt " in write mode
ID: 21331A0211
write the content onto the file
close the file
open an existing file " TestDataFile1.txt " in read mode
open a new file " TestDataFile2.txt " in write mode
read the number of characters to copy
set the cursor position by using fseek()
copy the content from existing file to new file
close the files
open the copied file " TestDataFile2.txt " in read mode
read the text from file and print on the screen
close the file
Source Code:
Program1505.c
#include <stdio.h>
void main() {
FILE *fp, *fp1, *fp2;
int num, length;
char ch;
fp = fopen("TestDataFile1.txt", "w"); // Write the mode
printf("Enter the text with @ at end : ");
while ((ch = getchar()) != '@') {
putc(ch, fp);
}
putc(ch, fp);
fclose(fp);
fp1 = fopen("TestDataFile1.txt", "r"); // Write the mode
fp2 = fopen("TestDataFile2.txt", "w"); // Write the mode
2021-2025-EEE-A
printf("Enter number of characters to copy : ");
scanf("%d",&num); // Read number
fseek(fp1, 0L, SEEK_END); // Write the correct operation
length = ftell(fp1);
fseek(fp1, (length - num - 1), SEEK_SET); // Write the correct operation
while ((ch = getc(fp1)) != '@') { // Write the condition
MVGR College of Engineering (Autonomous)
putc(ch, fp2);
}
putc(ch, fp2);
fclose(fp1);
fclose(fp2);
fp2 = fopen("TestDataFile2.txt", "r");// Write the mode
printf("Copied text is : ");
while ((ch = getc(fp2)) != '@') {
putchar(ch); // Write the condition
}
printf("\n");
fclose(fp2);
}
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=63&qId=5c514be164bac114aabade51&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950… 1/2
24/08/2022, 21:59 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=63&qId=5c514be164bac114aabade51&bd=AY3RFZHVEQg%3D%3…
Page No:
Test Case - 1
User Output
ID: 21331A0211
Enter the text with @ at end : We should not give up
and we should not allow the problem to defeat and we should not allow the
us@ problem to defeat us@
Enter number of characters to copy : 15
Copied text is : em to defeat us
Test Case - 2
User Output
Enter the text with @ at end : You have to dream
before before
Your dreams can come true@ Your dreams can come true@
Enter number of characters to copy : 20
Copied text is : dreams can come true
2021-2025-EEE-A
MVGR College of Engineering (Autonomous)
https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=63&qId=5c514be164bac114aabade51&bd=AY3RFZHVEQg%3D%3D&lid=5d4bebb54950… 2/2