Lab Record Download
Lab Record Download
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
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]".
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);
}
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
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
arithmeticOperations.c
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
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);
}
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);
}
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:
2024-2028-CSE-D
then the program should print the result as:
Note: Do use the printf() function and ensure that there is a '\n' at the end after print
the result.
Source Code:
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
User Output
Enter an integer:
2
Square root: 1.414
4
User Output
Enter an integer:
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
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):
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
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:
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>
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 :
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)
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;
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;
printf("num2: ");
scanf("%d",&num2);
printf("num3: ");
scanf("%d",&num3);
greatest = ((num1>num2)?(num1>num3?num1:num3):(num2>num3)?
num2:num3);
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:
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.
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(){
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
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
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
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);
}
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
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).
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;
}
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
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
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);
}
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.
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
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;
}
}
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
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){
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);
}
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);
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
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:
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
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++){
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);
User Output
143
121
User Output
121 is a palindrome.
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:
2024-2028-CSE-D
1
1 2
1 2 3
Source Code:
#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
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];
}
User Output
5
Elements:
49682
Min an Max: 2 and 9
1
216
Elements:
User Output
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).
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);
}
}
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
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]);
}
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
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--){
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);
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
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
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];
}
printf("\n");
}*/
}
printf("\n");
}
}
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
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
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");
Page No: 67
printf("\n");
}
}
else{
printf("Multiplication not possible\n");
ID: 24B91A05R0
}
}
User Output
22
2024-2028-CSE-D
11 22
33 44
22
11 22
33 44
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:
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]);
}
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
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 :
string1 : ILove
Next, the program should print the message on the console as:
2024-2028-CSE-D
string2 :
string2 : Coding
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);
}
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 :
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
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.
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);
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
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
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
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); //
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:
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
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;
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;
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
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.
2024-2028-CSE-D
Subject-1 marks : 58
Total marks : 214
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
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;
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:
Next, the program should print the message on the console as:
2024-2028-CSE-D
Do u want another list (y|n) :
Finally, the program should print the result on the console as:
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 : ");
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
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
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;
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
}
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
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);
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"
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);
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
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:
2024-2028-CSE-D
Enter the values of n and r : 4 2
If the input is given as 2 and 5 then the program should print the result as:
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>
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");
}
2024-2028-CSE-D
Test Case - 1
User Output
Enter the values of n and r :
10 4
The value of 10c4 = 210
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
Aim:
Write a C program to find the length of a given string.
ID: 24B91A05R0
Sample Input and Output - 1:
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));
}
}
return l;
}
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
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
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++)
int main() {
printf("rows: ");
scanf("%d", &rows);
printf("columns: ");
scanf("%d", &cols);
int matrix[rows][cols];
// Input: Read the matrix elements
ID: 24B91A05R0
transposeMatrix(matrix);
return 0;
}
2024-2028-CSE-D
User Output
rows:
2
columns:
2
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-
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.
2024-2028-CSE-D
equation.
Source Code:
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
}
}
int main() {
}*/
User Output
initial value of y (y0):
1
initial value of t (t0):
1
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
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));
}
}
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
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:
2024-2028-CSE-D
Enter two integer values : 25 15
Note: Write the function lcm() and recursive function gcd() in Program907a.c .
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){
ID: 24B91A05R0
}
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
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
ID: 24B91A05R0
2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Exp. Name: Write a C program to find
Date: 2024-12-
Aim:
ID: 24B91A05R0
Write a program to find the factorial of a given number using recursion process.
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
User Output
Enter an integer :
5
Factorial of 5 is : 120
Test Case - 2
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
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:
2024-2028-CSE-D
A(2, 1) = 5
Source Code:
AckermannFunction.c
AckermannFunction1.c
long long int ackermannFun(int m,int n){
ID: 24B91A05R0
else{
return ackermannFun(m-1,ackermannFun(m,n-1));
}
}
2024-2028-CSE-D
User Output
Enter two numbers :
01
A(0, 1) = 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
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
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 :
Enter value of n : 6
2024-2028-CSE-D
Sum of 6 natural numbers = 21
Program903.c
Program903a.c
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-
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:
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 ).
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){
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
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
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
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>
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));
2024-2028-CSE-D
ptr1=&value;
return 0;
}
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
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);
}
#include<stdio.h>
void copyString(char *target, char *source){
while(*source != '\0'){
*target = *source;
target++;
source++;
}
*target = '\0';
}
User Output
Enter source string :
CodeTantra
Target string : CodeTantra
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-
Aim:
ID: 24B91A05R0
Write a C program to find number of lowercase , uppercase , digits and
other characters using pointers.
2024-2028-CSE-D
Source Code:
CountCharDigitOthers.c
#include <stdio.h>
#include "CountCharDigitOthers1.c"
void main() {
CountCharDigitOthers1.c
void countCharDigitOthers(char str[80],int *upperCount,int
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.
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
ID: 24B91A05R0
2024-2028-CSE-D
SRKR Engineering College (Autonomous)
Date: 2024-12-
S.No: 56 Exp. Name: Write the code
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);
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
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.
Test Case - 2
User Output
Enter the name of the file to read:
file2.txt
Content of the file file2.txt:
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-
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>
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");
User Output
Roll no:
501
Name:
Ganga
Marks:
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
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
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);
}
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-
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:
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);
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 :
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>
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
User Output
Enter the text with @ at end :
Arise! Awake!
and stop not until
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
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)){
input1.txt
input2.txt
Beautiful is better than ugly.
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
User Output
good idea.
Test Case - 2
User Output
verything matters.