Aim:
Page No: 1
Fill in the missing code to swap two integer values with using a third variable.
Note: Use the printf() function with a newline character (\n) at the end.
ID: 92132313206
Instruction: Strictly follow the input and output layout mentioned in the visible test cases.
Source Code:
Swapv.c
#include <stdio.h>
int main() {
int a, b, temp;
// Fill the missing code
printf("Enter two integers : ");
scanf("%d %d", &a, &b);
printf("Before swapping a = %d, b = %d\n",a,b);
2023-2027-CSE-D
temp=a;
a=b;
b=temp;
return 0;
}
User Output
Enter two integers :
27 26
Before swapping a = 27, b = 26
After swapping a = 26, b = 27
Test Case - 2
User Output
Enter two integers :
58 75
Before swapping a = 58, b = 75
After swapping a = 75, b = 58
Exp. Name: C program to perform arithmetic
S.No: 2 Date: 2024-03-12
operations using switch
Aim:
Page No: 2
Write a C program to create a simple calculator to perform addition (+), multiplication (*), subtraction (-)
and division (/) using switch case condition.
ID: 92132313206
arithmetic.c
#include<stdio.h>
int main()
{
float num1,num2, result;
char op;
printf("Enter two numbers: ");
scanf("%f %f",&num1, &num2);
printf("choose an operator (+,-,*,/): ");
scanf(" %c", &op);
switch(op) {
2023-2027-CSE-D
case'+':
result = num1+num2;
printf("%.2f + %.2f = %.2f\n",num1, num2, result);
break;
case '-':
result = num1 - num2;
printf("%.2f - %.2f = %.2f\n", num1, num2, result);
break;
User Output
Enter two numbers:
10 20
choose an operator (+,-,*,/):
+
10.00 + 20.00 = 30.00
Page No: 3
Test Case - 2
User Output
Enter two numbers:
ID: 92132313206
45
choose an operator (+,-,*,/):
*
4.00 * 5.00 = 20.00
Test Case - 3
User Output
Enter two numbers:
84
2023-2027-CSE-D
choose an operator (+,-,*,/):
?
Invalid operator
Aim:
Write a C program that demonstrates the use of relational operators (<, <=, >, >=, ==, !=). Prompt the user
Page No: 4
to enter two integer values and then use relational operators to compare the numbers and print the results.
Input Format
The program prompts the user to enter two integers.
ID: 92132313206
Output Format
The program prints the result of various comparison operations between the two integers.
Source Code:
relationalOperators.c
#include<stdio.h>
int main()
{
int a,b;
printf("Enter two numbers: ");
scanf("%d %d",&a,&b);
printf("%d < %d: %s",a,b,(a<b)?"True":"False");
2023-2027-CSE-D
printf("\n%d <= %d: %s",a,b,(a<=b)?"True":"False");
printf("\n%d > %d: %s",a,b,(a>b)?"True":"False");
printf("\n%d >= %d: %s",a,b,(a>=b)?"True":"False");
printf("\n%d == %d: %s",a,b,(a==b)?"True":"False");
printf("\n%d != %d: %s",a,b,(a!=b)?"True":"False");
printf("\n");
return 0;
}
User Output
Enter two numbers:
5 10
5 < 10: True
5 <= 10: True
5 > 10: False
5 >= 10: False
5 == 10: False
5 != 10: True
Test Case - 2
User Output
Enter two numbers:
10 5
10 < 5: False
10 <= 5: False
10 > 5: True
10 != 5: True
10 >= 5: True
10 == 5: False
PSNA College of Engineering and Technology 2023-2027-CSE-D ID: 92132313206 Page No: 5
S.No: 4 Exp. Name: Arithmetic expression Date: 2024-03-06
Aim:
Write a C program to perform arithmetic expression
Page No: 6
Source Code:
arithmetic_exoression.c
#include<stdio.h>
ID: 92132313206
int main()
{
int a,b;
float c;
printf("First number: ");
scanf("%d",&a);
printf("Second number: ");
scanf("%d",&b);
printf("Sum: %d\n",a+b);
printf("Difference: %d\n",a-b);
printf("Product: %d\n",a*b);
c=(float)a/b;
printf("Quotient: %.2f\n",c);
2023-2027-CSE-D
printf("Remainder: %d\n",a%b);
return 0;
}
Execution Results - All test cases have succeeded! PSNA College of Engineering and Technology
Test Case - 1
User Output
First number:
10
Second number:
2
Sum: 12
Difference: 8
Product: 20
Quotient: 5.00
Remainder: 0
Test Case - 2
User Output
First number:
25
Second number:
5
Page No: 7
Sum: 30
Difference: 20
Product: 125
Quotient: 5.00
ID: 92132313206
Remainder: 0
Test Case - 3
User Output
First number:
30
Second number:
6
Sum: 36
Difference: 24
2023-2027-CSE-D
Product: 180
Quotient: 5.00
Remainder: 0
Aim:
Tina's brother gave her a fun mathematical challenge involving a chessboard-like grid. The task is to
Page No: 8
determine the total number of squares that can be found in an n × n board, where each square is
1 cm × 1 cm. However, Tina needs to count not only the smallest squares but also the larger squares that
can be formed on the board.
For example, on a 3 × 3 board, there are 9 squares of size 1 × 1, 4 squares of size 2 × 2, and 1 square of
ID: 92132313206
size 3 × 3, resulting in a total of 14 squares.
Your task is to help Tina calculate the total number of squares of all possible sizes on the board.
Constraints
• 2 ≤ n ≤ 20
Input Format
• The input consists of a single integer n, representing the size of the board.
Output Format
• Print the total number of squares in the n × n board.
2023-2027-CSE-D
Formula Hint
The formula to calculate the total number of squares on an n × n board is given by:
n × (n + 1) × (2n + 1)
Total squares =
6
Source Code:
#include<stdio.h>
void main()
{
int n,c;
scanf("%d",&n);
c=(n*(n+1)*(2*n+1))/6;
printf("%d",c);
}
User Output
2
5
Test Case - 2
User Output
4
30
Page No: 9
Test Case - 3
User Output
ID: 92132313206
6
91
Test Case - 4
User Output
8
204
2023-2027-CSE-D
PSNA College of Engineering and Technology
S.No: 6 Exp. Name: Volume of the Ball Date: 2024-03-06
Aim:
Laasya recently bought a new volleyball from the sports shop. The ball appears to be medium-sized, and
Page No: 10
she is curious about its volume. Fortunately, she managed to measure the radius r of the ball accurately.
Now, she wants to calculate the volume V of the spherical ball based on the radius. As a friend, you decide
to help her find the volume using the mathematical formula for the volume of a sphere. Your task is to
compute the volume of the volleyball and display the result.
ID: 92132313206
The volume of a sphere can be calculated using the formula:
V = ( 4.0
3.0 ) × π × (r × r × r)
Take π = 3.14
Constraint:
1.00 ≤ r ≤ 5.00
Input Format:
The input consists of a single float value representing the radius of the ball.
Output Format:
The program should output the volume rounded to six decimal places.
2023-2027-CSE-D
Source Code:
volumeOfBall.c
#include<stdio.h>
#include<math.h>
void main()
{
float a,v;
User Output
1.72
21.303637
Test Case - 2
User Output
4.5
381.510010
1.5
3.3
14.130000
150.456238
User Output
User Output
Test Case - 3
Test Case - 4
PSNA College of Engineering and Technology 2023-2027-CSE-D ID: 92132313206 Page No: 11
S.No: 7 Exp. Name: Delay Time in Hours and Minutes Date: 2024-03-12
Aim:
Binita recently traveled from Chennai to Delhi on the Rajdhani Express. Unfortunately, the train arrived at
Page No: 12
the destination later than expected, resulting in a delay. Binita wants to find out the exact duration of the
delay in terms of hours and minutes.
She only knows the total delay time in minutes tot_mins, and she needs to convert this value into an
equivalent format showing both hours and minutes.
ID: 92132313206
Your task is to help Binita calculate the delay in the format:
{Hours} Hours and {Minutes} Minutes
Constraints
• 100 ≤ tot_mins ≤ 550
Input Format
• The input consists of a single integer, tot_mins, representing the total delay of the train in minutes.
Output Format
• Print the delay in the format: {Hours} Hours and {Minutes} Minutes.
Source Code:
2023-2027-CSE-D
journey.c
#include<stdio.h>
int main()
{
int s;
scanf("%d",&s);
User Output
540
9 Hours and 0 Minutes
Test Case - 2
User Output
240
4 Hours and 0 Minutes
Test Case - 3
User Output
325
293
User Output
Test Case - 4
PSNA College of Engineering and Technology 2023-2027-CSE-D ID: 92132313206 Page No: 13
S.No: 8 Exp. Name: Even or odd Date: 2024-03-06
Aim:
Write a C program to check whether the entered number is even or odd
Page No: 14
Source Code:
evenodd.c
#include<stdio.h>
int main()
ID: 92132313206
{
int a;
printf("Number: ");
scanf("%d",&a);
if(a%2==0)
printf("%d is even.\n",a);
else
printf("%d is odd.\n",a);
return 0;
}
2023-2027-CSE-D
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Number:
Test Case - 2
User Output
Number:
5
5 is odd.
Test Case - 3
User Output
Number:
10
10 is even.
Exp. Name: Demonstrate the use of GOTO
S.No: 9 Date: 2024-03-12
statement
Aim:
Page No: 15
Write a C program to demonstrate the use of GOTO statement
Source Code:
goto.c
ID: 92132313206
#include<stdio.h>
int main()
{
int a;
printf("Enter a number: ");
scanf("%d ",&a);
for(int i=0;i<=a;i++)
{
printf("%d ", i);
}
printf("\n");
return 0;
}
2023-2027-CSE-D
Execution Results - All test cases have succeeded!
Test Case - 1
Test Case - 2
User Output
Enter a number:
10
0 1 2 3 4 5 6 7 8 9 10
S.No: 10 Exp. Name: Switch case Date: 2024-03-12
Aim:
Write a program that accepts two numbers and one code (1,2,3,4) from the user. According to the code, the
Page No: 16
operations to be performed, using switch case statements as follows:
1. Addition
2. Subtraction
3. Multiplication
4. Division
ID: 92132313206
Source Code:
switchCase.c
#include<stdio.h>
int main()
{
int a,b,c,ch;
float r;
printf("Enter number 1: ");
scanf("%d",&a);
printf("Enter number 2: ");
scanf("%d",&b);
2023-2027-CSE-D
printf("1 -> Addition");
printf("\n2 -> Subtraction");
printf("\n3 -> Multiplication");
printf("\n4 -> Division");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
Page No: 17
1 -> Addition
2 -> Subtraction
3 -> Multiplication
4 -> Division
ID: 92132313206
Enter your choice:
1
Result: 30
Test Case - 2
User Output
Enter number 1:
100
Enter number 2:
50
2023-2027-CSE-D
1 -> Addition
2 -> Subtraction
3 -> Multiplication
4 -> Division
Enter your choice:
2
Result: 50
User Output
Enter number 1:
264
Enter number 2:
10
1 -> Addition
2 -> Subtraction
3 -> Multiplication
4 -> Division
Enter your choice:
4
Result: 26.40
S.No: 11 Exp. Name: Break and continue Date: 2024-03-06
Aim:
Write a C program to demonstrate break and continue and print sum of numbers
Page No: 18
Input Format:
• Multiple Lines: The program continuously prompts for input until a negative number is entered.
• Each line contains a single integer (num).
ID: 92132313206
Output Format:
• Single Line: The final sum of all positive integers entered, printed as "Sum: [sum]".
Note: Whenever a negative integer is entered the loop will break, And entered number is equal to
zero then continue.
Source Code:
break_continue.c
#include <stdio.h>
int main() {
int num;
2023-2027-CSE-D
int sum = 0;
while (1) {
printf("Number: ");
scanf("%d",&num);
if (num>=0)
if (num==0)
continue;
break;
}
printf("Sum: %d\n",sum);
User Output
Number:
23
Number:
0
Number:
25
Number:
1
Number:
-1
Sum: 49
Test Case - 2
Page No: 19
User Output
Number:
1
ID: 92132313206
Number:
2
Number:
5
Number:
0
Number:
-9
Sum: 8
2023-2027-CSE-D
Test Case - 3
User Output
Number:
0
Number:
0
Number:
Aim:
Page No: 20
Three brothers want to take a family photo, and the photographer is capturing the image from a
considerable distance. Due to the distance, some family members are standing behind the brothers and are
not visible to the photographer.
The photographer is having difficulty determining the heights of the three brothers and asks, "Who is the
ID: 92132313206
tallest person among you three?" However, no one responds clearly.
Can you assist the photographer in identifying the tallest among the three brothers?
Constraint:
60 ≤ bro1 ≤ 100
60 ≤ bro2 ≤ 100
60 ≤ bro3 ≤ 100
Input Format:
The input line reads 3 space-separated integers representing the heights of three brothers.
2023-2027-CSE-D
Output Format:
The output is an integer representing the height of the tallest person among the three brothers.
Source Code:
TallestBrother.c
#include<stdio.h>
int main()
{
User Output
65 70 89
89
Test Case - 2
User Output
88
94
72 88 65
94 78 63
User Output
Test Case - 3
PSNA College of Engineering and Technology 2023-2027-CSE-D ID: 92132313206 Page No: 21
S.No: 13 Exp. Name: Digit in Words Date: 2024-03-19
Aim:
Mrs. Johnson is teaching her students about numbers. As part of a fun exercise, she asks her students to
Page No: 22
enter a single-digit number, and she wants to show them the word representation of that digit. Mrs.
Johnson decided to use a program for this purpose.
Can you help Mrs. Johnson by providing a simple C program that takes a digit as input from a student and
prints the word representation of that digit?
ID: 92132313206
Input Format:
The input line reads an integer between 0 and 9(both inclusive).
Output Format:
The output is a string representing the digit in words.
DigitSwitch.c
2023-2027-CSE-D
PSNA College of Engineering and Technology
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
Page No: 23
switch(n)
{
case 0:
printf("Zero\n");
break;
case 1:
ID: 92132313206
printf("One\n");
break;
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
case 4:
printf("Four\n");
break;
case 5:
2023-2027-CSE-D
printf("Five\n");
break;
case 6:
printf("Six\n");
break;
case 7:
printf("Seven\n");
break;
User Output
0
Zero
Test Case - 2
User Output
5
15
Five
User Output
Invalid digit
Test Case - 3
PSNA College of Engineering and Technology 2023-2027-CSE-D ID: 92132313206 Page No: 24
S.No: 14 Exp. Name: Finding Profit or Loss Date: 2024-03-06
Aim:
In primary mathematics classes, you all have learned about profit and loss.
Page No: 25
If the cost price is greater than the selling price, then there is a loss otherwise profit
ID: 92132313206
Constraints:
10 ≤ cp ≤ 108
10 ≤ sp ≤ 108
Input Format:
The first line reads an integer representing the cost price.
The second line reads an integer representing the Selling Price.
Output Format:
If Cost Price > Selling Price Print as “Loss” along with the amount of loss as shown below
Loss: <loss>
If Cost Price < Selling Price Print as "Profit" along with the amount of profit as shown below
2023-2027-CSE-D
Profit: <profit>
If Cost Price = Selling Price Print “No Profit No Loss” as shown below
No Profit No Loss
Source Code:
ProfitorLoss.c
#include<stdio.h>
int main()
Page No: 26
Test Case - 2
User Output
26832
ID: 92132313206
28513
Profit: 1681
Test Case - 3
User Output
100
100
No Profit No Loss
2023-2027-CSE-D
PSNA College of Engineering and Technology
Exp. Name: Right angled triangle pattern using
S.No: 15 Date: 2024-03-19
asterisks
Aim:
Page No: 27
Write a C program that generates a pattern of asterisks based on the number of rows specified by the user.
The pattern should form a right-angled triangle of asterisks.
Input Format:
• The program expects a single integer input from the user, which specifies the number of rows in the
ID: 92132313206
pattern.
Output Format:
• The program prints a right-angled triangle pattern of asterisks (*), where each row contains one more
asterisk than the previous row.
• Each asterisk is followed by a space.
• Each row of the pattern ends with a newline.
Source Code:
pattern.c
#include<stdio.h>
int main()
2023-2027-CSE-D
{
int i,j,n;
printf("Enter No Rows : ");
scanf("%d",&n);
for(i=0;i<n;i++){
for(j=0;j<i+1;j++){
printf("* ");
}
User Output
Enter No Rows :
4
*
* *
* * *
* * * *
Test Case - 2
User Output
Enter No Rows :
5
*
*
* *
* *
* * *
* * *
* * * *
* * * * *
User Output
Enter No Rows :
Test Case - 3
PSNA College of Engineering and Technology 2023-2027-CSE-D ID: 92132313206 Page No: 28
Exp. Name: C program to check whether a given
S.No: 16 Date: 2024-03-29
number is an armstrong number or not.
Aim:
Page No: 29
Write the C program to check whether a given number is an armstrong number or not.
Input format:
• The program prompts the user to enter an integer.
ID: 92132313206
Output format:
• The program checks if the entered integer is an Armstrong number.
• It then prints either that the number is an Armstrong number or that it is not.
Source Code:
Armstrong.c
#include<stdio.h>
#include<math.h>
void main()
{
int num,ori,rem,res=0,s,count;
printf("Enter an integer: ");
2023-2027-CSE-D
scanf("%d",&num);
ori=s=num;
while (s!=0)
{
s=s/10;
count=count+1;
}
while (ori !=0)
User Output
Enter an integer:
153
153 is an Armstrong number.
Test Case - 2
456
User Output
Enter an integer:
PSNA College of Engineering and Technology 2023-2027-CSE-D ID: 92132313206 Page No: 30
Exp. Name: Printing the square of each entered
S.No: 17 Date: 2024-03-19
number.
Aim:
Page No: 31
Write a C program for printing the square of each entered number.
ID: 92132313206
dowhile.c
#include <stdio.h>
int main() {
int num;
do {
printf("Number: ");
scanf("%d",&num);
if(num>=0)
{
num=num*num;
2023-2027-CSE-D
printf("Square of number is %d\n",num);
}
else
{
break;
}
}while(num>=0);
User Output
Number:
25
Square of number is 625
Number:
15
Square of number is 225
Number:
-1
Test Case - 2
User Output
Number:
10
Square of number is 100
Number:
0
Square of number is 0
Number:
-3
Page No: 32
Test Case - 3
User Output
ID: 92132313206
Number:
11
Square of number is 121
Number:
30
Square of number is 900
Number:
25
Square of number is 625
Number:
-6
2023-2027-CSE-D
PSNA College of Engineering and Technology
S.No: 18 Exp. Name: Seating Arrangement Date: 2024-04-16
Aim:
Joseph, a mathematics professor, has distributed answer sheets to his students after the examination. In
Page No: 33
his class, some students passed while others failed.
To analyze individual student performances, he instructed the students to sit in a specific order: students
who passed the exam should sit in odd-numbered rows, and students who failed should sit in even-
numbered rows.
ID: 92132313206
The total number of rows is provided by the professor. However, since the classroom is triangular in shape,
the seating arrangement may confuse the students.
Your task is to help the students determine the correct seating order based on the number of rows given by
the professor.
Constraints:
• 1 ≤ no of rows in class ≤ 15
Input Format:
2023-2027-CSE-D
• The only line of input has a single integer representing the number of rows in which students have to
sit.
Output Format:
The program will print Pass or Fail for each row, depending on whether the row number is odd or even:
• If the row number is odd, it prints Pass for each column in that row.
• If the row number is even, it prints Fail for each column in that row.
• Each row's output is followed by a newline.
seatingArrangement.c
#include<stdio.h>
int main()
{
int rows;
scanf("%d",&rows);
for(int i = 1; i <= rows;i++)
{
for(int j = 1;j <= i;j++)
{
if(i % 2 != 0) {
printf("Pass ");
} else {
printf("Fail ");
}
}
printf("\n");
}
return 0;
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Page No: 34
3
Pass
Fail Fail
Pass Pass Pass
ID: 92132313206
Test Case - 2
User Output
4
Pass
Fail Fail
Pass Pass Pass
Fail Fail Fail Fail
Test Case - 3
2023-2027-CSE-D
User Output
6
Pass
Fail Fail
Pass Pass Pass
Fail Fail Fail Fail
Test Case - 4
User Output
10
Pass
Fail Fail
Pass Pass Pass
Fail Fail Fail Fail
Pass Pass Pass Pass Pass
Fail Fail Fail Fail Fail Fail
Pass Pass Pass Pass Pass Pass Pass
Fail Fail Fail Fail Fail Fail Fail Fail
Pass Pass Pass Pass Pass Pass Pass Pass Pass
Fail Fail Fail Fail Fail Fail Fail Fail Fail Fail
S.No: 19 Exp. Name: Reverse of a Number Date: 2024-04-16
Aim:
Kiran is a student who is good at writing programs. One day his friend Ramesh gave him the number N for
Page No: 35
which Kiran had to tell the reverse of the number.
Input Format:
The input line reads an integer N.
ID: 92132313206
Output Format:
The output consists of an integer representing the reverse of the given number N.
2023-2027-CSE-D
ReverseNum.c
#include<stdio.h>
int main()
{
int n,reverse=0,remainder;
scanf("%d",&n);
while(n!=0)
{
}
printf("%d\n",reverse);
return 0;
}
User Output
1556
6551
Test Case - 2
User Output
7
7
S.No: 20 Exp. Name: Maximum of given integers Date: 2024-05-06
Aim:
Ravi and Raju are thinking of printing the maximum of given N positive integers without using Arrays.
Page No: 36
Help them to print the maximum number by using iterative statements.
Input Format:
The first line reads an integer representing the number of positive integers N.
The second line reads N space-separated positive integers.
ID: 92132313206
Output Format:
The output is an integer representing the maximum in given numbers.
MaximumNumber.c
#include <stdio.h>
int main() {
2023-2027-CSE-D
int N, num, max;
return 0;
}
User Output
5
10 200 50 90 1
200
3
3
4
100
333
User Output
User Output
Test Case - 3
PSNA College of Engineering and Technology 2023-2027-CSE-D ID: 92132313206 Page No: 37
S.No: 21 Exp. Name: Code to find transpose of a matrix Date: 2024-05-06
Aim:
Write a C program to print perform the transpose of the matrix
Page No: 38
Input1: an integer (row size)
Input2: an integer (column size)
Input3: a matrix elements (row × col size elements)
Output: print the transpose of the matrix(print using \n)
ID: 92132313206
Source Code:
transpose.c
#include <stdio.h>
int main() {
int rows, cols;
2023-2027-CSE-D
// Input number of columns
printf("Enter the number of columns: ");
scanf("%d", &cols);
int matrix[rows][cols];
return 0;
}
User Output
Enter the number of rows:
2
Enter the number of columns:
2
Enter elements for the matrix:
10 20
Page No: 39
30 40
Transpose of the matrix:
10 30
20 40
ID: 92132313206
Test Case - 2
User Output
Enter the number of rows:
2
Enter the number of columns:
3
Enter elements for the matrix:
10 20 30
40 50 60
2023-2027-CSE-D
Transpose of the matrix:
10 40
20 50
30 60
Aim:
Write a C program to find the multiplication of two matrices of size r × c.
Page No: 40
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
ID: 92132313206
Output Format:
• Prints the matrix1 and matrix2 and finally the result of multiplication of both the matrices
Sample Input:
2 2
11 22
33 44
2 2
11 22
33 44
2023-2027-CSE-D
Sample Output:
matrix1:
11 22
33 44
matrix2:
11 22
33 44
result:
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>
int main()
{
int a[10][10],b[10][10],m[10][10],r,c,r1,c1,i,j,k;
printf("no of rows, columns of matrix1: ");
Page No: 41
scanf("%d%d",&r,&c);
printf("matrix1 elements:\n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&a[i][j]);
ID: 92132313206
}
}
printf("no of rows, columns of matrix2: ");
scanf("%d%d",&r1,&c1);
printf("matrix2 elements:\n");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
scanf("%d",&b[i][j]);
}
}
printf("Given matrix1:\n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
2023-2027-CSE-D
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("Given matrix2:\n");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
printf("%d ",b[i][j]);
User Output
Page No: 42
22
11 22
33 44
22
11 22
ID: 92132313206
33 44
matrix1:
11 22
33 44
matrix2:
11 22
33 44
result:
847 1210
1815 2662
2023-2027-CSE-D
Test Case - 2
User Output
33
123
456
Aim:
Page No: 43
Write a sample code to find the largest element of each row of a two-dimensional array using functions.
ID: 92132313206
Enter the value of arr[0][0] : 1
Enter the value of arr[0][1] : 4
Enter the value of arr[0][2] : 7
Enter the value of arr[1][0] : 9
Enter the value of arr[1][1] : 6
Enter the value of arr[1][2] : 3
Enter the value of arr[2][0] : 2
Enter the value of arr[2][1] : 8
Enter the value of arr[2][2] : 5
The given matrix is
1 4 7
9 6 3
2023-2027-CSE-D
2 8 5
Largest of row - 0 elements = 7
Largest of row - 1 elements = 9
Largest of row - 2 elements = 8
Source Code:
FunctionsAndArrays4.c
Page No: 44
int arr[5][5], i, j, m, n, largest;
printf("Enter row and column sizes : ");
scanf("%d %d", &m, &n);
read1(arr, m, n);
printf("The given matrix is\n");
display(arr, m, n);
ID: 92132313206
largest_of_each_row(arr, m, n);
}
void read1(int arr[5][5],int m,int n) {
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("Enter the value of arr[%d][%d] :
",i,j);
scanf("%d",&arr[i][j]);
}
}
2023-2027-CSE-D
}
void display(int arr[5][5],int m,int n) {
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%d ",arr[i][j]);
}
User Output
Enter row and column sizes :
33
Enter the value of arr[0][0] :
1
Enter the value of arr[0][1] :
Page No: 45
4
Enter the value of arr[0][2] :
7
Enter the value of arr[1][0] :
9
ID: 92132313206
Enter the value of arr[1][1] :
6
Enter the value of arr[1][2] :
3
Enter the value of arr[2][0] :
2
Enter the value of arr[2][1] :
8
Enter the value of arr[2][2] :
5
2023-2027-CSE-D
The given matrix is
1 4 7
9 6 3
2 8 5
Largest of row - 0 elements = 7
Largest of row - 1 elements = 9
Largest of row - 2 elements = 8
User Output
Enter row and column sizes :
32
Enter the value of arr[0][0] :
23
Enter the value of arr[0][1] :
45
Enter the value of arr[1][0] :
26
Enter the value of arr[1][1] :
17
Enter the value of arr[2][0] :
29
Enter the value of arr[2][1] :
34
The given matrix is
23 45
26 17
29 34
Largest of row - 0 elements = 45
Largest of row - 1 elements = 26
Largest of row - 2 elements = 34
Test Case - 3
User Output
Enter row and column sizes :
22
Page No: 46
Enter the value of arr[0][0] :
34
Enter the value of arr[0][1] :
35
ID: 92132313206
Enter the value of arr[1][0] :
28
Enter the value of arr[1][1] :
26
The given matrix is
34 35
28 26
Largest of row - 0 elements = 35
Largest of row - 1 elements = 28
2023-2027-CSE-D
PSNA College of Engineering and Technology
Exp. Name: C program that implements the
S.No: 24 Date: 2024-05-06
Selection sort
Aim:
Page No: 47
Write a C program that implements the Selection Sort algorithm to sort a given list of integers in ascending
order. The program should display the elements of the array before and after sorting.
Input Format:
• First line of input takes an integer n representing the number of elements (1 ≤ n ≤ 20).
ID: 92132313206
• Second line of input takes n integers representing the elements to be sorted separated by space.
Output Format:
• Print the elements of the array before sorting separated by space.
• Print the elements of the array after sorting separated by space.
selectionSort.c
#include<stdio.h>
int main()
2023-2027-CSE-D
{
int n,t;
printf("Enter no of elements: ");
scanf("%d",&n);
printf("Enter the elements: ");
int a[n];
for(int i=0;i<n;i++){
scanf("%d ",&a[i]);
User Output
Page No: 48
Enter no of elements:
5
Enter the elements:
26157
Array before sort: 2 6 1 5 7
ID: 92132313206
Array after sort: 1 2 5 6 7
Test Case - 2
User Output
Enter no of elements:
6
Enter the elements:
62 51 58 96 32 14
Array before sort: 62 51 58 96 32 14
2023-2027-CSE-D
Array after sort: 14 32 51 58 62 96
Test Case - 3
User Output
Enter no of elements:
5
Aim:
Page No: 49
Mahesh has given a two-dimensional 3*3 array starting from A [0][0].You should add the alternate elements
of the array and print their sum.
It should print two different numbers. The first is the sum ofA 0 0, A 0 2, A 1 1, A 2 0, A 2 2and A 0 1, A 1 0,
A 1 2, A 2 1.
ID: 92132313206
Constraints:
1 <= values of array <= 200
Input Format:
• First and only line contains the value of the array separated by a space.
Output Format:
• The first line should print the sum of A 0 0, A 0 2, A 1 1, A 2 0, A 2 2
• Second line should print sum of A 0 1, A 1 0, A 1 2, A 2 1
Instruction: To run your custom test cases strictly map your input and output layout with the visible test
cases.
2023-2027-CSE-D
Source Code:
sum.c
#include<stdio.h>
int main()
{
int m[3][3];
int sum1=0,sum2=0;
User Output
81
58
247
367
User Output
9 5 8 47 6 2 31 27 4
11 12 15 36 95 84 74 115 172
Test Case - 2
PSNA College of Engineering and Technology 2023-2027-CSE-D ID: 92132313206 Page No: 50
S.No: 26 Exp. Name: Count the total score of an over Date: 2024-05-06
Aim:
While watching the India vs. Australia final match, Suresh found himself fascinated by the process of
Page No: 51
recording individual runs for each ball in an over and calculating the total runs for that over. Motivated by
this curiosity, Suresh aimed to develop a program using arrays, to store the runs for every ball in an over.
His ultimate goal is to create a tool that could efficiently calculate and display the total runs conceded for
the entire over.
ID: 92132313206
Can you help Suresh to complete his goal?
Input Format:
The input is space-separated integer values representing runs for every ball.
Output Format:
The program outputs the total sum of the 6 integers in the format: Total: <sum>, where <sum> is the
calculated total.
Note:
• Print a Newline character(\n) in the output statement.
• An over contains 6 balls.
Source Code:
2023-2027-CSE-D
totalscore.c
#include<stdio.h>
int main()
{
int r[6],sum=0;
for(int i=0;i<6;i++)
User Output
210601
Total: 10
Test Case - 2
User Output
112012
Total: 7
S.No: 27 Exp. Name: Display minimum salary in an array Date: 2024-05-06
Aim:
Imagine you are working on a payroll management system for a company. As part of the system, you need
Page No: 52
to develop a C program that calculates the minimum salary among a group of employees.
Write a program that prompts the user to input the number of employees and their respective salaries.
Utilize an array to store the salaries and to find and display the minimum salary in the dataset.
ID: 92132313206
Constraints:
0 < number of employees < 105
Input Format:
The first line reads an integer value representing the no. of employees
The next lines read the salary of the respective employee which is a float data type
Output Format:
The output is a float value rounded to two decimal places which represents the minimum salary of the
employee.
If the number of employees is less than then print "Invalid number".
Note: Driver code is provided to you in the editor. Finish the function to achieve the task.
2023-2027-CSE-D
Source Code:
Min_salary.c
#include<stdio.h>
int main()
{
int numEmployees,i;
Page No: 53
Salary for employee 2:
26012.52
Salary for employee 3:
15325.65
ID: 92132313206
Salary for employee 4:
25000
Salary for employee 5:
27000
Salary for employee 6:
15326
Salary for employee 7:
18000.325
Salary for employee 8:
18365.23
Minimum salary: 15325.65
2023-2027-CSE-D
Test Case - 2
User Output
Employees:
-1
Invalid number
Aim:
Page No: 54
Write a program to reverse the given string without using the library functions.
At the time of execution, the program should print the message on the console as:
Enter a string :
ID: 92132313206
For example, if the user gives the input as:
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
2023-2027-CSE-D
Program609.c
#include<stdio.h>
#include<string.h>
int main()
{
char s[100];
printf("Enter a string : ");
User Output
Enter a string :
Dallas
Reverse string : sallaD
Exp. Name: Write a C program to Count the
S.No: 29 Characters in a given string without using string Date: 2024-05-06
library functions.
Page No: 55
Aim:
Write a C program to count the characters in a given string without using string library functions.
At the time of execution, the program should print the following message on the console as:
ID: 92132313206
Enter a string:
Characters: 5
Source Code:
2023-2027-CSE-D
Count.c
#include<stdio.h>
int main()
{
char s[100];
printf("Enter a string: ");
scanf("%[^\n]",s);
User Output
Enter a string:
CodeTantra
Characters: 10
Test Case - 2
User Output
Enter a string:
Hello, World!
Characters: 13
PSNA College of Engineering and Technology 2023-2027-CSE-D ID: 92132313206 Page No: 56
Exp. Name: Write a C program to check given
S.No: 30 Date: 2024-05-06
string is a palindrome or not.
Aim:
Page No: 57
Create a program that checks whether a given input string is a palindrome or not.
Input:
• A string of characters entered by the user. (Note: The string can have a maximum length of 79
characters due to the array size limit.)
ID: 92132313206
Output:
• Displays "Palindrome" if the input string is a palindrome.
• Displays "Not a palindrome" if the input string is not a palindrome.
Constraints:
• The input string can have a maximum length of 79 characters (due to the size of the ch array).
• The input string can consist of alphanumeric characters, spaces, or special characters.
Note: Due to binary output even or odd, you have to pass all test cases to score marks.
Instruction: Strictly follow the input and output layout mentioned in the visible test cases.
Source Code:
2023-2027-CSE-D
palindrome.c
#include<stdio.h>
#include<string.h>
int main()
{
char s[79],r[79];
int l;
printf("String: ");
User Output
String:
madam
Palindrome
Test Case - 2
Page No: 58
User Output
String:
Madam
Not a palindrome
ID: 92132313206
Test Case - 3
User Output
String:
123321
Palindrome
2023-2027-CSE-D
PSNA College of Engineering and Technology
Exp. Name: Write a C program to Replace a
S.No: 31 Date: 2024-05-06
character of a given String at a given Position
Aim:
Page No: 59
Write a program to replace a character of a given string at a given position.
At the time of execution, the program should print the message on the console as:
Enter a string :
ID: 92132313206
For example, if the user gives the input as:
Next, the program should print the message on the console as:
2023-2027-CSE-D
Next, the program should print the message on the console as:
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program615.c
#include<stdio.h>
#include<string.h>
int main()
{
int i,pos;
Page No: 60
char str[20];
char ch;
printf("Enter a string : ");
fgets(str, sizeof(str), stdin);
printf("Enter the position : ");
scanf("%d",&pos);
ID: 92132313206
printf("Enter the character to replace : ");
scanf(" %c",&ch);
for(i=0;str[i]!='\0';i++)
{
if(i==pos)
{
str[i] = ch;
}
}
printf("After replacement the string is : %s", str);
return 0;
}
2023-2027-CSE-D
Execution Results - All test cases have succeeded!
Test Case - 1
Test Case - 2
User Output
Enter a string :
Kohli
Enter the position :
6
Enter the character to replace :
e
After replacement the string is : Kohli
Test Case - 3
User Output
Enter a string :
CodeTantra
Enter the position :
4
Enter the character to replace :
Page No: 61
t
After replacement the string is : Codetantra
ID: 92132313206
2023-2027-CSE-D
PSNA College of Engineering and Technology
Exp. Name: Write a C program to find the Length
S.No: 32 of a given string without using string library Date: 2024-05-06
functions
Page No: 62
Aim:
Fill the missing code in below to find the length of a given string without using string library functions.
Source Code:
ID: 92132313206
Program601.c
#include<stdio.h>
void main() {
char ch[20];
int i;
printf("Enter a string : ");
scanf("%s", ch);
for (i=0;ch[i]!='\0';++i) {
}
printf("length %d\n", i );
2023-2027-CSE-D
}
Aim:
Rohit has a unique way of transforming strings. He repeatedly reverses substrings within a string. Write a C
Page No: 63
program to help Rohit transform a given string using this unique process.
Input Format:
• A single line containing a string of characters (1 <= length of string <= 100).
• The string may contain letters (uppercase and lowercase) and other printable characters, but no
spaces.
ID: 92132313206
Output Format:
• A single line containing the transformed string.
Source Code:
reverse.c
#include<stdio.h>
#include<string.h>
void main()
{
char str[100],t;
2023-2027-CSE-D
int i=0,j=0;
scanf("%s",str);
j=strlen(str)-1;
while(i<j)
{
t=str[j];
str[j]=str[i];
str[i]=t;
User Output
Processing
gnissecorP
Test Case - 2
User Output
CodeTantra
artnaTedoC
S.No: 34 Exp. Name: Name Reversal Date: 2024-05-06
Aim:
A team of developers is working on a new feature for a text-processing module. The requirement is to
Page No: 64
reverse the order of names in a given sentence. The team needs your help to implement this feature.
Input format:
The input is the string of different names(words).
Output format:
ID: 92132313206
The output is the reversed order of the names(words).
Note:
Refer to the displayed test cases for better understanding.
Source Code:
nameReversal.c
2023-2027-CSE-D
PSNA College of Engineering and Technology
#include<stdio.h>
int main()
{
char names[500];
int i,j,start,end;
Page No: 65
fgets(names,sizeof(names),stdin);
for(i=0;names[i]!='\0';i++)
{
if(names[i]=='\n')
{
names[i]='\0';
ID: 92132313206
break;
}
}
start=0;
end=0;
while(names[end]!='\0'){
end++;
}
for(i=end-1;i>=0;i--){
if(names[i]==' ' || i==0){
if(i==0){
start=i;
2023-2027-CSE-D
}
else{
start=i+1;
}
for(j=start;j<end;j++)
{
printf("%c",names[j]);
}
User Output
Alice Bob Maiden
Maiden Bob Alice
Test Case - 2
User Output
Dohni Virat Rohith Shami Surya Gill
Gill Surya Shami Rohith Virat Dohni
PSNA College of Engineering and Technology 2023-2027-CSE-D ID: 92132313206 Page No: 66
S.No: 35 Exp. Name: EA Cricket 2023 Date: 2024-05-06
Aim:
Seeing the success of the Cricket 22 video game, you have been hired as a game developer by EA to
Page No: 67
develop a cricket video game. But this game is different, instead of having 10 wickets you have only 3
wickets after which the game will be over. Another twist is, after every wicket is lost, players have an option
to pay $10 to add 10 runs to the score. Your job is to create a program that calculates the total runs scored
and money paid by the player after the end of the game.
ID: 92132313206
Constraints:
1 <= length of the string <= 50
Input Format:
The first line of input is a string that represents your scorecard. "W" stands for the wicket. "P" stands for
$10 paid. Numbers 1,2,3,4 etc stand for the run scored.
Output Format:
The first line of output is your score and the second line is the amount paid
Sample input-1:
W641234WP6432W
2023-2027-CSE-D
Sample output-1:
45
10
Explanation:
It can be seen from the string initially a wicket is lost. After the second wicket is lost user pays $10 which
adds 10 runs to the score. During the tail end of innings wicket is lost thus ending the game.
Sample output-2:
30
30
Points to be noted:
1) Game ends exactly after three wickets
2) You can only pay $10 after you have lost a wicket
3) After your third wicket is lost, the only next action you can do is pay $10 to increase the score and quit or
just quit the game without paying.
Source Code:
EA_game.c
#include<stdio.h>
#include<string.h>
void main()
{
int i,c=0,t=0,sum=0;
Page No: 68
char str[50];
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
if((str[i]>='0') && (str[i]<='9'))
{
ID: 92132313206
sum+=(str[i]-'0');
}
if(str[i]=='P')
{
sum+=10;
}
}
printf("%d\n", sum);
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='P')
{
2023-2027-CSE-D
c+=1;
}
}printf("%d\n",c*10);
}
User Output
W641234WP6432W
45
10
Test Case - 2
User Output
WPWPWP
30
30
Exp. Name: Write a C program to demonstrate
S.No: 36 Functions without arguments and with return Date: 2024-05-06
value
Page No: 69
Aim:
Write a C program to demonstrate functions without arguments and with return value.
The below code is used to check whether the given number is a prime number or not.
ID: 92132313206
Write the function prime().
Enter a number : 5
The given number is a prime number
Source Code:
FunctionCategories8.c
2023-2027-CSE-D
#include <stdio.h>
int prime();
void main() {
if (prime() == 0) {
printf("The given number is a prime number\n");
} else {
printf("The given number is not a prime number\n");
}
}
Page No: 70
Test Case - 2
User Output
Enter a number :
ID: 92132313206
27
The given number is not a prime number
Test Case - 3
User Output
Enter a number :
121
The given number is not a prime number
2023-2027-CSE-D
Test Case - 4
User Output
Enter a number :
1
The given number is not a prime number
User Output
Enter a number :
117
The given number is not a prime number
Test Case - 6
User Output
Enter a number :
137
The given number is a prime number
S.No: 37 Exp. Name: Call by value Date: 2024-05-06
Aim:
Write a C program to swap given two numbers using Call by value.
Page No: 71
Source Code:
callByValue.c
#include <stdio.h>
void swap (int a, int b) {
ID: 92132313206
int t=a;
a=b;
b=t;
printf("After swapping values in function a = %d, b = %d\n",a,b);
}
int main() {
int a, b;
printf("Enter two numbers a, b: ");
scanf("%d %d", &a, &b);
// printing the value of a and b in main
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
2023-2027-CSE-D
swap(a,b);
// The value of actual parameters do not change by changing the formal
parameters in call by value, a, b
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
User Output
Enter two numbers a, b:
10 20
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20
Test Case - 2
User Output
Enter two numbers a, b:
12 35
Before swapping the values in main a = 12, b = 35
After swapping values in function a = 35, b = 12
After swapping values in main a = 12, b = 35
S.No: 38 Exp. Name: Call-by-Reference Date: 2024-05-06
Aim:
Write a C program to Swap two values by using Call-by-Reference method.
Page No: 72
Source Code:
swap.c
#include<stdio.h>
int swap(int *x, int *y); //prototype of the function
ID: 92132313206
int main() {
int a,b;
printf("enter a, b values: ");
scanf("%d%d", &a,&b);
printf("before swapping a,b values:%d %d\n",a,b);
swap(&a,&b);
printf("after swapping a,b values:%d %d\n",a,b);
return 0;
}
int swap(int *x, int *y)
{
int t=*x;
2023-2027-CSE-D
*x=*y;
*y=t;
return 0;
}
User Output
enter a, b values:
10 100
before swapping a,b values:10 100
after swapping a,b values:100 10
Test Case - 2
User Output
enter a, b values:
5 10
before swapping a,b values:5 10
after swapping a,b values:10 5
Exp. Name: Write a program to display the
S.No: 39 Date: 2024-05-06
Alternative elements of an Array
Aim:
Page No: 73
Write a program to print the alternative elements of the given array (with max size 10).
At the time of execution, the program should print the message on the console as:
ID: 92132313206
For example, if the user gives the input as:
Next, the program should print the message on the console as:
Enter 5 elements :
Enter 5 elements : 11 22 33 44 55
2023-2027-CSE-D
then the program should print the result as:
Page No: 74
int a[10], i, n;
printf("Enter size of the array : ");
scanf("%d", &n);
printf("Enter %d elements : ", n);
read1(a, n);
printf("The given elements : ");
ID: 92132313206
display(a, n);
printf("The alternative elements : ");
alternativeEleDisplay(a, n);
}
2023-2027-CSE-D
printf("%d ",a[i]);
}
printf("\n");
}
void alternativeEleDisplay(int a[],int n ) {
for(int i=0;i<n;i=i+2){
printf("%d ",a[i]);
}
User Output
Enter size of the array :
5
Enter 5 elements :
11 22 33 44 55
The given elements : 11 22 33 44 55
The alternative elements : 11 33 55
Test Case - 2
User Output
Enter size of the array :
6
Enter 6 elements :
10 20 30 40 50 60
The given elements : 10 20 30 40 50 60
The alternative elements : 10 30 50
Test Case - 3
Page No: 75
User Output
Enter size of the array :
4
Enter 4 elements :
ID: 92132313206
-1 -2 -4 -6
The given elements : -1 -2 -4 -6
The alternative elements : -1 -4
2023-2027-CSE-D
PSNA College of Engineering and Technology
Exp. Name: Calculator program by using
S.No: 40 Date: 2024-05-06
Functions
Aim:
Page No: 76
Sara is developing a calculator program in C for a basic math utility. Her goal is to design a program that
allows users to perform arithmetic operations interactively by reading two float values and one character
value from the user. She needs your help to complete her goal.
Can you help her by writing a program with separate functions for addition, subtraction, multiplication, and
ID: 92132313206
division?
Input Format:
• The first line reads two float values from the user separated by space.
• The second line reads a character (+, -, *, / ) representing the operation to be performed.
Output Format:
• The output consists of a float value representing the result of the specific operation rounded up to
two decimal places.
Note:
• The driver code is already present in the editor, your task is to write the respective functions to
2023-2027-CSE-D
complete the program.
• In the divide function if the divisor is zero print "Cannot divide by zero" and return 0.
Source Code:
clac_func.c
Page No: 77
float subtract(float a,float b)
{
return a-b;
}
float multiply(float a,float b)
{
ID: 92132313206
return (float)a*b;
}
float divide(float a,float b)
{
if(b!=0){
return a/b;
}
else{
printf("Cannot divide by zero\n");
}
return 0;
}
2023-2027-CSE-D
int main() {
float num1, num2, result;
char operator;
return 0;
}
User Output
10.5 6
*
Page No: 78
Result: 63.00
Test Case - 2
ID: 92132313206
User Output
50
/
Cannot divide by zero
Result: 0.00
Test Case - 3
User Output
25
2023-2027-CSE-D
+
Result: 7.00
Test Case - 4
User Output
93
Test Case - 5
User Output
84
/
Result: 2.00
Exp. Name: Finding Prime numbers in given
S.No: 41 Date: 2024-05-06
range using functions
Aim:
Page No: 79
Mia, a math enthusiast in the village of PrimaVille, was given a task by the mayor. The challenge was to
make a program using functions that could find and show all the prime numbers in a given range.
ID: 92132313206
Input Format:
The input line reads two space-separated positive integers from the user representing the starting and
ending range.
Output Format:
The output line consists of all prime numbers between a given range.
Note: Print the newline character (\n) after the output statement.
Source Code:
PrimNumbersInRange.c
#include<stdio.h>
2023-2027-CSE-D
int isprime(int n)
{
if(n<=1){
return 0;
}
for(int i=2;i<=n/2;i++)
{
if(n%i==0){
int main() {
int lower, upper;
scanf("%d %d", &lower, &upper);
generatePrimesInRange(lower, upper);
return 0;
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Page No: 80
2 10
Prime numbers between 2 and 10 are: 2 3 5 7
Test Case - 2
ID: 92132313206
User Output
1 50
Prime numbers between 1 and 50 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Test Case - 3
User Output
22
Prime numbers between 2 and 2 are: 2
2023-2027-CSE-D
PSNA College of Engineering and Technology
S.No: 42 Exp. Name: Average of three heights Date: 2024-05-06
Aim:
Mounish wants to determine the average heights of his 3 brothers. For this, he needs your help to
Page No: 81
implement a program that calculates the average by using a function.
Input Format:
ID: 92132313206
The input line reads three space-separated positive float values representing the heights of 3 brothers.
Output Format:
The output is a float value representing the average height of three brothers formatted to two decimal
points.
Source Code:
AverageHeights.c
#include<stdio.h>
float calculateAverage(float a1,float a2,float a3){
float a=(a1+a2+a3)/3;
return a;
2023-2027-CSE-D
}
int main() {
float brother1, brother2, brother3;
scanf("%f %f %f", &brother1, &brother2, &brother3);
float average= calculateAverage(brother1, brother2, brother3);
printf("%.2f\n", average);
return 0;
}
User Output
161 160 160.53
160.51
Test Case - 2
User Output
158.55 162.336 160
160.30
Exp. Name: Write a C program to find the GCD of
S.No: 43 Date: 2024-05-06
two numbers using Recursion
Aim:
Page No: 82
Write a program to find the gcd (Greatest Common Divisor) of a given two numbers using recursion
process.
The greatest common divisor ( gcd ) of two or more integers, when at least one of them is not zero, is the
largest positive integer that is a divisor of both numbers.
ID: 92132313206
Note: Write the recursive function gcd() in Program906a.c .
Source Code:
Program906.c
#include <stdio.h>
#include "Program906a.c"
void main() {
int a, b;
printf("Enter two integer values : ");
2023-2027-CSE-D
scanf("%d %d", &a, &b);
printf("The gcd of two numbers %d and %d = %d\n", a, b, gcd(a, b));
}
Program906a.c
#include<stdio.h>
User Output
Enter two integer values :
12 15
The gcd of two numbers 12 and 15 = 3
Test Case - 2
User Output
36 124
Enter two integer values :
PSNA College of Engineering and Technology 2023-2027-CSE-D ID: 92132313206 Page No: 83
Exp. Name: Write a C program to find the e^x
S.No: 44 Date: 2024-05-06
value using Functions
Aim:
Page No: 84
Write a program to find out the ex using functions.
At the time of execution, the program should print the message on the console as:
ID: 92132313206
Enter n value :
Enter n value : 10
Next, the program should print the message on the console as:
Enter x value :
2023-2027-CSE-D
Enter x value : 2
Program712.c
#include <stdio.h>
#include "Program712a.c"
void main() {
int op, n;
float x;
printf("Enter n value : ");
scanf("%d", &n);
printf("Enter x value : ");
scanf("%f", &x);
x = (x * 3.14) / 180;
printf("Exponential result = %f\n", exponential(n, x));
}
Program712a.c
#include<stdio.h>
float factorial(int n)
{
if(n==0 || n==1)
{
Page No: 85
return 1;
}
else{
return n*(factorial(n-1));
}
}
ID: 92132313206
float exponential(int n,float x)
{
float result=1.0,pow_x=1.0,fact=1.0;
for(int i=1;i<=n;i++)
{
pow_x*=x;
fact=factorial(i);
result+=pow_x / fact;
}
return result;
}
2023-2027-CSE-D
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Test Case - 2
User Output
Enter n value :
8
Enter x value :
5
Exponential result = 1.091139
Test Case - 3
User Output
Enter n value :
8
Enter x value :
6
Exponential result = 1.110340
Exp. Name: Write a C program to solve the
S.No: 45 Date: 2024-05-06
Towers of Hanoi problem using Recursion
Aim:
Page No: 86
Write a program to solve the towers of hanoi problem using recursion process.
At the time of execution, the program should print the message on the console as :
ID: 92132313206
For example, if the user gives the input as:
2023-2027-CSE-D
Move disk - 2 from pole B to C
Move disk - 1 from pole A to C
Note: Write the recursive function hanoi() in Program909a.c and do use the printf() function with a
newline character ( \n ) at the end.
Source Code:
Program909.c
Program909a.c
#include<stdio.h>
void hanoi(int n,char a,char b,char c)
{
if(n==1)
{
printf("Move disk - 1 from pole %c to %c\n",a,c);
return;
}
hanoi(n-1,a,c,b);
printf("Move disk - %d from pole %c to %c\n",n,a,c);
hanoi(n-1,b,a,c);
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Page No: 87
Enter number of disks :
2
Move disk - 1 from pole A to B
Move disk - 2 from pole A to C
Move disk - 1 from pole B to C
ID: 92132313206
Test Case - 2
User Output
Enter number of disks :
3
Move disk - 1 from pole A to C
Move disk - 2 from pole A to B
Move disk - 1 from pole C to B
Move disk - 3 from pole A to C
2023-2027-CSE-D
Move disk - 1 from pole B to A
Move disk - 2 from pole B to C
Move disk - 1 from pole A to C
Aim:
Write a C program that prints Fibonacci series of N numbers
Page No: 88
Source Code:
fib.c
#include<stdio.h>
int main()
ID: 92132313206
{
int n;
printf("n: ");
scanf("%d",&n);
int a=0,b=1;
printf("%d %d ",a,b);
int i=2;
while(i<n)
{
int t;
t=b;
b=a+b;
2023-2027-CSE-D
a=t;
printf("%d ",b);
i++;
}
return 0;
}
User Output
n:
5
0 1 1 2 3
Test Case - 2
User Output
n:
8
0 1 1 2 3 5 8 13
S.No: 47 Exp. Name: Factorial of a given number Date: 2024-05-06
Aim:
Write a C program to find the factorial of a given number
Page No: 89
Source Code:
factorialOfInt.c
#include<stdio.h>
int main()
ID: 92132313206
{
int n,fact=1;
printf("Integer: ");
scanf("%d",&n);
int i=1;
while(i<=n)
{
fact=fact*i;
i++;
}
printf("Factorial: %d\n",fact);
}
2023-2027-CSE-D
Execution Results - All test cases have succeeded!
Test Case - 1
Test Case - 2
User Output
Integer:
4
Factorial: 24
S.No: 48 Exp. Name: GCD using Recursion Date: 2024-05-06
Aim:
Jessy, an enthusiastic programming learner, is exploring algorithms and recursion. Fascinated by the idea of
Page No: 90
calculating the Greatest Common Divisor (GCD) of two numbers, num1, and num2, using recursion, she
wants to create a C program for this challenge. However, Jessy is struggling with it.
Can you assist her in figuring out how to accomplish this task?
Constraints:
ID: 92132313206
0 <= num1, num2 <= 105
Input Format:
The input line reads two space-separated integers representing num1 and num2.
Output Format:
The output is an integer representing the GCD of num1 and num2.
Source Code:
GCDbyRecusrion.c
#include <stdio.h>
2023-2027-CSE-D
// fill the missing code
int GCD(int x,int y) {
if (y==0) {
return x;
} else {
return GCD(y,x%y);
}
}
User Output
12 18
6
Test Case - 2
User Output
7 11
1
Exp. Name: Sum of N natural numbers using
S.No: 49 Date: 2024-05-06
Recursion
Aim:
Page No: 91
Mark is willing to design a program that returns the sum of N natural numbers using recursion.
Find out how Mark can print the sum of N natural numbers by using recursion.
Input Format:
ID: 92132313206
The input line reads a positive integer N.
Output Format:
The output is a positive integer representing the sum of N natural numbers.
SumofNusingRecursion.c
#include<stdio.h>
2023-2027-CSE-D
int sum(int a){
if(a==0)
{
return 0;
}
else
{
void main(){
int a;
printf("Enter a number : ");
scanf("%d",&a);
printf("%d\n",sum(a));
}
User Output
Enter a number :
10
55
Test Case - 2
100
5050
User Output
Enter a number :
PSNA College of Engineering and Technology 2023-2027-CSE-D ID: 92132313206 Page No: 92
S.No: 50 Exp. Name: Factorial by using recursion Date: 2024-05-06
Aim:
Virat is exploring the concept of factorials and aims to create a program that calculates the factorial of a
Page No: 93
given number using recursion.
Can you assist Virat in writing a recursive C program to find the factorial of a given number?
Input Format:
The input line reads an integer.
ID: 92132313206
Output Format:
The output is an integer representing the factorial of the given integer.
Source Code:
FactorialUsingRecursion.c
#include<stdio.h>
2023-2027-CSE-D
return 1;
}
else
{
return a*(fact(a-1));
}
return 0;
void main(){
int a;
scanf("%d",&a);
printf("%d\n",fact(a));
}
User Output
5
120
Test Case - 2
User Output
6
720
S.No: 51 Exp. Name: Fill in the missing code Date: 2024-05-19
Aim:
Write a program to interchange the two specified rows of a matrix using pointer to an array.
Page No: 94
Fill in the missing code so that it produces the desired output.
ID: 92132313206
PointersToArray2.c
#include <stdio.h>
#include <stdlib.h>
void main() {
int (*a)[10], i, j, m, n, r1, r2, temp;
printf("Enter the row & column sizes of matrix : ");
scanf("%d %d", &m, &n);
a = (int(*)[10])malloc(m*n*sizeof(int)); // Allocate heap memory
printf("Enter %d elements : ", m * n);
for (i = 0; i < m; i++) {
2023-2027-CSE-D
for (j = 0; j < n; j++) {
scanf("%d",&a[i][j]); // Complete the statement
}
}
printf("The given matrix is\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", a[i][j]); // Complete the statement
User Output
Enter the row & column sizes of matrix :
22
Page No: 95
Enter 4 elements :
1234
The given matrix is
1 2
3 4
ID: 92132313206
Enter row numbers to be interchange :
12
Interchange of rows are not possible
Test Case - 2
User Output
Enter the row & column sizes of matrix :
32
Enter 6 elements :
2023-2027-CSE-D
395104
The given matrix is
3 9
5 1
0 4
Enter row numbers to be interchange :
12
Test Case - 3
User Output
Enter the row & column sizes of matrix :
43
Enter 12 elements :
257896420386
The given matrix is
2 5 7
8 9 6
4 2 0
3 8 6
Enter row numbers to be interchange :
01
After interchange the matrix is
8 9 6
2 5 7
4 2 0
3 8 6
Exp. Name: Program to search an element in
S.No: 52 Date: 2024-05-07
array using pointers
Aim:
Page No: 96
Program to search an element in an array using pointers
Input format:
The first line contains an integer n, no.of elements in the array.
The following line contains n space-separated integers, representing the elements in the first array.
ID: 92132313206
The last line contains an integer k, representing the element to be searched in the array.
Output format:
Return 1 if the element is found otherwise print -1.
Source Code:
searching.c
#include <stdio.h>
#define MAX_SIZE 100
// prototype functions
void inputArray(int * arr, int size);
int search(int * arr, int size, int toSearch);
2023-2027-CSE-D
int main() {
int n,x,b[50];
int *arr=b;
scanf("%d",&n);
inputArray(arr,n);
scanf("%d",&x);
int result=search(arr,n,x);
}
void inputArray(int * arr, int size) {
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
}
int search(int * arr, int size, int toSearch) {
int found=0;
for(int i=0;i<size;i++)
{
if(toSearch==arr[i])
{
found=1;
break;
}
else
continue;}
if(found==0)
return -1;
else
return 1;
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Page No: 97
5
12345
1
1
ID: 92132313206
Test Case - 2
User Output
7
8945627
10
-1
Test Case - 3
2023-2027-CSE-D
User Output
7
54 21 26 95 85 74 27
25
-1
User Output
6
584972
8
1
Exp. Name: Write a C program to Copy one String
S.No: 53 Date: 2024-05-07
into another using Pointers
Aim:
Page No: 98
Write a C program to copy one string into another using pointers.
ID: 92132313206
Source Code:
CopyStringPointers.c
#include <stdio.h>
#include "CopyStringPointers1.c"
void main() {
char source[100], target[100];
printf("Enter source string : ");
fgets(source, sizeof(source), stdin);
copyString(target, source);
printf("Target string : %s\n", target);
}
2023-2027-CSE-D
CopyStringPointers1.c
#include<stdio.h>
void copyString(char *target,char *source)
{
while(*source)
}
*target='\0';
}
User Output
Enter source string :
CodeTantra
Target string : CodeTantra
Test Case - 2
User Output
Enter source string :
Robotic Tool
Target string : Robotic Tool
PSNA College of Engineering and Technology 2023-2027-CSE-D ID: 92132313206 Page No: 99
Exp. Name: Demonstrate Pointers to Pointers
S.No: 54 Date: 2024-05-07
and Array of Pointers
Aim:
ID: 92132313206
• Declare an integer variable n and create an array of integer pointers named arr. Allow the user to
input the size of the array and then input n integers to store in the array using dynamic memory
allocation. Finally, print the elements of the array.
Source Code:
pointersArray.c
2023-2027-CSE-D
PSNA College of Engineering and Technology
#include <stdio.h>
#include <stdlib.h>
int main() {
int x;
int updatedValue;
ID: 92132313206
printf("Value of x: %d\n", x);
printf("Value pointed to by ptr1: %d\n",*ptr1);
printf("Value pointed to by ptr2 (using double indirection): %d\n",**ptr2);
2023-2027-CSE-D
int n;
printf("size: ");
scanf("%d", &n);
int *arr[n]; // Array of integer pointers
int a[n];
for(int i=0;i<n;i++)
arr[i]=&a[i];
printf("Elements: ");
return 0;
}
User Output
number:
8
Value of x: 8
Value pointed to by ptr1: 8
Value pointed to by ptr2 (using double indirection): 8
Modification value:
16
Updated value of x through double indirection: 16
size:
ID: 92132313206
Test Case - 2
User Output
number:
50
Value of x: 50
Value pointed to by ptr1: 50
Value pointed to by ptr2 (using double indirection): 50
Modification value:
45
2023-2027-CSE-D
Updated value of x through double indirection: 45
size:
3
Elements:
456
Array: 4 5 6
Aim:
Your task is to help him by writing a program that reads three variables of types int, float, and char and then
displays their values using pointers.
ID: 92132313206
Requirements:
• Declare the necessary variables to store an integer, a float, and a character.
• Use pointers to access and display the values of these variables.
• Ensure that you correctly handle user input.
Input Format:
• The program should prompt the user to enter values for an integer, a float, and a character in a single
line.
• The input should be in the following order: an integer, a float, and a character (e.g., 10 3.14 a).
Output Format:
2023-2027-CSE-D
The program should display the values in the following format:
• Given int value: <int_value>
• Given float value: <float_value>
• Given char value: <char_value>
Source Code:
pointer.c
#include <stdio.h>
User Output
Enter int, float and char values:
23 3.56 A
Given int value:23
Given float value:3.560000
Given char value:A
Test Case - 2
ID: 92132313206
Given char value:k
2023-2027-CSE-D
PSNA College of Engineering and Technology
Exp. Name: Store the address of the another
S.No: 56 Date: 2024-05-07
variable using pointer.
Aim:
ID: 92132313206
Instruction: To run custom test cases strictly map your input and output layout with the visible test cases.
Source Code:
type.c
#include<stdio.h>
int main()
{
int x,*px;
float y,*py;
char z,*pz;
2023-2027-CSE-D
printf("Enter the value: ");
scanf("%d %f %c",&x,&y,&z);
px=&x;
py=&y;
pz=&z;
printf("size of an int ptr = %d\n",sizeof(px));
printf("size of a float ptr = %d\n",sizeof(py));
printf("size of a char ptr = %d\n",sizeof(pz));
User Output
Enter the value:
10 5.5 a
size of an int ptr = 8
size of a float ptr = 8
size of a char ptr = 8
size of an int type = 4
size of a float type = 4
size of a char type = 1
S.No: 57 Exp. Name: String Transformation Date: 2024-05-07
Aim:
Mike has a unique way of modifying strings using pointers. He wants to remove all the characters other than
Input Format:
• A single line containing a string of characters (1 <= length of string <= 100).
ID: 92132313206
Output Format:
• A single line containing the modified string.
Note: The string may contain letters (uppercase and lowercase) and other printable characters, but no
spaces.
Source Code:
string.c
#include <stdio.h>
2023-2027-CSE-D
char a[50];
int j=0;
for(int i=0;str[i]!='\0';i++)
{
if(str[i]>='A'&&str[i]<='Z')
{
a[j]=str[i];
j++;
}
else{
continue;
}
}
for(int i=0;i<=j;i++)
{
str[i]=a[i];
}
int main() {
char str[100];
scanf("%s", str);
modifyString(str);
printf("%s\n", str);
return 0;
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Test Case - 2
ID: 92132313206
User Output
Mike123Rocks!
MIKEROCKS
2023-2027-CSE-D
PSNA College of Engineering and Technology
Exp. Name: Write a C program to implement a
S.No: 58 Nested Structure to store and display the Date: 2024-05-19
Student information
The structure student contains the fields regdno, name and date. Date is the nested structure and it
contains the fields day, month and year.
ID: 92132313206
At the time of execution, the program should print the following messages one by one on the console as:
Student number:
Student name:
Joining date:
For example, if the user gives the input as:
Student number: 1002
Student name: Saraswathi
Joining date: 31 01 2019
then the program should print the result as:
Number: 1002
Name: Saraswathi
Joining date: 31-1-2019
2023-2027-CSE-D
Note - 1: Do use the printf() function with a newline character (\n).
NestedStructureMain.c
#include <stdio.h>
NestedStructureFunctions.c
#include<stdio.h>
void read1(struct student *ps)
{
printf("Enter student number : ");
scanf("%d",&ps->regdno);
ID: 92132313206
{
printf("Number : %d",s.regdno);
printf("\nName : %s",s.name);
printf("\nDate of joining : %d-%d-%d\n",s.doj.day,s.doj.month,s.doj.year);}
2023-2027-CSE-D
User Output
Student number:
1001
Student name:
Sachin
Joining date:
Let us consider a structure student , containing name, age and height fields.
ID: 92132313206
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.
At the time of execution, the program should print the following messages one by one on the console as:
2023-2027-CSE-D
then the program should print the result as:
Note: Write the declaration of structure student and the functions read1(), copyStructureVariable(),
display() in CopyStructureMain.c .
CopyStructureMain.c
#include <stdio.h>
#include "CopyStructureFunctions.c"
void main() {
struct student s1, s2;
read1(&s1);
s2 = copyStructureVariable(s1, s2);
display(s2);
}
CopyStructureFunctions.c
#include<string.h>
#include<stdio.h>
struct student
{
char name[50];
ID: 92132313206
scanf("%s %d %f",ps->name,&ps->age,&ps->height);}
struct student copyStructureVariable(struct student s1,struct student s2)
{
strcpy(s2.name,s1.name);
s2.age=s1.age;
s2.height=s1.height;
return s2;
}
void display(struct student s)
{
printf("Student name : %s\n",s.name);
printf("Age : %d\n",s.age);
2023-2027-CSE-D
printf("Height : %f\n",s.height);}
Test Case - 2
User Output
Enter student name, age and height :
Kohli 21 5.11
Student name : Kohli
Age : 21
Height : 5.110000
Test Case - 3
User Output
Enter student name, age and height :
Nadal 20 6.3
Student name : Nadal
Age : 20
Height : 6.300000
PSNA College of Engineering and Technology 2023-2027-CSE-D ID: 92132313206 Page No: 112
S.No: 60 Exp. Name: Illustrate Self-Referential Structures Date: 2024-05-19
Aim:
Write a C program that demonstrates the concept of self-referential structures by implementing an array of
After initializing the array, prompt the user to input integer values for each element. Then, display the
elements of the array along with their indices and data.
ID: 92132313206
Sample Test Case:
Size of array: 5
Data for element 0: 6
Data for element 1: 4
Data for element 2: 8
Data for element 3: 2
Data for element 4: 10
Array elements:
Index: 0, Data: 6
Index:·1, Data: 4
Index:·2, Data: 8
2023-2027-CSE-D
Index:·3, Data: 2
Index:·4, Data: 10
Source Code:
SelfReferentialStructures.c
#include <stdio.h>
#include <stdlib.h>
User Output
Size of array:
5
Data for element 0:
6
Data for element 1:
ID: 92132313206
Data for element 4:
10
Array elements:
Index: 0, Data: 6
Index: 1, Data: 4
Index: 2, Data: 8
Index: 3, Data: 2
Index: 4, Data: 10
Test Case - 2
2023-2027-CSE-D
User Output
Size of array:
4
Data for element 0:
65
Data for element 1:
Aim:
StudentDataUsingUnion.c
ID: 92132313206
#include<stdio.h>
union student {
char name[50];
int rn;
int marks[3];
};
void main() {
int n;
printf("Enter the number of students : ");
scanf("%d",&n);
union student s[n];
for(int i=0;i<n;i++){
2023-2027-CSE-D
printf("Enter student - %d data\n",i+1);
printf("Rollno : ");
scanf("%d",&s[i].rn);
printf("Rollno : %d\n",s[i].rn);
printf("Name : ");
scanf("%s",s[i].name);
printf("Name : %s\n",s[i].name);
for(int j=0;j<3;j++){
User Output
Enter the number of students :
2
Enter student - 1 data
Rollno :
101
Rollno : 101
Name :
Sachin
Name : Sachin
Subject - 1 marks :
67
subject - 1 marks : 67
Subject - 2 marks :
89
ID: 92132313206
Rollno :
102
Rollno : 102
Name :
Kohli
Name : Kohli
Subject - 1 marks :
89
subject - 1 marks : 89
Subject - 2 marks :
64
2023-2027-CSE-D
subject - 2 marks : 64
Subject - 3 marks :
72
subject - 3 marks : 72
Aim:
Implement a structure to represent a student in the Weather Monitoring System and write a function to
calculate and display the highest value among the three parameters (Temperature, Humidity, and Wind
ID: 92132313206
Speed) for accurate weather reporting.
Input Format:
5. The first line is an integer representing the roll number of the student.
6. The second line is a float value representing the Temperature recorded by the student.
7. The third line is a float value representing the Humidity recorded by the student.
8. The fourth line is a float value representing the Wind Speed recorded by the student.
Output Format:
The output is a float value rounded to two decimal places, representing the highest value among
Temperature, Humidity, and Wind Speed for accurate weather reporting.
2023-2027-CSE-D
Note:
• The main function is provided to you in the editor. You just need to fill in the required code.
Source Code:
weatherSystem.c
};
// Function to calculate and display the highest value among Temperature, Humidity,
ID: 92132313206
and Wind Speed
float calculate_and_display_highest_value( struct WeatherMonitoringStudent w ) {
if(w.temperature>w.humidity&&w.temperature>w.wind_speed)
{
return w.temperature;}
else if(w.humidity>w.wind_speed)
{
return w.humidity;
}
else
return w.wind_speed;
2023-2027-CSE-D
}
int main() {
// Input from the user
struct WeatherMonitoringStudent student_info;
scanf("%d", &student_info.roll_number);
scanf("%f", &student_info.humidity);
scanf("%f", &student_info.wind_speed);
return 0;
}
User Output
101
30.25
12.03
74
121
85.46
74.00
12.236
25.623
85.456
User Output
Test Case - 2
PSNA College of Engineering and Technology 2023-2027-CSE-D ID: 92132313206 Page No: 119
S.No: 63 Exp. Name: Student Database - Average of marks Date: 2024-05-19
Aim:
A university is building a student database system. Each student has the following information: name, roll
Input format:
The first line is the string that represents the name of the student.
The second line is the integer that represents the roll number of the student.
ID: 92132313206
The third line is the float value that represents the Math marks.
The fourth line is the float value that represents the Physics marks.
The fifth line is the float value that represents the Chemistry marks.
Output format:
The output is a float value rounded up to 2 decimal places that represents the average of the marks.
Note:
• The main function is provided to you in the editor. You just need to fill in the required code.
Source Code:
2023-2027-CSE-D
studentDatabase.c
#include <stdio.h>
// Definition of the Student structure
struct Student {
char name[30];
int rollNumber;
float mathMarks,physicsMarks,chemistryMarks;
};
}
int main() {
// Example usage of the Student structure and calculateAverage function
struct Student student1;
scanf("%d", &student1.rollNumber);
scanf("%f", &student1.mathMarks);
scanf("%f", &student1.physicsMarks);
scanf("%f", &student1.chemistryMarks);
return 0;
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
ID: 92132313206
86.07
Test Case - 2
User Output
Kyathi
890
98.5
92.5
2023-2027-CSE-D
89
93.33
Aim:
You are developing a program to manage movie details for a film festival. Each movie has the following
Input format:
ID: 92132313206
The first line is the string that represents the title of a movie.
The second line is the string that represents the name of a director.
The third line is the integer that represents the released year.
After displaying the information, the input is the integer that represents the updated released year.
Output format:
After entering all the details of the movie, It will display the information as shown in the displayed test
cases.
After entering the updated released year, It will display the updated information as shown in the displayed
test cases.
Note:
2023-2027-CSE-D
• The main function is provided to you in the editor. You just need to fill in the required code.
Source Code:
movieDetails.c
struct Movie {
char title[30];
char director[30];
};
ID: 92132313206
printf("Title: %s\n",m.title);
printf("Director: %s\n",m.director);
printf("Release Year: %d\n",m.releaseYear);
}
int main() {
2023-2027-CSE-D
// Example usage of the Movie structure and functions
struct Movie movie1;
return 0;
}
User Output
Bahubali
SS Rajamouli
2014
Title: Bahubali
Director: SS Rajamouli
Release Year: 2014
2015
Test Case - 2
ID: 92132313206
User Output
Titanic
James Cameroon
195
Title: Titanic
Director: James Cameroon
Release Year: 195
1997
Title: Titanic
2023-2027-CSE-D
Director: James Cameroon
Release Year: 1997
Aim:
The program is to write a structure containing student roll number, name, marks into a file and read them to
print on the standard output device.
Source Code:
ID: 92132313206
FilesStructureDemo1.c
#include <stdio.h>
#include <stdlib.h>
struct student {
int roll;
char name[50];
float marks;
};
int main() {
int n = 0;
char choice;
2023-2027-CSE-D
struct student *students = NULL;
do {
free(students);
return 0;
}
User Output
Roll no:
501
ID: 92132313206
y
Roll no:
502
Name:
Smith
Marks:
65
Want to add another data (y/n):
n
Data written successfully
2023-2027-CSE-D
Roll Name Marks
501 Ganga 92.000000
502 Smith 65.000000
Aim:
moveFilePointer.c
#include <stdio.h>
ID: 92132313206
#include <unistd.h>
int main() {
FILE *fptr;
char filename[100];
long position;
printf("Enter the filename: ");
scanf("%s", filename);
if ((fptr = fopen(filename, "r")) == NULL) {
printf("Error opening the file.\n");
return 1;
}
fclose(fptr);
2023-2027-CSE-D
if ((fptr = fopen(filename, "r+")) == NULL) {
printf("Error opening the file.\n");
return 1;
}
printf("Enter the position to move the file pointer to: ");
scanf("%ld", &position);
if (fseek(fptr, position, SEEK_SET) != 0) {
printf("Error moving the file pointer to position %ld.\n", position);
file1.txt
An eccentric inventor named Professor Westwood built a time machine and accidentally
transported himself to the past. He encountered his younger self, creating a paradox
that threatened to unravel time itself.
file2.txt
Deep in the heart of the forest, a young girl named Maya discovered a hidden
entrance to an enchanted realm.
User Output
Enter the filename:
file1.txt
ID: 92132313206
Test Case - 2
User Output
Enter the filename:
file3.txt
Error opening the file.
2023-2027-CSE-D
PSNA College of Engineering and Technology
Exp. Name: Creating a text file using file
S.No: 67 Date: 2024-06-04
handling, example of fopen, fclose
Aim:
ID: 92132313206
• Close the file
Instruction: Map your input and output as per sample test cases given.
Source Code:
file.c
#include <stdio.h>
void main() {
FILE *fp;
char input[100];
char ch;
2023-2027-CSE-D
int i = 0;
printf("Enter the text with @ at end : ");
while (1) {
ch = getchar();
if (ch == '@') {
break;
}
User Output
Enter the text with @ at end :
A quick brown fox love coding@
Given message is : A quick brown fox love coding
Test Case - 2
User Output
Enter the text with @ at end :
@
Given message is :
Exp. Name: Write a C program to Insert, Update,
S.No: 68 Delete and Append Telephone details into a Date: 2024-06-04
Telephone directory
ID: 92132313206
Phonebook Menu
(1) Add Friend
(2) Delete Friend
(3) Display Phonebook Entries
(4) Search for Phone Number
(5) Exit Phonebook
What would you like to do? : 1
First Name: Venkat
Last Name: Govind
Phone Number (XXX-XXX-XXXX): 9908125678
Friend successfully added to Phonebook
2023-2027-CSE-D
Phonebook Menu
(1) Add Friend
(2) Delete Friend
(3) Display Phonebook Entries
(4) Search for Phone Number
(5) Exit Phonebook
What would you like to do? : 3
Phonebook Entries:
Source Code:
TelephoneBook.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 20
ID: 92132313206
void deleteFriend(phone *phonebook, int *counter) {
char deleteFirstName[MAX_NAME_LENGTH];
char deleteLastName[MAX_NAME_LENGTH];
2023-2027-CSE-D
strcmp(phonebook[x].LastName, deleteLastName) == 0) {
// Shift elements to overwrite the deleted entry
for (int i = x; i < *counter - 1; i++) {
phonebook[i] = phonebook[i + 1];
}
(*counter)--; // Decrement the counter
printf("Record deleted from the phonebook.\n");
return;
printf("Please type the name of the friend you wish to find a number for.\n");
printf("First Name: ");
scanf("%19s", TempFirstName);
printf("Last Name: ");
scanf("%19s", TempLastName);
for (int x = 0; x < counter; x++) {
if (strcmp(phonebook[x].FirstName, TempFirstName) == 0 &&
strcmp(phonebook[x].LastName, TempLastName) == 0) {
printf("%s %s's phone number is %s\n", phonebook[x].FirstName,
phonebook[x].LastName, phonebook[x].PhoneNumber);
return;
}
}
printf("That contact was not found, please try again.\n");
int main() {
int choice, counter = 0;
phone phonebook[100]; // Assuming a maximum of 100 entries
ID: 92132313206
do {
printf("\t\t\tPhonebook Menu\n");
printf("\t(1)\tAdd Friend\n");
printf("\t(2)\tDelete Friend\n");
printf("\t(3)\tDisplay Phonebook Entries\n");
printf("\t(4)\tSearch for Phone Number\n");
printf("\t(5)\tExit Phonebook\n");
printf("What would you like to do? : ");
scanf("%d", &choice);
switch (choice) {
case 1: {
2023-2027-CSE-D
printf("First Name: ");
scanf("%19s", phonebook[counter].FirstName);
printf("Last Name: ");
scanf("%19s", phonebook[counter].LastName);
printf("Phone Number (XXX-XXX-XXXX): ");
scanf("%19s", phonebook[counter].PhoneNumber);
printf("\tFriend successfully added to Phonebook\n");
counter++;
return 0;
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
ID: 92132313206
What would you like to do? :
1
First Name:
Sachin
Last Name:
Tendulkar
Phone Number (XXX-XXX-XXXX):
1234567890
Friend successfully added to Phonebook
Phonebook Menu
2023-2027-CSE-D
(1) Add Friend
(2) Delete Friend
(3) Display Phonebook Entries
(4) Search for Phone Number
(5) Exit Phonebook
What would you like to do? :
3
Test Case - 2
User Output
Phonebook Menu
(1) Add Friend
(2) Delete Friend
(3) Display Phonebook Entries
(4) Search for Phone Number
(5) Exit Phonebook
What would you like to do? :
1
First Name:
Virat
Last Name:
Kohli
Phone Number (XXX-XXX-XXXX):
ID: 92132313206
(3) Display Phonebook Entries
(4) Search for Phone Number
(5) Exit Phonebook
What would you like to do? :
3
Phonebook Entries:
(1)
Name: Virat Kohli
Number: 9873425674
Phonebook Menu
(1) Add Friend
2023-2027-CSE-D
(2) Delete Friend
(3) Display Phonebook Entries
(4) Search for Phone Number
(5) Exit Phonebook
What would you like to do? :
2
First name:
kohli
ID: 92132313206
2023-2027-CSE-D
PSNA College of Engineering and Technology
S.No: 69 Exp. Name: Usage Pre-processor Directives Date: 2024-05-21
Aim:
Write a C program that uses preprocessor directives to define macros for calculating the sum and product
SumProduct.c
#include <stdio.h>
ID: 92132313206
#define SUM(a,b) (a+b)
#define PRODUCT(a,b) (a*b)
int main()
{
float n1,n2;
printf("n1: ");
scanf("%f",&n1);
printf("n2: ");
scanf("%f",&n2);
printf("Sum: %.2f\n",SUM(n1,n2));
printf("Product: %.2f\n",PRODUCT(n1,n2));
2023-2027-CSE-D
return 0;
}
User Output
n1:
3
n2:
8
Sum: 11.00
Product: 24.00
Test Case - 2
User Output
n1:
4
n2:
6
Sum: 10.00
Product: 24.00
S.No: 70 Exp. Name: Characters count in a file Date: 2024-05-21
Aim:
Imagine you are helping a friend, Alex, who is writing a story. Alex has completed a few lines and saved
Can you assist Alex by providing a C program that counts the characters in the file up to the character @
and displays the result?
Input Format:
ID: 92132313206
The input line reads a string representing the file name.
Output Format:
The output is an integer representing the count of characters in the file till @. If the file does not exist then
print "Error opening the file. Make sure the file exists.".
Note:
• Count all characters including spaces and special characters till @.
• Print a newline character (\n) after every output statement.
Source Code:
2023-2027-CSE-D
FileCharactersCount.c
#include<stdio.h>
#define MAX_FILE_NAME 100
int main(){
FILE*fp;
int count =0;
char filename[MAX_FILE_NAME];
char c;
fp=fopen(filename,"r");
if(fp==NULL){
printf("Error opening the file. Make sure the file exists.\n");
return 0;
}
while(1){
c=fgetc(fp);
if(c=='@')
break;
++count;
}
fclose(fp);
printf("%d\n",count);
return 0;
}
text.txt
123456789@
sample.txt
kl.txt
ID: 92132313206
KL University@
User Output
text.txt
2023-2027-CSE-D
50
Test Case - 2
User Output
hello.txt
Error opening the file. Make sure the file exists.
Aim:
Emma, a young student, has written a poem for her school project and saved it in a file. Emma is curious to
Can you help Emma by providing a simple C program that reads and displays the contents of her poem file?
Input Format:
The input line is a string representing the file name. If the file does not exist print "Error opening the file.
ID: 92132313206
Make sure the file exists.".
Output Format:
The output is a poem from the file.
Note: The file contents end with '@', make sure to read the contents till @.
Source Code:
FilesofPoems.c
#include<stdio.h>
#include<stdlib.h>
2023-2027-CSE-D
int main(){
FILE *fptr;
char filename[100],c;
scanf("%s",filename);
fptr = fopen(filename,"r");
if(fptr == NULL){
c=fgetc(fptr);
printf("Contents of the file:\n");
while(c != '@'){
printf("%c",c);
c = fgetc(fptr);
}
fclose(fptr);
return 0;
}
MorningLight.txt
Sunflower so bright,
Follows the sun's light.
ID: 92132313206
Petals in a golden swirl,
Nature's radiant twirl. @
RainDrop.txt
2023-2027-CSE-D
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
MorningLight.txt
Test Case - 2
User Output
Sample.txt
Error opening the file. Make sure the file exists.
S.No: 72 Exp. Name: Add Entries at the end of a File Date: 2024-06-04
Aim:
We are maintaining a file for employees working in an office of their logged in timings and sign off timings.
Sample input:
Enter number of employees: 5
login: 6AM
signoff: 4PM
ID: 92132313206
login: 8AM
signoff: 6PM
login: 10:15AM
signoff: 7PM
login: 11AM
signoff: 9PM
login: 9AM
signoff: 8PM
Sample output:
Employees timings:
(1) login: 6AM
2023-2027-CSE-D
signoff: 4PM
(2) login: 8AM
signoff: 6PM
(3) login: 10:15AM
signoff: 7PM
(4) login: 11AM
signoff: 9PM
(5) login: 9AM
EmployeeDataFile.c
#include <stdio.h>
#include <stdlib.h>
#define MAX_EMPLOYEES 100
#define MAX_TIME_LENGTH 9
ID: 92132313206
void add_entry(int employee_id, char *login_time, char *signoff_time, FILE *file) {
fprintf(file, "(%d) login: %s\nsignoff: %s\n", employee_id, login_time,
signoff_time);
}
int main() {
struct Employee employees[MAX_EMPLOYEES];
int num_employees;
2023-2027-CSE-D
FILE *file = fopen("employee_timings.txt", "a");
if (file == NULL) {
printf("Error opening file for writing.\n");
return 1;
}
fclose(file);
printf("Employees timings:\n");
for (int i = 0; i < num_employees; ++i) {
printf("(%d)login: %s\nsingoff: %s\n", i + 1, employees[i].login,
employees[i].signoff);
}
return 0;
}
User Output
no of employees:
5
ID: 92132313206
8AM
signoff:
6PM
login:
10:15AM
signoff:
7PM
login:
11AM
signoff:
2023-2027-CSE-D
9PM
login:
9AM
signoff:
8PM
Employees timings:
(1)login: 6AM