0% found this document useful (0 votes)
4 views

Lab Record Download

The document contains a series of C programming experiments conducted at SRKR Engineering College, detailing various programming tasks such as displaying 'Hello World', scanning data types, performing arithmetic operations, calculating sum and average, temperature conversions, and calculating simple and compound interest. Each experiment includes the aim, source code, input/output formats, and execution results indicating successful test cases. The document serves as a practical guide for students learning C programming through hands-on coding exercises.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab Record Download

The document contains a series of C programming experiments conducted at SRKR Engineering College, detailing various programming tasks such as displaying 'Hello World', scanning data types, performing arithmetic operations, calculating sum and average, temperature conversions, and calculating simple and compound interest. Each experiment includes the aim, source code, input/output formats, and execution results indicating successful test cases. The document serves as a practical guide for students learning C programming through hands-on coding exercises.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 150

Exp.

Name: Display hello world Date: 2024-09-

Page No: 1
S.No: 1
message 21

Aim:

ID: 24B91A05R0
Write a C program to display hello world message
Source Code:

hello.c
#include<stdio.h>
int main(){
printf("Hello World\n");
}

2024-2028-CSE-D
Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Hello World

SRKR Engineering College (Autonomous)


Exp. Name: Scan all data type variables Date: 2024-09-
S.No: 2

Page No: 2
and display them 21

Aim:
Write a C program to scan all data type variables(int, float, char, double) as input and

ID: 24B91A05R0
print them as output.

Input Format:
• First Line: An integer, entered after the prompt "integer: ".
• Second Line: A floating-point number, entered after the prompt "floating-point
number: ".
• Third Line: A character, entered after the prompt "character: ".
• Fourth Line: A double-precision floating-point number, entered after the prompt
"double: ".

Output Format:

2024-2028-CSE-D
• First Line: A message "You entered:".
• Second Line: The integer entered, in the format "Integer: [integerVar]".
• Third Line: The floating-point number entered, formatted to six decimal places, in
the format "Float: [floatVar]".
• Fourth Line: The character entered, in the format "Character: [charVar]".
• Fifth Line: The double-precision floating-point number entered, formatted to six
decimal places, in the format "Double: [doubleVariable]".

SRKR Engineering College (Autonomous)


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

scan.c
// scan of all types

Page No: 3
#include<stdio.h>
int main(){
int a;
float b;
char c;

ID: 24B91A05R0
double d;
printf("integer: ");
scanf(" %d ", &a);
printf("floating-point number: ");
scanf(" %f ", &b);
printf("character: ");
scanf(" %c ", &c);
printf("double: ");
scanf(" %lf ", &d);
printf("You entered:\n");
printf("Integer: %d\n",a);

2024-2028-CSE-D
printf("Float: %f\n",b);
printf("Character: %c\n",c);
printf("Double: %lf",d);
}

SRKR Engineering College (Autonomous)


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

Page No: 4
User Output
integer:
-10

ID: 24B91A05R0
floating-point number:
12.2546
character:
T
double:
12.6789678
You entered:
Integer: -10
Float: 12.254600
Character: T

2024-2028-CSE-D
Double: 12.678968

SRKR Engineering College (Autonomous)


Date: 2024-09-
S.No: 3 Exp. Name: Arithmetic operations

Page No: 5
21

Aim:
Write a C program to perform arithmetic operations like +,-,*,/,% on two input variables

ID: 24B91A05R0
num1 and num2.

Input Format:
• The first line of input is an integer representing the value for first number num1
• The second line of input is an integer representing the value of second number
num2

Output Format:
• The program prints the integers that represents the results of addition,
subtraction, multiplication, division, and modulus each on a new line.

2024-2028-CSE-D
Note : For Division and Modulo operation, the value of num2 must be greater than 0

Add new line char \n at the end of output.


Source Code:

arithmeticOperations.c

SRKR Engineering College (Autonomous)


// arthmetic operations
#include<stdio.h>
int main(){
int num1,num2,sum,difference,product,division,modulus;
printf("num1: ");
scanf("%d",&num1);
printf("num2: ");
scanf("%d",&num2);
sum=num1+num2;
difference=num1-num2;
product=num1*num2;
division=num1/num2;
modulus=num1%num2;
printf("Sum: %d\n",sum);
printf("Difference: %d\n",difference);
printf("Product: %d\n",product);
printf("Division: %d\n",division);
printf("Modulus: %d\n",modulus);
}
Execution Results - All test cases have succeeded!

Page No: 6
Test Case - 1

User Output
9

ID: 24B91A05R0
8
17
1
72
1
1

Test Case - 2

2024-2028-CSE-D
User Output
1000
2
1002
998
2000

SRKR Engineering College (Autonomous)


500
0
Exp. Name: Write a C program to find Date: 2024-09-
S.No: 4

Page No: 7
Sum and Average of three numbers 21

Aim:
Write a program to find the sum and average of the three given integers.

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

Program314.c

#include<stdio.h>
int main()
{
int a,b,c,sum;
float avg;

2024-2028-CSE-D
printf("Enter three integers : ");
scanf( "%d %d %d ",&a,&b,&c);
sum=(a+b+c);
printf("Sum of %d, %d and %d : %d\n", a , b , c , sum);
avg=(a+b+c)/3.0;
printf("Average of %d, %d and %d : %f\n",a,b,c,avg);
}

SRKR Engineering College (Autonomous)


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

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
Enter three integers :
583

Page No: 8
Sum of 5, 8 and 3 : 16
Average of 5, 8 and 3 : 5.333333

ID: 24B91A05R0
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

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: Temperature conversions
Date: 2024-09-

Page No: 9
S.No: 5 from Centigrade to Fahrenheit and vice
22
versa.

Aim:

ID: 24B91A05R0
Write a C program to perform temperature conversions from Centigrade to Fahrenheit

Note : Refer to sample test cases for input and output format
Source Code:

temperature.c

#include<stdio.h>
int main(){
float c,f;
scanf("%f", &c);

2024-2028-CSE-D
f=(1.8*c)+32;
printf("%.2f Celsius = %.2f Fahrenheit\n",c,f);
}

Execution Results - All test cases have succeeded!

SRKR Engineering College (Autonomous)


Test Case - 1

User Output
37.5
37.50 Celsius = 99.50 Fahrenheit

Test Case - 2

User Output
-20
-20.00 Celsius = -4.00 Fahrenheit
Date: 2024-09-
S.No: 6 Exp. Name: Problem Solving

Page No: 10
22

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

ID: 24B91A05R0
of interest and time.

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

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

For example, if the user gives the input as:

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


3.5 2.5

2024-2028-CSE-D
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:

SRKR Engineering College (Autonomous)


Program3.c
#include<stdio.h>
#include<math.h>
int main(){
int p,r,t;
float s;
printf("Enter principle amount, rate of interest, time of loan
: ");
scanf("%d%d%d",&p,&r,&t);
s=(p*t*r)/100;
printf("Simple Interest = %f\n",s);
}

Execution Results - All test cases have succeeded!


Test Case - 1
User Output

Page No: 11
Enter principle amount, rate of interest, time of loan :
2500 5 2
Simple Interest = 250.000000

ID: 24B91A05R0
2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: Calculate the square root of Date: 2024-09-
S.No: 7

Page No: 12
an integer 22

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

ID: 24B91A05R0
Note:Print the result up to 3 decimal places.

Input format:
The program takes an integer as input with the print statement "Enter an integer: "
followed by the integer.

Output format:
The output is the floating point value formatted to three decimals that represents the
square root value of the user-given integer.

2024-2028-CSE-D
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

SRKR Engineering College (Autonomous)


#include<stdio.h>
#include<math.h>
int main(){
float a,b;
printf("Enter an integer: ");
scanf("%f",&a);
b=sqrt(a);
printf("Square root: %.3f\n",b);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter an integer:
2
Square root: 1.414
4
User Output
Enter an integer:

Square root: 2.000


Test Case - 2

SRKR Engineering College (Autonomous) 2024-2028-CSE-D ID: 24B91A05R0 Page No: 13


Exp. Name: Calculate simple interest Date: 2024-09-
S.No: 8

Page No: 14
and compound interest 24

Aim:
Write a C program to calculate the simple interest and compound interest by reading

ID: 24B91A05R0
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.

Input Format:
• Prompt the user to enter the values of P , R, T separated by space

Output Format:
• The First line of output represents the value of Simple Interest (SI)
• The Second line of output represents the value of Compount Interest (CI)

2024-2028-CSE-D
Example:
Input:
5000 7 5
Output:
SI= 1750.000000
CI= 2012.760376

SRKR Engineering College (Autonomous)


The formula to find simple interest is SI =
(principal∗rate∗time)

100

The formula to find compound interest is CI =


rate
principal ∗ pow(1 + ( ), time) − principal
100

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

Program315.c

#include<stdio.h>
#include<math.h>
int main(){
float p,r,t;
float SI,CI;
scanf("%f%f%f",&p,&r,&t);
SI=(p*r*t)/100.0;
CI=p*pow(1+r/100,t)-p;
printf("SI= %f\n",SI);
printf("CI= %f\n",CI);
}
Execution Results - All test cases have succeeded!

Page No: 15
Test Case - 1

User Output
5000 7 5

ID: 24B91A05R0
SI= 1750.000000
CI= 2012.760376

Test Case - 2

User Output
10000 4.5 3.5
SI= 1575.000000
CI= 1665.596558

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: Area of a triangle using Date: 2024-09-
S.No: 9

Page No: 16
heron's formula 22

Aim:
Write a program to find the area of a triangle using Heron's formula.

ID: 24B91A05R0
During execution, the program should print the following message on the console:

sides:

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:

2024-2028-CSE-D
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

SRKR Engineering College (Autonomous)


the perimeter, or (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>
int main(){
float a,b,c,s,area;
printf("sides: ");
scanf("%f%f%f",&a,&b,&c);
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("area: %.2f",area);
}
Execution Results - All test cases have succeeded!

Page No: 17
Test Case - 1

User Output
sides:

ID: 24B91A05R0
2.3 2.4 2.5
area: 2.49

Test Case - 2

User Output
sides:
2.6 2.7 2.8
area: 3.15

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: Distance travelled by an Date: 2024-09-
S.No: 10

Page No: 18
object 22

Aim:
Write a program to find the distance travelled by an object.

ID: 24B91A05R0
Sample Input and Output:

Enter the acceleration value : 2.5


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.

2024-2028-CSE-D
Source Code:

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

SRKR Engineering College (Autonomous)


int main(){
float a,u,t,d;
printf("Enter the acceleration value : ");
scanf("%f",&a);
printf("Enter the initial velocity : ");
scanf("%f",&u);
printf("Enter the time taken : ");
scanf("%f",&t);
d=u*t+(0.5)*a*(t*t);
printf("Distance travelled : %f\n",d);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter the acceleration value :
Enter the initial velocity :
5

Page No: 19
Enter the time taken :
6
Distance travelled : 102.000000

ID: 24B91A05R0
Test Case - 2

User Output
Enter the acceleration value :
5
Enter the initial velocity :
0
Enter the time taken :
10

2024-2028-CSE-D
Distance travelled : 250.000000

Test Case - 3

User Output
Enter the acceleration value :

SRKR Engineering College (Autonomous)


2.5
Enter the initial velocity :
5.7
Enter the time taken :
20
Distance travelled : 614.000000

Test Case - 4

User Output
Enter the acceleration value :
50
Enter the initial velocity :
34.67
Enter the time taken :
6
Distance travelled : 1108.020020
Test Case - 5

Page No: 20
User Output
Enter the acceleration value :
125.6
Enter the initial velocity :

ID: 24B91A05R0
45.8
Enter the time taken :
4
Distance travelled : 1188.000000

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Date: 2024-09-
S.No: 11 Exp. Name: Evaluate the expressions

Page No: 21
28

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

ID: 24B91A05R0
a. A+B*C+(D*E) + F*G
b. A/B*C-B+A*D/3
c. A+++B---A
d. J= (i++) + (++i)

Note: consider expression as A++ + ++B - --A


Source Code:

evaluate.c

#include<stdio.h>

2024-2028-CSE-D
#include<math.h>
int 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);
int exp1 =A+B*C+(D*E)+F*G;
int exp2=A/B*C - B+A*D/3;

SRKR Engineering College (Autonomous)


int exp3=++A + B - --A;
int exp4=(i++)+(++i);
printf("a.A+B*C+(D*E) + F*G = %d\n",exp1);
printf("b.A/B*C-B+A*D/3 = %d\n",exp2);
printf("c.A+++B---A = %d\n",exp3);
printf("d.J = (i++) + (++i) = %d\n",exp4);
}

Execution Results - All test cases have succeeded!


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

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

ID: 24B91A05R0
b.A/B*C-B+A*D/3 = 80
c.A+++B---A = 21
d.J = (i++) + (++i) = 4

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: Greatest of three numbers Date: 2024-09-
S.No: 12

Page No: 23
using a conditional operator 28

Aim:
Write a C program to display the greatest of three numbers using a conditional operator

ID: 24B91A05R0
(ternary operator).

Input Format
The program prompts the user to enter three integers.

Output Format
The program prints the greatest of the three integers.

Source Code:

greatest.c

2024-2028-CSE-D
#include <stdio.h>

int main() {
int num1, num2, num3, greatest;

// Take input from user

SRKR Engineering College (Autonomous)


printf("num1: ");
scanf("%d",&num1);

printf("num2: ");
scanf("%d",&num2);

printf("num3: ");
scanf("%d",&num3);

greatest = ((num1>num2)?(num1>num3?num1:num3):(num2>num3)?
num2:num3);

// Display the greatest number


printf("Greatest number: %d\n",greatest);

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

Page No: 24
Test Case - 1

User Output
num1:

ID: 24B91A05R0
8
num2:
9
num3:
90
Greatest number: 90

Test Case - 2

2024-2028-CSE-D
User Output
num1:
5
num2:
45
num3:

SRKR Engineering College (Autonomous)


6
Greatest number: 45
Exp. Name: Write a C program to Total Date: 2024-09-
S.No: 13

Page No: 25
and Average of 5 subjects marks 28

Aim:
Write a program to take marks of 5 subjects in integers, and find the total , average

ID: 24B91A05R0
in float.

Sample Input and Output:

Enter 5 subjects marks : 55 56 57 54 55


Total marks : 277.000000
Average marks : 55.400002

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

2024-2028-CSE-D
Source Code:

TotalAndAvg.c
#include<stdio.h>
#include<math.h>
int main(){

SRKR Engineering College (Autonomous)


int a,b,c,d,e;
float total,avg;
printf("Enter 5 subjects marks : ");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
total=a+b+c+d+e;
avg=total/5.0;
printf("Total marks : %f\n",total);
printf("Average marks : %f\n",avg);
}

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

Page No: 26
Test Case - 2

User Output
Enter 5 subjects marks :

ID: 24B91A05R0
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

2024-2028-CSE-D
Total marks : 465.000000
Average marks : 93.000000

Test Case - 4

User Output

SRKR Engineering College (Autonomous)


Enter 5 subjects marks :
20 30 66 77 44
Total marks : 237.000000
Average marks : 47.400002

Test Case - 5

User Output
Enter 5 subjects marks :
56 78 88 79 64
Total marks : 365.000000
Average marks : 73.000000

Test Case - 6

User Output
Enter 5 subjects marks :
Total marks : 246.000000
Average marks : 49.200001

SRKR Engineering College (Autonomous) 2024-2028-CSE-D ID: 24B91A05R0 Page No: 27


Exp. Name: Write a Program to find the Date: 2024-09-
S.No: 14

Page No: 28
Max and Min of Four numbers 28

Aim:
Write a program to find the max and min of four numbers.

ID: 24B91A05R0
Sample Input and Output :

Enter 4 numbers : 9 8 5 2
Max value : 9
Min value : 2

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

2024-2028-CSE-D
Source Code:

MinandMaxOf4.c

SRKR Engineering College (Autonomous)


#include<stdio.h>

Page No: 29
int main(){
int a,b,c,d;
printf("Enter 4 numbers : ");
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a>b && a>c && a>d){

ID: 24B91A05R0
printf("Max value : %d\n",a);
}
else if(b>a && b>c && b>d){
printf("Max value : %d\n",b);
}
else if(c>a && c>b && c>d){
printf("Max value : %d\n",c);
}
else{
printf("Max value : %d\n",d);
}

2024-2028-CSE-D
if(a<b && a<c && a<d){
printf("Min value : %d\n",a);
}
else if(b<a && b<c && b<d){
printf("Min value : %d\n",b);
}

SRKR Engineering College (Autonomous)


else if(c<a && c<b && c<d){
printf("Min value : %d\n",c);
}
else{
printf("Min value : %d\n",d);
}
}

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

Page No: 30
User Output
Enter 4 numbers :
112 245 167 321
Max value : 321

ID: 24B91A05R0
Min value : 112

Test Case - 3

User Output
Enter 4 numbers :
110 103 113 109
Max value : 113
Min value : 103

2024-2028-CSE-D
Test Case - 4

User Output
Enter 4 numbers :
-34 -35 -24 -67

SRKR Engineering College (Autonomous)


Max value : -24
Min value : -67

Test Case - 5

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

Test Case - 6

User Output
Enter 4 numbers :
564 547 574 563
Max value : 574
Exp. Name: Find out the electricity bill Date: 2024-09-
S.No: 15

Page No: 31
charges 29

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

ID: 24B91A05R0
• 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.

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%

2024-2028-CSE-D
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 as shown in the sample test cases.

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

SRKR Engineering College (Autonomous)


Source Code:

electricityBillCharges.c
#include<stdio.h>

Page No: 32
int main(){
int a;
char name[20];
float c,s,t;
s=0;

ID: 24B91A05R0
printf("Enter customer name: ");
scanf("%s",name);
printf("Units consumed: ");
scanf("%d",&a);
if(a<=200)
c=a*.80;
else if (a<=300)
c=a*.90;
else
c=a*1.0;
if (c<=100)

2024-2028-CSE-D
t=100;
else if (c<=400)
t=c;
else{
s=c*.15;
t=c+s;
}

SRKR Engineering College (Autonomous)


printf("Customer name: %s\n",name);
printf("Units consumed: %d\n",a);
printf("Amount charged: %.2f\n",c);
printf("Surcharges: %.2f\n",s);
printf("Amount to be paid: %.2f\n",t);
}

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
Surcharges: 0.00
Amount to be paid: 100.00

Page No: 33
Test Case - 2

User Output

ID: 24B91A05R0
Enter customer name:
Rosy
Units consumed:
325
Customer name: Rosy
Units consumed: 325
Amount charged: 325.00
Surcharges: 0.00
Amount to be paid: 325.00

2024-2028-CSE-D
Test Case - 3

User Output
Enter customer name:
Amar

SRKR Engineering College (Autonomous)


Units consumed:
801
Customer name: Amar
Units consumed: 801
Amount charged: 801.00
Surcharges: 120.15
Amount to be paid: 921.15

Test Case - 4

User Output
Enter customer name:
Raman
Units consumed:
300
Customer name: Raman
Units consumed: 300
Amount charged: 270.00
Amount to be paid: 270.00

SRKR Engineering College (Autonomous) 2024-2028-CSE-D ID: 24B91A05R0 Page No: 34


Exp. Name: C program to find roots and Date: 2024-09-
S.No: 16

Page No: 35
nature of quadratic equation. 29

Aim:
Write a C program to find the roots of a quadratic equation, given its coefficients.

ID: 24B91A05R0
Source Code:

quad.c
#include<stdio.h>
#include<math.h>
int main(){
int a,b,c,d;
float real,imaginary;
printf("Enter coefficients a, b and c: ");
scanf("%d%d%d",&a,&b,&c);

2024-2028-CSE-D
real = -b/(2.0*a);
d=(b*b)-4*a*c;
imaginary=sqrt(-d)/(2*a);
printf("root1 = %.2f+%.2fi and root2 = %.2f-
%.2fi",real,imaginary,real,imaginary);
}

SRKR Engineering College (Autonomous)


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
Date: 2024-10-
S.No: 17 Exp. Name: Simulate a basic calculator

Page No: 36
01

Aim:
Write a program to perform basic calculator operations [+, -, *, /] of two integers a and b

ID: 24B91A05R0
using switch statement.

Constraints:
• 10-4 <= a,b = 104
• 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

2024-2028-CSE-D
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

SRKR Engineering College (Autonomous)


#include<stdio.h>

Page No: 37
int main(){
char operators;
int a,b,sum,diff,product,division;
scanf("%d",&a);
scanf("%c",&operators);

ID: 24B91A05R0
scanf("%d",&b);
sum=a+b;
diff=a-b;
product=a*b;
division=a/b;
switch(operators){
case '+': printf("%d",sum);
break;
case '-': printf("%d",diff);
break;
case '*': printf("%d",product);

2024-2028-CSE-D
break;
case '/': printf("%d",division);
break;
}
}

SRKR Engineering College (Autonomous)


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

User Output
36-31
5

Test Case - 2

User Output
89/45
1

Test Case - 3

User Output
1013

12500
25*500
User Output
Test Case - 4

SRKR Engineering College (Autonomous) 2024-2028-CSE-D ID: 24B91A05R0 Page No: 38


Exp. Name: Write a program to find Date: 2024-09-
S.No: 18

Page No: 39
leap year or not 30

Aim:
Lucy is celebrating her 15th birthday. Her father promised her that he will buy her a new

ID: 24B91A05R0
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.

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

2024-2028-CSE-D
#include<stdio.h>
int main(){
int year;
scanf("%d",&year);
if(year%4==0){
if(year%100==0){
if(year%400==0){

SRKR Engineering College (Autonomous)


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

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
1900
1900 is not a leap year
Test Case - 2

Page No: 40
User Output
2004
2004 is a leap year

ID: 24B91A05R0
Test Case - 3

User Output
1995
1995 is not a leap year

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Date: 2024-10-
S.No: 19 Exp. Name: Factorial of a given number

Page No: 41
02

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

ID: 24B91A05R0
Source Code:

factorialOfInt.c

#include<stdio.h>
int main(){
int factorial=1,N;
printf("Integer: ");
scanf("%d",&N);
for(int i=1;i<=N;i++){
factorial=factorial*i;

2024-2028-CSE-D
}
printf("Factorial: %d\n",factorial);
}

Execution Results - All test cases have succeeded!

SRKR Engineering College (Autonomous)


Test Case - 1

User Output
Integer:
5
Factorial: 120

Test Case - 2

User Output
Integer:
4
Factorial: 24
Exp. Name: C program to determine
Date: 2024-10-

Page No: 42
S.No: 20 whether a given number is prime or
03
not.

Aim:

ID: 24B91A05R0
Write the C program to determine whether a given number is prime or not.
Source Code:

Prime.c
#include<stdio.h>
int main(){
int n,i=2,s=0;
printf("Enter a number: ");
scanf("%d",&n);
for( i=2;i<n;i++)

2024-2028-CSE-D
{
if(n%i==0){
s+=1;
}
}
if(s==0){
printf("%d is a prime number\n",n);

SRKR Engineering College (Autonomous)


}
else{
printf("%d is not a prime number\n",n);
}
}

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
11
Enter a number:

11 is a prime number

SRKR Engineering College (Autonomous) 2024-2028-CSE-D ID: 24B91A05R0 Page No: 43


Exp. Name: Compute sine and cos series Date: 2024-10-
S.No: 21

Page No: 44
using taylor series 13

Aim:
Write a C program to compute the sine and cosine series using the Taylor series.

ID: 24B91A05R0
Taylor series:

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

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

Note: Print the result up to 4 decimal places. Use the double data type for all variables
except for the number of terms in the series, which should be an integer. Additionally,
initialize the variables that will store the results of the sine and cosine series to 0.0 at the
beginning.

2024-2028-CSE-D
Source Code:

taylor.c

SRKR Engineering College (Autonomous)


#include<stdio.h>

Page No: 45
#include<math.h>
int main(){
int n,c,i,j;
double x,sin,cos,s;
printf("angle in radians: ");

ID: 24B91A05R0
scanf("%lf",&x);
printf("number of terms in the series: ");
scanf("%d",&n);
sin=x;
cos=1;
s=1;
j=1;
c=1;
for(i=2;i<=n;i++){
s=s*(-x*x);
c=c*j*(j+1);

2024-2028-CSE-D
j=j+2;
cos=cos+s/c;
}
s=x;
j=2;
c=1;
for(i=2;i<=n;i++){

SRKR Engineering College (Autonomous)


s=s*(-x*x);
c=c*j*(j+1);
j=j+2;
sin=sin+s/c;
}
printf("Sine = %.4lf\n",sin);
printf("Cosine = %.4lf\n",cos);
}

Execution Results - All test cases have succeeded!


Test Case - 1

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

Page No: 46
Test Case - 2

User Output
angle in radians:

ID: 24B91A05R0
0.6
number of terms in the series:
5
Sine = 0.5646
Cosine = 0.8253

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: Check given number is Date: 2024-10-
S.No: 22

Page No: 47
palindrome or not 05

Aim:
Write an C program to check given number is palindrome or not

ID: 24B91A05R0
Input Format:
• Single Line: An integer value representing the number to be checked for
palindrome status.

Output Format:
• Single Line: A message indicating whether the number is a palindrome or not. The
format of the message will be:
• "[number] is a palindrome." if the number is a palindrome.
• "[number] is not a palindrome." if the number is not a palindrome.
Source Code:

2024-2028-CSE-D
palindrome.c

#include<stdio.h>
int main(){
int rev=0,a,r,temp;
scanf("%d",&a);

SRKR Engineering College (Autonomous)


temp=a;
while(a>0){
r=a%10;
rev=(rev*10)+r;
a=a/10;
}
if(temp==rev){
printf("%d is a palindrome.",temp);
}
else{
printf("%d is not a palindrome.",temp);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
143
121

User Output
121 is a palindrome.

143 is not a palindrome.


Test Case - 2

SRKR Engineering College (Autonomous) 2024-2028-CSE-D ID: 24B91A05R0 Page No: 48


Date: 2024-10-
S.No: 23 Exp. Name: Pyramid with numbers

Page No: 49
05

Aim:
Write a program to print a pyramid of numbers separated by spaces for the given

ID: 24B91A05R0
number of rows.

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

Enter number of rows :

For example, if the user gives the input as :

Enter number of rows : 3

then the program should print the result as:

2024-2028-CSE-D
1
1 2
1 2 3

Source Code:

SRKR Engineering College (Autonomous)


PyramidDemo15.c

#include <stdio.h>
void main() {
int n, i, j, s;
printf("Enter number of rows : ");
scanf("%d", &n);
// Fill the missing code
for(i=1;i<=n;i++){
for(j=i;j<n;j++){
printf(" ");
}
for(j=1;j<=i;j++){
printf("%d ",j);
}
printf("\n");
}

}
Execution Results - All test cases have succeeded!

Page No: 50
Test Case - 1

User Output
Enter number of rows :

ID: 24B91A05R0
3
1
1 2
1 2 3

Test Case - 2

User Output
Enter number of rows :

2024-2028-CSE-D
6
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6

SRKR Engineering College (Autonomous)


Test Case - 3

User Output
Enter number of rows :
8
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
Exp. Name: Minimum and maximum in Date: 2024-10-
S.No: 24

Page No: 51
an array of integers. 19

Aim:
Write a C program to find the minimum and maximum in an array of integers.

ID: 24B91A05R0
Source Code:

ArrayElements.c
#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]);

2024-2028-CSE-D
}
max=arr[0];
min=arr[0];
for(int i=0;i<number;i++){
if(arr[i]>max){
max=arr[i];
}

SRKR Engineering College (Autonomous)


if(arr[i]<min){
min=arr[i];
}
}
printf("Min an Max: %d and %d",min,max);
}

Execution Results - All test cases have succeeded!


Test Case - 1

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

216
Elements:
User Output

Min an Max: 216 and 216

SRKR Engineering College (Autonomous) 2024-2028-CSE-D ID: 24B91A05R0 Page No: 52


Exp. Name: Search for an element using Date: 2024-11-
S.No: 25

Page No: 53
Linear search 02

Aim:
Write a C program to check whether a given element is present in an array of elements

ID: 24B91A05R0
using linear search. The program should prompt the user to enter the size of the array,
the elements of the array, and the element to search for. It should then output whether
the element is found, and if so, at which position in the array.

Input Format:
• Prompt the user to enter an integer n representing the size of the array (1 ≤ n ≤
20) as follows:
• In the next line enter n integers representing the elements of the array separated
by space.
• Next line should input an integer key representing the element to search for.

2024-2028-CSE-D
Output Format:
• If the element is found, output: Found at position X (where X is the 0-based index
of the found element).
• If the element is not found, output: Y is not found (where Y is the key that was
searched for).

Refer to visible test cases to strictly match with input/output layout.

SRKR Engineering College (Autonomous)


Source Code:

SearchEle.c
#include<stdio.h>

Page No: 54
int main(){
int i,n,k,arr[100],found=0;
printf("Enter size: ");
scanf("%d",&n);
printf("Enter %d element: ",n);

ID: 24B91A05R0
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("Enter search element: ");
scanf("%d",&k);
for(i=0;i<n;i++){
if(arr[i]==k){
found=1;
break;
}
}

2024-2028-CSE-D
if(found==1){
printf("Found at position %d\n",i);
}
else{
printf("%d is not found\n",k);
}
}

SRKR Engineering College (Autonomous)


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

Test Case - 2

User Output
Enter size:
6

Page No: 55
Enter 6 element:
248135
Enter search element:
2

ID: 24B91A05R0
Found at position 0

Test Case - 3

User Output
Enter size:
6
Enter 6 element:
248135

2024-2028-CSE-D
Enter search element:
9
9 is not found

SRKR Engineering College (Autonomous)


Exp. Name: C program to reverse an Date: 2024-11-
S.No: 26

Page No: 56
array. 02

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

ID: 24B91A05R0
Source Code:

reverseArray.c

#include<stdio.h>
int main(){
int arr[100];
int i,n;
printf("Enter no of elements: ");
scanf("%d",&n);
printf("Enter elements: ");

2024-2028-CSE-D
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("The reversed array: ");
for(i=n-1;i>=0;i--){
printf("%d ",arr[i]);
}

SRKR Engineering College (Autonomous)


}

Execution Results - All test cases have succeeded!


Test Case - 1

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:
Enter elements:
2 5 1 77 33 88 2 9
The reversed array: 9 2 88 33 77 1 5 2

SRKR Engineering College (Autonomous) 2024-2028-CSE-D ID: 24B91A05R0 Page No: 57


Exp. Name: 2’s complement of a given Date: 2024-11-
S.No: 27

Page No: 58
Binary number 11

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

ID: 24B91A05R0
Note: The binary input should be separated by a space.
Source Code:

twosComplement.c

#include<stdio.h>
int main(){
int n,sum;
int arr[100];
printf("Enter size: ");

2024-2028-CSE-D
scanf("%d",&n);
printf("Enter %d bit binary number: ",n);
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
int f=1;
for(int i=n-1;i>=0;i--){

SRKR Engineering College (Autonomous)


sum=!arr[i]+f;
arr[i]=sum%2;
f=sum/2;
}
printf("2's complement: ");
for(int i=0;i<n;i++){
printf("%d ",arr[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: 59
Test Case - 2

User Output

ID: 24B91A05R0
Enter size:
6
Enter 6 bit binary number:
100011
2's complement: 0 1 1 1 0 1

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: Eliminate duplicate Date: 2024-11-
S.No: 28

Page No: 60
elements in an array 11

Aim:
Write a C program to eliminate duplicate elements of an array.

ID: 24B91A05R0
Input Format:
• First Line: An integer n representing the size of the array.
• Second Line: n integers representing the elements of the array.

Output Format:
• Single Line: A space-separated list of the unique elements of the array after
duplicates have been removed.
Source Code:

eliminateDuplicates.c

2024-2028-CSE-D
#include<stdio.h>
int main(){
int a[50],i,j,k,number;
printf("Enter size: ");
scanf("%d",&number);
printf("Enter %d elements: ",number);

SRKR Engineering College (Autonomous)


for(i=0;i<number;i++){
scanf("%d",&a[i]);
}
for(i=0;i<number;i++){
for(j=i+1;j<number;j++){
if(a[i]==a[j]){
for(k=j;k<number;k++){
a[k]=a[k+1];
}
j--;
number--;
}
}
}
printf("After eliminating duplicates: ");
for(i=0;i<number;i++){
printf("%d ",a[i]);
}
printf("\n");
}
Execution Results - All test cases have succeeded!

Page No: 61
Test Case - 1

User Output
Enter size:

ID: 24B91A05R0
5
Enter 5 elements:
12123
After eliminating duplicates: 1 2 3

Test Case - 2

User Output
Enter size:

2024-2028-CSE-D
5
Enter 5 elements:
11 13 11 12 13
After eliminating duplicates: 11 13 12

SRKR Engineering College (Autonomous)


Date: 2024-11-
S.No: 29 Exp. Name: Addition of Two Matrices

Page No: 62
03

Aim:
Write a C program to perform the addition of two matrices each with the size m × n .

ID: 24B91A05R0
Input Format:
• First, enter the number of rows and columns (two integers separated by space).
• Then, enter the elements of the first matrix (m * n integers).
• Finally, enter the elements of the second matrix (m * n integers).

Output Format:
• Print the resulting matrix after addition, formatted as a matrix.

Note: The addition of two matrices can only be done when the dimensions of both

2024-2028-CSE-D
matrices are the same, so we are taking the same dimensions for both matrices.
Source Code:

addTwoMatrices.c

SRKR Engineering College (Autonomous)


#include<stdio.h>

Page No: 63
int main(){
int matrix1[100][100];
int matrix2[100][100];
int matrix[100][100];
int r1,c1,i,j;

ID: 24B91A05R0
printf("Enter no of rows, columns: ");
scanf("%d%d",&r1,&c1);
printf("Elements of matrix 1: ");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
scanf("%d",&matrix1[i][j]);
}
}
printf("Elements of matrix 2: ");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){

2024-2028-CSE-D
scanf("%d",&matrix2[i][j]);
}
}
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
matrix[i][j]=matrix1[i][j]+matrix2[i][j];
}

SRKR Engineering College (Autonomous)


}
printf("Addition of matrices:\n");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
printf("%d ",matrix[i][j]);
/*if(j==c1-1){

printf("\n");
}*/
}
printf("\n");
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter no of rows, columns:
12

Page No: 64
Elements of matrix 1:
12
Elements of matrix 2:
98

ID: 24B91A05R0
Addition of matrices:
10 10

Test Case - 2

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

2024-2028-CSE-D
123456
Elements of matrix 2:
987654
Addition of matrices:
10 10 10
10 10 10

SRKR Engineering College (Autonomous)


Exp. Name: Multiplication of Two Date: 2024-11-
S.No: 30

Page No: 65
Matrices 16

Aim:
Write a C program to find the multiplication of two matrices of size r × c.

ID: 24B91A05R0
Input Format:
• First line contains an integer r and an integer c, representing the number of rows
and columns
• Next r rows contains c number of integers representing the elements of the
matrix1
• Repeat the Same for matrix2

Output Format:
• Prints the matrix1 and matrix2 and finally the result of multiplication of both the
matrices

2024-2028-CSE-D
Refer to the sample test cases for better understanding

Sample Input:
2 2
11 22
33 44

SRKR Engineering College (Autonomous)


2 2
11 22
33 44

Sample Output:
matrix1:
11 22
33 44
matrix2:
11 22
33 44
result:
847 1210
1815 2662

Note: Add new line char \n at the end of each line in the output.
Refer to visible test cases for better understanding
Source Code:

matrixMul.c
#include<stdio.h>

Page No: 66
int main(){
int arr1[100][100];
int arr2[100][100];
int arr[100][100];
int r1,r2,c1,c2,i,j,k;

ID: 24B91A05R0
scanf("%d %d",&r1,&c1);
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
scanf("%d",&arr1[i][j]);
}
}
scanf("%d %d",&r2,&c2);
for(i=0;i<r2;i++){
for(j=0;j<c2;j++){
scanf("%d",&arr2[i][j]);
}

2024-2028-CSE-D
}
printf("matrix1:\n");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
printf("%d ",arr1[i][j]);
}
printf("\n");

SRKR Engineering College (Autonomous)


}
printf("matrix2:\n");
for(i=0;i<r2;i++){
for(j=0;j<c2;j++){
printf("%d ",arr2[i][j]);
}
printf("\n");
}
if(c1==r2){
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
arr[i][j]=0;
for(k=0;k<c1;k++){
arr[i][j] = arr[i][j] + arr1[i][k] *
arr2[k][j];
}
}
}
printf("result:\n");
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
printf("%d ",arr[i][j]);
}

Page No: 67
printf("\n");
}
}
else{
printf("Multiplication not possible\n");

ID: 24B91A05R0
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
22

2024-2028-CSE-D
11 22
33 44
22
11 22
33 44

SRKR Engineering College (Autonomous)


matrix1:
11 22
33 44
matrix2:
11 22
33 44
result:
847 1210
1815 2662

Test Case - 2

User Output
33
123
456
789
23
456

4 5 6
1 2 3
7 8 9
4 5 6
1 2 3

matrix2:
matrix1:

Multiplication not possible

SRKR Engineering College (Autonomous) 2024-2028-CSE-D ID: 24B91A05R0 Page No: 68


Exp. Name: Write a C program to Sort Date: 2024-11-
S.No: 31

Page No: 69
given elements using Bubble sort 09

Aim:
Develop an algorithm, implement and execute a C program that reads n integer numbers

ID: 24B91A05R0
and arrange them in ascending order using Bubble Sort.
Source Code:

Lab7.c
#include<stdio.h>
int main(){
int n,i,arr[20],j;
//printf("");
scanf("%d",&n);
printf("Elements: ");

2024-2028-CSE-D
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("Before sorting: ");
for(i=0;i<n;i++){
printf("%d ",arr[i]);
}

SRKR Engineering College (Autonomous)


printf("\n");
for(i=0;i<n;i++){
for(j=0;j<=n-1;j++){
if(arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("After sorting: ");
for(j=1;j<=n;j++){
printf("%d ",arr[j]);
}
printf("\n");
}

Execution Results - All test cases have succeeded!


Test Case - 1

Page No: 70
User Output
4
Elements:
44 22 66 11

ID: 24B91A05R0
Before sorting: 44 22 66 11
After sorting: 11 22 44 66

Test Case - 2

User Output
5
Elements:
92716

2024-2028-CSE-D
Before sorting: 9 2 7 1 6
After sorting: 1 2 6 7 9

SRKR Engineering College (Autonomous)


Exp. Name: Concatenate two given
Date: 2024-11-

Page No: 71
S.No: 32 strings without using string library
09
functions

Aim:

ID: 24B91A05R0
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 :

For example, if the user gives the input as:

string1 : ILove

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

2024-2028-CSE-D
string2 :

For example, if the user gives the input as:

string2 : Coding

SRKR Engineering College (Autonomous)


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
#include<stdio.h>

Page No: 72
int main(){
char str1[50];
char str2[50];
printf("string1 : ");
scanf("%s",str1);

ID: 24B91A05R0
printf("string2 : ");
scanf("%s",str2);
int i=0,j=0;
while(str1[i] != '\0'){
i++;
}
while(str2[j] !='\0'){
str1[i]=str2[j];
i++;
j++;
}

2024-2028-CSE-D
str1[i]='\0';
printf("concatenated string = %s\n",str1);
}

Execution Results - All test cases have succeeded!

SRKR Engineering College (Autonomous)


Test Case - 1

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

Test Case - 2

User Output
string1 :
1234
string2 :
567
concatenated string = 1234567
Exp. Name: Reverse the given string Date: 2024-11-
S.No: 33

Page No: 73
without using the library functions 11

Aim:
Write a program to reverse the given string without using the library functions.

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

Enter a string :

For example, if the user gives the input as:

Enter a string : Dallas

then the program should print the result as:

2024-2028-CSE-D
Reverse string : sallaD

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

Program609.c

SRKR Engineering College (Autonomous)


#include<stdio.h>
#include<string.h>
int main(){
char str1[50],temp;
int i,len;
printf("Enter a string : ");
scanf("%s",str1);
len=strlen(str1)-1;
for(i=0;i<strlen(str1)/2;i++){
temp=str1[i];
str1[i]=str1[len];
str1[len--]=temp;
}
printf("Reverse string : %s\n",str1);
}

Execution Results - All test cases have succeeded!


Test Case - 1
User Output

Page No: 74
Enter a string :
Dallas
Reverse string : sallaD

ID: 24B91A05R0
Test Case - 2

User Output
Enter a string :
CodeTantra
Reverse string : artnaTedoC

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: Write a C program to find
Date: 2024-11-

Page No: 75
S.No: 34 Sum of array elements by allocating
30
memory using malloc() function

Aim:

ID: 24B91A05R0
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


Source Code:

SumOfArray1.c
#include <stdio.h>
#include <stdlib.h>
#include "UsingMalloc.c"

2024-2028-CSE-D
void main() {
int *p, n, i;
printf("Enter n value : ");
scanf("%d", &n);
p = allocateMemory(n);
printf("Enter %d values : ", n);
read1(p, n);

SRKR Engineering College (Autonomous)


printf("The sum of given array elements : %d\n", sum(p, n));
}

UsingMalloc.c
int *allocateMemory(int n){

Page No: 76
int *p;
p = malloc(n*sizeof(int));
return p;
}
void read1(int *p,int n){

ID: 24B91A05R0
for(int i=0;i<n;i++){
scanf("%d",p+i);
}
}
int sum(int *p,int n){
int sum=0;
for(int i=0;i<n;i++){
sum=sum+ *(p+i);
}
return sum;
}

2024-2028-CSE-D
Execution Results - All test cases have succeeded!
Test Case - 1

SRKR Engineering College (Autonomous)


User Output
Enter n value :
3
Enter 3 values :
10 20 30
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
Exp. Name: Write a program to find
Date: 2024-11-

Page No: 77
S.No: 35 Total and Average gained by Students
23
in a Section using Array of Structures

Aim:

ID: 24B91A05R0
Write a C program to find out the total and average marks gained by the students in a
section using array of structures.

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

2024-2028-CSE-D
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

Source Code:

ArrayOfStructures2.c

SRKR Engineering College (Autonomous)


#include <stdio.h>

Page No: 78
struct student {
int regdno;
int marks[3]; int total;
float average;
};

ID: 24B91A05R0
void main() {
struct student students[60];
int i, n;
printf("Enter number of students : ");
scanf("%d", &n);
for (int i=0;i<n;i++) {
printf("Enter regdno, three subjects marks of student-
%d: ", i);

scanf("%d%d%d%d",&students[i].regdno,&students[i].marks[0],&students[i].marks[1],&students[i].mar

2024-2028-CSE-D
students[i].total=students[i].marks[0]+students[i].marks[1]+students[i].marks[2];
students[i].average = students[i].total/3.0;
}
for (int i=0;i<n;i++) {
printf("Student-%d Regdno = %d\tTotal marks =
%d\tAverage marks =
%f\n",i,students[i].regdno,students[i].total,students[i].average); //

SRKR Engineering College (Autonomous)


Fill the code in printf()
}
}

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

Page No: 79
Student-1 Regdno = 201 Total marks = 256 Average marks =
85.333336
Student-2 Regdno = 301 Total marks = 164 Average marks =
54.666668

ID: 24B91A05R0
Test Case - 2

User Output
Enter number of students :
10
Enter regdno, three subjects marks of student-0:
501 23 45 67
Enter regdno, three subjects marks of student-1:

2024-2028-CSE-D
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
Enter regdno, three subjects marks of student-4:

SRKR Engineering College (Autonomous)


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-5 Regdno = 506 Total marks = 204 Average marks =
68.000000

Page No: 80
Student-6 Regdno = 507 Total marks = 246 Average marks =
82.000000
Student-7 Regdno = 508 Total marks = 140 Average marks =
46.666668

ID: 24B91A05R0
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

2024-2028-CSE-D
Enter regdno, three subjects marks of student-0:
101 76 78 73
Enter regdno, three subjects marks of student-1:
102 89 57 68
Enter regdno, three subjects marks of student-2:
103 77 67 59

SRKR Engineering College (Autonomous)


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
Student-4 Regdno = 105 Total marks = 204 Average marks =
68.000000
Exp. Name: Write a Program to enter n
Date: 2024-12-

Page No: 81
S.No: 36 students data using calloc() and
18
display Failed Students List

Aim:

ID: 24B91A05R0
Write a C program to enter n students' data using calloc() and display the students list.

Note: If marks are less than 35 in any subject, the student will fail
Source Code:

FailedList.c
#include <stdio.h>
#include <stdlib.h>
struct student {
int roll;

2024-2028-CSE-D
int marks[6], sum;
float avg;
};
#include "FailedList1.c"
void main() {
struct student *s;
int i, n;

SRKR Engineering College (Autonomous)


printf("Enter the number of students : ");
scanf("%d", &n);
s = allocateMemory(s, n);
read1(s, n);
calculateMarks(s, n);
displayFailedList(s, n);
}

FailedList1.c
struct student* allocateMemory(struct student *s, int n) {

Page No: 82
struct student *p;
p=(struct student*)calloc(n,sizeof(struct student));
return p;
}

ID: 24B91A05R0
void read1(struct student *s, int n) {
for(int i=0;i<n;i++){
printf("Enter the details of student - %d\n",i+1);
printf("Enter the roll number : ");
scanf("%d",&(s+i)->roll);
printf("Enter 6 subjects marks : ");
for(int j=0;j<6;j++){
scanf("%d",&(s+i)->marks[j]);
}
}
}

2024-2028-CSE-D
void calculateMarks(struct student *s, int n) {
int i;
for(i=0;i<n;i++){
(s+i)->sum=0;
for(int j=0;j<6;j++)
(s+i)->sum=(s+i)->sum+(s+i)->marks[j];
(s+i)->avg=(s+i)->sum/6.0;

SRKR Engineering College (Autonomous)


}
}
void displayFailedList(struct student *s, int n) {
int i,c=1;
printf("RollNo\tTotalMarks\tAverageMarks\tStatus\n");
for (i = 0; i < n; i++) {
c=1;
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
for(int j=0;j<6;j++)
if((s+i)->marks[j]<35)
c=0;
if (c==0) // Fill the missing code
printf("Fail");
else
printf("Pass");
printf("\n");
}
}
Execution Results - All test cases have succeeded!

Page No: 83
Test Case - 1

User Output
Enter the number of students :

ID: 24B91A05R0
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

2024-2028-CSE-D
Enter 6 subjects marks :
34 56 98 39 78 89
Enter the details of student - 3
Enter the roll number :
103
Enter 6 subjects marks :
35 67 89 98 76 56

SRKR Engineering College (Autonomous)


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
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
58 67 58 89 87 76
RollNo TotalMarks AverageMarks Status

Page No: 84
1001 350 58.333332 Fail
1002 435 72.500000 Pass

ID: 24B91A05R0
2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: Write a C program to find
Date: 2024-12-

Page No: 85
S.No: 37 Total Marks of a Student using
17
Command-line arguments

Aim:

ID: 24B91A05R0
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:

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

2024-2028-CSE-D
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 print the output as:

SRKR Engineering College (Autonomous)


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:

TotalMarksArgs.c
#include<stdio.h>

Page No: 86
void main(int argc,char *argv[]){
int i;
if(argc !=5){
printf("Arguments passed through command line are not
equal to 4\n");

ID: 24B91A05R0
}
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]));
}

2024-2028-CSE-D
Execution Results - All test cases have succeeded!
Test Case - 1

SRKR Engineering College (Autonomous)


User Output
Student name : Sachin
Subject-1 marks : 67
Subject-2 marks : 89
Subject-3 marks : 58
Total marks : 214

Test Case - 2

User Output
Arguments passed through command line are not equal to 4
Exp. Name: Write a C program to Date: 2024-12-
S.No: 38

Page No: 87
implement realloc() 17

Aim:
Write a C program to implement realloc() .

ID: 24B91A05R0
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
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

2024-2028-CSE-D
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int) * 2);
int i;
int *ptr_new;
*ptr = 10;

SRKR Engineering College (Autonomous)


*(ptr + 1) = 20;
*(ptr+2)=30;
ptr_new=ptr;
for (i = 0; i < 3; i++)
printf("%d ", *(ptr_new + i));
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
10 20 30
Exp. Name: Write a C Program to store Date: 2024-12-
S.No: 39

Page No: 88
information using Structures with DMA 18

Aim:
Write a program to create a list of nodes using self-referential structure and print

ID: 24B91A05R0
that data.

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

Enter an integer value :

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:

2024-2028-CSE-D
Do u want another list (y|n) :

if the user gives the input as:

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

SRKR Engineering College (Autonomous)


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

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>

Page No: 89
#include <stdlib.h>
struct list {
int data;
struct list *next;
};

ID: 24B91A05R0
#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

2024-2028-CSE-D
struct list* create(struct list *first) {
char op;
struct list *q, *temp;
do {
temp = (struct list*)malloc(sizeof(struct list)); //
Allocate memory
printf("Enter an integer value : ");

SRKR Engineering College (Autonomous)


scanf("%d",&temp->data ); // Read data
temp -> next = NULL; // Place NULL
if (first == NULL) {
first = temp; // Assign temp to the first node
} else {
q -> next = temp; // Create a link from the
last node to new node temp
}
q = temp;
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!

Page No: 90
Test Case - 1

User Output
Enter an integer value :

ID: 24B91A05R0
10
Do u want another list (y|n) :
y
Enter an integer value :
20
Do u want another list (y|n) :
y
Enter an integer value :
30

2024-2028-CSE-D
Do u want another list (y|n) :
n
The elements in the single linked lists are : 10-->20-->30-->NULL

SRKR Engineering College (Autonomous)


Exp. Name: Write a C program to
Date: 2024-12-

Page No: 91
S.No: 40 demonstrate the differences between
18
Structures and Unions

Aim:

ID: 24B91A05R0
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
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

2024-2028-CSE-D
12. Find and display total and average marks of student-2
13. Display the size of union student-2
Sample Input and Output:
Student 1 rollno and marks: 101 76 58 67
Total and average: 201 67.000000
Student 1 struct size: 24
Student 2 rollno: 102

SRKR Engineering College (Autonomous)


Student 2 marks 1: 76
Student 2 marks 2: 87
Student 2 marks 3: 69
Student 2 total marks: 232
Student 2 average marks: 77.333336
Student 2 union size: 4
Source Code:

StructureAndUnion.c
#include<stdio.h>

Page No: 92
//void main(){
struct student1{
int rollno,m1,m2,m3,total;
float avg;
};

ID: 24B91A05R0
union student2{
int rollno;
int a1;
int a2;
int a3;
int total1;
float avg2;
};
int main(){
struct student1 s1;
printf("Student 1 rollno and marks: ");

2024-2028-CSE-D
scanf("%d%d%d%d",&s1.rollno,&s1.m1,&s1.m2,&s1.m3);
s1.total=s1.m1+s1.m2+s1.m3;
s1.avg=s1.total/3.0;
printf("Total and average: %d %.6f\n",s1.total,s1.avg);
printf("Student 1 struct size: %lu\n",sizeof(s1));
union student2 s2;
int total,m1,m2,m3;

SRKR Engineering College (Autonomous)


float avg;
/*printf("Student 2 rollno: ");
scanf("%d",&s2.rollno);
printf("Student 2 marks 1: ");
scanf("%d",&s2.a1);
printf("Student 2 marks 2: ");
scanf("%d",&s2.a2);
printf("Student 2 marks 3: ");
scanf("%d",&s2.a3);
s2.total1=s2.a1 + s2.a2 + s2.a3;
s2.avg2=s2.total/3.0;
printf("Student 2 total marks: %d\n",s2.total1);
printf("Student 2 average marks: %.6f\n",s2.avg2);
printf("Student 2 union size: %lu\n",sizeof(s2));*/
printf("Student 2 rollno: ");
scanf("%d",&s2.rollno);
printf("Student 2 marks 1: ");
scanf("%d",&m1);
printf("Student 2 marks 2: ");
scanf("%d",&m2);
printf("Student 2 marks 3: ");
scanf("%d",&m3);
total=m1+m2+m3;

Page No: 93
avg=total/3.0;
printf("Student 2 total marks: %d\n",total);
printf("Student 2 average marks: %.6f\n",avg);
printf("Student 2 union size: %lu\n",sizeof(s2));
return 0;

ID: 24B91A05R0
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Student 1 rollno and marks:

2024-2028-CSE-D
101 76 58 67
Total and average: 201 67.000000
Student 1 struct size: 24
Student 2 rollno:
102
Student 2 marks 1:
76

SRKR Engineering College (Autonomous)


Student 2 marks 2:
87
Student 2 marks 3:
69
Student 2 total marks: 232
Student 2 average marks: 77.333336
Student 2 union size: 4

Test Case - 2

User Output
Student 1 rollno and marks:
105 66 65 68
Total and average: 199 66.333336
Student 1 struct size: 24
Student 2 rollno:
106
Student 2 marks 1:
Student 2 marks 2:
89

Page No: 94
Student 2 marks 3:
79
Student 2 total marks: 256
Student 2 average marks: 85.333336

ID: 24B91A05R0
Student 2 union size: 4

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: Demonstrate left shift Date: 2024-12-
S.No: 41

Page No: 95
operation 17

Aim:
Write a C program to demonstrate left shift operation

ID: 24B91A05R0
Source Code:

shift.c

#include<stdio.h>
#include<string.h>
char val[10]={"0000"};
void conv(int n){
if(n>1)
conv(n/2);
printf("%d",n%2);

2024-2028-CSE-D
}
void main(){
int n,d;
printf("Enter an integer: ");
scanf("%d",&n);
printf("Original value: ");
conv(n);

SRKR Engineering College (Autonomous)


printf("\nnumber of bits to left shift: ");
scanf("%d",&d);
int shift=n<<d;
printf("After left shift: %d\n",shift);
printf("Binary representation:");
conv(shift);
}

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
Binary representation:110000

Page No: 96
Test Case - 2

User Output
Enter an integer:

ID: 24B91A05R0
5
Original value: 101
number of bits to left shift:
3
After left shift: 40
Binary representation:101000

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: Copy the contents of one
Date: 2024-11-

Page No: 97
S.No: 42 structure variable to another structure
23
variable

Aim:

ID: 24B91A05R0
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 same to another structure variable, finally display the
copied data.

Note: Driver code is provided to you in the CopyStructureMain.c file. You need to fill the
missing code in CopyStructureFunctions.c

2024-2028-CSE-D
Source Code:

CopyStructureMain.c

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

SRKR Engineering College (Autonomous)


int main() {
struct student s1, s2;
read1(&s1);
s2 = copyStructureVariable(s1, s2);
display1(s2);
}

CopyStructureFunctions.c
#include<stdio.h>

Page No: 98
#include<string.h>
struct student {
char name[50];
int age;
float height;

ID: 24B91A05R0
};
struct student s1;
void read1(struct student *s1){
printf("Enter student name, age and height: ");
scanf("%s",s1->name);
scanf("%d",&s1->age);
scanf("%f",&s1->height);
}
struct student copyStructureVariable(struct student s1,struct student
s2){
strcpy(s2.name, s1.name);

2024-2028-CSE-D
s2.age = s1.age;
s2.height = s1.height;
return s2;
}
void display1(struct student s){
printf("Student name: %s\n",s.name);
printf("Age: %d\n",s.age);

SRKR Engineering College (Autonomous)


printf("Height: %.6f\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
Student name: Yamuna
Age: 19
Height: 5.200000

Test Case - 2

User Output
Age: 21
Kohli 21 5.11

Height: 5.110000
Student name: Kohli

SRKR Engineering College (Autonomous) 2024-2028-CSE-D ID: 24B91A05R0 Page No: 99


Exp. Name: Write a C Program to find Date: 2024-11-
S.No: 43

Page No: 100


nCr using Factorial recursive function 30

Aim:
Draw the flowchart and write a recursive C function to find the factorial of a number, n! ,

ID: 24B91A05R0
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 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:

2024-2028-CSE-D
Enter the values of n and r : 4 2

then the program should print the result as:

The value of 4c2 = 6

If the input is given as 2 and 5 then the program should print the result as:

SRKR Engineering College (Autonomous)


Enter valid input data

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


Source Code:

Lab14a.c

#include<stdio.h>
int factorial(int n){
if(n==0){
return 1;
}
else{
return n*factorial(n-1);
}
}

Lab14.c
#include <stdio.h>

Page No: 101


#include "Lab14a.c"
void main() {
int n, r;
printf("Enter the values of n and r : ");
scanf("%d %d", &n, &r);
if (n >= r)

ID: 24B91A05R0
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!

2024-2028-CSE-D
Test Case - 1

User Output
Enter the values of n and r :
10 4
The value of 10c4 = 210

SRKR Engineering College (Autonomous)


Test Case - 2

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

Test Case - 3

User Output
Enter the values of n and r :
52
The value of 5c2 = 10
Exp. Name: Write a Program to find the Date: 2024-11-
S.No: 44

Page No: 102


Length of a String 30

Aim:
Write a C program to find the length of a given string.

ID: 24B91A05R0
Sample Input and Output - 1:

Enter the string : CodeTantra


Length of CodeTantra : 10

Source Code:

StrLength.c

#include <stdio.h>

2024-2028-CSE-D
#include "StrLength1.c"
void main() {
char str[30];
printf("Enter the string : ");
scanf("%s", str);
printf("Length of %s : %d\n", str, myStrLen(str));
}

SRKR Engineering College (Autonomous)


StrLength1.c

int myStrLen(char s[])


{
int i=0,l=0;
while(s[i]!='\0')
{
i++;
l++;

}
return l;
}

Execution Results - All test cases have succeeded!


Test Case - 1
User Output

Page No: 103


Enter the string :
CodeTantra
Length of CodeTantra : 10

ID: 24B91A05R0
Test Case - 2

User Output
Enter the string :
IndoUsUk
Length of IndoUsUk : 8

Test Case - 3

2024-2028-CSE-D
User Output
Enter the string :
MalayalaM
Length of MalayalaM : 9

Test Case - 4

SRKR Engineering College (Autonomous)


User Output
Enter the string :
Oh!MyGod
Length of Oh!MyGod : 8
Date: 2024-12-
S.No: 45 Exp. Name: Transpose using functions.

Page No: 104


18

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

ID: 24B91A05R0
Input Format
• First Line: The user will input the number of rows for the matrix.
• Second Line: The user will input the number of columns for the matrix.
• Subsequent Lines: The user will input the matrix elements row by row.

Output Format
• First Line: The program will print the matrix in its original form.
• Second Line: The program will print the transpose of the matrix.
Source Code:

2024-2028-CSE-D
transpose.c

SRKR Engineering College (Autonomous)


#include <stdio.h>

Page No: 105


int rows=5, cols=5;
void readMatrix(int matrix[5][5])
{
printf("Elements:\n");
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)

ID: 24B91A05R0
scanf("%d",&matrix[i][j]);
}
void printMatrix(int matrix[5][5])
{
printf("Matrix:\n");
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
printf("%d ",matrix[i][j]);
printf("\n");

2024-2028-CSE-D
}
}
void transposeMatrix(int matrix[5][5])
{
int a[5][5];
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)

SRKR Engineering College (Autonomous)


a[j][i]=matrix[i][j];
printf("Transpose:\n");
for(int i=0;i<cols;i++)
{
for(int j=0;j<rows;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}

int main() {
printf("rows: ");
scanf("%d", &rows);
printf("columns: ");
scanf("%d", &cols);
int matrix[rows][cols];
// Input: Read the matrix elements

Page No: 106


readMatrix(matrix);

// Print the original matrix


printMatrix(matrix);

// Print the transpose of the matrix

ID: 24B91A05R0
transposeMatrix(matrix);

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

2024-2028-CSE-D
User Output
rows:
2
columns:
2

SRKR Engineering College (Autonomous)


Elements:
89
65
Matrix:
8 9
6 5
Transpose:
8 6
9 5

Test Case - 2

User Output
rows:
1
columns:
2
Elements:
9
6
6 9
Matrix:

Transpose:

SRKR Engineering College (Autonomous) 2024-2028-CSE-D ID: 24B91A05R0 Page No: 107
Exp. Name: Demonstrate numerical
Date: 2024-12-

Page No: 108


S.No: 46 integration of differential equations
17
using Euler’s method

Aim:

ID: 24B91A05R0
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.

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

2024-2028-CSE-D
equation.

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

Source Code:

SRKR Engineering College (Autonomous)


euler.c
#include <stdio.h>

Page No: 109


void main(){
float y0,t0,h,t,x0;
printf("initial value of y (y0): ");
scanf("%f",&y0);
printf("initial value of t (t0): ");
scanf("%f",&t0);

ID: 24B91A05R0
printf("step size (h): ");
scanf("%f",&h);
printf("end value for t: ");
scanf("%f",&t);
x0=t0;
while(t0<t){
printf("t = %.2f y = %.2f\n",t0,y0);
t0+=h;
y0=y0+h*(x0*y0);
x0=x0+h;

2024-2028-CSE-D
}
}

/*double f(double y, double t) {

// Example: dy/dt = t*y


}

SRKR Engineering College (Autonomous)


// Euler's method for numerical integration
void eulerIntegration( ) {

int main() {

}*/

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
initial value of y (y0):
1
initial value of t (t0):
1

Page No: 110


step size (h):
3
end value for t:
10

ID: 24B91A05R0
t = 1.00 y = 1.00
t = 4.00 y = 4.00
t = 7.00 y = 52.00

Test Case - 2

User Output
initial value of y (y0):
1

2024-2028-CSE-D
initial value of t (t0):
1
step size (h):
3
end value for t:
3

SRKR Engineering College (Autonomous)


t = 1.00 y = 1.00
Exp. Name: Fibonacci series up to the Date: 2024-12-
S.No: 47

Page No: 111


given number of terms using Recursion 17

Aim:
Write a program to display the fibonacci series up to the given number of terms using
recursion process.

ID: 24B91A05R0
Source Code:

fibonacciSeries.c

#include <stdio.h>
#include "fibonacciSeriesa.c"
void main() {
int n, i;
printf("n: ");
scanf("%d", &n);

2024-2028-CSE-D
printf("%d terms: ", n);
for (i = 0; i < n; i++) {
printf("%d ", fib(i));
}
}

SRKR Engineering College (Autonomous)


fibonacciSeriesa.c

// Complete the function fib()....


int fib(int i){
if(i==0){
return 0;
}
else if(i==1){
return 1;
}
else{
return fib(i-1)+fib(i-2);
}

Execution Results - All test cases have succeeded!


Test Case - 1
User Output

Page No: 112


n:
4
4 terms: 0 1 1 2

ID: 24B91A05R0
Test Case - 2

User Output
n:
10
10 terms: 0 1 1 2 3 5 8 13 21 34

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: LCM of two numbers using Date: 2024-12-
S.No: 48

Page No: 113


recursion 07

Aim:
Write a program to find the lcm (Least Common Multiple) of a given two numbers using

ID: 24B91A05R0
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.

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:

2024-2028-CSE-D
Enter two integer values : 25 15

then the program should print the result as:

The lcm of two numbers 25 and 15 = 75

Note: Write the function lcm() and recursive function gcd() in Program907a.c .

SRKR Engineering College (Autonomous)


Source Code:

Program907.c

#include <stdio.h>
#include "Program907a.c"
void main() {
int a, b;
printf("Enter two integer values : ");
scanf("%d %d", &a, &b);
printf("The lcm of two numbers %d and %d = %d\n", a, b, lcm(a,
b));
}

Program907a.c
int lcm(int a,int b){

Page No: 114


static int common=1;
if(common%a==0 && common%b==0){
return common;
}
common++;
return lcm(a,b);

ID: 24B91A05R0
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output

2024-2028-CSE-D
Enter two integer values :
34 24
The lcm of two numbers 34 and 24 = 408

Test Case - 2

User Output

SRKR Engineering College (Autonomous)


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

Test Case - 3

User Output
Enter two integer values :
345 467
The lcm of two numbers 345 and 467 = 161115

Test Case - 4

User Output
Enter two integer values :
100 88
The lcm of two numbers 100 and 88 = 2200
Test Case - 5

Page No: 115


User Output
Enter two integer values :
123 420
The lcm of two numbers 123 and 420 = 17220

ID: 24B91A05R0
2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: Write a C program to find
Date: 2024-12-

Page No: 116


S.No: 49 the Factorial of a given number using
07
Recursion

Aim:

ID: 24B91A05R0
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:

Program901.c

#include <stdio.h>
#include "Program901a.c"
void main() {
long int n;

2024-2028-CSE-D
printf("Enter an integer : ");
scanf("%ld", &n);
printf("Factorial of %ld is : %ld\n", n ,factorial(n));
}

Program901a.c

SRKR Engineering College (Autonomous)


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

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter an integer :
5
Factorial of 5 is : 120
Test Case - 2

Page No: 117


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

ID: 24B91A05R0
Test Case - 3

User Output
Enter an integer :
8
Factorial of 8 is : 40320

2024-2028-CSE-D
Test Case - 4

User Output
Enter an integer :
0
Factorial of 0 is : 1

SRKR Engineering College (Autonomous)


Exp. Name: Ackermann function using Date: 2024-12-
S.No: 50

Page No: 118


Recursion 17

Aim:
Write a program to implement Ackermann function using recursion process.

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

Enter two numbers :

For example, if the user gives the input as:

Enter two numbers : 2 1

then the program should print the result as:

2024-2028-CSE-D
A(2, 1) = 5

Source Code:

AckermannFunction.c

SRKR Engineering College (Autonomous)


#include <stdio.h>
#include "AckermannFunction1.c"
void main() {
long long int m, n;
printf("Enter two numbers : ");
scanf("%lli %lli", &m, &n);
printf("A(%lli, %lli) = %lli\n", m, n, ackermannFun(m, n));
}

AckermannFunction1.c
long long int ackermannFun(int m,int n){

Page No: 119


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

ID: 24B91A05R0
else{
return ackermannFun(m-1,ackermannFun(m,n-1));
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

2024-2028-CSE-D
User Output
Enter two numbers :
01
A(0, 1) = 2

SRKR Engineering College (Autonomous)


Test Case - 2

User Output
Enter two numbers :
22
A(2, 2) = 7

Test Case - 3

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

Test Case - 4

User Output
11
A(1, 1) = 3

Page No: 120


Test Case - 5

User Output

ID: 24B91A05R0
Enter two numbers :
10
A(1, 0) = 2

Test Case - 6

User Output
Enter two numbers :
23

2024-2028-CSE-D
A(2, 3) = 9

SRKR Engineering College (Autonomous)


Date: 2024-12-
S.No: 51 Exp. Name: Problem solving

Page No: 121


17

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

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

Enter value of n :

For example, if the user gives the input as:

Enter value of n : 6

then the program should print the result as:

2024-2028-CSE-D
Sum of 6 natural numbers = 21

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


Source Code:

Program903.c

SRKR Engineering College (Autonomous)


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

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

Page No: 122


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

ID: 24B91A05R0
Test Case - 2

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

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: Write a C program to Swap
Date: 2024-12-

Page No: 123


S.No: 52 two values by using Call-by-Address
17
method

Aim:

ID: 24B91A05R0
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 :

For example, if the user gives the input as:

Enter two integer values : 12 13

then the program should print the result as:

2024-2028-CSE-D
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 ( \n ).

SRKR Engineering College (Autonomous)


Source Code:

Program1002.c

#include <stdio.h>
#include "Program1002a.c"
void main() {
int a, b;
printf("Enter two integer values : ");
scanf("%d %d", &a, &b);
printf("Before swapping in main : a = %d b = %d\n", a, b);
swap(&a, &b);
printf("After swapping in main : a = %d b = %d\n", a, b);
}

Program1002a.c
void swap(int *a,int *b){

Page No: 124


int t=*a;
*a=*b;
*b=t;
printf("After swapping in swap : *p = %d *q = %d\n",*a,*b);
}

ID: 24B91A05R0
Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Enter two integer values :
121 131

2024-2028-CSE-D
Before swapping in main : a = 121 b = 131
After swapping in swap : *p = 131 *q = 121
After swapping in main : a = 131 b = 121

Test Case - 2

SRKR Engineering College (Autonomous)


User Output
Enter two integer values :
555 999
Before swapping in main : a = 555 b = 999
After swapping in swap : *p = 999 *q = 555
After swapping in main : a = 999 b = 555

Test Case - 3

User Output
Enter two integer values :
1001 101
Before swapping in main : a = 1001 b = 101
After swapping in swap : *p = 101 *q = 1001
After swapping in main : a = 101 b = 1001

Test Case - 4
Enter two integer values :
9999 2999

Page No: 125


Before swapping in main : a = 9999 b = 2999
After swapping in swap : *p = 2999 *q = 9999
After swapping in main : a = 2999 b = 9999

ID: 24B91A05R0
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

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Date: 2024-12-
S.No: 53 Exp. Name: Dangling Pointers

Page No: 126


17

Aim:
Demonstrate Dangling pointer problem using a C program.

ID: 24B91A05R0
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

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
#include <stdio.h>

Page No: 127


#include <stdlib.h>

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

ID: 24B91A05R0
// Allocate memory for an integer
ptr1=(int*)malloc(sizeof(int));
ptr2=(int*)malloc(sizeof(int));

// Input the integer value


printf("Enter an integer value: ");
scanf("%d",&value);

// Assign the input value to the allocated memory

2024-2028-CSE-D
ptr1=&value;

// Point ptr2 to the same memory location as ptr1


ptr2=ptr1;

SRKR Engineering College (Autonomous)


// 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


//free(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

Page No: 128


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

ID: 24B91A05R0
Test Case - 2

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

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: Write a C program to Copy Date: 2024-11-
S.No: 54

Page No: 129


one String into another using Pointers 23

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

ID: 24B91A05R0
Sample Input and Output:
Enter source string : Robotic Tool
Target string : Robotic Tool
Source Code:

CopyStringPointers.c

#include <stdio.h>
#include "CopyStringPointers1.c"
void main() {

2024-2028-CSE-D
char source[100], target[100];
printf("Enter source string : ");
fgets(source, sizeof(source), stdin);
copyString(target, source);
printf("Target string : %s\n", target);
}

SRKR Engineering College (Autonomous)


CopyStringPointers1.c

#include<stdio.h>
void copyString(char *target, char *source){
while(*source != '\0'){
*target = *source;
target++;
source++;
}
*target = '\0';
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter source string :
CodeTantra
Target string : CodeTantra

Page No: 130


Test Case - 2

User Output

ID: 24B91A05R0
Enter source string :
Robotic Tool
Target string : Robotic Tool

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: Write a C program to Count
Date: 2024-12-

Page No: 131


S.No: 55 number of Lowercase, Uppercase, digits
17
and Other Characters using Pointers

Aim:

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

Sample Input and Output:

Enter a string : Indo Pak 125 143 *.$


Number of uppercase letters = 2
Number of lowercase letters = 5
Number of digits = 6
Number of other characters = 7

2024-2028-CSE-D
Source Code:

CountCharDigitOthers.c

#include <stdio.h>
#include "CountCharDigitOthers1.c"
void main() {

SRKR Engineering College (Autonomous)


char str[80];
int upperCount = 0, lowerCount = 0, digitCount = 0, otherCount
= 0;
printf("Enter a string : ");
gets(str);
countCharDigitOthers(str, &upperCount, &lowerCount,
&digitCount, &otherCount);
printf("Number of uppercase letters = %d\n", upperCount);
printf("Number of lowercase letters = %d\n", lowerCount);
printf("Number of digits = %d\n", digitCount);
printf("Number of other characters = %d\n", otherCount);
}

CountCharDigitOthers1.c
void countCharDigitOthers(char str[80],int *upperCount,int

Page No: 132


*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')

ID: 24B91A05R0
*upperCount=*upperCount+1;
else if(str[i]>='0' && str[i]<='9')
*digitCount=*digitCount+1;
else
*otherCount=*otherCount+1;
}
}

2024-2028-CSE-D
Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Enter a string :
CodeTantra123&*@987.

SRKR Engineering College (Autonomous)


Number of uppercase letters = 2
Number of lowercase letters = 8
Number of digits = 6
Number of other characters = 4

Test Case - 2

User Output
Enter a string :
Indo Pak 125 143 *.$
Number of uppercase letters = 2
Number of lowercase letters = 5
Number of digits = 6
Number of other characters = 7

Test Case - 3

User Output
Enter a string :
12345

Page No: 133


Number of uppercase letters = 0
Number of lowercase letters = 0
Number of digits = 5
Number of other characters = 0

ID: 24B91A05R0
2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Date: 2024-12-
S.No: 56 Exp. Name: Write the code

Page No: 134


17

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

ID: 24B91A05R0
Source Code:

readFilePrint.c

#include <stdio.h>
void main(){
char fname[100],ch;
FILE *fp;
printf("Enter the name of the file to read: ");
scanf("%s",fname);

2024-2028-CSE-D
fp=fopen(fname,"r");
printf("Content of the file %s:\n",fname);
while(!feof(fp)){
ch=fgetc(fp);
if(feof(fp))
break;
printf("%c",ch);

SRKR Engineering College (Autonomous)


}
}

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.
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

Page No: 135


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.

ID: 24B91A05R0
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.

2024-2028-CSE-D
Her husbands shop was closed. She had no idea what to do.

Execution Results - All test cases have succeeded!


Test Case - 1

SRKR Engineering College (Autonomous)


User Output
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.
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
Content of the file file2.txt:

Page No: 136


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.

ID: 24B91A05R0
One day the poor friend really needed money, and he thought that he
would ask his friend.

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: C program to write and read
Date: 2024-12-

Page No: 137


S.No: 57 text into a binary file using fread() and
18
fwrite()

Aim:

ID: 24B91A05R0
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:

FilesStructureDemo1.c

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
#include<stdio.h>

Page No: 138


struct student {
int roll;
char name[25];
float marks;
};
void main() {

ID: 24B91A05R0
FILE *fp;
char ch;
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

2024-2028-CSE-D
printf("Marks: ");
scanf("%f",&s.marks); // Complete the statement
fwrite(&s,sizeof(s),1,fp); // Complete the statement
printf("Want to add another data (y/n): ");
scanf(" %c", &ch);
}while (ch=='y' || ch=='Y'); // Complete the condition
printf("Data written successfully\n");

SRKR Engineering College (Autonomous)


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:

Page No: 139


92
Want to add another data (y/n):
y
Roll no:

ID: 24B91A05R0
502
Name:
Smith
Marks:
65
Want to add another data (y/n):
n
Data written successfully
Roll Name Marks

2024-2028-CSE-D
501 Ganga 92.000000
502 Smith 65.000000

SRKR Engineering College (Autonomous)


Exp. Name: Copy contents of one file Date: 2024-12-
S.No: 58

Page No: 140


into another file. 17

Aim:
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:

ID: 24B91A05R0
SampleTextFile2.txt) and copies the contents of the first file to the second file. Finally, it
should display the contents of the second file.

Input Format:
The program prompts the user to enter text, which can include any characters. The input
ends when the user types the character @.

Output Format:
The program outputs the copied text from the second file in the following format:
"Copied text is : [text]" where [text] is the content copied from the first text file.

2024-2028-CSE-D
Source Code:

copy.c

SRKR Engineering College (Autonomous)


#include <stdio.h>

Page No: 141


void main() {
FILE *fp, *fp1, *fp2;
char ch;

fp = fopen("one.txt","w"); // write the missing code

ID: 24B91A05R0
printf("Enter the text with @ at end : ");
while ((ch = getchar()) != '@') {
putc(ch, fp);
}
putc(ch, fp);
fclose(fp);
fp1=fopen("one.txt","r");
fp2=fopen("two.txt","w");
printf("Copied text is : ");

2024-2028-CSE-D
while(!feof(fp1)){
fscanf(fp1,"%c",&ch);
if(feof(fp1)||ch=='@')
break;
fprintf(fp2,"%c",ch);
printf("%c",ch);
}

SRKR Engineering College (Autonomous)


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: Merge two files and store
Date: 2024-12-

Page No: 142


S.No: 59 their contents in another file using
18
command-line arguments

Aim:

ID: 24B91A05R0
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
• 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

2024-2028-CSE-D
• 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:

SRKR Engineering College (Autonomous)


MergeFilesArgs.c
#include <stdio.h>

Page No: 143


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
printf("Enter the text with @ at end for file-1 :\n");
while ((ch=getchar())!='@') { // Write the condition

ID: 24B91A05R0
fputc(ch, fp1);
}
fputc(ch, fp1);
fclose(fp1);
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
fputc(ch, fp2);
}
fputc(ch, fp2);

2024-2028-CSE-D
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 ((ch=fgetc(fp1))!='@') { // Repeat loop till get @ at the
end of existed file
fputc(ch, fp3);

SRKR Engineering College (Autonomous)


}
fclose(fp1); // Close the first existed file
fp2 = fopen( argv[2], "r"); // Open a secong existed file in
read mode
while ((ch=fgetc(fp2))!='@') { // Repeat loop till get @ at the
end of existed file
fputc(ch, fp3);
}
fputc(ch, fp3);
fclose(fp2);
fclose(fp3);
fp3 = fopen(argv[3] , "r"); // Open the merged file in read
mode
printf("Merged text is : ");
while ((ch=fgetc(fp3))!='@') { // Repeat loop till get @ at the
end of merged file
putchar(ch);
}
printf("\n");
fclose(fp3); // Close the merged file
}
Execution Results - All test cases have succeeded!

Page No: 144


Test Case - 1

User Output
Enter the text with @ at end for file-1 :
This is CodeTantra

ID: 24B91A05R0
They implemented automatic
robotic tool@
Enter the text with @ at end for file-2 :
Started the company in
2014@
Merged text is : This is CodeTantra
They implemented automatic robotic tool
Started the company in

2024-2028-CSE-D
2014

Test Case - 2

User Output
Enter the text with @ at end for file-1 :

SRKR Engineering College (Autonomous)


Best
Fair
Awesome@
Enter the text with @ at end for file-2 :
False@
Merged text is : Best
Fair
Awesome
False
Exp. Name: Write a C program to Count
Date: 2024-12-

Page No: 145


S.No: 60 number of Characters, Words and Lines
18
of a given File

Aim:

ID: 24B91A05R0
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
• 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

2024-2028-CSE-D
SRKR Engineering College (Autonomous)
#include <stdio.h>

Page No: 146


void main() {
FILE *fp;
char ch;
int charCount = 0, wordCount = 0, lineCount = 0;
fp = fopen("DemoTextFile2.txt", "w"); // Open a new file in
write mode

ID: 24B91A05R0
printf("Enter the text with @ at end : ");
while ((ch=getchar())!='@') { // Repeat loop till read @ at the
end
fputc(ch,fp); // Put read character onto the file
}
fputc(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 {

2024-2028-CSE-D
ch=fgetc(fp);
if (ch== '\n'|| ch==' '|| ch=='\t'|| ch=='\0') // Write
the condition to count words
wordCount++;
else
charCount++;
if (ch== '\n'|| ch== '\0') // Write the condition to

SRKR Engineering College (Autonomous)


count lines
lineCount++;
} while (!feof(fp));// Repeat loop till read @ at the end
if(charCount>0)
{
charCount-=2;wordCount++;lineCount++;
}
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

Page No: 147


the goal is reached@
Total characters : 43
Total words : 10
Total lines : 3

ID: 24B91A05R0
Test Case - 2

User Output
Enter the text with @ at end :
Believe in your self
and the world will be
at your feet@
Total characters : 44

2024-2028-CSE-D
Total words : 12
Total lines : 3

SRKR Engineering College (Autonomous)


Exp. Name: Print the last n characters of Date: 2024-12-
S.No: 61

Page No: 148


a file by reading the file 17

Aim:
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.

ID: 24B91A05R0
Source Code:

file.c

#include<stdio.h>
void main(int argc,char *argv[]){
FILE *fp;
int n;
char ch;
if(argc !=3){

2024-2028-CSE-D
printf("Invalid arguments");
}
else{
n=atoi(argv[2]);
fp=fopen(argv[1],"r");
fseek(fp,-n,2);
while(!feof(fp)){

SRKR Engineering College (Autonomous)


ch=fgetc(fp);
if(feof(fp))
break;
printf("%c",ch);
}
}
}

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.

Page No: 149


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.

ID: 24B91A05R0
Everything matters.

test1.txt

CodeTantra
Start coding in 60 mins

test2.txt

2024-2028-CSE-D
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

test3.txt

SRKR Engineering College (Autonomous)


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!


Test Case - 1

User Output
good idea.

Test Case - 2

User Output
verything matters.

You might also like