0% found this document useful (0 votes)
2 views122 pages

CLab

The document contains a series of C programming exercises aimed at teaching basic programming concepts such as input/output, arithmetic operations, and data type handling. Each exercise includes the program's aim, source code, and execution results demonstrating successful test cases. The exercises cover topics like displaying greetings, scanning variables, performing arithmetic operations, calculating averages, temperature conversions, simple and compound interest, and finding the area of a triangle using Heron's formula.

Uploaded by

22jr1a05j1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views122 pages

CLab

The document contains a series of C programming exercises aimed at teaching basic programming concepts such as input/output, arithmetic operations, and data type handling. Each exercise includes the program's aim, source code, and execution results demonstrating successful test cases. The exercises cover topics like displaying greetings, scanning variables, performing arithmetic operations, calculating averages, temperature conversions, simple and compound interest, and finding the area of a triangle using Heron's formula.

Uploaded by

22jr1a05j1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 122

Exp.

Name: Display hello world and a greeting to


S.No: 1 Date: 2023-10-27
the user.

Aim:

Page No: 1
Write a C program to display hello world and a greeting to the user.
Source Code:

hello.c

ID: 23BQ1A47A1
#include<stdio.h>
int main()
{
char name[20];
printf("Enter your name:");
scanf("%s",name);
printf("Hello World\n");
printf("Hello %s\n",name);
}

2023-2027-CIC&CSO-B
Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Enter your name:
Jack
Hello World

Vasireddy Venkatadri Institute of Technology


Hello Jack

Test Case - 2

User Output
Enter your name:
Jonathan
Hello World
Hello Jonathan
Exp. Name: Scan all data type variables and display
S.No: 2 Date: 2023-10-28
them

Aim:

Page No: 2
Write a C program to scan all data type variables(int, float, char, double) as input and print them as output.

Note: Please add Space before %c which removes any white space (blanks, tabs, or newlines).
Source Code:

ID: 23BQ1A47A1
scan.c
#include<stdio.h>
int main()
{
int i;
float f;
char c;
double d;
printf("integer: ");
scanf("%d", &i);
printf("floating-point number: ");

2023-2027-CIC&CSO-B
scanf("%f", &f);
printf("character: ");
scanf(" %c", &c);
printf("double: ");
scanf("%lf", &d);
printf("You entered:\n");
printf("Integer: %d\n", i);
printf("Float: %f\n", f);
printf("Character: %C\n", c);

Vasireddy Venkatadri Institute of Technology


printf("Double: %lf\n", d);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
integer:
9
floating-point number:
12.0254
character:
C
double:
12.02543124
You entered:
Integer: 9
Float: 12.025400
Character: C
Double: 12.025431
Test Case - 2

User Output
integer:
-10

Page No: 3
floating-point number:
12.2546
character:
T

ID: 23BQ1A47A1
double:
12.6789678
You entered:
Integer: -10
Float: 12.254600
Character: T
Double: 12.678968

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
Exp. Name: Perform arithmetic operations like
S.No: 3 Date: 2023-10-29
+,-,*,/,% on two input variables.

Aim:

Page No: 4
Write a C program to perform arithmetic operations like +,-,*,/,% on two input variables.
Source Code:

arithmeticOperations.c

ID: 23BQ1A47A1
#include<stdio.h>
int main()
{
int a,b;
printf("num1: ");
scanf("%d",&a);
printf("num2: ");
scanf("%d",&b);
int Sum=a+b;
int Difference=a-b;
int product=a*b;
int Division=a/b;

2023-2027-CIC&CSO-B
int Modulus=a%b;
printf("Sum: %d\n",a+b);
printf("Difference: %d\n",a-b);
printf("Product: %d\n",a*b);
b>0?printf("Division: %d\n",a/b):printf("Infinity\n");
b>0?printf("Modulus: %d\n",a%b):printf("Modulo by zero is not allowed\n");
}

Vasireddy Venkatadri Institute of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
num1:
9
num2:
8
Sum: 17
Difference: 1
Product: 72
Division: 1
Modulus: 1

Test Case - 2

User Output
num1:
25
num2:
0
Sum: 25

Infinity
Product: 0
Difference: 25

Modulo by zero is not allowed

Vasireddy Venkatadri Institute of Technology 2023-2027-CIC&CSO-B ID: 23BQ1A47A1 Page No: 5


Exp. Name: Write a C program to find Sum and
S.No: 4 Date: 2023-10-30
Average of three numbers

Aim:

Page No: 6
Write a program to find the sum and average of the three given integers.

Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:

ID: 23BQ1A47A1
Program314.c
#include<stdio.h>
int main()
{
int a,b,c;
int Sum;
float avg;
printf("Enter three integers : ");
scanf("%d%d%d", &a, &b, &c);
Sum = a + b + c;
avg = (a+b+c)/ 3.0;

2023-2027-CIC&CSO-B
printf("Sum of %d, %d and %d : %d\n", a, b, c, Sum);
printf("Average of %d, %d and %d : %.6f\n", a, b, c, avg);
}

Execution Results - All test cases have succeeded!


Test Case - 1

Vasireddy Venkatadri Institute of Technology


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 :
583
Sum of 5, 8 and 3 : 16
Average of 5, 8 and 3 : 5.333333

Test Case - 3

User Output
Enter three integers :
-1 5 -6
Sum of -1, 5 and -6 : -2
Average of -1, 5 and -6 : -0.666667

Vasireddy Venkatadri Institute of Technology 2023-2027-CIC&CSO-B ID: 23BQ1A47A1 Page No: 7


Exp. Name: Temperature conversions from
S.No: 5 Date: 2023-10-30
Centigrade to Fahrenheit and vice versa.

Aim:

Page No: 8
Write a C program to perform temperature conversions from Centigrade to Fahrenheit and vice versa.
Source Code:

temperature.c

ID: 23BQ1A47A1
#include<stdio.h>
int main()
{
float fh,cl;
int ch;
printf("Temperature Conversion:\n");
printf("1.Celsius to Fahrenheit\n");
printf("2.Fahrenheit to Celsius\n");
printf("choice: ");
scanf(" %d",&ch);
if(ch==1)
{

2023-2027-CIC&CSO-B
printf("Enter Temperature in Celsius: ");
scanf("%f",&cl);
fh=(cl*1.8)+32;
printf("Fahrenheit Temperature: %.2f\n",fh);
}
else if(ch==2)
{
printf("Enter Temperature in Fahrenheit: ");
scanf("%f",&fh);

Vasireddy Venkatadri Institute of Technology


cl=(fh-32)/1.8;
printf("Celsius Temperature: %.2f\n",cl);
}
else
{
printf("Invalid choice\n");
}
return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Temperature Conversion:
1.Celsius to Fahrenheit
2.Fahrenheit to Celsius
choice:
1
Enter Temperature in Celsius:
35.78
Fahrenheit Temperature: 96.40

Test Case - 2

User Output

Page No: 9
Temperature Conversion:
1.Celsius to Fahrenheit
2.Fahrenheit to Celsius
choice:

ID: 23BQ1A47A1
2
Enter Temperature in Fahrenheit:
96.40
Celsius Temperature: 35.78

Test Case - 3

User Output
Temperature Conversion:
1.Celsius to Fahrenheit

2023-2027-CIC&CSO-B
2.Fahrenheit to Celsius
choice:
3
Invalid choice

Vasireddy Venkatadri Institute of Technology


S.No: 6 Exp. Name: Problem Solving Date: 2023-10-30

Aim:
Write a program to calculate the simple interest by reading principle amount, rate of interest and time.

Page No: 10
At the time of execution, the program should print the message on the console as:

Enter principle amount, rate of interest, time of loan :

ID: 23BQ1A47A1
For example, if the user gives the input as:

Enter principle amount, rate of interest, time of loan : 23456.78 3.5 2.5

then the program should print the result as:

Simple Interest = 2052.468018

Note: Do use the printf() function and ensure that there is a '\n' at the end after print the result.
Source Code:

Program3.c

2023-2027-CIC&CSO-B
#include<stdio.h>
int main()
{
float principle, rate, time, SI;

printf("Enter principle amount, rate of interest, time of loan : ");


scanf("%f", &principle);
scanf("%f", &rate);

Vasireddy Venkatadri Institute of Technology


scanf("%f", &time);
SI = principle * rate * time / 100;
printf("Simple Interest = %f\n", SI);
return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter principle amount, rate of interest, time of loan :
2500 5 2
Simple Interest = 250.000000
S.No: 7 Exp. Name: Calculate the square root of an integer Date: 2023-10-31

Aim:
Write a program that prompts the user to enter an integer and calculates its square root.

Page No: 11
Note:Print the result up to 3 decimal places.

Input format:
The program takes an integer as input.

ID: 23BQ1A47A1
Output format:
The output is the floating point value that represents the square root value of the user-given integer.

Hint: You can use math library to perform mathematical operations.

Instruction: During writing your code, please follow the input and output layout as mentioned in the
sample test case.
Source Code:

squareRoot.c

2023-2027-CIC&CSO-B
#include<stdio.h>
#include<math.h>
int main()
{
int a;
float b;
printf("Enter an integer: ");
scanf("%d", &a);
b = sqrt(a);

Vasireddy Venkatadri Institute of Technology


printf("Square root: %.3f\n",a,b);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter an integer:
2
Square root: 1.414

Test Case - 2

User Output
Enter an integer:
4
Square root: 2.000
Exp. Name: Write a program to calculate Simple
S.No: 8 Date: 2023-10-31
interest and Compound interest

Aim:

Page No: 12
Write a program to calculate the simple interest and compound interest by reading principal amount, rate
of interest and time.

Note: Use the printf() function and ensure that the character '\n' is printed at the end of the result.

ID: 23BQ1A47A1
The formula to find simple interest is simpleInterest = (principal * rate * time) / 100 .

The formula to find compound interest is


compoundInterest = principal * pow(1 + (rate / 100), time) - principal .

Note: Use float data type for all the involved variables.
Source Code:

Program315.c

#include<stdio.h>

2023-2027-CIC&CSO-B
#include<math.h>
void main()
{
float p,r,t,ci;
printf("Enter P,R,T: ");
scanf("%f%f%f",&p,&r,&t);
printf("SI= %f\n",(p*r*t)/100.0);
ci=p*pow(1+(r/100),t)-p;
printf("CI= %f\n",ci);

Vasireddy Venkatadri Institute of Technology


}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter P,R,T:
5000 7 5
SI= 1750.000000
CI= 2012.760376

Test Case - 2

User Output
Enter P,R,T:
1000 6 4
SI= 240.000000
CI= 262.476685
Exp. Name: Write a C program to find Area of a
S.No: 9 Date: 2023-10-31
Triangle using Heron's formula

Aim:

Page No: 13
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:

sides:

ID: 23BQ1A47A1
For example, if the user gives the following as input (input is positive floating decimal point numbers):

sides: 2.3 2.4 2.5

Then the program should print the result round off upto 2 decimal places as:

area: 2.49

Instruction: Your input and output layout must match with the sample test cases (values as well as text strings).

The area of a triangle is given by Area = √ p(p − a)(p − b)(p − c) , where p is half of the perimeter, or

2023-2027-CIC&CSO-B
(a + b + c) / 2 . Let a,b,c be the lengths of the sides of the given triangle.

Hint: Use sqrt function defined in math.h header file


Source Code:

Program313.c

#include<stdio.h>
#include<math.h>

Vasireddy Venkatadri Institute of Technology


int main()
{
float a,b,c,p,q;
printf("sides: ");
scanf("%f%f%f",&a,&b,&c);
p=(a+b+c)/2;
q=sqrt(p*(p-a)*(p-b)*(p-c));
printf("area: %.2f\n",q);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
sides:
2.3 2.4 2.5
area: 2.49

Test Case - 2
sides:
2.6 2.7 2.8
area: 3.15
User Output

Vasireddy Venkatadri Institute of Technology 2023-2027-CIC&CSO-B ID: 23BQ1A47A1 Page No: 14


Exp. Name: Write a C program to find the Distance
S.No: 10 Date: 2023-10-31
Travelled by an Object

Aim:

Page No: 15
Write a program to find the distance travelled by an object.

Sample Input and Output:

Enter the acceleration value : 2.5

ID: 23BQ1A47A1
Enter the initial velocity : 5.7
Enter the time taken : 20
Distance travelled : 614.000000

Note - 1: Use the formula to find distance, distance = ut + (1/2) at2 .

Note: Use the printf() function with a newline character ( \n ) at the end.

Source Code:

DistanceTravelled.c

2023-2027-CIC&CSO-B
#include<stdio.h>
#include<math.h>
int main()
{
float a,u,t,d;
printf("Enter the acceleration value : ");
scanf("%f",&a);
printf("Enter the initial velocity : ");
scanf("%f",&u);

Vasireddy Venkatadri Institute of Technology


printf("Enter the time taken : ");
scanf("%f",&t);
d=u*t+(0.5)*a*t*t;
printf("Distance travelled : %lf\n",d);
return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter the acceleration value :
4
Enter the initial velocity :
5
Enter the time taken :
6
Distance travelled : 102.000000
Test Case - 2

User Output
Enter the acceleration value :
5

Page No: 16
Enter the initial velocity :
0
Enter the time taken :
10

ID: 23BQ1A47A1
Distance travelled : 250.000000

Test Case - 3

User Output
Enter the acceleration value :
2.5
Enter the initial velocity :
5.7
Enter the time taken :

2023-2027-CIC&CSO-B
20
Distance travelled : 614.000000

Test Case - 4

User Output
Enter the acceleration value :

Vasireddy Venkatadri Institute of Technology


50
Enter the initial velocity :
34.67
Enter the time taken :
6
Distance travelled : 1108.020020

Test Case - 5

User Output
Enter the acceleration value :
125.6
Enter the initial velocity :
45.8
Enter the time taken :
4
Distance travelled : 1188.000000
S.No: 11 Exp. Name: Evaluate the expressions Date: 2023-11-01

Aim:
Write a C program to evaluate the following expressions.

Page No: 17
a. A+B*C+(D*E) + F*G
b. A/B*C-B+A*D/3
c. A+++B---A
d. J= (i++) + (++i)

ID: 23BQ1A47A1
Note: consider expression as A++ + ++B - --A
Source Code:

evaluate.c
#include<stdio.h>
void main()
{
int a,b,c,d,e,f,g,i;
printf("Enter values for A, B, C, D, E, F, G, i: ");
scanf("%d%d%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f,&g,&i);
printf("a.A+B*C+(D*E) + F*G = %d\n",a+b*c+(d*e)+f*g);

2023-2027-CIC&CSO-B
printf("b.A/B*C-B+A*D/3 = %d\n",a/b*c-b+a*d/3);
printf("c.A+++B---A = %d\n",(a++)+(++b)-(--a));
printf("d.J = (i++) + (++i) = %d\n",(i++)+(++i));
}

Execution Results - All test cases have succeeded!

Vasireddy Venkatadri Institute of Technology


Test Case - 1

User Output
Enter values for A, B, C, D, E, F, G, i:
12345678
a.A+B*C+(D*E) + F*G = 69
b.A/B*C-B+A*D/3 = -1
c.A+++B---A = 3
d.J = (i++) + (++i) = 18

Test Case - 2

User Output
Enter values for A, B, C, D, E, F, G, i:
10 20 60 30 40 4 6 1
a.A+B*C+(D*E) + F*G = 2434
b.A/B*C-B+A*D/3 = 80
c.A+++B---A = 21
d.J = (i++) + (++i) = 4
Exp. Name: Greatest of three numbers using a
S.No: 12 Date: 2023-11-01
conditional operator

Aim:

Page No: 18
Take three numbers from the user. Write a C program to display the greatest of three numbers using a
conditional operator.
Source Code:

greatest.c

ID: 23BQ1A47A1
#include<stdio.h>
int main()
{
int a,b,c,max;
printf("num1: ");
scanf("%d",&a);
printf("num2: ");
scanf("%d",&b);
printf("num3: ");
scanf("%d",&c);
max=(a>b)?((a>c)?a:c):((b>c)?b:c);

2023-2027-CIC&CSO-B
printf("Greatest number: %d\n",max);
}

Execution Results - All test cases have succeeded!


Test Case - 1

Vasireddy Venkatadri Institute of Technology


User Output
num1:
8
num2:
9
num3:
90
Greatest number: 90

Test Case - 2

User Output
num1:
5
num2:
45
num3:
6
Greatest number: 45
Exp. Name: Write a C program to Total and Average
S.No: 13 Date: 2023-11-01
of 5 subjects marks

Aim:

Page No: 19
Write a program to take marks of 5 subjects in integers, and find the total , average in float.

Sample Input and Output:

Enter 5 subjects marks : 55 56 57 54 55

ID: 23BQ1A47A1
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>
int main()
{

2023-2027-CIC&CSO-B
int a,b,c,d,e;
float tl,av;
printf("Enter 5 subjects marks : ");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
tl=a+b+c+d+e;
printf("Total marks : %f\n",tl);
av=tl/5;
printf("Average marks : %f\n",av);
}

Vasireddy Venkatadri Institute of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Enter 5 subjects marks :
45 67 89 57 49
Total marks : 307.000000
Average marks : 61.400002

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

Page No: 20
Total marks : 465.000000
Average marks : 93.000000

Test Case - 4

ID: 23BQ1A47A1
User Output
Enter 5 subjects marks :
20 30 66 77 44
Total marks : 237.000000
Average marks : 47.400002

Test Case - 5

User Output

2023-2027-CIC&CSO-B
Enter 5 subjects marks :
56 78 88 79 64
Total marks : 365.000000
Average marks : 73.000000

Test Case - 6

Vasireddy Venkatadri Institute of Technology


User Output
Enter 5 subjects marks :
44 35 67 49 51
Total marks : 246.000000
Average marks : 49.200001
Exp. Name: Write a Program to find the Max and
S.No: 14 Date: 2023-11-07
Min of Four numbers

Aim:

Page No: 21
Write a program to find the max and min of four numbers.

Sample Input and Output :

Enter 4 numbers : 9 8 5 2

ID: 23BQ1A47A1
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()
{

2023-2027-CIC&CSO-B
int a,b,c,d;
printf("Enter 4 numbers : ");
scanf("%d%d%d%d",&a,&b,&c,&d);
int max=a,min=a;
if(b>max) max=b;
if(c>max) max=c;
if(d>max) max=d;
if(b<min) min=b;
if(c<min) min=c;

Vasireddy Venkatadri Institute of Technology


if(d<min) min=d;
printf("Max value : %d\n",max);
printf("Min value : %d\n",min);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter 4 numbers :
9852
Max value : 9
Min value : 2

Test Case - 2

User Output
Enter 4 numbers :
112 245 167 321
Min value : 112

Test Case - 3

User Output

Page No: 22
Enter 4 numbers :
110 103 113 109
Max value : 113
Min value : 103

ID: 23BQ1A47A1
Test Case - 4

User Output
Enter 4 numbers :
-34 -35 -24 -67
Max value : -24
Min value : -67

2023-2027-CIC&CSO-B
Test Case - 5

User Output
Enter 4 numbers :
24 28 34 16
Max value : 34
Min value : 16

Vasireddy Venkatadri Institute of Technology


Test Case - 6

User Output
Enter 4 numbers :
564 547 574 563
Max value : 574
Min value : 547
S.No: 15 Exp. Name: Find out the electricity bill charges Date: 2023-11-07

Aim:
An electricity board charges the following rates for the use of electricity:

Page No: 23
• If units are less than or equal to 200, then the charge is calculated as 80 paise per unit.
• If units are less than or equal to 300, then the charge is calculated as 90 paise per unit.
• If units are beyond 300, then the charge is calculated as 1 Rupee per unit.

ID: 23BQ1A47A1
All users are charged a minimum of Rs. 100 as a meter charge even though the amount calculated is less than Rs.
100.

If the total amount charged is greater than Rs. 400, then an additional surcharge of 15% of the total amount is
charged.

Write a C program to read the name of the user, and the number of units consumed and print out the charges.

Note: Print the amount charged up to 2 decimal places (actual amount, surcharges, amount to be paid).
Source Code:

electricityBillCharges.c

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
#include<stdio.h>
int main()
{
int uc;
float ac,su,ap;

Page No: 24
char b[15];
printf("Enter customer name: ");
scanf("%s",&b);
printf("Units consumed: ");
scanf("%d",&uc);

ID: 23BQ1A47A1
if(uc<=200)
{
ac=uc*0.80;
}
if(uc<=300&&uc>200)
{
ac=uc*0.90;
}
if(uc>300)
{
ac=uc*1.00;
}

2023-2027-CIC&CSO-B
if(ac>400)
{
su=ac*0.15;
}
ap=ac+su;
if(ap<100)
{
ap=100;
}

Vasireddy Venkatadri Institute of Technology


printf("Customer name: %s\n",b);
printf("Units consumed: %d\n",uc);
printf("Amount charged: %.2f\n",ac,uc);
printf("Surcharges: %.2f\n",su);
printf("Amount to be paid: %.2f\n",ap);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter customer name:
John
Units consumed:
78
Customer name: John
Units consumed: 78
Amount charged: 62.40
Surcharges: 0.00
Amount to be paid: 100.00
Test Case - 2

User Output
Enter customer name:
Rosy

Page No: 25
Units consumed:
325
Customer name: Rosy
Units consumed: 325

ID: 23BQ1A47A1
Amount charged: 325.00
Surcharges: 0.00
Amount to be paid: 325.00

Test Case - 3

User Output
Enter customer name:
Amar
Units consumed:

2023-2027-CIC&CSO-B
801
Customer name: Amar
Units consumed: 801
Amount charged: 801.00
Surcharges: 120.15
Amount to be paid: 921.15

Test Case - 4

Vasireddy Venkatadri Institute of Technology


User Output
Enter customer name:
Raman
Units consumed:
300
Customer name: Raman
Units consumed: 300
Amount charged: 270.00
Surcharges: 0.00
Amount to be paid: 270.00
Exp. Name: C program to find roots and nature of
S.No: 16 Date: 2023-11-07
quadratic equation.

Aim:

Page No: 26
Write a C program to find the roots of a quadratic equation, given its coefficients.
Source Code:

quad.c

ID: 23BQ1A47A1
#include<stdio.h>
#include<math.h>
int main()
{
double a,b,c,discriminant,root1,root2,realpart,imagpart;
printf("Enter coefficients a, b and c: ");
scanf("%lf%lf%lf",&a,&b,&c);
discriminant=b*b-4*a*c;
if(discriminant>0)
{
root1=(-b+sqrt(discriminant))/(2*a);
root2=(-b-sqrt(discriminant))/(2*a);

2023-2027-CIC&CSO-B
printf("root1 = %.2lf and root2 = %.2lf\n",root1,root2);
}
else if(discriminant==0)
{
root1=root2=-b/(2*a);
printf("root1 = root2 = %.2lf",root1);
}
else
{

Vasireddy Venkatadri Institute of Technology


realpart=-b/(2*a);
imagpart=sqrt(-discriminant)/(2*a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2lf-
%.2lfi",realpart,imagpart,realpart,imagpart);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter coefficients a, b and c:
379
root1 = -1.17+1.28i and root2 = -1.17-1.28i

Test Case - 2

User Output
Enter coefficients a, b and c:
886
root1 = -0.50+0.71i and root2 = -0.50-0.71i

Vasireddy Venkatadri Institute of Technology 2023-2027-CIC&CSO-B ID: 23BQ1A47A1 Page No: 27


Exp. Name: Write program to simulate a basic
S.No: 17 Date: 2023-11-24
calculator

Aim:

Page No: 28
Write a program to perform basic calculator operations [+, -, *, /] of two integers a and b using switch
statement.

Constraints:
• 10-4 <= a,b = 104

ID: 23BQ1A47A1
• operations allowed are +, -, *, /
• "/" divisibility will perform integer division operation.

Input Format: The first line of the input consists of an integer which corresponds to a, character which
corresponds to the operator and an integer which corresponds to b.

Output format: Output consists of result after performing mentioned operation (a operation b).

Instruction: To run your custom test cases strictly map your input and output layout with the visible test cases.
Source Code:

calculator.c

2023-2027-CIC&CSO-B
#include<stdio.h>
int main()
{
int a,b,ans;
char c;
scanf("%d%c%d",&a,&c,&b);
if(c=='/')
{

Vasireddy Venkatadri Institute of Technology


printf("%d",a/b);
}
else if(c=='-')
{
printf("%d",a-b);
}
else if(c=='*')
{
printf("%d",a*b);
}
else
{
printf("%d",a+b);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
36-31
5
1
1
89/45

User Output
User Output

10000/10000
Test Case - 3
Test Case - 2

Vasireddy Venkatadri Institute of Technology 2023-2027-CIC&CSO-B ID: 23BQ1A47A1 Page No: 29


Exp. Name: Write a program to find leap year or
S.No: 18 Date: 2023-11-07
not

Aim:

Page No: 30
Lucy is celebrating her 15th birthday. Her father promised her that he will buy her a new computer on her
birthday if she solves the question asked by him.

He asks Lucy to find whether the year on which she had born is leap year or not.

ID: 23BQ1A47A1
Help her to solve this puzzle so that she celebrates her birthday happily. If her birth year is 2016 and it is a leap
year display 2016 is a leap year.? Else display 2016 is not a leap year and check with other leap year conditions.
Source Code:

leapYear.c
#include<stdio.h>
int main()
{
int a;
printf("");
scanf("%d",&a);

2023-2027-CIC&CSO-B
if(a%4==0)
{
if(a%100==0)
{
if(a%400==0)
printf("%d is a leap year",a);
else
printf("%d is not a leap year",a);
}

Vasireddy Venkatadri Institute of Technology


else
printf("%d is a leap year",a);
}
else
printf("%d is not a leap year",a);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
1900
1900 is not a leap year

Test Case - 2

User Output
2004
2004 is a leap year
1995
User Output

1995 is not a leap year


Test Case - 3

Vasireddy Venkatadri Institute of Technology 2023-2027-CIC&CSO-B ID: 23BQ1A47A1 Page No: 31


S.No: 19 Exp. Name: Factorial of a given number Date: 2023-11-24

Aim:
Write a C program to find the factorial of a given number

Page No: 32
Source Code:

factorialOfInt.c

#include<stdio.h>

ID: 23BQ1A47A1
int main()
{
int i,n,f=1;
printf("Integer: ");
scanf("%d",&n);
for(i=1;i<=n;++i)
{
f*=i;
}
printf("Factorial: %d\n",f);
}

2023-2027-CIC&CSO-B
Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Integer:
5

Vasireddy Venkatadri Institute of Technology


Factorial: 120

Test Case - 2

User Output
Integer:
4
Factorial: 24
Exp. Name: C program to to determine whether a
S.No: 20 Date: 2023-11-24
given number is prime or not.

Aim:

Page No: 33
Write the C program to to determine whether a given number is prime or not.
Source Code:

Prime.c

ID: 23BQ1A47A1
#include<stdio.h>
int main()
{
int i,n,count,temp;
printf("Enter a number: ");
scanf("%d",&n);
temp=2;
for(i=1;i<=n;i++)
{
if(n%i==0)
++count;
}

2023-2027-CIC&CSO-B
if(count==2)
{
printf("%d is a prime number\n",n);
}
else
{
printf("%d is not a prime number\n",n);
}
}

Vasireddy Venkatadri Institute of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Enter a number:
9
9 is not a prime number

Test Case - 2

User Output
Enter a number:
11
11 is a prime number
Exp. Name: compute sine and cos series using taylor
S.No: 21 Date: 2023-12-24
series

Aim:

Page No: 34
Write a C program to compute the sine and cosine series using the Taylor series.

Taylor series:

sin x = x - (x3/3!) + (x5/5!) - (x7/7!) + .....

ID: 23BQ1A47A1
cos x = 1 - (x2/2!) + (x4/4!) - (x6/6!) + ....

Note: Print the result up to 4 decimal places.


Source Code:

taylor.c

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
#include<stdio.h>
#include<math.h>
int fact(int n)
{
if(n==1)

Page No: 35
return 1;
else
return n*fact(n-1);
}
void main()

ID: 23BQ1A47A1
{
int n,p=3,i;
float x,sinx,cosx;
printf("angle in radians: ");
scanf("%f",&x);
printf("number of terms in the series: ");
scanf("%d",&n);
sinx=x;
cosx=1;
for(i=2;i<=n;i++)
{
if(i%2==0)

2023-2027-CIC&CSO-B
sinx-=pow(x,p)/fact(p);
else
sinx+=pow(x,p)/fact(p);
p+=2;
}
p=2;
for(i=2;i<=n;i++)
{
if(i%2==0)

Vasireddy Venkatadri Institute of Technology


cosx-=pow(x,p)/fact(p);
else
cosx+=pow(x,p)/fact(p);
p+=2;
}
printf("Sine = %.4f\n",sinx);
printf("Cosine = %.4f\n",cosx);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
angle in radians:
0.5
number of terms in the series:
3
Sine = 0.4794
Cosine = 0.8776

Test Case - 2

Page No: 36
User Output
angle in radians:
0.6
number of terms in the series:

ID: 23BQ1A47A1
5
Sine = 0.5646
Cosine = 0.8253

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
Exp. Name: C program to check given number is
S.No: 22 Date: 2023-12-04
palindrome or not

Aim:

Page No: 37
Write an C program to check given number is palindrome or not
Source Code:

palindrome.c

ID: 23BQ1A47A1
#include<stdio.h>
int main()
{
int r,n,c,s=0;
scanf("%d",&n);
printf("%d",n);
c=n;
while(n>0)
{
r=n%10;
s=(s*10)+r;
n=n/10;

2023-2027-CIC&CSO-B
}
if(s==c)
{
printf(" is a palindrome.\n");
}
else
{
printf(" is not a palindrome.\n");
}

Vasireddy Venkatadri Institute of Technology


}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
121
121 is a palindrome.

Test Case - 2

User Output
143
143 is not a palindrome.
Exp. Name: Write a C program to print the Pyramid
S.No: 23 Date: 2023-12-04
with numbers

Aim:

Page No: 38
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:

Enter number of rows :

ID: 23BQ1A47A1
For example, if the user gives the input as :

Enter number of rows : 3

then the program should print the result as:

1
1 2
1 2 3

Source Code:

2023-2027-CIC&CSO-B
PyramidDemo15.c
#include <stdio.h>
void main() {
int n, i, j, s;
printf("Enter number of rows : ");
scanf("%d", &n);
for(i=1;i<=n;i++)

Vasireddy Venkatadri Institute of Technology


{
for(s=n-1;s>=i;s--)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter number of rows :
3
1
1 2
1 2 3

Test Case - 2

Page No: 39
User Output
Enter number of rows :
6
1

ID: 23BQ1A47A1
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 :

2023-2027-CIC&CSO-B
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

Vasireddy Venkatadri Institute of Technology


1 2 3 4 5 6 7 8
Exp. Name: Write a C program to find the minimum
S.No: 24 Date: 2023-12-24
and maximum in an array of integers.

Aim:

Page No: 40
Write a C program to find the minimum and maximum in an array of integers.
Source Code:

ArrayElements.c

ID: 23BQ1A47A1
#include <stdio.h>
void main() {
int arr[20], number, min = 0, max = 0;
scanf("%d", &number);
printf("Elements: ");
for (int i = 0; i < number; i++) {
scanf("%d", &arr[i]);
}
/* Write your logic here to find the maximum and minimum in the given integer
array*/for( int i=0;i<number;i++){
if(arr[i]>max)
max=arr[i];

2023-2027-CIC&CSO-B
}
min=arr[0];
for(int i=1;i<number;i++){
if(arr[i]<min)
min=arr[i];
}
printf("Min an Max: %d and %d",min,max);
}

Vasireddy Venkatadri Institute of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
5
Elements:
49682
Min an Max: 2 and 9

Test Case - 2

User Output
1
Elements:
216
Min an Max: 216 and 216
Exp. Name: Search for an element using Linear
S.No: 25 Date: 2023-12-24
search

Aim:

Page No: 41
Write a C program to check whether the given element is present or not in the array of elements using linear
search.
Source Code:

SearchEle.c

ID: 23BQ1A47A1
#include<stdio.h>
void main ()
{
int A[100],n,i,ele;
printf("Enter size: ");
scanf("%d",&n);
printf("Enter %d element: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}

2023-2027-CIC&CSO-B
printf("Enter search element: ");
scanf("%d",&ele);
for(i=0;i<n;i++)
{
if (A[i]==ele)
break;
}
if(i==n)
{

Vasireddy Venkatadri Institute of Technology


printf("%d is not found\n",ele);
}
else{
printf("Found at position %d\n",i);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter size:
6
Enter 6 element:
248135
Enter search element:
6
6 is not found
2
6

248135
Enter size:
User Output

Enter 6 element:

Found at position 0
Enter search element:

Vasireddy Venkatadri Institute of Technology 2023-2027-CIC&CSO-B ID: 23BQ1A47A1 Page No: 42


S.No: 26 Exp. Name: C program to reverse an array. Date: 2023-12-24

Aim:
Write a C program to reverse the elements an array of integers.

Page No: 43
Source Code:

reverseArray.c

#include<stdio.h>

ID: 23BQ1A47A1
int main()
{
int a[10],i,n;
printf("Enter no of elements: ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("The reversed array: ");
for(i=n-1;i>=0;i--)

2023-2027-CIC&CSO-B
printf("%d ",a[i]);
}

Execution Results - All test cases have succeeded!


Test Case - 1

Vasireddy Venkatadri Institute of Technology


User Output
Enter no of elements:
5
Enter elements:
34124
The reversed array: 4 2 1 4 3

Test Case - 2

User Output
Enter no of elements:
8
Enter elements:
2 5 1 77 33 88 2 9
The reversed array: 9 2 88 33 77 1 5 2
Exp. Name: 2’s complement of a given Binary
S.No: 27 Date: 2023-12-24
number

Aim:

Page No: 44
Write a C program to find 2’s complement of a given binary number.

Note: The binary input should be separated by a space.


Source Code:

ID: 23BQ1A47A1
twosComplement.c

#include <stdio.h>
void main()
{
int a[100],b[100], n,i,carry=1;
printf("Enter size: ");
scanf("%d", &n);
printf("Enter %d bit binary number: ", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n; i++)

2023-2027-CIC&CSO-B
{
if(a[i]==0)
a[i]=1;
else
a[i]=0;
}
for( i=n-1;i>=0;i--)
{
if( a[i]==1 && carry==1)

Vasireddy Venkatadri Institute of Technology


{
b[i]=0;
}
else if( a[i]==0 && carry==1)
{
b[i]=1;
carry=0;
}
else
b[i]=a[i];
}
printf("2\'s complement: ");
for( i=0;i<n;i++)
printf("%d ",b[i]);
printf("\n");
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter size:
5
Enter 5 bit binary number:
10010
2's complement: 0 1 1 1 0

Page No: 45
Test Case - 2

User Output
Enter size:

ID: 23BQ1A47A1
6
Enter 6 bit binary number:
100011
2's complement: 0 1 1 1 0 1

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
Exp. Name: Eliminate Duplicate elements in an
S.No: 28 Date: 2023-12-24
array

Aim:

Page No: 46
Write a C program to eliminate duplicate elements of an array.
Source Code:

eliminateDuplicates.c

ID: 23BQ1A47A1
#include<stdio.h>
void main()
{
int a[100],b[100],n1,n2=0,i,j,flag;
printf("Enter size: ");
scanf("%d",&n1);
printf("Enter %d elements: ", n1);
for(i=0; i<n1; i++)
scanf("%d",&a[i]);
for(i=0;i<n1;i++)
{
flag=0;

2023-2027-CIC&CSO-B
for(j=0;j<n2;j++)
{
if(a[i]==b[j])
flag=1;
}
if(flag==0)
{
b[n2]=a[i];
n2++;

Vasireddy Venkatadri Institute of Technology


}
}
printf("After eliminating duplicates: ");
for(i=0;i<n2;i++)
printf("%d ",b[i]);
printf("\n");
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter size:
5
Enter 5 elements:
12123
After eliminating duplicates: 1 2 3

Test Case - 2
User Output
Enter size:
5
Enter 5 elements:
11 13 11 12 13

Page No: 47
After eliminating duplicates: 11 13 12

ID: 23BQ1A47A1
2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
S.No: 29 Exp. Name: Addition of two matrices Date: 2023-12-24

Aim:
Write a C program to perform the addition of two matrices.

Page No: 48
Source Code:

addTwoMatrices.c

#include<stdio.h>

ID: 23BQ1A47A1
void main()
{
int a[10][10],b[10][10],c[10][10],m,n,i,j;
printf("Enter no of rows, columns: ");
scanf("%d%d",&m,&n);
printf("Elements of matrix 1: ");
for( i=0;i<m;i++ )
for( j=0;j<n;j++ )
scanf("%d",&a[i][j]);
printf("Elements of matrix 2: ");
for( i=0;i<m;i++ )
for( j=0;j<n;j++ )

2023-2027-CIC&CSO-B
scanf("%d",&b[i][j]);
for( i=0;i<m;i++ )
for( j=0;j<n;j++ )
c[i][j]= a[i][j] + b[i][j];
printf("Addition of matrices:\n");
for( i=0;i<m;i++ )
{
for( j=0;j<n;j++ )
printf("%d ",c[i][j]);

Vasireddy Venkatadri Institute of Technology


printf("\n");
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter no of rows, columns:
12
Elements of matrix 1:
12
Elements of matrix 2:
98
Addition of matrices:
10 10

Test Case - 2

User Output
Enter no of rows, columns:
23
Elements of matrix 1:
123456
Elements of matrix 2:

Page No: 49
987654
Addition of matrices:
10 10 10
10 10 10

ID: 23BQ1A47A1
2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
S.No: 30 Exp. Name: Multiplication of two matrices Date: 2023-12-24

Aim:
Write a C program to find the multiplication of two matrices

Page No: 50
Source Code:

matrixMul.c

ID: 23BQ1A47A1
2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],m,n,p,q,i,j,k;
printf("no of rows, columns of matrix1: ");

Page No: 51
scanf("%d%d",&m,&n);
printf("matrix1 elements:");
for( i=0;i<m;i++ )
for( j=0;j<n;j++ )
scanf("%d",&a[i][j]);

ID: 23BQ1A47A1
printf("no of rows, columns of matrix2: ");
scanf("%d%d",&p,&q);
printf("matrix2 elements:");
for( i=0;i<p;i++ )
for( j=0;j<q;j++ )
scanf("%d",&b[i][j]);
printf("Given matrix1:\n");
for( i=0;i<m;i++ )
{
for( j=0;j<n;j++ )
printf("%d ",a[i][j]);
printf("\n");

2023-2027-CIC&CSO-B
}
printf("Given matrix2:\n");
for( i=0;i<p;i++)
{
for( j=0;j<q;j++ )
printf("%d ",b[i][j]);
printf("\n");
}
if( n==p){

Vasireddy Venkatadri Institute of Technology


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];
}
printf("Multiplication of two matrices:\n");
for( i=0;i<m;i++ )
{
for( j=0;j<q;j++ )
printf("%d ",c[i][j]);
printf("\n");
}
}
else
printf("Multiplication not possible\n");
}

Execution Results - All test cases have succeeded!


Test Case - 1
User Output
no of rows, columns of matrix1:
22
matrix1 elements:
11 22

Page No: 52
33 44
no of rows, columns of matrix2:
22
matrix2 elements:

ID: 23BQ1A47A1
11 22
33 44
Given matrix1:
11 22
33 44
Given matrix2:
11 22
33 44
Multiplication of two matrices:
847 1210

2023-2027-CIC&CSO-B
1815 2662

Test Case - 2

User Output
no of rows, columns of matrix1:
33
matrix1 elements:

Vasireddy Venkatadri Institute of Technology


123
456
789
no of rows, columns of matrix2:
23
matrix2 elements:
123
456
Given matrix1:
1 2 3
4 5 6
7 8 9
Given matrix2:
1 2 3
4 5 6
Multiplication not possible
Exp. Name: Write a C program to Sort given
S.No: 31 Date: 2023-12-24
elements using Bubble sort

Aim:

Page No: 53
Develop an algorithm, implement and execute a C program that reads n integer numbers and arrange them in
ascending order using Bubble Sort.
Source Code:

Lab7.c

ID: 23BQ1A47A1
#include<stdio.h>
void main()
{
int A[100],n,i,j,l;
scanf("%d",&n);
printf("Elements: ");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
printf("Before sorting: ");
for(i=0;i<n;i++)
printf("%d ",A[i]);

2023-2027-CIC&CSO-B
printf("\n");
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(A[j]>A[j+1])
{
l=A[j];
A[j]=A[j+1];

Vasireddy Venkatadri Institute of Technology


A[j+1]=l;
}
}
}
printf("After sorting: ");
for(i=0;i<n;i++)
printf("%d ",A[i]);
printf("\n");
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
4
Elements:
44 22 66 11
Before sorting: 44 22 66 11
After sorting: 11 22 44 66
Test Case - 2

User Output
5
Elements:

Page No: 54
92716
Before sorting: 9 2 7 1 6
After sorting: 1 2 6 7 9

ID: 23BQ1A47A1
2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
Exp. Name: Write a C program to Concatenate two
S.No: 32 Date: 2023-12-24
given strings without using string library functions

Aim:

Page No: 55
Write a program to concatenate two given strings without using string library functions.

At the time of execution, the program should print the message on the console as:

string1 :

ID: 23BQ1A47A1
For example, if the user gives the input as:

string1 : ILove

Next, the program should print the message on the console as:

string2 :

For example, if the user gives the input as:

string2 : Coding

2023-2027-CIC&CSO-B
then the program should print the result as:

concatenated string = ILoveCoding

Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:

Program605.c

Vasireddy Venkatadri Institute of Technology


#include<stdio.h>
void main()
{
char s1[100],s2[100];
int i,j,len;
printf("string1 : ");
scanf("%s",s1);
printf("string2 : ");
scanf("%s",s2);
for( len=0;s1[len]!='\0';len++);
for( i=len, j=0;s2[j]!='\0';j++,i++)
s1[i]=s2[j];
s1[i]='\0';
printf("concatenated string = %s\n",s1);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
string1 :
ILove
string2 :
Coding
concatenated string = ILoveCoding

Page No: 56
Test Case - 2

User Output
string1 :

ID: 23BQ1A47A1
1234
string2 :
567
concatenated string = 1234567

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
Exp. Name: Write a C program to Reverse the given
S.No: 33 Date: 2023-12-24
string without using the Library Functions

Aim:

Page No: 57
Write a program to reverse the given string without using the library functions.

At the time of execution, the program should print the message on the console as:

Enter a string :

ID: 23BQ1A47A1
For example, if the user gives the input as:

Enter a string : Dallas

then the program should print the result as:

Reverse string : sallaD

Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:

2023-2027-CIC&CSO-B
Program609.c

#include<stdio.h>
void main()
{
char s1[100],rev[100];
int i,j,len;
printf("Enter a string : ");
scanf("%s",s1);

Vasireddy Venkatadri Institute of Technology


for( len=0;s1[len]!='\0';len++);
for( i=0, j=len-1;s1[i]!='\0';i++,j--)
rev[j]=s1[i];
rev[len]='\0';
printf("Reverse string : %s\n",rev);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter a string :
Dallas
Reverse string : sallaD
Exp. Name: Write a C program to find Sum of array
S.No: 34 elements by allocating memory using malloc() Date: 2024-01-04
function

Page No: 58
Aim:
Write a program to find the sum of n elements by allocating memory by using malloc() function.

Note: Write the functions allocateMemory(), read1() and sum() in UsingMalloc.c .

ID: 23BQ1A47A1
Source Code:

SumOfArray1.c
#include <stdio.h>
#include <stdlib.h>
#include "UsingMalloc.c"
void main() {
int *p, n, i;
printf("Enter n value : ");
scanf("%d", &n);
p = allocateMemory(n);
printf("Enter %d values : ", n);

2023-2027-CIC&CSO-B
read1(p, n);
printf("The sum of given array elements : %d\n", sum(p, n));
}

UsingMalloc.c

#include<stdio.h>
#include<stdlib.h>

Vasireddy Venkatadri Institute of Technology


#include<string.h>
int*allocateMemory(int n)
{
return(int*)malloc(n*4);
}
void read1(int*p,int n)
{
int i;
for(i=0;i<n;i++)
scanf("%d",(p+i));
}
int sum(int*p,int n)
{
int i,r=0;
for(i=0;i<n;i++)
r=r+*(p+i);
return r;
}
Execution Results - All test cases have succeeded!
Test Case - 1

User Output

Page No: 59
Enter n value :
3
Enter 3 values :
10 20 30

ID: 23BQ1A47A1
The sum of given array elements : 60

Test Case - 2

User Output
Enter n value :
4
Enter 4 values :
-5 -6 -4 -2
The sum of given array elements : -17

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
Exp. Name: Write a program to find Total and
S.No: 35 Average gained by Students in a Section using Date: 2024-01-04
Array of Structures

Page No: 60
Aim:
Write a C program to find out the total and average marks gained by the students in a section using array of
structures.

ID: 23BQ1A47A1
Note: Consider that regdno, marks of 3 subjects, total and average are the members of a structure and make sure
to provide the int value for number of students which are lessthan 60

Sample Input and Output:

Enter number of students : 3


Enter regdno, three subjects marks of student-0: 101 56 78 76
Enter regdno, three subjects marks of student-1: 201 76 89 91
Enter regdno, three subjects marks of student-2: 301 46 57 61
Student-0 Regdno = 101 Total marks = 210 Average marks = 70.000000
Student-1 Regdno = 201 Total marks = 256 Average marks = 85.333336
Student-2 Regdno = 301 Total marks = 164 Average marks = 54.666668

2023-2027-CIC&CSO-B
Source Code:

ArrayOfStructures2.c

#include<stdio.h>
struct student{
int rno,m1,m2,m3,tot;
float avg;

Vasireddy Venkatadri Institute of Technology


};
void main(){
struct student s[60];
int i, n;
printf("Enter number of students : ");
scanf("%d", &n);
for ( i=0;i<n;i++) {
printf("Enter regdno, three subjects marks of student-%d: ", i);
// Read regdno and 3 subjects marks
scanf("%d%d%d%d",&s[i].rno,&s[i].m1,&s[i].m2,&s[i].m3);
}
for (i=0;i<n;i++ ) {
s[i].tot=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=(float)s[i].tot/3;
printf("Student-%d Regdno = %d\tTotal marks = %d\tAverage marks =
%f\n",i,s[i].rno,s[i].tot,s[i].avg);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1
User Output
Enter number of students :
3
Enter regdno, three subjects marks of student-0:
101 56 78 76

Page No: 61
Enter regdno, three subjects marks of student-1:
201 76 89 91
Enter regdno, three subjects marks of student-2:
301 46 57 61

ID: 23BQ1A47A1
Student-0 Regdno = 101 Total marks = 210 Average marks = 70.000000
Student-1 Regdno = 201 Total marks = 256 Average marks = 85.333336
Student-2 Regdno = 301 Total marks = 164 Average marks = 54.666668

Test Case - 2

User Output
Enter number of students :
10
Enter regdno, three subjects marks of student-0:

2023-2027-CIC&CSO-B
501 23 45 67
Enter regdno, three subjects marks of student-1:
502 78 65 76
Enter regdno, three subjects marks of student-2:
503 99 87 67
Enter regdno, three subjects marks of student-3:
504 89 78 82

Vasireddy Venkatadri Institute of Technology


Enter regdno, three subjects marks of student-4:
505 37 59 76
Enter regdno, three subjects marks of student-5:
506 78 59 67
Enter regdno, three subjects marks of student-6:
507 92 72 82
Enter regdno, three subjects marks of student-7:
508 45 47 48
Enter regdno, three subjects marks of student-8:
509 55 52 59
Enter regdno, three subjects marks of student-9:
510 62 61 66
Student-0 Regdno = 501 Total marks = 135 Average marks = 45.000000
Student-1 Regdno = 502 Total marks = 219 Average marks = 73.000000
Student-2 Regdno = 503 Total marks = 253 Average marks = 84.333336
Student-3 Regdno = 504 Total marks = 249 Average marks = 83.000000
Student-4 Regdno = 505 Total marks = 172 Average marks = 57.333332
Student-5 Regdno = 506 Total marks = 204 Average marks = 68.000000
Student-6 Regdno = 507 Total marks = 246 Average marks = 82.000000
Student-7 Regdno = 508 Total marks = 140 Average marks = 46.666668
Student-8 Regdno = 509 Total marks = 166 Average marks = 55.333332
Student-9 Regdno = 510 Total marks = 189 Average marks = 63.000000
Test Case - 3

User Output
Enter number of students :
5

Page No: 62
Enter regdno, three subjects marks of student-0:
101 76 78 73
Enter regdno, three subjects marks of student-1:
102 89 57 68

ID: 23BQ1A47A1
Enter regdno, three subjects marks of student-2:
103 77 67 59
Enter regdno, three subjects marks of student-3:
104 37 47 52
Enter regdno, three subjects marks of student-4:
105 88 47 69
Student-0 Regdno = 101 Total marks = 227 Average marks = 75.666664
Student-1 Regdno = 102 Total marks = 214 Average marks = 71.333336
Student-2 Regdno = 103 Total marks = 203 Average marks = 67.666664
Student-3 Regdno = 104 Total marks = 136 Average marks = 45.333332

2023-2027-CIC&CSO-B
Student-4 Regdno = 105 Total marks = 204 Average marks = 68.000000

Vasireddy Venkatadri Institute of Technology


Exp. Name: Write a Program to enter n students
S.No: 36 Date: 2024-01-06
data using calloc() and display Failed Students List

Aim:

Page No: 63
Write a C program to enter n students data using calloc() and display the students list.
Source Code:

FailedList.c

ID: 23BQ1A47A1
#include <stdio.h>
#include <stdlib.h>
struct student {
int roll;
int marks[6], sum;
float avg;
};
#include "FailedList1.c"
void main() {
struct student *s;
int i, n;
printf("Enter the number of students : ");

2023-2027-CIC&CSO-B
scanf("%d", &n);
s = allocateMemory(s, n);
read1(s, n);
calculateMarks(s, n);
displayFailedList(s, n);
}

FailedList1.c

Vasireddy Venkatadri Institute of Technology


struct student* allocateMemory(struct student *s, int n) {

// Write the code

s=(struct student*)calloc(n,sizeof(struct student));

Page No: 64
return s;

ID: 23BQ1A47A1
void read1(struct student *s, int n) {

// write the code.

int i;

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

printf("Enter the details of student - %d\n",i+1);

2023-2027-CIC&CSO-B
printf("Enter the roll number : ");

scanf("%d",&s[i].roll);

printf("Enter 6 subjects marks : ");

scanf("%d%d%d%d%d%d",&s[i].marks[0],&s[i].marks[1],&s[i].marks[2],&s[i].marks[3],&s[i].marks[4],
&s[i].marks[5]);

Vasireddy Venkatadri Institute of Technology


}

void calculateMarks(struct student *s, int n) {

// write the code

int i,j;

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

for(j=0;j<6;j++)

s[i].sum=s[i].sum+s[i].marks[j];

for(i=0;i<n;i++){

s[i].avg=(float)s[i].sum/6;
}

void displayFailedList(struct student *s, int n) {

int i;

Page No: 65
printf("RollNo\tTotalMarks\tAverageMarks\tStatus\n");

for (i=0;i<n;i++) {

ID: 23BQ1A47A1
printf("%d\t",s[i].roll ); // Fill the missing code

printf("%d\t",s[i].sum ); // Fill the missing code

printf("%f\t",s[i].avg ); // Fill the missing code

if (s[i].marks[0]

<35||s[i].marks[1]

<35||s[i].marks[2]

2023-2027-CIC&CSO-B
<35||s[i].marks[3]

<35||s[i].marks[4]

<35||s[i].marks[5]<35) // Fill the missing code

printf("Fail");

else

Vasireddy Venkatadri Institute of Technology


printf("Pass");

printf("\n");

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter the number of students :
3
Enter the details of student - 1
Enter the roll number :
101
Enter 6 subjects marks :
45 67 58 36 59 63
Enter the details of student - 2
Enter the roll number :
102
Enter 6 subjects marks :
34 56 98 39 78 89
Enter the details of student - 3

Page No: 66
Enter the roll number :
103
Enter 6 subjects marks :
35 67 89 98 76 56

ID: 23BQ1A47A1
RollNo TotalMarks AverageMarks Status
101 328 54.666668 Pass
102 394 65.666664 Fail
103 421 70.166664 Pass

Test Case - 2

User Output
Enter the number of students :
2

2023-2027-CIC&CSO-B
Enter the details of student - 1
Enter the roll number :
1001
Enter 6 subjects marks :
26 57 68 67 67 65
Enter the details of student - 2
Enter the roll number :
1002

Vasireddy Venkatadri Institute of Technology


Enter 6 subjects marks :
58 67 58 89 87 76
RollNo TotalMarks AverageMarks Status
1001 350 58.333332 Fail
1002 435 72.500000 Pass
Exp. Name: Write a C program to find Total Marks
S.No: 37 Date: 2024-01-04
of a Student using Command-line arguments

Aim:

Page No: 67
Write a C program to read student name and 3 subjects marks from the command line and display the student
details along with total.

Sample Input and Output - 1:

ID: 23BQ1A47A1
If the arguments passed as $./TotalMarksArgs.c Sachin 67 89 58 , then the program should
print the output as:
Cmd Args : Sachin 67 89 58
Student name : Sachin
Subject-1 marks : 67
Subject-1 marks : 89
Subject-1 marks : 58
Total marks : 214

Sample Input and Output - 2:

If the arguments passed as $./TotalMarksArgs.c Johny 45 86 57 48 , then the program should

2023-2027-CIC&CSO-B
print the output as:
Cmd Args : Johny 45 86 57 48
Arguments passed through command line are not equal to 4

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:

Vasireddy Venkatadri Institute of Technology


TotalMarksArgs.c
#include<stdio.h>
void main(int argc,char*argv[])
{
int i;
//printf("CmdArgs:");
//for(i=1;i<argc;i++)
//printf("%s",argv[i]);
if(argc!=5)
printf("Arguments passed through command line are not equal to 4\n");
else{
printf("Student name : %s\n",argv[1]);
printf("Subject-1 marks : %s\n",argv[2]);
printf("Subject-2 marks : %s\n",argv[3]);
printf("Subject-3 marks : %s\n",argv[4]);
printf("Total marks : %d\n",atoi(argv[2])+atoi(argv[3])+atoi(argv[4]));
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Student name : Sachin
Subject-1 marks : 67

Page No: 68
Subject-2 marks : 89
Subject-3 marks : 58
Total marks : 214

ID: 23BQ1A47A1
Test Case - 2

User Output
Arguments passed through command line are not equal to 4

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
Exp. Name: Write a C program to implement
S.No: 38 Date: 2024-01-04
realloc()

Aim:

Page No: 69
Write a C program to implement realloc() .

The process is
1. Allocate memory of an array with size 2 by using malloc()
2. Assign the values 10 and 20 to the array

ID: 23BQ1A47A1
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;

2023-2027-CIC&CSO-B
int *ptr_new;
*ptr = 10;
*(ptr + 1) = 20;
ptr=realloc(ptr,sizeof(int)*3);
*(ptr+2)=30;
ptr_new=ptr;
for (i = 0; i < 3; i++)
printf("%d ", *(ptr_new + i));
}

Vasireddy Venkatadri Institute of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
10 20 30
Exp. Name: Write a C Program to store information
S.No: 39 Date: 2024-01-06
using Structures with DMA

Aim:

Page No: 70
Write a program to create a list of nodes using self-referential structure and print that data.

At the time of execution, the program should print the message on the console as:

Enter an integer value :

ID: 23BQ1A47A1
For example, if the user gives the input as:

Enter an integer value : 10

Next, the program should print the message on the console as:

Do u want another list (y|n) :

if the user gives the input as:

Do u want another list (y|n) : y

2023-2027-CIC&CSO-B
The input to the list is continued up to the user says n (No)

For example, if the user gives the input as:

Enter an integer value : 20


Do u want another list (y|n) : y
Enter an integer value : 30
Do u want another list (y|n) : n

Vasireddy Venkatadri Institute of Technology


Finally, the program should print the result on the console as:

The elements in the single linked lists are : 10-->20-->30-->NULL

Note: Write the functions create() and display() in CreateNodes.c .


Source Code:

StructuresWithDma.c

#include <stdio.h>
#include <stdlib.h>
struct list {
int data;
struct list *next;
};
#include "CreateNodes.c"
void main() {
struct list *first = NULL;
first = create(first);
printf("The elements in the single linked lists are : ");
display(first);
}
CreateNodes.c

struct list* create(struct list *first) {

char op;

Page No: 71
struct list *q, *temp;

do {

temp = (struct list*)malloc(sizeof(struct

ID: 23BQ1A47A1
list)); // Allocate memory

printf("Enter an integer value : ");

scanf("%d",&temp->data); // Read data

temp -> next = NULL; // Place NULL

if (first == NULL) {

first = temp; // Assign temp to the first node

2023-2027-CIC&CSO-B
} else {

q->next = temp; // Create a link from the last node to new node temp

q = temp;

Vasireddy Venkatadri Institute of Technology


printf("Do u want another list (y|n) : ");

scanf(" %c", &op);

} while(op == 'y' || op == 'Y');

return first;

void display(struct list *first) {

struct list *temp = first;

while (temp!=NULL ) { // Stop the loop where temp is NULL

printf("%d-->", temp->data);

temp = temp->next; // Assign next of temp to temp

printf("NULL\n");

}
Execution Results - All test cases have succeeded!
Test Case - 1

User Output

Page No: 72
Enter an integer value :
10
Do u want another list (y|n) :
y

ID: 23BQ1A47A1
Enter an integer value :
20
Do u want another list (y|n) :
y
Enter an integer value :
30
Do u want another list (y|n) :
n
The elements in the single linked lists are : 10-->20-->30-->NULL

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
Exp. Name: Write a C program to demonstrate the
S.No: 40 Date: 2024-01-05
differences between Structures and Unions

Aim:

Page No: 73
Write a C program to demonstrate the differences between structures and unions .

The process is
6. Create a structure student-1 with members rollno, m1, m2, m3, total of int type and avg of float type
7. Read rollno, m1, m2 and m3 of student-1

ID: 23BQ1A47A1
8. Find and display total and average marks of student-1
9. Display the size of struct student-1
10. Create a union student-2 with members rollno, m1, m2, m3, total of int type and avg of float type
11. Read rollno, m1, m2 and m3 of student-2
12. Find and display total and average marks of student-2
13. Display the size of union student-2
Sample Input and Output:

Enter rollno and 3 subjects marks of student - 1 : 101 76 58 67


Total and average marks of student - 1 : 201 67.000000
Size of struct student - 1 : 24
Enter rollno of student - 2 : 102

2023-2027-CIC&CSO-B
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
Size of union student - 2 : 4

Source Code:

Vasireddy Venkatadri Institute of Technology


StructureAndUnion.c
#include<stdio.h>
struct StudentS
{
int rno,m1,m2,m3,tot;
float avg;

Page No: 74
};
union StudentU
{
int rno,m1,m2,m3,tot;
float avg;

ID: 23BQ1A47A1
};
void main()
{
struct StudentS s1;
union StudentU s2;
int t;
printf("Enter rollno and 3 subjects marks of student - 1 : ");
scanf("%d%d%d%d",&s1.rno,&s1.m1,&s1.m2,&s1.m3);
s1.tot=s1.m1+s1.m2+s1.m3;
s1.avg=(float)s1.tot/3;
printf("Total and average marks of student - 1 : ");
printf("%d %f\n",s1.tot,s1.avg);

2023-2027-CIC&CSO-B
printf("Size of struct student - 1 : %d\n",sizeof(struct StudentS));
printf("Enter rollno of student - 2 : ");
scanf("%d",&s2.rno);
printf("Enter first subject marks of student - 2 : ");
scanf("%d",&s2.m1);
t=s2.m1;
printf("Enter second subject marks of student - 2 : ");
scanf("%d",&s2.m2);
t+=s2.m2;

Vasireddy Venkatadri Institute of Technology


printf("Enter third subject marks of student - 2 : ");
scanf("%d",&s2.m3);
t+=s2.m3;
printf("Total marks of student - 2 : %d\n",t);
s2.avg=(float)t/3;
printf("Average marks of student - 2 : %f\n",s2.avg);
printf("Size of union student - 2 : %d\n",sizeof(union StudentU));
}

Execution Results - All test cases have succeeded!


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
Size of struct student - 1 : 24
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

Page No: 75
Average marks of student - 2 : 77.333336
Size of union student - 2 : 4

Test Case - 2

ID: 23BQ1A47A1
User Output
Enter rollno and 3 subjects marks of student - 1 :
105 66 65 68
Total and average marks of student - 1 : 199 66.333336
Size of struct student - 1 : 24
Enter rollno of student - 2 :
106
Enter first subject marks of student - 2 :
88

2023-2027-CIC&CSO-B
Enter second subject marks of student - 2 :
89
Enter third subject marks of student - 2 :
79
Total marks of student - 2 : 256
Average marks of student - 2 : 85.333336
Size of union student - 2 : 4

Vasireddy Venkatadri Institute of Technology


Test Case - 3

User Output
Enter rollno and 3 subjects marks of student - 1 :
501 76 85 84
Total and average marks of student - 1 : 245 81.666664
Size of struct student - 1 : 24
Enter rollno of student - 2 :
502
Enter first subject marks of student - 2 :
99
Enter second subject marks of student - 2 :
57
Enter third subject marks of student - 2 :
69
Total marks of student - 2 : 225
Average marks of student - 2 : 75.000000
Size of union student - 2 : 4

Test Case - 4
Enter rollno and 3 subjects marks of student - 1 :
201 75 46 59
Total and average marks of student - 1 : 180 60.000000
Size of struct student - 1 : 24
Enter rollno of student - 2 :

Page No: 76
201
Enter first subject marks of student - 2 :
66
Enter second subject marks of student - 2 :

ID: 23BQ1A47A1
57
Enter third subject marks of student - 2 :
61
Total marks of student - 2 : 184
Average marks of student - 2 : 61.333332
Size of union student - 2 : 4

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
S.No: 41 Exp. Name: Demonstrate left shift operation Date: 2024-01-05

Aim:
Write a C program to demonstrate left shift operation

Page No: 77
Source Code:

shift.c
#include<stdio.h>

ID: 23BQ1A47A1
#include<math.h>
int main()
{
int n,rem,i=0,s,t;
long int b=0;
printf("Enter an integer: ");
scanf("%d",&n);
t=n;
while(n>0)
{
rem = n%2;
b = b+rem*pow(10,i++);

2023-2027-CIC&CSO-B
n = n/2;
}
printf("Original value: %d\n",b);
printf("number of bits to left shift: ");
scanf("%d",&s);
t= t<<s;
n=t;
i=0;
b=0;

Vasireddy Venkatadri Institute of Technology


while(n>0)
{
rem = n%2;
b = b+rem*pow(10,i++);
n = n/2;
}
printf("After left shift: %d\n",t);
printf("Binary representation:%d\n",b);
return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter an integer:
12
Original value: 1100
number of bits to left shift:
2
After left shift: 48
Binary representation:110000

Test Case - 2

Page No: 78
User Output
Enter an integer:
5

ID: 23BQ1A47A1
Original value: 101
number of bits to left shift:
3
After left shift: 40
Binary representation:101000

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
Exp. Name: Copy the contents of one structure
S.No: 42 Date: 2024-01-05
variable to another structure variable

Aim:

Page No: 79
Write a C program to Copy the contents of one structure variable to another structure variable.

Let us consider a structure student, containing name, age and height fields.

Declare two structure variables to the structure student, read the contents of one structure variable and copy the

ID: 23BQ1A47A1
same to another structure variable, finally display the copied data.

Source Code:

CopyStructureMain.c
#include <stdio.h>
#include "CopyStructureFunctions.c"

void main() {
struct student s1, s2;
read(&s1);

2023-2027-CIC&CSO-B
s2 = copyStructureVariable(s1, s2);
display(s2);
}

CopyStructureFunctions.c
struct student {
//write the code

Vasireddy Venkatadri Institute of Technology


char sname[100];
int age;
float height;
};
void read(struct student *p) {
printf("Enter student name, age and height: ");
// Write the code to take inputs to structure
scanf("%s%d%f",&(*p).sname,&p->age,&p->height);
}
struct student copyStructureVariable(struct student s1, struct student s2) {
//write your code here to copy the structure
s2=s1;
return s2;
}
void display(struct student s) {
//write your code here to display the structure data
printf("Student name: %s\n",s.sname);
printf("Age: %d\n",s.age);
printf("Height: %f\n",s.height);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter student name, age and height:
Yamuna 19 5.2

Page No: 80
Student name: Yamuna
Age: 19
Height: 5.200000

ID: 23BQ1A47A1
Test Case - 2

User Output
Enter student name, age and height:
Kohli 21 5.11
Student name: Kohli
Age: 21
Height: 5.110000

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
Exp. Name: Write a C Program to find ncr using
S.No: 43 Date: 2024-01-05
Factorial recursive function

Aim:

Page No: 81
Draw the flowchart and write a recursive C function to find the factorial of a number, n! , defined by fact(n) = 1,
if n = 0. Otherwise fact(n) = n * fact(n-1).

Using this function, write a C program to compute the binomial coefficient ncr . Tabulate the results for different

ID: 23BQ1A47A1
values of n and r with suitable messages.

At the time of execution, the program should print the message on the console as:

Enter the values of n and r :

For example, if the user gives the input as:

Enter the values of n and r : 4 2

then the program should print the result as:

The value of 4c2 = 6

2023-2027-CIC&CSO-B
If the input is given as 2 and 5 then the program should print the result as:

Enter valid input data

Note: Write the recursive function factorial() in Lab14a.c .


Source Code:

Vasireddy Venkatadri Institute of Technology


Lab14a.c
int factorial(int n)
{
if(n==1 || n==0)
return 1;
else
return n*factorial(n-1);
}

Lab14.c
#include <stdio.h>
#include "Lab14a.c"
void main() {
int n, r;
printf("Enter the values of n and r : ");
scanf("%d %d", &n, &r);
if (n >= r)
printf("The value of %dc%d = %d\n", n, r, factorial(n) / (factorial(r) *
factorial(n - r)));
else
printf("Enter valid input data\n");
}
Execution Results - All test cases have succeeded!
Test Case - 1

User Output

Page No: 82
Enter the values of n and r :
10 4
The value of 10c4 = 210

ID: 23BQ1A47A1
Test Case - 2

User Output
Enter the values of n and r :
79
Enter valid input data

Test Case - 3

User Output

2023-2027-CIC&CSO-B
Enter the values of n and r :
52
The value of 5c2 = 10

Vasireddy Venkatadri Institute of Technology


Exp. Name: Write a Program to find the Length of a
S.No: 44 Date: 2024-01-05
String

Aim:

Page No: 83
Write a C program to find the length of a given string.

Sample Input and Output - 1:

Enter the string : CodeTantra

ID: 23BQ1A47A1
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);

2023-2027-CIC&CSO-B
printf("Length of %s : %d\n", str, myStrLen(str));
}

StrLength1.c

int myStrLen(char str[30])


{
int len;

Vasireddy Venkatadri Institute of Technology


for( len=0;
str[len]!='\0';len++);
return len;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter the string :
CodeTantra
Length of CodeTantra : 10

Test Case - 2

User Output
Enter the string :
IndoUsUk
Length of IndoUsUk : 8
Test Case - 3

User Output
Enter the string :
MalayalaM

Page No: 84
Length of MalayalaM : 9

Test Case - 4

ID: 23BQ1A47A1
User Output
Enter the string :
Oh!MyGod
Length of Oh!MyGod : 8

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
S.No: 45 Exp. Name: Transpose using functions. Date: 2024-01-06

Aim:
Write a C program to print the transpose of a matrix using functions.

Page No: 85
Source Code:

transpose.c

ID: 23BQ1A47A1
2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
#include <stdio.h>
int rows=5, cols=5;

void readMatrix(int x[rows] [cols])

Page No: 86
{

int i,j;

printf("Elements:\n");

ID: 23BQ1A47A1
for(i=0;i<rows;i++)

for(j=0;j<cols;j++)

scanf("%d",&x[i][j]);

2023-2027-CIC&CSO-B
}

void printMatrix(int y[rows][cols])

Vasireddy Venkatadri Institute of Technology


int i,j;

printf("Matrix:\n");

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

for(j=0;j<cols;j++)

printf("%d ",y[i][j]);

printf("\n");

void transposeMatrix(int a[rows][cols])


int i,j;

printf("Transpose:\n");

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

Page No: 87
{

for(j=0;j<rows;j++)

ID: 23BQ1A47A1
{

printf("%d ",a[j][i]);

printf("\n");

2023-2027-CIC&CSO-B
int main() {
printf("rows: ");
scanf("%d", &rows);
printf("columns: ");
scanf("%d", &cols);
int matrix[rows][cols];

Vasireddy Venkatadri Institute of Technology


// Input: Read the matrix elements
readMatrix(matrix);

// Print the original matrix


printMatrix(matrix);

// Print the transpose of the matrix


transposeMatrix(matrix);

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
rows:
2
columns:
2
Elements:
89
65
Matrix:
8 9

Page No: 88
6 5
Transpose:
8 6
9 5

ID: 23BQ1A47A1
Test Case - 2

User Output
rows:
1
columns:
2
Elements:
69

2023-2027-CIC&CSO-B
Matrix:
6 9
Transpose:
6
9

Vasireddy Venkatadri Institute of Technology


Exp. Name: Demonstrate numerical integration of
S.No: 46 Date: 2024-01-06
differential equations using Euler’s method

Aim:

Page No: 89
Write a C function to demonstrate the numerical integration of differential equations using Euler’s method.

Your program should prompt the user to input the initial value of y (yo)the initial value of t (to) the step size
(h).and the end value fort. Implement the Euler's method in a function, and print the values oft andy at each step.

ID: 23BQ1A47A1
The formula for Euler's Method:

ynext= y + h * f(y ,t)


wheref(y, t) is the derivative function representing dy/dtin the given Ordinary differential equation.

Note: print the values of t and y up to 2 decimal places.

Source Code:

euler.c

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
#include <stdio.h>

//write your code...

// Define the differential equation function f(y, t)

Page No: 90
double f(double y, double t) {
double s;
s=y*t;
return s;
}

ID: 23BQ1A47A1
// Example: dy/dt = t*y

// Euler's method for numerical integration


void eulerIntegration(float y, float t, float tn, float h) {

float y0;
while(t<tn)
{
{
y0=y+h*f(y,t);
printf("t = %.2f y = %.2f\n",t,y);

2023-2027-CIC&CSO-B
}
y=y0;
t=t+h;
}
}
int main() {
float y,t,y0,tn,h;
int i;
printf("initial value of y (y0): ");

Vasireddy Venkatadri Institute of Technology


scanf("%f", &y);
printf("initial value of t (t0): ");
scanf("%f", &t);
printf("step size (h): ");
scanf("%f", &h);
printf("end value for t: ");
scanf("%f", &tn);
eulerIntegration(y,t,tn,h);
return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
initial value of y (y0):
1
initial value of t (t0):
1
step size (h):
3
end value for t:
10
t = 1.00 y = 1.00
t = 4.00 y = 4.00
t = 7.00 y = 52.00

Page No: 91
Test Case - 2

User Output

ID: 23BQ1A47A1
initial value of y (y0):
1
initial value of t (t0):
1
step size (h):
3
end value for t:
3
t = 1.00 y = 1.00

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
Exp. Name: Fibonacci series up to the given number
S.No: 47 Date: 2024-01-05
of terms using Recursion

Aim:

Page No: 92
Write a program to display the fibonacci series up to the given number of terms using recursion process.
Source Code:

fibonacciSeries.c

ID: 23BQ1A47A1
#include <stdio.h>
#include "fibonacciSeriesa.c"
void main() {
int n, i;
printf("n: ");
scanf("%d", &n);
printf("%d terms: ", n);
for (i = 0; i < n; i++) {
printf("%d ", fib(i));
}
}

2023-2027-CIC&CSO-B
fibonacciSeriesa.c
int fib(int i){
if( i==0 )
return 0;
else if(i==1)
return 1;
else

Vasireddy Venkatadri Institute of Technology


return fib(i-1)+fib(i-2);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
n:
4
4 terms: 0 1 1 2

Test Case - 2

User Output
n:
10
10 terms: 0 1 1 2 3 5 8 13 21 34
Exp. Name: Write a C program to find the LCM of
S.No: 48 Date: 2024-01-05
two numbers using Recursion

Aim:

Page No: 93
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 both
a and b.

ID: 23BQ1A47A1
At the time of execution, the program should print the message on the console as:

Enter two integer values :

For example, if the user gives the input as:

Enter two integer values : 25 15

then the program should print the result as:

The lcm of two numbers 25 and 15 = 75

2023-2027-CIC&CSO-B
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;

Vasireddy Venkatadri Institute of Technology


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
int gcd(int a,int b)
{
int r=a%b;
if( r==0 )
return b;
else
return gcd(b,r);
}
int lcm(int a, int b)
{
return (a*b)/gcd(a,b);

}
Execution Results - All test cases have succeeded!
Test Case - 1

User Output

Page No: 94
Enter two integer values :
34 24
The lcm of two numbers 34 and 24 = 408

ID: 23BQ1A47A1
Test Case - 2

User Output
Enter two integer values :
69
The lcm of two numbers 6 and 9 = 18

Test Case - 3

User Output

2023-2027-CIC&CSO-B
Enter two integer values :
345 467
The lcm of two numbers 345 and 467 = 161115

Test Case - 4

User Output

Vasireddy Venkatadri Institute of Technology


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
Exp. Name: Write a C program to find the Factorial
S.No: 49 Date: 2024-01-05
of a given number using Recursion

Aim:

Page No: 95
Write a program to find the factorial of a given number using recursion process.

Note: Write the recursive function factorial() in Program901a.c .


Source Code:

ID: 23BQ1A47A1
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));
}

2023-2027-CIC&CSO-B
Program901a.c
long int factorial(int n)
{
if( n==1 || n==0)
return 1;
else
return n*factorial(n-1);
}

Vasireddy Venkatadri Institute of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Enter an integer :
5
Factorial of 5 is : 120

Test Case - 2

User Output
Enter an integer :
4
Factorial of 4 is : 24

Test Case - 3
Enter an integer :
8
Factorial of 8 is : 40320

Test Case - 4

Page No: 96
User Output
Enter an integer :
0

ID: 23BQ1A47A1
Factorial of 0 is : 1

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
Exp. Name: Write a program to implement
S.No: 50 Date: 2024-01-05
Ackermann function using Recursion

Aim:

Page No: 97
Write a program to implement Ackermann function using recursion process.

At the time of execution, the program should print the message on the console as:

Enter two numbers :

ID: 23BQ1A47A1
For example, if the user gives the input as:

Enter two numbers : 2 1

then the program should print the result as:

A(2, 1) = 5

Source Code:

2023-2027-CIC&CSO-B
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));

Vasireddy Venkatadri Institute of Technology


}

AckermannFunction1.c
int ackermannFun(int m, int n)
{
if(m==0)
return n+1;
else if(n==0)
return ackermannFun(m-1,1);
else
return ackermannFun(m-1,ackermannFun(m,n-1));
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter two numbers :
01
A(0, 1) = 2
Test Case - 2

User Output
Enter two numbers :
22

Page No: 98
A(2, 2) = 7

Test Case - 3

ID: 23BQ1A47A1
User Output
Enter two numbers :
21
A(2, 1) = 5

Test Case - 4

User Output
Enter two numbers :
11

2023-2027-CIC&CSO-B
A(1, 1) = 3

Test Case - 5

User Output
Enter two numbers :
10

Vasireddy Venkatadri Institute of Technology


A(1, 0) = 2

Test Case - 6

User Output
Enter two numbers :
23
A(2, 3) = 9
S.No: 51 Exp. Name: Problem solving Date: 2024-01-05

Aim:
Write a program to find the sum of n natural numbers using recursion process.

Page No: 99
At the time of execution, the program should print the message on the console as:

Enter value of n :

ID: 23BQ1A47A1
For example, if the user gives the input as:

Enter value of n : 6

then the program should print the result as:

Sum of 6 natural numbers = 21

Note: Write the recursive function sum() in Program903a.c .


Source Code:

Program903.c

2023-2027-CIC&CSO-B
#include <stdio.h>
#include "Program903a.c"
void main() {
int n;
printf("Enter value of n : ");
scanf("%d", &n);
printf("Sum of %d natural numbers = %d\n", n, sum(n));
}

Vasireddy Venkatadri Institute of Technology


Program903a.c

int sum(int n)
{
if(n==1)
return 1;
else
return n+sum(n-1);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter value of n :
5
Sum of 5 natural numbers = 15
9
User Output
Enter value of n :

Sum of 9 natural numbers = 45


Test Case - 2

Vasireddy Venkatadri Institute of Technology 2023-2027-CIC&CSO-B ID: 23BQ1A47A1 Page No: 100
Exp. Name: Write a C program to Swap two values
S.No: 52 Date: 2024-01-05
by using Call-by-Address method

Aim:

Page No: 101


Write a program to swap two values by using call by address method.

At the time of execution, the program should print the message on the console as:

Enter two integer values :

ID: 23BQ1A47A1
For example, if the user gives the input as:

Enter two integer values : 12 13

then the program should print the result as:

Before swapping in main : a = 12 b = 13


After swapping in swap : *p = 13 *q = 12
After swapping in main : a = 13 b = 12

Note: Write the function swap() in Program1002a.c and do use the printf() function with a newline character

2023-2027-CIC&CSO-B
( \n ).
Source Code:

Program1002.c

#include <stdio.h>
#include "Program1002a.c"
void main() {
int a, b;

Vasireddy Venkatadri Institute of Technology


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
void swap(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
printf("After swapping in swap : *p = %d *q = %d\n", *a, *b);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
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

Page No: 102


Test Case - 2

User Output

ID: 23BQ1A47A1
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 :

2023-2027-CIC&CSO-B
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

Vasireddy Venkatadri Institute of Technology


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

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
S.No: 53 Exp. Name: Dangling Pointers Date: 2024-01-05

Aim:
Demonstrate Dangling pointer problem using a C program.

Page No: 103


Note: The dangling pointers are set to NULL at the end of the program to avoid undefined behavior on the code.
Source Code:

danglingPointer.c

ID: 23BQ1A47A1
#include <stdio.h>
#include <stdlib.h>

int main() {
int *ptr1 = NULL;
int *ptr2 = NULL;
int value;

// Allocate memory for an integer

2023-2027-CIC&CSO-B
// Input the integer value
printf("Enter an integer value: ");
scanf("%d",&value);
ptr1=&value;
ptr2=ptr1;

Vasireddy Venkatadri Institute of Technology


// Assign the input value to the allocated memory

// Point ptr2 to the same memory location as ptr1

// Check if ptr2 is a valid pointer before accessing


if (ptr2 != NULL) {
printf("Value through ptr2: %d\n",*ptr2 );
} else {
printf("ptr2 is a dangling pointer (invalid)\n");
}

// Deallocate the memory pointed to by ptr1

// Set ptr1 and ptr2 to NULL to avoid dangling pointers


ptr1 = NULL;
ptr2 = NULL;

return 0;
}
Execution Results - All test cases have succeeded!
Test Case - 1

User Output

Page No: 104


Enter an integer value:
54
Value through ptr2: 54

ID: 23BQ1A47A1
Test Case - 2

User Output
Enter an integer value:
10
Value through ptr2: 10

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
Exp. Name: Write a C program to Copy one String
S.No: 54 Date: 2024-01-05
into another using Pointers

Aim:

Page No: 105


Write a C program to copy one string into another using pointers.

Sample Input and Output:

Enter source string : Robotic Tool

ID: 23BQ1A47A1
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 : ");
fgets(source, sizeof(source), stdin);

2023-2027-CIC&CSO-B
copyString(target, source);
printf("Target string : %s\n", target);
}

CopyStringPointers1.c
#include<string.h>
void copyString(char target[100], char source[100])

Vasireddy Venkatadri Institute of Technology


{
strcpy(target,source);
}

Execution Results - All test cases have succeeded!


Test Case - 1

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

User Output
Enter source string :
Virat Pointer Sachin Ganguly Dravid Warne

Page No: 106


Target string : Virat Pointer Sachin Ganguly Dravid Warne

Test Case - 4

ID: 23BQ1A47A1
User Output
Enter source string :
Hyderabad London Losangels Weelington
Colombo
Target string : Hyderabad London Losangels Weelington Colombo

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
Exp. Name: Write a C program to Count number of
S.No: 55 Lowercase, Uppercase, digits and Other Characters Date: 2024-01-05
using Pointers

Page No: 107


Aim:
Write a C program to find number of lowercase , uppercase , digits and other characters using pointers.

Sample Input and Output:

ID: 23BQ1A47A1
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

Source Code:

CountCharDigitOthers.c
#include <stdio.h>
#include "CountCharDigitOthers1.c"

2023-2027-CIC&CSO-B
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);

Vasireddy Venkatadri Institute of Technology


printf("Number of other characters = %d\n", otherCount);
}

CountCharDigitOthers1.c
void countCharDigitOthers(char str[80],int *upperCount, int *lowerCount, int *digitCount,
int *otherCount)
{
int i;
for( i=0;str[i]!='\0';i++)
{
if( str[i]>='a' && str[i]<='z')
*lowerCount=*lowerCount+1;
else if(str[i]>='A' && str[i]<='Z')
*upperCount=*upperCount+1;
else if(str[i]>='0' && str[i]<='9')
*digitCount=*digitCount+1;
else
*otherCount=*otherCount+1;
}
}
Execution Results - All test cases have succeeded!
Test Case - 1

User Output

Page No: 108


Enter a string :
CodeTantra123&*@987.
Number of uppercase letters = 2
Number of lowercase letters = 8
Number of digits = 6

ID: 23BQ1A47A1
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

2023-2027-CIC&CSO-B
Number of other characters = 7

Test Case - 3

User Output
Enter a string :
12345

Vasireddy Venkatadri Institute of Technology


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@
Number of uppercase letters = 3
Number of lowercase letters = 0
Number of digits = 0
Number of other characters = 1

Test Case - 5

User Output
Enter a string :
Wellington@NZ I will Stay Here
Number of uppercase letters = 6
Number of digits = 0
Number of other characters = 5

Vasireddy Venkatadri Institute of Technology 2023-2027-CIC&CSO-B ID: 23BQ1A47A1 Page No: 109
S.No: 56 Exp. Name: Write the code Date: 2024-01-06

Aim:
Write a program to read a text content from a file and display on the monitor with the help of C program.

Page No: 110


Source Code:

readFilePrint.c
#include <stdio.h>
void main()

ID: 23BQ1A47A1
{
char fname[100],ch;
FILE *fp;
printf("Enter the name of the file to read: ");
scanf("%s",fname);
fp=fopen(fname,"r");
printf("Content of the file %s:\n",fname);
while( !feof(fp))
{
ch=fgetc(fp);
if( feof(fp))

2023-2027-CIC&CSO-B
break;
printf("%c",ch);
}
}

file1.txt

A man was very upset with his old parents. He sometimes beat them in anger.
One day he threw them out of his house.

Vasireddy Venkatadri Institute of Technology


They both left the house sadly and never came back.
Now, the man lived happily with his wife and children.
Twenty years later, now his children had grown up, and all of them had gotten married.
They were doing the same with the man as he used to with his old parents.

file2.txt

There were two very close friends. One friend was rich and the other was poor.
The rich friend would often ask the other to tell him whenever he needed money so that he
could help him.
But, the poor friend never got such a chance.
One day the poor friend really needed money, and he thought that he would ask his friend.

file3.txt
A couple was living their life happily. The womans husband had a clothing business.
One day suddenly his health deteriorated very much and he died.
Now calamity had arisen in front of the woman.
She was very depressed about how she would take care of herself and her children.
Her husbands shop was closed. She had no idea what to do.
Execution Results - All test cases have succeeded!
Test Case - 1

User Output

Page No: 111


Enter the name of the file to read:
file1.txt
Content of the file file1.txt:
A man was very upset with his old parents. He sometimes beat them in anger.
One day he threw them out of his house.

ID: 23BQ1A47A1
They both left the house sadly and never came back.
Now, the man lived happily with his wife and children.
Twenty years later, now his children had grown up, and all of them had gotten married.
They were doing the same with the man as he used to with his old parents.

Test Case - 2

User Output
Enter the name of the file to read:
file2.txt

2023-2027-CIC&CSO-B
Content of the file file2.txt:
There were two very close friends. One friend was rich and the other was poor.
The rich friend would often ask the other to tell him whenever he needed money so that he
could help him.
But, the poor friend never got such a chance.
One day the poor friend really needed money, and he thought that he would ask his friend.

Vasireddy Venkatadri Institute of Technology


Exp. Name: Write a C program to write and read
S.No: 57 Date: 2024-01-05
text into a binary file using fread() and fwrite()

Aim:

Page No: 112


Write a C program to write and read text into a binary file using fread() and fwrite().

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:

ID: 23BQ1A47A1
FilesStructureDemo1.c

#include<stdio.h>
struct student {
int roll;
char name[25];
float marks;
};
void main() {
FILE *fp;
char ch;

2023-2027-CIC&CSO-B
struct student s;
fp = fopen("student-information.txt","w" ); // Complete the statement
do {
printf("Roll no: ");
scanf("%d",&s.roll); // Complete the statement
printf("Name: ");
scanf("%s",&s.name); // Complete the statement
printf("Marks: ");
scanf("%f",&s.marks); // Complete the statement

Vasireddy Venkatadri Institute of Technology


fwrite(&s,sizeof(s),1,fp); // Complete the statement
printf("Want to add another data (y/n): ");
scanf(" %c", &ch);
}while (ch!='n'); // Complete the condition
printf("Data written successfully\n");
fclose(fp);
fp = fopen("student-information.txt","r" ); // Complete the statement
printf("Roll\tName\tMarks\n");
while (fread(&s,sizeof(s),1,fp) > 0) { // Complete the condition
printf("%d\t%s\t%f\n",s.roll,s.name,s.marks ); // complete the statement
}
fclose(fp);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Roll no:
501
Name:
Ganga
Marks:
92
Want to add another data (y/n):
y

Page No: 113


Roll no:
502
Name:
Smith

ID: 23BQ1A47A1
Marks:
65
Want to add another data (y/n):
n
Data written successfully
Roll Name Marks
501 Ganga 92.000000
502 Smith 65.000000

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
Exp. Name: Write C program to copy contents of
S.No: 58 Date: 2024-01-06
one file into another file.

Aim:

Page No: 114


Write a C program that creates a file (eg: SampleTextFile1.txt) and allows a user to input text until they enter '@'
into the file. The program then creates another file (eg: SampleTextFile2.txt) and copies the contents of the first
file to the second file. Finally, it should display the contents of the second file.
Source Code:

ID: 23BQ1A47A1
copy.c
#include <stdio.h>

void main() {
FILE *fp, *fp1, *fp2;
char ch;
fp = fopen("one.txt","w"); // write the missing code
printf("Enter the text with @ at end : ");
while ((ch = getchar()) != '@') {
putc(ch, fp);
}

2023-2027-CIC&CSO-B
putc(ch, fp);
fclose(fp);
fp1=fopen("one.txt","r");
fp2=fopen("two.txt","w");
printf("Copied text is : ");
while( !feof(fp))
{
fscanf(fp1,"%c",&ch);
if(feof(fp1)||ch=='@')

Vasireddy Venkatadri Institute of Technology


break;
fprintf(fp2,"%c",ch);
printf("%c",ch);
}
printf("\n");
fclose(fp1);
fclose(fp2);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter the text with @ at end :
CodeTantra received
best Startup award from Hysea in 2016@
Copied text is : CodeTantra received
best Startup award from Hysea in 2016
Exp. Name: Write a C program to Merge two Files
S.No: 59 and stores their contents in another File using Date: 2024-01-05
Command-Line arguments

Page No: 115


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
• Write the content onto the file
• Close the file

ID: 23BQ1A47A1
• 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:

2023-2027-CIC&CSO-B
MergeFilesArgs.c

Vasireddy Venkatadri Institute of Technology


#include <stdio.h>
void main(int argc,char*argv[]) {// fill argument parameters
FILE *fp1, *fp2, *fp3;
char ch;
fp1 = fopen(argv[1] , "w"); // Open file in corresponding mode

Page No: 116


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);

ID: 23BQ1A47A1
fp2 = fopen(argv[2] , "w"); // Open file in corresponding mode
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);
fp1 = fopen(argv[1] , "r"); // Open a first existed file in read mode
fp3 = fopen(argv[3] , "w"); // Open a new file in write mode
while (!feof(fp1)) { // Repeat loop till get @ at the end of existed file
ch=fgetc(fp1);

2023-2027-CIC&CSO-B
if(ch=='@')
break;
putc(ch, fp3);
}
fclose(fp1); // Close the first existed file
fp2 = fopen(argv[2] , "r"); // Open a secong existed file in read mode
while (!feof(fp2)) { // Repeat loop till get @ at the end of existed file
ch=fgetc(fp2);
if(ch=='@')

Vasireddy Venkatadri Institute of Technology


break;
putc(ch, fp3);
}
putc(ch, fp3);
fclose(fp2);
fclose(fp3);
fp3 = fopen(argv[3] , "r"); // Open the merged file in read mode
printf("Merged text is : ");
while (!feof(fp3)) { // Repeat loop till get @ at the end of merged file
ch=fgetc(fp3);
if(ch=='@')
break;
putchar(ch);
}
printf("\n");
fclose(fp3); // Close the merged file
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter the text with @ at end for file-1 :
This is CodeTantra
They implemented automatic robotic tool@
Enter the text with @ at end for file-2 :
Started the company in

Page No: 117


2014@
Merged text is : This is CodeTantra
They implemented automatic robotic tool
Started the company in

ID: 23BQ1A47A1
2014

Test Case - 2

User Output
Enter the text with @ at end for file-1 :
Best
Fair
Awesome@
Enter the text with @ at end for file-2 :

2023-2027-CIC&CSO-B
False@
Merged text is : Best
Fair
Awesome
False

Vasireddy Venkatadri Institute of Technology


Exp. Name: Write a C program to Count number of
S.No: 60 Date: 2024-01-06
Characters, Words and Lines of a given File

Aim:

Page No: 118


Write a program to count number of characters, words and lines of given text file.
• open a new file " DemoTextFile2.txt " in write mode
• write the content onto the file
• close the file
• open the same file in read mode

ID: 23BQ1A47A1
• 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 = 1, lineCount = 1;

2023-2027-CIC&CSO-B
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 {

Vasireddy Venkatadri Institute of Technology


ch = fgetc(fp);
if (ch==' '|| ch=='\n') // Write the condition to count words
wordCount++;
else if(ch!='@')
charCount++;
if (ch=='\n') // Write the condition to count lines
lineCount++;
} while (ch!='@'); // Repeat loop till read @ at the end
fclose(fp);
printf("Total characters : %d\n", charCount);
printf("Total words : %d\n", wordCount);
printf("Total lines : %d\n", lineCount);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter the text with @ at end :
Arise! Awake!
and stop not until
the goal is reached@
Total characters : 43
Total words : 10
Total lines : 3

Page No: 119


Test Case - 2

User Output
Enter the text with @ at end :

ID: 23BQ1A47A1
Believe in your self
and the world will be
at your feet@
Total characters : 44
Total words : 12
Total lines : 3

2023-2027-CIC&CSO-B
Vasireddy Venkatadri Institute of Technology
Exp. Name: Print the last n characters of a file by
S.No: 61 Date: 2024-01-06
reading the file

Aim:

Page No: 120


Write a C program to print the last n characters of a file by reading the file name and n value from the command
line.
Source Code:

file.c

ID: 23BQ1A47A1
#include<stdio.h>
void main(int argc,char *argv[])
{
FILE *fp;
int n;
char ch;
if( argc!=3)
printf("Invalid arguments");
else
{
n=atoi(argv[2]);

2023-2027-CIC&CSO-B
fp=fopen(argv[1],"r");
fseek(fp,-n,2);
while(!feof(fp))
{
ch=fgetc(fp);
if( feof(fp))
break;
printf("%c",ch);
}

Vasireddy Venkatadri Institute of Technology


}
}

input1.txt
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
Now is better than never.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.

input2.txt
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Everything matters.
test1.txt

CodeTantra
Start coding in 60 mins

Page No: 121


test2.txt
Hydrofoil is an underwater fin with a falt or curved wing-like
surface that is designed to lift a moving boat or ship
by means of the reaction upon its surface

ID: 23BQ1A47A1
test3.txt
Count the sentences in the file.
Count the words in the file.
Count the characters in the file.

Execution Results - All test cases have succeeded!

2023-2027-CIC&CSO-B
Test Case - 1

User Output
good idea.

Test Case - 2

User Output

Vasireddy Venkatadri Institute of Technology


verything matters.

You might also like