Lab Record Download
Lab Record Download
1 :oN egaP
S.No: 1 Exp. Name: Display hello world message
19
Aim:
2 Q 4 5 A 1 9 B 4 2 : DI
Write a C program to display hello world message
Source Code:
hello.c
#include<stdio.h>
void main()
{
printf("Hello World\n");
}
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Hello World
2 :oN egaP
and display them 19
Aim:
Write a C program to scan all data type variables(int, float, char, double) as input and print
2 Q 4 5 A 1 9 B 4 2 : DI
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:
D- B S C & DI A- 8 2 0 2- 4 2 0 2
• 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
#include<stdio.h>
3 :oN egaP
int main()
{
int a;
float f;
char c;
2 Q 4 5 A 1 9 B 4 2 : DI
double d;
printf("integer: ");
scanf("%d",&a);
printf("floating-point number: ");
scanf("%f",&f);
printf("character: ");
scanf(" %c",&c);
printf("double: ");
scanf("%lf",&d);
printf("You entered:\n");
printf("Integer: %d\n",a);
D- B S C & DI A- 8 2 0 2- 4 2 0 2
printf("Float: %f\n",f);
printf("Character: %c\n",c);
printf("Double: %lf\n",d);
return 0;
}
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
4 :oN egaP
User Output
integer:
-10
2 Q 4 5 A 1 9 B 4 2 : DI
floating-point number:
12.2546
character:
T
double:
12.6789678
You entered:
Integer: -10
Float: 12.254600
Character: T
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Double: 12.678968
5 :oN egaP
20
Aim:
Write a C program to perform arithmetic operations like +,-,*,/,% on two input variables.
2 Q 4 5 A 1 9 B 4 2 : DI
Input Format:
• The first line of input is an integer representing the value for first number
• The second line of input is an integer representing the value of second number
Output Format:
• The program prints the results of addition, subtraction, multiplication, division, and
modulus each on a new line.
Note : For Division and Modulo operation, the value of num2 must be greater than 0
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Add new line char \n at the end of output.
Source Code:
arithmeticOperations.c
#include<stdio.h>
int main()
{
int num1,num2,Sum,Difference,Product,Division,Modulus;
Test Case - 2
User Output
User Output
1000
1002
2000
998
500
17
72
9
8
2
1
1
1
0
Exp. Name: Write a C program to find Date: 2024-09-
S.No: 4
7 :oN egaP
Sum and Average of three numbers 20
Aim:
Write a program to find the sum and average of the three given integers.
2 Q 4 5 A 1 9 B 4 2 : DI
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 Average;
D- B S C & DI A- 8 2 0 2- 4 2 0 2
printf("Enter three integers : ");
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&c);
Sum=a+b+c;
printf("Sum of %d, %d and %d : %d\n",a,b,c,Sum);
Average=(a+b+c)/3.0;
printf("Average of %d, %d and %d : %f\n",a,b,c,Average);
Test Case - 1
User Output
Test Case - 2
User Output
8 :oN egaP
Test Case - 3
2 Q 4 5 A 1 9 B 4 2 : DI
User Output
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
Exp. Name: Temperature conversions
Date: 2024-09-
9 :oN egaP
S.No: 5 from Centigrade to Fahrenheit and vice
22
versa.
Aim:
2 Q 4 5 A 1 9 B 4 2 : DI
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;
D- B S C & DI A- 8 2 0 2- 4 2 0 2
scanf("%f",&c);
f=(c*1.8)+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
01 :oN egaP
20
Aim:
Write a program to calculate the simple interest by reading principle amount, rate of
2 Q 4 5 A 1 9 B 4 2 : DI
interest and time.
At the time of execution, the program should print the message on the console as:
D- B S C & DI A- 8 2 0 2- 4 2 0 2
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:
11 :oN egaP
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
Exp. Name: Calculate the square root of Date: 2024-09-
S.No: 7
21 :oN egaP
an integer 20
Aim:
Write a program that prompts the user to enter an integer and calculates its square root.
2 Q 4 5 A 1 9 B 4 2 : DI
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.
D- B S C & DI A- 8 2 0 2- 4 2 0 2
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
Test Case - 1
31 :oN egaP 2 Q 4 5 A 1 9 B 4 2 : DI D- B S C & DI A- 8 2 0 2- 4 2 0 2 )suomonotuA( egelloC gnireenignE RKRS
Test Case - 2
Square root: 1.414
Enter an integer:
User Output
2
4
Exp. Name: Calculate simple interest and Date: 2024-09-
S.No: 8
41 :oN egaP
compound interest 20
Aim:
Write a C program to calculate the simple interest and compound interest by reading
2 Q 4 5 A 1 9 B 4 2 : DI
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)
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Example:
Input:
5000 7 5
Output:
SI= 1750.000000
CI= 2012.760376
Program315.c
//simple c program about SI and CI
51 :oN egaP
#include<stdio.h>
#include<math.h>
int main()
{
float p,r,t,si,ci;
2 Q 4 5 A 1 9 B 4 2 : DI
printf("Enter P,R,T: ");
scanf("%f",&p);
scanf("%f",&r);
scanf("%f",&t);
si=(p*t*r)/100;
printf("SI= %f\n",si);
ci=p*(pow(1+(r/100),t))-p;
printf("CI= %f\n",ci);
}
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
5000 7 5
SI= 1750.000000
Test Case - 2
User Output
61 :oN egaP
heron's formula 20
Aim:
Write a program to find the area of a triangle using Heron's formula.
2 Q 4 5 A 1 9 B 4 2 : DI
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:
D- B S C & DI A- 8 2 0 2- 4 2 0 2
area: 2.49
Instruction: Your input and output layout must match with the sample test cases (values
as well as text strings).
Program313.c
//to find the area of the triangle//
71 :oN egaP
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,p,area;
2 Q 4 5 A 1 9 B 4 2 : DI
printf("sides: ");
scanf("%f",&a);
scanf("%f",&b);
scanf("%f",&c);
p=(a+b+c)/2;
area=sqrt(p*(p-a)*(p-b)*(p-c));
printf("area: %.2f",area);
}
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
sides:
2.3 2.4 2.5
area: 2.49
User Output
sides:
2.6 2.7 2.8
area: 3.15
Exp. Name: Distance travelled by an Date: 2024-09-
S.No: 10
81 :oN egaP
object 20
Aim:
Write a program to find the distance travelled by an object.
2 Q 4 5 A 1 9 B 4 2 : DI
Sample Input and Output:
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
DistanceTravelled.c
Test Case - 1
User Output
91 :oN egaP
5
Enter the time taken :
6
Distance travelled : 102.000000
2 Q 4 5 A 1 9 B 4 2 : DI
Test Case - 2
User Output
D- B S C & DI A- 8 2 0 2- 4 2 0 2
10
Distance travelled : 250.000000
Test Case - 3
User Output
Test Case - 4
User Output
02 :oN egaP
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
45.8
Enter the time taken :
4
Distance travelled : 1188.000000
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
Date: 2024-09-
S.No: 11 Exp. Name: Evaluate the expressions
12 :oN egaP
20
Aim:
Write a C program to evaluate the following expressions.
2 Q 4 5 A 1 9 B 4 2 : DI
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>
D- B S C & DI A- 8 2 0 2- 4 2 0 2
int main()
{
int a,b,c,d,e,f,g,i,j,k,l,m;
printf("Enter values for A, B, C, D, E, F, G, i: ");
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&c);
scanf("%d",&d);
scanf("%d",&e);
Test Case - 1
Enter values for A, B, C, D, E, F, G, i:
12345678
22 :oN egaP
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
2 Q 4 5 A 1 9 B 4 2 : DI
Test Case - 2
User Output
D- B S C & DI A- 8 2 0 2- 4 2 0 2
d.J = (i++) + (++i) = 4
32 :oN egaP
using a conditional operator 20
Aim:
Write a C program to display the greatest of three numbers using a conditional operator
2 Q 4 5 A 1 9 B 4 2 : DI
(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
D- B S C & DI A- 8 2 0 2- 4 2 0 2
#include <stdio.h>
int main() {
int num1, num2, num3, greatest;
printf("num3: ");
scanf("%d",&num3);
return 0;
}
Execution Results - All test cases have succeeded!
42 :oN egaP
Test Case - 1
User Output
num1:
2 Q 4 5 A 1 9 B 4 2 : DI
8
num2:
9
num3:
90
Greatest number: 90
Test Case - 2
D- B S C & DI A- 8 2 0 2- 4 2 0 2
User Output
num1:
5
num2:
45
num3:
6
52 :oN egaP
and Average of 5 subjects marks 20
Aim:
Write a program to take marks of 5 subjects in integers, and find the total , average
2 Q 4 5 A 1 9 B 4 2 : DI
in float.
Note: Use the printf() function with a newline character ( \n ) to print the output at the
end.
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Source Code:
TotalAndAvg.c
#include<stdio.h>
int main()
{
int a,b,c,d,e;
Test Case - 1
User Output
62 :oN egaP
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
Average marks : 55.400002
Test Case - 3
User Output
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Test Case - 4
User Output
Test Case - 5
User Output
Test Case - 6
User Output
72 :oN egaP
Max and Min of Four numbers 24
Aim:
Write a program to find the max and min of four numbers.
2 Q 4 5 A 1 9 B 4 2 : DI
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.
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Source Code:
MinandMaxOf4.c
82 :oN egaP
int main()
{
int a,b,c,d;
printf("Enter 4 numbers : ");
scanf("%d %d %d %d",&a ,&b ,&c ,&d);
2 Q 4 5 A 1 9 B 4 2 : DI
if ((a>b)&&(a>c)&&(a>d))
{
printf("Max value : %d\n",a);
}
if((b>a)&&(b>c)&&(b>d))
{
printf("Max value : %d\n",b);
}
if((c>a)&&(c>b)&&(c>d))
{
printf("Max value : %d\n",c);
D- B S C & DI A- 8 2 0 2- 4 2 0 2
}
if((d>a)&&(d>b)&&(d>c))
{
printf("Max value : %d\n",d);
}
if((a<b)&&(a<c)&&(a<d))
{
printf("Min value : %d\n",a);
}
Test Case - 1
User Output
92 :oN egaP
Enter 4 numbers :
9852
Max value : 9
Min value : 2
2 Q 4 5 A 1 9 B 4 2 : DI
Test Case - 2
User Output
Enter 4 numbers :
112 245 167 321
Max value : 321
Min value : 112
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Test Case - 3
User Output
Enter 4 numbers :
110 103 113 109
Max value : 113
Min value : 103
User Output
Enter 4 numbers :
-34 -35 -24 -67
Max value : -24
Min value : -67
Test Case - 5
User Output
Enter 4 numbers :
24 28 34 16
Max value : 34
Min value : 16
03 :oN egaP 2 Q 4 5 A 1 9 B 4 2 : DI D- B S C & DI A- 8 2 0 2- 4 2 0 2 )suomonotuA( egelloC gnireenignE RKRS
Enter 4 numbers :
13 :oN egaP
charges 26
Aim:
An electricity board charges the following rates for the use of electricity:
2 Q 4 5 A 1 9 B 4 2 : DI
• 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%
D- B S C & DI A- 8 2 0 2- 4 2 0 2
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).
Source Code:
23 :oN egaP
int main()
{
char a[5];
printf("Enter customer name: ");
scanf("%s",a);
2 Q 4 5 A 1 9 B 4 2 : DI
float s,c,A;
int d;
printf("Units consumed: ");
scanf("%d",&d);
printf("Customer name: %s\n",a);
printf("Units consumed: %d\n",d);
if(d<=200)
{
s=(d*80)/100.0;
printf("Amount charged: %.2f\n",s);
}
D- B S C & DI A- 8 2 0 2- 4 2 0 2
else if((d>200)&&(d<=300))
{
s=(d*90)/100;
printf("Amount charged: %.2f\n",s);
}
else if(d>300)
{
s=d*1;
printf("Amount charged: %.2f\n",s);
33 :oN egaP
Test Case - 1
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
John
Units consumed:
78
Customer name: John
Units consumed: 78
Amount charged: 62.40
Surcharges: 0.00
Amount to be paid: 100.00
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Test Case - 2
User Output
Test Case - 3
User Output
43 :oN egaP
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
300
Customer name: Raman
Units consumed: 300
Amount charged: 270.00
Surcharges: 0.00
Amount to be paid: 270.00
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
Exp. Name: C program to find roots and Date: 2024-09-
S.No: 16
53 :oN egaP
nature of quadratic equation. 24
Aim:
Write a C program to find the roots of a quadratic equation, given its coefficients.
2 Q 4 5 A 1 9 B 4 2 : DI
Source Code:
quad.c
#include<stdio.h>
#include<math.h>
int main()
{
double a,b,c,root1,root2,d,realpart,imaginarypart;
printf("Enter coefficients a, b and c: ");
scanf("%lf%lf%lf",&a,&b,&c);
D- B S C & DI A- 8 2 0 2- 4 2 0 2
d=(b*b)-4*a*c;
if(d>0)
{
root1=((-b)+sqrt(d))/2*a;
root2=((-b)-sqrt(d))/2*a;
printf("root1 =%.2lf and root2 = %.2lf\n",root1,root2);
}
else if(d==0)
{
Test Case - 1
User Output
63 :oN egaP
Test Case - 2
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
Enter coefficients a, b and c:
886
root1 = -0.50+0.71i and root2 = -0.50-0.71i
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
Date: 2024-09-
S.No: 17 Exp. Name: Simulate a basic calculator
73 :oN egaP
26
Aim:
Write a program to perform basic calculator operations [+, -, *, /] of two integers a and b
2 Q 4 5 A 1 9 B 4 2 : DI
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.
D- B S C & DI A- 8 2 0 2- 4 2 0 2
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
#include<stdio.h>
}
}
83 :oN egaP
User Output
36-31
5
2 Q 4 5 A 1 9 B 4 2 : DI
Test Case - 2
User Output
89/45
1
Test Case - 3
D- B S C & DI A- 8 2 0 2- 4 2 0 2
User Output
13+1000
1013
Test Case - 4
User Output
93 :oN egaP
year or not 26
Aim:
Lucy is celebrating her 15th birthday. Her father promised her that he will buy her a new
2 Q 4 5 A 1 9 B 4 2 : DI
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
D- B S C & DI A- 8 2 0 2- 4 2 0 2
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
scanf("%d",&year);
if(((year%4==0)&&(year%100!=0))||(year%400==0))
{
printf("%d is a leap year",year);
Test Case - 1
User Output
1900
1900 is not a leap year
Test Case - 2
04 :oN egaP 2 Q 4 5 A 1 9 B 4 2 : DI D- B S C & DI A- 8 2 0 2- 4 2 0 2 )suomonotuA( egelloC gnireenignE RKRS
Test Case - 3
User Output
2004
1995
Date: 2024-09-
S.No: 19 Exp. Name: Factorial of a given number
14 :oN egaP
24
Aim:
Write a C program to find the factorial of a given number
2 Q 4 5 A 1 9 B 4 2 : DI
Source Code:
factorialOfInt.c
#include<stdio.h>
int main()
{
int i,factorial=1,n;
printf("Integer: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
D- B S C & DI A- 8 2 0 2- 4 2 0 2
{
factorial=factorial*i;
}
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-09-
S.No: 20
24 :oN egaP
whether a given number is prime or not. 24
Aim:
Write the C program to determine whether a given number is prime or not.
2 Q 4 5 A 1 9 B 4 2 : DI
Source Code:
Prime.c
#include<stdio.h>
int main()
{
int n,i,temp=0;
printf("Enter a number: ");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
D- B S C & DI A- 8 2 0 2- 4 2 0 2
{
if(n%i==0)
{
temp++;
break;
}
}
if(temp==0&&n!=1)
{
Test Case - 1
User Output
Enter a number:
9
9 is not a prime number
34 :oN egaP 2 Q 4 5 A 1 9 B 4 2 : DI D- B S C & DI A- 8 2 0 2- 4 2 0 2 )suomonotuA( egelloC gnireenignE RKRS
Test Case - 2
11 is a prime number
Enter a number:
User Output
11
Exp. Name: Compute sine and cos series Date: 2024-10-
S.No: 21
44 :oN egaP
using taylor series 15
Aim:
Write a C program to compute the sine and cosine series using the Taylor series.
2 Q 4 5 A 1 9 B 4 2 : DI
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.
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Source Code:
taylor.c
#include<stdio.h>
#include<math.h>
void main()
{
int terms,fact=1;
54 :oN egaP
Test Case - 1
User Output
angle in radians:
2 Q 4 5 A 1 9 B 4 2 : DI
0.5
number of terms in the series:
3
Sine = 0.4794
Cosine = 0.8776
Test Case - 2
User Output
D- B S C & DI A- 8 2 0 2- 4 2 0 2
angle in radians:
0.6
number of terms in the series:
5
Sine = 0.5646
Cosine = 0.8253
64 :oN egaP
palindrome or not 24
Aim:
Write an C program to check given number is palindrome or not
2 Q 4 5 A 1 9 B 4 2 : DI
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:
D- B S C & DI A- 8 2 0 2- 4 2 0 2
palindrome.c
#include<stdio.h>
int main()
{
int n,r,a,sum=0;
scanf("%d",&n);
a=n;
Test Case - 2
User Output
121
143
Date: 2024-09-
S.No: 23 Exp. Name: Pyramid with numbers
84 :oN egaP
26
Aim:
Write a program to print a pyramid of numbers separated by spaces for the given
2 Q 4 5 A 1 9 B 4 2 : DI
number of rows.
At the time of execution, the program should print the message on the console as:
D- B S C & DI A- 8 2 0 2- 4 2 0 2
1
1 2
1 2 3
Source Code:
PyramidDemo15.c
94 :oN egaP
Test Case - 1
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
3
1
1 2
1 2 3
Test Case - 2
User Output
D- B S C & DI A- 8 2 0 2- 4 2 0 2
6
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
User Output
05 :oN egaP
array of integers. 15
Aim:
Write a C program to find the minimum and maximum in an array of integers.
2 Q 4 5 A 1 9 B 4 2 : DI
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]);
D- B S C & DI A- 8 2 0 2- 4 2 0 2
}
/* Write your logic here to find the maximum and minimum
in the given integer array*/
min=max=arr[0];
for(int i=0;i<number;i++)
{
if(arr[i]>max) max=arr[i];
if(arr[i]<min)
min=arr[i];
Test Case - 1
User Output
5
Elements:
49682
Min an Max: 2 and 9
Test Case - 2
15 :oN egaP 2 Q 4 5 A 1 9 B 4 2 : DI D- B S C & DI A- 8 2 0 2- 4 2 0 2 )suomonotuA( egelloC gnireenignE RKRS
Min an Max: 216 and 216
User Output
Elements:
216
1
Exp. Name: Search for an element using Date: 2024-10-
S.No: 25
25 :oN egaP
Linear search 15
Aim:
Write a C program to check whether a given element is present in an array of elements
2 Q 4 5 A 1 9 B 4 2 : DI
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.
D- B S C & DI A- 8 2 0 2- 4 2 0 2
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).
35 :oN egaP
int main()
{
int a[10],i,size,s;
printf("Enter size: ");
scanf("%d",&size);
2 Q 4 5 A 1 9 B 4 2 : DI
printf("Enter %d element: ",size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
printf("Enter search element: ");
scanf("%d",&s);
i=0;
while((i<size)&&(a[i]!=s))
i++;
if(i==size)
printf("%d is not found\n",s);
else
D- B S C & DI A- 8 2 0 2- 4 2 0 2
printf("Found at position %d\n",i);
}
Test Case - 1
Enter size:
6
Enter 6 element:
248135
Enter search element:
6
6 is not found
Test Case - 2
User Output
Enter size:
6
Enter 6 element:
248135
2
Found at position 0
45 :oN egaP
Test Case - 3
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
Enter size:
6
Enter 6 element:
248135
Enter search element:
9
9 is not found
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
Exp. Name: C program to reverse an Date: 2024-10-
S.No: 26
55 :oN egaP
array. 08
Aim:
Write a C program to reverse the elements an array of integers.
2 Q 4 5 A 1 9 B 4 2 : DI
Source Code:
reverseArray.c
#include<stdio.h>
int main()
{
int a[10],i,n;
printf("Enter no of elements: ");
scanf("%d",&n);
printf("Enter elements: ");
D- B S C & DI A- 8 2 0 2- 4 2 0 2
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("The reversed array: ");
for(i=n-1;i>=0;i--)
{
printf("%d ",a[i]);
}
Test Case - 1
User Output
Enter no of elements:
5
Enter elements:
34124
The reversed array: 4 2 1 4 3
Test Case - 2
User Output
65 :oN egaP 2 Q 4 5 A 1 9 B 4 2 : DI D- B S C & DI A- 8 2 0 2- 4 2 0 2 )suomonotuA( egelloC gnireenignE RKRS
The reversed array: 9 2 88 33 77 1 5 2
Enter no of elements:
2 5 1 77 33 88 2 9
Enter elements:
8
Exp. Name: 2’s complement of a given Date: 2024-11-
S.No: 27
75 :oN egaP
Binary number 19
Aim:
Write a C program to find 2’s complement of a given binary number.
2 Q 4 5 A 1 9 B 4 2 : DI
Note: The binary input should be separated by a space.
Source Code:
twosComplement.c
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
#include<stdio.h>
85 :oN egaP
int main()
{
int n,i;
int a[10];
printf("Enter size: ");
2 Q 4 5 A 1 9 B 4 2 : DI
scanf("%d",&n);
printf("Enter %d bit binary number: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("2's complement: ");
for(i=0;i<n;i++)
{
if(a[i]==0){
a[i]=1;
D- B S C & DI A- 8 2 0 2- 4 2 0 2
}
else{
a[i]=0;
}
}
for(i=n-1;i>=0;i--)
{
if(a[i]==0){
a[i]=1;
Test Case - 1
User Output
95 :oN egaP
Enter size:
5
Enter 5 bit binary number:
10010
2 Q 4 5 A 1 9 B 4 2 : DI
2's complement: 0 1 1 1 0
Test Case - 2
User Output
Enter size:
6
Enter 6 bit binary number:
100011
D- B S C & DI A- 8 2 0 2- 4 2 0 2
2's complement: 0 1 1 1 0 1
06 :oN egaP
in an array 19
Aim:
Write a C program to eliminate duplicate elements of an array.
2 Q 4 5 A 1 9 B 4 2 : DI
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
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
#include<stdio.h>
16 :oN egaP
int main()
{
int n,i,j,k;
int a[10];
printf("Enter size: ");
2 Q 4 5 A 1 9 B 4 2 : DI
scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=0;i<n;i++)
{
scanf("%d ",&a[i]);
}
printf("After eliminating duplicates: ");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
D- B S C & DI A- 8 2 0 2- 4 2 0 2
if(a[i]==a[j]){
for(k=j;k<n-
1;k++){
a[k]=a[k+1];
}
n--;
j--;
}
Test Case - 1
User Output
Enter size:
5
Enter 5 elements:
12123
After eliminating duplicates: 1 2 3
26 :oN egaP
Test Case - 2
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
Enter size:
5
Enter 5 elements:
11 13 11 12 13
After eliminating duplicates: 11 13 12
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
Date: 2024-10-
S.No: 29 Exp. Name: Addition of Two Matrices
36 :oN egaP
01
Aim:
Write a C program to perform the addition of two matrices each with the size m × n .
2 Q 4 5 A 1 9 B 4 2 : DI
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
D- B S C & DI A- 8 2 0 2- 4 2 0 2
matrices are the same, so we are taking the same dimensions for both matrices.
Source Code:
addTwoMatrices.c
46 :oN egaP
int main()
{
int a[10][10],b[10][10],c[10][10],i,j,rows,columns;
printf("Enter no of rows, columns: ");
scanf("%d%d",&rows,&columns);
2 Q 4 5 A 1 9 B 4 2 : DI
printf("Elements of matrix 1: ");
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Elements of matrix 2: ");
for(i=0;i<rows;i++)
{
D- B S C & DI A- 8 2 0 2- 4 2 0 2
for(j=0;j<columns;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
Test Case - 1
User Output
56 :oN egaP
Enter no of rows, columns:
12
Elements of matrix 1:
12
2 Q 4 5 A 1 9 B 4 2 : DI
Elements of matrix 2:
98
Addition of matrices:
10 10
Test Case - 2
User Output
D- B S C & DI A- 8 2 0 2- 4 2 0 2
23
Elements of matrix 1:
123456
Elements of matrix 2:
987654
Addition of matrices:
10 10 10
10 10 10
66 :oN egaP
Matrices 15
Aim:
Write a C program to find the multiplication of two matrices of size r × c.
2 Q 4 5 A 1 9 B 4 2 : DI
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
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Refer to the sample test cases for better understanding
Sample Input:
2 2
11 22
33 44
2 2
11 22
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>
76 :oN egaP
int main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,r1,c1,r2,c2;
printf("no of rows, columns of matrix1: ");
scanf("%d%d",&r1,&c1);
2 Q 4 5 A 1 9 B 4 2 : DI
printf("matrix1 elements:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("no of rows, columns of matrix2: ");
scanf("%d%d",&r2,&c2);
printf("matrix2 elements:\n");
D- B S C & DI A- 8 2 0 2- 4 2 0 2
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Given matrix1:\n");
for(i=0;i<r1;i++)
86 :oN egaP
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<r2;k++)
{
2 Q 4 5 A 1 9 B 4 2 : DI
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d ",c[i][j]);
}
D- B S C & DI A- 8 2 0 2- 4 2 0 2
printf("\n");
}
}
else{
printf("Multiplication not possible\n");
}
}
Test Case - 1
User Output
22
11 22
33 44
22
11 22
33 44
matrix1:
11 22
33 44
matrix2:
33 44
result:
96 :oN egaP
847 1210
1815 2662
Test Case - 2
2 Q 4 5 A 1 9 B 4 2 : DI
User Output
33
123
456
789
23
123
456
D- B S C & DI A- 8 2 0 2- 4 2 0 2
matrix1:
1 2 3
4 5 6
7 8 9
matrix2:
1 2 3
4 5 6
07 :oN egaP
given elements using Bubble sort 15
Aim:
Develop an algorithm, implement and execute a C program that reads n integer numbers
2 Q 4 5 A 1 9 B 4 2 : DI
and arrange them in ascending order using Bubble Sort.
Source Code:
Lab7.c
#include<stdio.h>
int main()
{
int i,j,n,size,temp;
int arr[20];
printf("Elements: ");
D- B S C & DI A- 8 2 0 2- 4 2 0 2
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("Before sorting: ");
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
17 :oN egaP
Test Case - 1
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
Elements:
44 22 66 11
Before sorting: 44 22 66 11
After sorting: 11 22 44 66
Test Case - 2
User Output
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Elements:
92716
Before sorting: 9 2 7 1 6
After sorting: 1 2 6 7 9
27 :oN egaP
S.No: 32 strings without using string library
19
functions
Aim:
2 Q 4 5 A 1 9 B 4 2 : DI
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
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Next, the program should print the message on the console as:
string2 :
string2 : Coding
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program605.c
#include<stdio.h>
37 :oN egaP
int main()
{
char a[20],b[40];
int j,i;
printf("string1 : ");
2 Q 4 5 A 1 9 B 4 2 : DI
scanf("%s",a);
printf("string2 : ");
scanf("%s",b);
for(j=0;a[j]!='\0';j++);
for(i=0;b[i]!='\0';j++,i++)
{
a[j]=b[i];
}
a[j]='\0';
printf("concatenated string = %s\n",a);
}
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Test Case - 2
User Output
string1 :
1234
string2 :
567
concatenated string = 1234567
Exp. Name: Reverse the given string Date: 2024-11-
S.No: 33
47 :oN egaP
without using the library functions 19
Aim:
Write a program to reverse the given string without using the library functions.
2 Q 4 5 A 1 9 B 4 2 : DI
At the time of execution, the program should print the message on the console as:
Enter a string :
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Reverse string : sallaD
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program609.c
#include<stdio.h>
int main()
57 :oN egaP
User Output
Enter a string :
Dallas
Reverse string : sallaD
2 Q 4 5 A 1 9 B 4 2 : DI
Test Case - 2
User Output
Enter a string :
CodeTantra
Reverse string : artnaTedoC
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
Exp. Name: Write a C program to find
Date: 2024-11-
67 :oN egaP
S.No: 34 Sum of array elements by allocating
28
memory using malloc() function
Aim:
2 Q 4 5 A 1 9 B 4 2 : DI
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"
D- B S C & DI A- 8 2 0 2- 4 2 0 2
void main() {
int *p, n, i;
printf("Enter n value : ");
scanf("%d", &n);
p = allocateMemory(n);
printf("Enter %d values : ", n);
read1(p, n);
printf("The sum of given array elements : %d\n", sum(p,
UsingMalloc.c
int *allocateMemory(int n)
77 :oN egaP
{
int *p;
p=malloc(n * sizeof(int));
return p;
}
2 Q 4 5 A 1 9 B 4 2 : DI
void read1(int *p,int n)
{
for(int i=0;i<n;i++)
{
scanf("%d",(p+i));
}
}
int sum(int *p,int n)
{
int total=0;
for(int i=0;i<n;i++)
D- B S C & DI A- 8 2 0 2- 4 2 0 2
{
total+=*(p+i);
}
return total;
}
User Output
Enter n value :
3
Enter 3 values :
10 20 30
The sum of given array elements : 60
Test Case - 2
User Output
Enter n value :
4
Enter 4 values :
-5 -6 -4 -2
Exp. Name: Write a program to find Total
Date: 2024-11-
87 :oN egaP
S.No: 35 and Average gained by Students in a
28
Section using Array of Structures
Aim:
2 Q 4 5 A 1 9 B 4 2 : DI
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
D- B S C & DI A- 8 2 0 2- 4 2 0 2
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
97 :oN egaP
struct student {
int regdno;
int marks[3];
// Write the members of structure
};
2 Q 4 5 A 1 9 B 4 2 : DI
void main() {
struct student s[60];
int i,n,total;
float average;
printf("Enter number of students : ");
scanf("%d", &n);
for (i=0;i<n;i++) { // Complete the code in for
printf("Enter regdno, three subjects marks of
student-%d: ", i);
scanf("%d %d %d
%d",&s[i].regdno,&s[i].marks[0],&s[i].marks[1],&s[i].marks[2]);
D- B S C & DI A- 8 2 0 2- 4 2 0 2
// Read regdno and 3 subjects marks
}
for (i=0;i<n;i++) { // Complete the code in for
total=s[i].marks[0]+s[i].marks[1]+s[i].marks[2];
average=total/3.0;// Find Total and Average
printf("Student-%d Regdno = %d\tTotal marks =
%d\tAverage marks = %f\n",i,s[i].regdno,total,average ); // Fill
the code in printf()
}
Test Case - 1
User Output
08 :oN egaP
Student-2 Regdno = 301 Total marks = 164 Average marks =
54.666668
Test Case - 2
2 Q 4 5 A 1 9 B 4 2 : DI
User Output
D- B S C & DI A- 8 2 0 2- 4 2 0 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:
505 37 59 76
Enter regdno, three subjects marks of student-5:
506 78 59 67
18 :oN egaP
Student-7 Regdno = 508 Total marks = 140 Average marks =
46.666668
Student-8 Regdno = 509 Total marks = 166 Average marks =
55.333332
Student-9 Regdno = 510 Total marks = 189 Average marks =
2 Q 4 5 A 1 9 B 4 2 : DI
63.000000
Test Case - 3
User Output
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Enter regdno, three subjects marks of student-1:
102 89 57 68
Enter regdno, three subjects marks of student-2:
103 77 67 59
Enter regdno, three subjects marks of student-3:
104 37 47 52
Enter regdno, three subjects marks of student-4:
28 :oN egaP
S.No: 36 students data using calloc() and display
03
Failed Students List
Aim:
2 Q 4 5 A 1 9 B 4 2 : DI
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;
D- B S C & DI A- 8 2 0 2- 4 2 0 2
int marks[6], sum;
float avg;
};
#include "FailedList1.c"
void main() {
struct student *s;
int i, n;
printf("Enter the number of students : ");
scanf("%d", &n);
FailedList1.c
struct student* allocateMemory(struct student *s, int n) {
38 :oN egaP
s=(struct student*)calloc(n,sizeof(struct student));
return s;
}
2 Q 4 5 A 1 9 B 4 2 : DI
int i;
for(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 : ");
scanf("%d%d%d%d%d%d",&s[i].marks[0],&s[i].marks[1],&s[i].marks[2],&s[i].marks[3],&s[i].ma
}
}
D- B S C & DI A- 8 2 0 2- 4 2 0 2
void calculateMarks(struct student *s, int n) {
int i,j;
for(i=0;i<n;i++){
for(j=0;j<6;j++)
s[i].sum=s[i].sum+s[i].marks[j];
}
for(i=0;i<n;i++){
s[i].avg=(float)s[i].sum/6;}
}
48 :oN egaP
Test Case - 1
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
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
D- B S C & DI A- 8 2 0 2- 4 2 0 2
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
RollNo TotalMarks AverageMarks Status
Test Case - 2
User Output
68 :oN egaP
S.No: 37 Total Marks of a Student using
28
Command-line arguments
Aim:
2 Q 4 5 A 1 9 B 4 2 : DI
Write a C program to read student name and 3 subjects marks from the command line
and display the student details along with total.
D- B S C & DI A- 8 2 0 2- 4 2 0 2
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>
78 :oN egaP
#include<stdlib.h>
int main(int argc,char*argv[])
{
if(argc!=5)
printf("Arguments passed through command line are
2 Q 4 5 A 1 9 B 4 2 : DI
not equal to 4\n",argc);
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]));
}
}
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Test Case - 2
User Output
88 :oN egaP
implement realloc() 28
Aim:
Write a C program to implement realloc() .
2 Q 4 5 A 1 9 B 4 2 : DI
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
D- B S C & DI A- 8 2 0 2- 4 2 0 2
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int) * 2);
int i;
int *ptr_new;
*ptr = 10;
*(ptr + 1) = 20;
Test Case - 1
User Output
10 20 30
Exp. Name: Write a C Program to store Date: 2024-12-
S.No: 39
98 :oN egaP
information using Structures with DMA 02
Aim:
Write a program to create a list of nodes using self-referential structure and
2 Q 4 5 A 1 9 B 4 2 : DI
print 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:
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Do u want another list (y|n) :
Finally, the program should print the result on the console as:
StructuresWithDma.c
#include <stdio.h>
09 :oN egaP
#include <stdlib.h>
struct list {
int data;
struct list *next;
};
2 Q 4 5 A 1 9 B 4 2 : DI
#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
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
struct list* create(struct list *first) {
19 :oN egaP
char op;
struct list *q, *temp;
do {
temp = (struct list *)malloc(sizeof(struct
list)); // Allocate memory
2 Q 4 5 A 1 9 B 4 2 : DI
printf("Enter an integer value : ");
scanf("%d",&temp->data ); // Read data
temp -> next = NULL; // Place NULL
if (first == NULL) {
first = temp; // Assign temp to the first
node
} else {
q -> next = temp; // Create a link from
the last node to new node temp
}
q = temp;
D- B S C & DI A- 8 2 0 2- 4 2 0 2
printf("Do u want another list (y|n) : ");
scanf(" %c", &op);
} while(op == 'y' || op == 'Y');
return first;
}
void display(struct list *first) {
struct list *temp = first;
while (temp!=NULL) { // Stop the loop where temp is NULL
printf("%d-->", temp->data);
Test Case - 1
User Output
29 :oN egaP
30
Do u want another list (y|n) :
n
The elements in the single linked lists are : 10-->20-->30--
2 Q 4 5 A 1 9 B 4 2 : DI
>NULL
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
Exp. Name: Write a C program to
Date: 2024-12-
39 :oN egaP
S.No: 40 demonstrate the differences between
02
Structures and Unions
Aim:
2 Q 4 5 A 1 9 B 4 2 : DI
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
D- B S C & DI A- 8 2 0 2- 4 2 0 2
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
Student 2 marks 1: 76
Student 2 marks 2: 87
StructureAndUnion.c
#include<stdio.h>
49 :oN egaP
void main()
{
struct stdent_1
{
int rollno,m1,m2,m3,total;
2 Q 4 5 A 1 9 B 4 2 : DI
float avg;
}ss;
union student_2
{
int rollno,m1,m2,m3,total;
float avg;
}us;
int temp;
printf("Student 1 rollno and marks: ");
scanf("%d%d%d%d",&ss.rollno,&ss.m1,&ss.m2,&ss.m3);
ss.total = ss.m1+ss.m2+ss.m3;
D- B S C & DI A- 8 2 0 2- 4 2 0 2
ss.avg = ss.total/3.0;
printf("Total and average: %d %f\n",ss.total,ss.avg);
printf("Student 1 struct size: %ld\n",sizeof(ss));
printf("Student 2 rollno: ");
scanf("%d",&us.rollno);
printf("Student 2 marks 1: ");
scanf("%d",&us.m1);
temp =us.m1;
printf("Student 2 marks 2: ");
Test Case - 1
User Output
Student 1 rollno and marks:
101 76 58 67
59 :oN egaP
Total and average: 201 67.000000
Student 1 struct size: 24
Student 2 rollno:
102
2 Q 4 5 A 1 9 B 4 2 : DI
Student 2 marks 1:
76
Student 2 marks 2:
87
Student 2 marks 3:
69
Student 2 total marks: 232
Student 2 average marks: 77.333336
Student 2 union size: 4
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Test Case - 2
User Output
69 :oN egaP
operation 29
Aim:
Write a C program to demonstrate left shift operation
2 Q 4 5 A 1 9 B 4 2 : DI
Source Code:
shift.c
#include<stdio.h>
void decimaltobinary(int n)
{
int binary[1000],i,j;
if(n==0)
{
printf("0");
D- B S C & DI A- 8 2 0 2- 4 2 0 2
}
while (n>0)
{
binary[i]=n%2;
n=n/2;
i++;
}
for(j=i-1;j>=0;j--)
{
79 :oN egaP
Test Case - 1
User Output
Enter an integer:
2 Q 4 5 A 1 9 B 4 2 : DI
12
Original value: 1100
number of bits to left shift:
2
After left shift: 48
Binary representation:110000
Test Case - 2
D- B S C & DI A- 8 2 0 2- 4 2 0 2
User Output
Enter an integer:
5
Original value: 101
number of bits to left shift:
3
After left shift: 40
Binary representation:101000
89 :oN egaP
S.No: 42 structure variable to another structure
02
variable
Aim:
2 Q 4 5 A 1 9 B 4 2 : DI
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
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Source Code:
CopyStructureMain.c
#include <stdio.h>
#include "CopyStructureFunctions.c"
int main() {
struct student s1, s2;
CopyStructureFunctions.c
struct student {
99 :oN egaP
//write the code
char name[20];
int age;
float height;
};
2 Q 4 5 A 1 9 B 4 2 : DI
void read1(struct student *p) {
printf("Enter student name, age and height: ");
// Write the code to take inputs to structure
scanf("%s%d%f",p->name,&p->age,&p->height);
}
D- B S C & DI A- 8 2 0 2- 4 2 0 2
strcpy(s2.name,s1.name);
s2.age=s1.age;
s2.height=s1.height;
return s2;
}
Test Case - 1
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
Age: 21
Height: 5.110000
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
Exp. Name: Write a C Program to find nCr Date: 2024-11-
S.No: 43
Aim:
Draw the flowchart and write a recursive C function to find the factorial of a number, n! ,
2 Q 4 5 A 1 9 B 4 2 : DI
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:
D- B S C & DI A- 8 2 0 2- 4 2 0 2
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
int factorial(int x)
{
if(x==0)
{
return 1;
}
else{
return x*factorial(x-1);
}
}
Lab14.c
#include <stdio.h>
2 Q 4 5 A 1 9 B 4 2 : DI
printf("The value of %dc%d = %d\n", n, r,
factorial(n) / (factorial(r) * factorial(n - r)));
else
printf("Enter valid input data\n");
}
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Test Case - 1
User Output
User Output
Test Case - 3
User Output
Aim:
Write a C program to find the length of a given string.
2 Q 4 5 A 1 9 B 4 2 : DI
Sample Input and Output - 1:
Source Code:
StrLength.c
#include <stdio.h>
D- B S C & DI A- 8 2 0 2- 4 2 0 2
#include "StrLength1.c"
void main() {
char str[30];
printf("Enter the string : ");
scanf("%s", str);
printf("Length of %s : %d\n", str, myStrLen(str));
}
#include<stdio.h>
int myStrLen(char*str)
{
int length=0;
while(str[length]!='\0')
{
length++;
}
return length;
}
Test Case - 1
Enter the string :
CodeTantra
Test Case - 2
2 Q 4 5 A 1 9 B 4 2 : DI
User Output
Test Case - 3
User Output
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Enter the string :
MalayalaM
Length of MalayalaM : 9
Test Case - 4
User Output
Aim:
Write a C program to print the transpose of a matrix using functions.
2 Q 4 5 A 1 9 B 4 2 : DI
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:
D- B S C & DI A- 8 2 0 2- 4 2 0 2
transpose.c
2 Q 4 5 A 1 9 B 4 2 : DI
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
scanf("%d",&matrix[i]
[j]);
}
}
}
void printMatrix(int matrix[rows][cols])
D- B S C & DI A- 8 2 0 2- 4 2 0 2
{
int i,j;
printf("Matrix:\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("%d ",matrix[i]
[j]);
2 Q 4 5 A 1 9 B 4 2 : DI
{
for(j=0;j<cols;j++)
{
printf("%d ",b[i]
[j]);
}
printf("\n");
}
}
}
D- B S C & DI A- 8 2 0 2- 4 2 0 2
int main() {
printf("rows: ");
scanf("%d", &rows);
printf("columns: ");
return 0;
}
rows:
2
columns:
2 Q 4 5 A 1 9 B 4 2 : DI
2
Elements:
89
65
Matrix:
8 9
6 5
Transpose:
8 6
D- B S C & DI A- 8 2 0 2- 4 2 0 2
9 5
Test Case - 2
User Output
rows:
1
Aim:
2 Q 4 5 A 1 9 B 4 2 : DI
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.
D- B S C & DI A- 8 2 0 2- 4 2 0 2
equation.
Source Code:
euler.c
2 Q 4 5 A 1 9 B 4 2 : DI
// Example: dy/dt = t*y
}
// Euler's method for numerical integration
void eulerIntegration( ) {
}
int main() {
D- B S C & DI A- 8 2 0 2- 4 2 0 2
*/
#include<stdio.h>
int main()
{
float y0,t0,h,t,x0;
printf("initial value of y (y0): ");
scanf("%f",&y0);
printf("initial value of t (t0): ");
scanf("%f",&t0);
Test Case - 1
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
3
end value for t:
10
t = 1.00 y = 1.00
t = 4.00 y = 4.00
t = 7.00 y = 52.00
Test Case - 2
D- B S C & DI A- 8 2 0 2- 4 2 0 2
User Output
Aim:
Write a program to display the fibonacci series up to the given number of terms using
recursion process.
2 Q 4 5 A 1 9 B 4 2 : DI
Source Code:
fibonacciSeries.c
#include <stdio.h>
#include "fibonacciSeriesa.c"
void main() {
int n, i;
printf("n: ");
scanf("%d", &n);
D- B S C & DI A- 8 2 0 2- 4 2 0 2
printf("%d terms: ", n);
for (i = 0; i < n; i++) {
printf("%d ", fib(i));
}
}
fibonacciSeriesa.c
Test Case - 1
User Output
n:
4
311 :oN egaP 2 Q 4 5 A 1 9 B 4 2 : DI D- B S C & DI A- 8 2 0 2- 4 2 0 2 )suomonotuA( egelloC gnireenignE RKRS
Test Case - 2
10 terms: 0 1 1 2 3 5 8 13 21 34
4 terms: 0 1 1 2
User Output
10
n:
Exp. Name: LCM of two numbers using Date: 2024-11-
S.No: 48
Aim:
Write a program to find the lcm (Least Common Multiple) of a given two numbers using
2 Q 4 5 A 1 9 B 4 2 : DI
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:
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Enter two integer values : 25 15
Note: Write the function lcm() and recursive function gcd() in Program907a.c .
Source Code:
#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)
2 Q 4 5 A 1 9 B 4 2 : DI
common++;
return lcm(a,b);
}
Test Case - 1
D- B S C & DI A- 8 2 0 2- 4 2 0 2
User Output
Test Case - 2
Test Case - 3
User Output
Test Case - 4
User Output
123 420
Exp. Name: Write a C program to find the
Date: 2024-11-
Aim:
2 Q 4 5 A 1 9 B 4 2 : DI
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;
D- B S C & DI A- 8 2 0 2- 4 2 0 2
printf("Enter an integer : ");
scanf("%ld", &n);
printf("Factorial of %ld is : %ld\n", n ,factorial(n));
}
Program901a.c
Test Case - 1
User Output
Enter an integer :
5
Factorial of 5 is : 120
User Output
Enter an integer :
2 Q 4 5 A 1 9 B 4 2 : DI
4
Factorial of 4 is : 24
Test Case - 3
User Output
Enter an integer :
8
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Factorial of 8 is : 40320
Test Case - 4
User Output
Enter an integer :
0
Aim:
Write a program to implement Ackermann function using recursion process.
2 Q 4 5 A 1 9 B 4 2 : DI
At the time of execution, the program should print the message on the console as:
D- B S C & DI A- 8 2 0 2- 4 2 0 2
A(2, 1) = 5
Source Code:
AckermannFunction.c
#include <stdio.h>
#include "AckermannFunction1.c"
AckermannFunction1.c
long long int ackermannFun(long long int m,long long int n)
2 Q 4 5 A 1 9 B 4 2 : DI
return ackermannFun(m-1,ackermannFun(m,n-1));
return -1;
}
Test Case - 1
D- B S C & DI A- 8 2 0 2- 4 2 0 2
User Output
Test Case - 2
Test Case - 3
User Output
Test Case - 4
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
Test Case - 6
User Output
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
Date: 2024-11-
S.No: 51 Exp. Name: Problem solving
Aim:
Write a program to find the sum of n natural numbers using recursion process.
2 Q 4 5 A 1 9 B 4 2 : DI
At the time of execution, the program should print the message on the console as:
Enter value of n :
Enter value of n : 6
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Sum of 6 natural numbers = 21
Program903.c
#include <stdio.h>
#include "Program903a.c"
Program903a.c
int sum(int n)
{
if(n==0)
{
return 0;
}
int result = n+sum(n-1);
return result;
}
Execution Results - All test cases have succeeded!
User Output
Enter value of n :
5
2 Q 4 5 A 1 9 B 4 2 : DI
Sum of 5 natural numbers = 15
Test Case - 2
User Output
Enter value of n :
9
Sum of 9 natural numbers = 45
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
Exp. Name: Write a C program to Swap
Date: 2024-11-
Aim:
2 Q 4 5 A 1 9 B 4 2 : DI
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:
D- B S C & DI A- 8 2 0 2- 4 2 0 2
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 ).
Source Code:
#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*p,int*q)
2 Q 4 5 A 1 9 B 4 2 : DI
%d\n",*p,*q);
}
Test Case - 1
User Output
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Enter two integer values :
121 131
Before swapping in main : a = 121 b = 131
After swapping in swap : *p = 131 *q = 121
After swapping in main : a = 131 b = 121
User Output
Test Case - 3
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
After swapping in swap : *p = 2999 *q = 9999
After swapping in main : a = 2999 b = 9999
Test Case - 5
User Output
D- B S C & DI A- 8 2 0 2- 4 2 0 2
After swapping in swap : *p = 11010 *q = 10101
After swapping in main : a = 11010 b = 10101
Aim:
Demonstrate Dangling pointer problem using a C program.
2 Q 4 5 A 1 9 B 4 2 : DI
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
#include <stdio.h>
#include <stdlib.h>
int main() {
D- B S C & DI A- 8 2 0 2- 4 2 0 2
int *ptr1 = NULL;
int *ptr2 = NULL;
int value;
ptr1=(int*)malloc(sizeof(int));
printf("Enter an integer value: ");
scanf("%d",&value);
*ptr1=value;
ptr2=ptr1;
if(ptr1!=NULL)
Test Case - 1
User Output
10
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.
2 Q 4 5 A 1 9 B 4 2 : DI
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() {
D- B S C & DI A- 8 2 0 2- 4 2 0 2
char source[100], target[100];
printf("Enter source string : ");
fgets(source, sizeof(source), stdin);
copyString(target, source);
printf("Target string : %s\n", target);
}
CopyStringPointers1.c
Test Case - 1
User Output
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
Enter source string :
Robotic Tool
Target string : Robotic Tool
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
Exp. Name: Write a C program to Count
Date: 2024-11-
Aim:
2 Q 4 5 A 1 9 B 4 2 : DI
Write a C program to find number of lowercase , uppercase , digits and
other characters using pointers.
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Source Code:
CountCharDigitOthers.c
#include <stdio.h>
#include "CountCharDigitOthers1.c"
void main() {
char str[80];
CountCharDigitOthers1.c
#include<stdio.h>
2 Q 4 5 A 1 9 B 4 2 : DI
if(isupper(*str))
*upperCount=*upperCount+1;
else if(islower(*str))
*lowerCount=*lowerCount+1;
else if (isdigit(*str))
*digitCount=*digitCount+1;
else
*otherCount=*otherCount+1;
D- B S C & DI A- 8 2 0 2- 4 2 0 2
str++;
}
}
User Output
Enter a string :
CodeTantra123&*@987.
Number of uppercase letters = 2
Number of lowercase letters = 8
Number of digits = 6
Number of other characters = 4
Test Case - 2
User Output
Enter a string :
Indo Pak 125 143 *.$
Number of uppercase letters = 2
Number of lowercase letters = 5
Number of digits = 6
Test Case - 3
Enter a string :
12345
Number of uppercase letters = 0
2 Q 4 5 A 1 9 B 4 2 : DI
Number of lowercase letters = 0
Number of digits = 5
Number of other characters = 0
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
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.
2 Q 4 5 A 1 9 B 4 2 : DI
Source Code:
readFilePrint.c
#include <stdio.h>
void main()
{
char filename[20],c;
FILE*fp=NULL;
printf("Enter the name of the file to read: ");
D- B S C & DI A- 8 2 0 2- 4 2 0 2
scanf("%s",filename);
printf("Content of the file %s:\n",filename);
fp=fopen(filename,"r");
if(fp==NULL)
return;
do{
c=fgetc(fp);
if(feof(fp))
break;
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
2 Q 4 5 A 1 9 B 4 2 : DI
he would ask his friend.
file3.txt
A couple was living their life happily. The womans husband had a
clothing business.
One day suddenly his health deteriorated very much and he died.
Now calamity had arisen in front of the woman.
She was very depressed about how she would take care of herself
D- B S C & DI A- 8 2 0 2- 4 2 0 2
and her children.
Her husbands shop was closed. She had no idea what to do.
Test Case - 1
Test Case - 2
User Output
file2.txt
Content of the file file2.txt:
2 Q 4 5 A 1 9 B 4 2 : DI
One day the poor friend really needed money, and he thought
that he would ask his friend.
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
Exp. Name: C program to write and read
Date: 2024-12-
Aim:
2 Q 4 5 A 1 9 B 4 2 : DI
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
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
#include<stdio.h>
2 Q 4 5 A 1 9 B 4 2 : DI
FILE *fp;
char ch;
struct student s;
fp = fopen("student-information.txt", "wb"); // Complete
the statement
do {
printf("Roll no: ");
scanf("%d",&s.roll); // Complete the statement
printf("Name: ");
scanf("%s",&s.name); // Complete the statement
D- B S C & DI A- 8 2 0 2- 4 2 0 2
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");
fclose(fp);
Test Case - 1
User Output
Roll no:
501
Name:
2 Q 4 5 A 1 9 B 4 2 : DI
y
Roll no:
502
Name:
Smith
Marks:
65
Want to add another data (y/n):
n
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Data written successfully
Roll Name Marks
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:
2 Q 4 5 A 1 9 B 4 2 : DI
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.
D- B S C & DI A- 8 2 0 2- 4 2 0 2
Source Code:
copy.c
2 Q 4 5 A 1 9 B 4 2 : DI
code
D- B S C & DI A- 8 2 0 2- 4 2 0 2
printf("Copied text is : ");
while(!feof(fp1)){
fscanf(fp1,"%c",&ch);
if(
feof(fp1)||ch=='@')
break;
fprintf(fp2,"%c",ch);
printf("%c",ch);
}
Test Case - 1
User Output
Aim:
2 Q 4 5 A 1 9 B 4 2 : DI
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
D- B S C & DI A- 8 2 0 2- 4 2 0 2
• Close the first existing file
• Open another existing file specified in argv[2] in read mode
• Copy its content from existing file to new file
• Close that existing file
• Close the merged file
Source Code:
MergeFilesArgs.c
2 Q 4 5 A 1 9 B 4 2 : DI
while ((ch=getchar())!='@') { // Write the condition
putc(ch, fp1);
}
putc(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
putc(ch, fp2);
D- B S C & DI A- 8 2 0 2- 4 2 0 2
}
putc(ch, fp2);
fclose(fp2);
fp1 = fopen(argv[1] , "r"); // Open a first existed file
in read mode
fp3 = fopen(argv[3] , "w"); // Open a new file in write
mode
while ((ch=fgetc(fp1))!='@') { // Repeat loop till get @
at the end of existed file
2 Q 4 5 A 1 9 B 4 2 : DI
Test Case - 1
User Output
D- B S C & DI A- 8 2 0 2- 4 2 0 2
2014@
Merged text is : This is CodeTantra
They implemented automatic robotic tool
Started the company in
2014
User Output
Aim:
2 Q 4 5 A 1 9 B 4 2 : DI
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
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
#include <stdio.h>
2 Q 4 5 A 1 9 B 4 2 : DI
printf("Enter the text with @ at end : ");
while ((ch=getchar())!='@') { // Repeat loop till read @
at the end
putc(ch,fp); // Put read character onto the file
}
putc(ch,fp); // Put delimiter @ at the end on the file
fclose(fp); // Close the file
fp = fopen("DemoTextFile2.txt", "r"); // Open the
existing file in read mode
do {
D- B S C & DI A- 8 2 0 2- 4 2 0 2
if (ch==' '||ch=='\t'||ch=='\n'||ch=='\0') //
Write the condition to count words
wordCount++;
else
charCount++;
if (ch=='\n'||ch=='\0') // Write the condition to
count lines
lineCount++;
} while ((ch=fgetc(fp))!='@'); // Repeat loop till read @
Test Case - 1
User Output
User Output
2 Q 4 5 A 1 9 B 4 2 : DI
Enter the text with @ at end :
Believe in your self
and the world will be
at your feet@
Total characters : 44
Total words : 12
Total lines : 3
D- B S C & DI A- 8 2 0 2- 4 2 0 2
)suomonotuA( egelloC gnireenignE RKRS
Exp. Name: Print the last n characters of Date: 2024-12-
S.No: 61
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.
2 Q 4 5 A 1 9 B 4 2 : DI
Source Code:
file.c
#include<stdio.h>
#include<stdlib.h>
void main(int argc,char *argv[]){
FILE *fp;
char ch;
int n;
D- B S C & DI A- 8 2 0 2- 4 2 0 2
long len;
fp = fopen(argv[1] , "r");
n=atoi(argv[2]);
fseek(fp,0,SEEK_END);
len = ftell(fp);
fseek(fp,(len-n),SEEK_SET);
while((ch=fgetc(fp))){
if(feof(fp)) break;
putchar(ch);
input1.txt
input2.txt
Beautiful is better than ugly.
2 Q 4 5 A 1 9 B 4 2 : DI
Everything matters.
test1.txt
CodeTantra
Start coding in 60 mins
test2.txt
D- B S C & DI A- 8 2 0 2- 4 2 0 2
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
Test Case - 1
User Output
good idea.
Test Case - 2
User Output
verything matters.