CP Lab Programs
CP Lab Programs
Aim:
1 :oN egaP
Write a C program to display hello world and a greeting to the user.
Source Code:
hello.c
8 4 2 4 A 1 3 3 3 2 : DI
#include<stdio.h>
#include<string.h>
void main()
{
char sa[20];
printf("Enter your name:");
scanf("%s",sa);
printf("Hello World\n");
printf("Hello %s\n",sa);
}
M S C- 7 2 0 2- 3 2 0 2
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Test Case - 2
User Output
Aim:
2 :oN egaP
Write a C program to scan all data type variables(int, float, char, double) as input and print them as output.
Note: Please add Space before %c which removes any white space (blanks, tabs, or newlines).
Source Code:
8 4 2 4 A 1 3 3 3 2 : DI
scan.c
#include<stdio.h>
void main()
{
char a;
int b;
double d;
float s;
printf("integer: ");
scanf("%d",&b);
printf("floating-point number: ");
M S C- 7 2 0 2- 3 2 0 2
scanf("%f",&s);
printf("character: ");
scanf(" %c",&a);
printf("double: ");
scanf("%lf",&d);
printf("You entered:");
printf("\nInteger: %d\n",b);
Test Case - 1
User Output
integer:
9
floating-point number:
12.0254
character:
C
double:
12.02543124
You entered:
Integer: 9
Float: 12.025400
Character: C
Double: 12.025431
Test Case - 2
User Output
integer:
-10
3 :oN egaP
floating-point number:
12.2546
character:
T
8 4 2 4 A 1 3 3 3 2 : DI
double:
12.6789678
You entered:
Integer: -10
Float: 12.254600
Character: T
Double: 12.678968
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
Exp. Name: Perform arithmetic operations like +,-,*,/,%
S.No: 3 Date: 2023-12-05
on two input variables.
Aim:
4 :oN egaP
Write a C program to perform arithmetic operations like +,-,*,/,% on two input variables.
Source Code:
arithmeticOperations.c
8 4 2 4 A 1 3 3 3 2 : DI
#include<stdio.h>
void main()
{
int num1,num2;
printf("num1: ");
scanf("%d",&num1);
printf("num2: ");
scanf("%d",&num2);
printf("Sum: %d\n",num1+num2);
printf("Difference: %d\n",num1-num2);
printf("Product: %d\n",num1*num2);
if(num2>0)
M S C- 7 2 0 2- 3 2 0 2
{
printf("Division: %d\n",num1/num2);
printf("Modulus: %d\n",num1%num2);
}
if(num2==0)
{
printf("Infinity");
Test Case - 1
User Output
num1:
9
num2:
8
Sum: 17
Difference: 1
Product: 72
Division: 1
Modulus: 1
Test Case - 2
User Output
5 :oN egaP 8 4 2 4 A 1 3 3 3 2 : DI M S C- 7 2 0 2- 3 2 0 2 )suomonotuA( gnireenignE fo egelloC RGVM
Modulo by zero is not allowed
Difference: 25
Product: 0
Infinity
Sum: 25
num1:
num2:
25
0
Exp. Name: Write a C program to find Sum and
S.No: 4 Date: 2023-12-05
Average of three numbers
Aim:
6 :oN egaP
Write a program to find the sum and average of the three given integers.
Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
8 4 2 4 A 1 3 3 3 2 : DI
Program314.c
#include<stdio.h>
void main()
{
int a,b,c;
float d;
printf("Enter three integers : ");
scanf("%d%d%d",&a,&b,&c);
d=a+b+c;
printf("Sum of %d, %d and %d : %d\n",a,b,c,a+b+c);
//av=(d)/3;
M S C- 7 2 0 2- 3 2 0 2
printf("Average of %d, %d and %d : %f\n",a,b,c,d/3);
}
User Output
Test Case - 2
User Output
Test Case - 3
User Output
Aim:
7 :oN egaP
Write a C program to perform temperature conversions from Centigrade to Fahrenheit and vice versa.
Source Code:
temperature.c
8 4 2 4 A 1 3 3 3 2 : DI
#include<stdio.h>
void main()
{
float f,c;
int n;
printf("Temperature Conversion:\n");
printf("1.Celsius to Fahrenheit\n");
printf("2.Fahrenheit to Celsius\n");
printf("choice: ");
scanf("%d",&n);
if(n==1)
M S C- 7 2 0 2- 3 2 0 2
{
printf("Enter Temperature in Celsius: ");
scanf("%f",&c);
f=c*9/5+32;
printf("Fahrenheit Temperature: %.2f\n",f);
}
else if(n==2)
Test Case - 1
User Output
Temperature Conversion:
1.Celsius to Fahrenheit
2.Fahrenheit to Celsius
choice:
1
Enter Temperature in Celsius:
35.78
Fahrenheit Temperature: 96.40
Test Case - 2
User Output
8 :oN egaP
Temperature Conversion:
1.Celsius to Fahrenheit
2.Fahrenheit to Celsius
choice:
8 4 2 4 A 1 3 3 3 2 : DI
2
Enter Temperature in Fahrenheit:
96.40
Celsius Temperature: 35.78
Test Case - 3
User Output
Temperature Conversion:
1.Celsius to Fahrenheit
M S C- 7 2 0 2- 3 2 0 2
2.Fahrenheit to Celsius
choice:
3
Invalid choice
Aim:
Write a program to calculate the simple interest by reading principle amount, rate of interest and time.
9 :oN egaP
At the time of execution, the program should print the message on the console as:
8 4 2 4 A 1 3 3 3 2 : DI
For example, if the user gives the input as:
Enter principle amount, rate of interest, time of loan : 23456.78 3.5 2.5
Note: Do use the printf() function and ensure that there is a '\n' at the end after print the result.
Source Code:
Program3.c
M S C- 7 2 0 2- 3 2 0 2
#include<stdio.h>
void main()
{
int p,t,r;
float s;
printf("Enter principle amount, rate of interest, time of loan : ");
Test Case - 1
User Output
Aim:
Write a program that prompts the user to enter an integer and calculates its square root.
01 :oN egaP
Note:Print the result up to 3 decimal places.
Input format:
The program takes an integer as input.
8 4 2 4 A 1 3 3 3 2 : DI
Output format:
The output is the floating point value that represents the square root value of the user-given integer.
Instruction: During writing your code, please follow the input and output layout as mentioned in the sample test
case.
Source Code:
squareRoot.c
M S C- 7 2 0 2- 3 2 0 2
#include<stdio.h>
#include<math.h>
void main()
{
int n;
float s;
printf("Enter an integer: ");
Test Case - 1
User Output
Enter an integer:
2
Square root: 1.414
Test Case - 2
User Output
Enter an integer:
4
Square root: 2.000
Exp. Name: Write a program to calculate Simple
S.No: 8 Date: 2023-12-06
interest and Compound interest
Aim:
11 :oN egaP
Write a program to calculate the simple interest and compound interest by reading principal amount, rate
of interest and time.
Note: Use the printf() function and ensure that the character '\n' is printed at the end of the result.
8 4 2 4 A 1 3 3 3 2 : DI
The formula to find simple interest is simpleInterest = (principal * rate * time) / 100 .
Note: Use float data type for all the involved variables.
Source Code:
Program315.c
#include<stdio.h>
M S C- 7 2 0 2- 3 2 0 2
#include<math.h>
void main()
{
float p,t,r;
float s,c;
printf("Enter P,R,T: ");
Test Case - 1
User Output
Enter P,R,T:
5000 7 5
SI= 1750.000000
CI= 2012.760376
Test Case - 2
User Output
Enter P,R,T:
1000 6 4
SI= 240.000000
21 :oN egaP 8 4 2 4 A 1 3 3 3 2 : DI M S C- 7 2 0 2- 3 2 0 2 )suomonotuA( gnireenignE fo egelloC RGVM
CI= 262.476685
Exp. Name: Write a C program to find Area of a
S.No: 9 Date: 2023-12-06
Triangle using Heron's formula
Aim:
31 :oN egaP
Write a program to find the area of a triangle using Heron's formula.
During execution, the program should print the following message on the console:
sides:
8 4 2 4 A 1 3 3 3 2 : DI
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:
area: 2.49
Instruction: Your input and output layout must match with the sample test cases (values as well as text strings).
The area of a triangle is given by Area = √ p(p − a)(p − b)(p − c) , where p is half of the perimeter, or
M S C- 7 2 0 2- 3 2 0 2
(a + b + c) / 2 . Let a,b,c be the lengths of the sides of the given triangle.
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,s,ar;
printf("sides: ");
scanf("%f%f%f",&a,&b,&c);
s=(a+b+c)/2;
ar=sqrt(s*(s-a)*(s-b)*(s-c));
printf("area: %.2f\n",ar);
}
Test Case - 1
User Output
sides:
2.3 2.4 2.5
area: 2.49
Test Case - 2
41 :oN egaP 8 4 2 4 A 1 3 3 3 2 : DI M S C- 7 2 0 2- 3 2 0 2 )suomonotuA( gnireenignE fo egelloC RGVM
area: 3.15
User Output
Aim:
51 :oN egaP
Write a program to find the distance travelled by an object.
8 4 2 4 A 1 3 3 3 2 : DI
Enter the initial velocity : 5.7
Enter the time taken : 20
Distance travelled : 614.000000
Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:
DistanceTravelled.c
M S C- 7 2 0 2- 3 2 0 2
#include<stdio.h>
#include<math.h>
void main()
{
float u,t,a,s;
printf("Enter the acceleration value : ");
Test Case - 1
User Output
User Output
61 :oN egaP
Enter the initial velocity :
0
Enter the time taken :
10
8 4 2 4 A 1 3 3 3 2 : DI
Distance travelled : 250.000000
Test Case - 3
User Output
M S C- 7 2 0 2- 3 2 0 2
20
Distance travelled : 614.000000
Test Case - 4
Test Case - 5
User Output
Aim:
Write a C program to evaluate the following expressions.
71 :oN egaP
a. A+B*C+(D*E) + F*G
b. A/B*C-B+A*D/3
c. A+++B---A
d. J= (i++) + (++i)
8 4 2 4 A 1 3 3 3 2 : DI
Note: consider expression as A++ + ++B - --A
Source Code:
evaluate.c
#include<stdio.h>
void main()
{
int A,B,C,D,E,F,G,i,a,b,m,d,J;
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);
a=A+B*C+(D*E)+F*G;
M S C- 7 2 0 2- 3 2 0 2
b=A/B*C-B+A*D/3;
m=A++ + ++B- --A;
J=(i++)+(++i);
printf("a.A+B*C+(D*E) + F*G = %d\n",a);
printf("b.A/B*C-B+A*D/3 = %d\n",b);
printf("c.A+++B---A = %d\n",m);
printf("d.J = (i++) + (++i) = %d\n",J);
Test Case - 1
User Output
Test Case - 2
User Output
Aim:
81 :oN egaP
Take three numbers from the user. Write a C program to display the greatest of three numbers using a conditional
operator.
Source Code:
greatest.c
8 4 2 4 A 1 3 3 3 2 : DI
#include<stdio.h>
void main()
{
int a,b,c,max;
printf("num1: ");
scanf("%d",&a);
printf("num2: ");
scanf("%d",&b);
printf("num3: ");
scanf("%d",&c);
max=(a>b)?((a>c)?a:c):((b>c)?b:c);
M S C- 7 2 0 2- 3 2 0 2
printf("Greatest number: %d\n",max);
}
User Output
num1:
8
num2:
9
num3:
90
Greatest number: 90
Test Case - 2
User Output
num1:
5
num2:
45
num3:
6
Greatest number: 45
Exp. Name: Write a C program to Total and Average of
S.No: 13 Date: 2023-12-07
5 subjects marks
Aim:
91 :oN egaP
Write a program to take marks of 5 subjects in integers, and find the total , average in float.
8 4 2 4 A 1 3 3 3 2 : DI
Total marks : 277.000000
Average marks : 55.400002
Note: Use the printf() function with a newline character ( \n ) to print the output at the end.
Source Code:
TotalAndAvg.c
#include<stdio.h>
void main()
{
M S C- 7 2 0 2- 3 2 0 2
float a,b,c,d,e,s,m;
printf("Enter 5 subjects marks : ");
scanf("%f%f%f%f%f",&a,&b,&c,&d,&e);
s=a+b+c+d+e;
printf("Total marks : %f\n",s);
m=s/5;
Test Case - 1
User Output
Test Case - 2
User Output
Test Case - 3
User Output
02 :oN egaP
Test Case - 4
User Output
8 4 2 4 A 1 3 3 3 2 : DI
Enter 5 subjects marks :
20 30 66 77 44
Total marks : 237.000000
Average marks : 47.400002
Test Case - 5
User Output
M S C- 7 2 0 2- 3 2 0 2
56 78 88 79 64
Total marks : 365.000000
Average marks : 73.000000
Test Case - 6
Aim:
12 :oN egaP
Write a program to find the max and min of four numbers.
Enter 4 numbers : 9 8 5 2
8 4 2 4 A 1 3 3 3 2 : DI
Max value : 9
Min value : 2
Note: Use the printf() function with a newline character ( \n ) to print the output at the end.
Source Code:
MinandMaxOf4.c
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
#include<stdio.h>
void main()
{
int a,b,c,d;
printf("Enter 4 numbers : ");
22 :oN egaP
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a>b&&a>c&&a>d)
{
printf("Max value : %d\n",a);
}
8 4 2 4 A 1 3 3 3 2 : DI
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);
}
if(d>a&&d>b&&d>c)
{
printf("Max value : %d\n",d);
}
M S C- 7 2 0 2- 3 2 0 2
if(a<b&&a<c&&a<d)
{
printf("Min value : %d\n",a);
}
if(b<a&&b<c&&b<d)
{
Test Case - 1
User Output
Enter 4 numbers :
9852
Max value : 9
Min value : 2
Test Case - 2
User Output
Enter 4 numbers :
112 245 167 321
Max value : 321
Min value : 112
32 :oN egaP
Test Case - 3
User Output
8 4 2 4 A 1 3 3 3 2 : DI
Enter 4 numbers :
110 103 113 109
Max value : 113
Min value : 103
Test Case - 4
User Output
Enter 4 numbers :
M S C- 7 2 0 2- 3 2 0 2
-34 -35 -24 -67
Max value : -24
Min value : -67
Test Case - 5
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
Min value : 547
S.No: 15 Exp. Name: Find out the electricity bill charges Date: 2023-12-10
Aim:
An electricity board charges the following rates for the use of electricity:
42 :oN egaP
• 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.
8 4 2 4 A 1 3 3 3 2 : DI
All users are charged a minimum of Rs. 100 as a meter charge even though the amount calculated is less than
Rs. 100.
If the total amount charged is greater than Rs. 400, then an additional surcharge of 15% of the total amount is
charged.
Write a C program to read the name of the user, and the number of units consumed and print out the charges.
Note: Print the amount charged up to 2 decimal places (actual amount, surcharges, amount to be paid).
Source Code:
electricityBillCharges.c
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
#include<stdio.h>
#include<string.h>
void main()
{
int u;
52 :oN egaP
float a,sc=0,t=0;
char sa[100];
printf("Enter customer name: ");
scanf("%s",sa);
printf("Units consumed: ");
8 4 2 4 A 1 3 3 3 2 : DI
scanf("%d",&u);
printf("Customer name: %s\n",sa);
printf("Units consumed: %d\n",u);
if(u<=200)
{
a=u*0.8;
}
else if(u>200&&u<=300)
{
a=u*0.9;
}
else if(u>300)
M S C- 7 2 0 2- 3 2 0 2
{
a=u;
}
if(a<100)
{
t=100.00;
{
t=a;
}
printf("Amount charged: %.2f\n",a);
printf("Surcharges: %.2f\n",sc);
printf("Amount to be paid: %.2f\n",t);
}
Test Case - 1
User Output
62 :oN egaP
Amount to be paid: 100.00
Test Case - 2
8 4 2 4 A 1 3 3 3 2 : DI
User Output
M S C- 7 2 0 2- 3 2 0 2
Test Case - 3
User Output
Test Case - 4
User Output
Aim:
72 :oN egaP
Write a C program to find the roots of a quadratic equation, given its coefficients.
Source Code:
quad.c
8 4 2 4 A 1 3 3 3 2 : DI
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,d;
float r,im;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
{
r=-b/(2*a);
M S C- 7 2 0 2- 3 2 0 2
im=sqrt(-d)/(2*a);
printf("root1 = %.2f+%.2fi and root2 = %.2f-%.2fi\n",r,im,r,im);
}
}
Test Case - 1
User Output
Test Case - 2
User Output
Aim:
82 :oN egaP
Write a program to perform basic calculator operations [+, -, *, /] of two integers a and b using switch statement.
Constraints:
• 10-4 <= a,b = 104
• operations allowed are +, -, *, /
8 4 2 4 A 1 3 3 3 2 : DI
• "/" divisibility will perform integer division operation.
Input Format: The first line of the input consists of an integer which corresponds to a, character which
corresponds to the operator and an integer which corresponds to b.
Output format: Output consists of result after performing mentioned operation (a operation b).
Instruction: To run your custom test cases strictly map your input and output layout with the visible test cases.
Source Code:
calculator.c
M S C- 7 2 0 2- 3 2 0 2
#include<stdio.h>
void main()
{
int a,b;
char i;
printf("");
scanf("%d%c%d",&a,&i,&b);
Test Case - 1
User Output
36-31
5
92 :oN egaP 8 4 2 4 A 1 3 3 3 2 : DI M S C- 7 2 0 2- 3 2 0 2 )suomonotuA( gnireenignE fo egelloC RGVM
Test Case - 2
Test Case - 3
10000/10000
User Output
User Output
89/45
1
1
S.No: 18 Exp. Name: Write a program to find leap year or not Date: 2023-12-10
Aim:
Lucy is celebrating her 15th birthday. Her father promised her that he will buy her a new computer on her birthday
03 :oN egaP
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
8 4 2 4 A 1 3 3 3 2 : DI
year display 2016 is a leap year.? Else display 2016 is not a leap year and check with other leap year conditions.
Source Code:
leapYear.c
#include<stdio.h>
void main()
{
int year;
scanf("%d",&year);
if(year%4==0)
{
M S C- 7 2 0 2- 3 2 0 2
if(year%100==0)
{
if(year%400==0)
printf("%d is a leap year\n",year);
else
printf("%d is not a leap year",year);
}
Test Case - 1
User Output
1900
1900 is not a leap year
Test Case - 2
User Output
2004
2004 is a leap year
13 :oN egaP 8 4 2 4 A 1 3 3 3 2 : DI M S C- 7 2 0 2- 3 2 0 2 )suomonotuA( gnireenignE fo egelloC RGVM
Test Case - 3
1995
S.No: 19 Exp. Name: Factorial of a given number Date: 2023-12-10
Aim:
Write a C program to find the factorial of a given number
23 :oN egaP
Source Code:
factorialOfInt.c
#include<stdio.h>
8 4 2 4 A 1 3 3 3 2 : DI
void main()
{
int a,f=1,i;
printf("Integer: ");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
f=f*i;
}
printf("Factorial: %d\n",f);
}
M S C- 7 2 0 2- 3 2 0 2
Execution Results - All test cases have succeeded!
Test Case - 1
Integer:
5
Factorial: 120
Test Case - 2
User Output
Integer:
4
Factorial: 24
Exp. Name: C program to to determine whether a
S.No: 20 Date: 2023-12-10
given number is prime or not.
Aim:
33 :oN egaP
Write the C program to to determine whether a given number is prime or not.
Source Code:
Prime.c
8 4 2 4 A 1 3 3 3 2 : DI
#include<stdio.h>
void main()
{
int i,n,count=0;
printf("Enter a number: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
M S C- 7 2 0 2- 3 2 0 2
}
if(count==2)
{
printf("%d is a prime number\n",n);
}
else
{
Test Case - 1
User Output
Enter a number:
9
9 is not a prime number
Test Case - 2
User Output
Enter a number:
11
11 is a prime number
Exp. Name: compute sine and cos series using taylor
S.No: 21 Date: 2023-12-12
series
Aim:
43 :oN egaP
Write a C program to compute the sine and cosine series using the Taylor series.
Taylor series:
8 4 2 4 A 1 3 3 3 2 : DI
cos x = 1 - (x2/2!) + (x4/4!) - (x6/6!) + ....
taylor.c
#include<stdio.h>
#include<math.h>
void main()
{
M S C- 7 2 0 2- 3 2 0 2
int i,n;
float sum1,sum2,r,t,x,x1;
printf("angle in radians: \nnumber of terms in the series: ");
scanf("%f%d",&x,&n);
sum1=r=x;
for(i=1;i<n;i++)
{
Test Case - 1
User Output
angle in radians:
0.5
number of terms in the series:
3
Sine = 0.4794
Cosine = 0.8776
Test Case - 2
User Output
angle in radians:
0.6
53 :oN egaP
number of terms in the series:
5
Sine = 0.5646
Cosine = 0.8253
8 4 2 4 A 1 3 3 3 2 : DI
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
Exp. Name: C program to check given number is
S.No: 22 Date: 2023-12-12
palindrome or not
Aim:
63 :oN egaP
Write an C program to check given number is palindrome or not
Source Code:
palindrome.c
8 4 2 4 A 1 3 3 3 2 : DI
#include<stdio.h>
void main()
{
int n,re=0,rem,ori;
scanf("%d",&n);
ori=n;
while(n!=0)
{
rem=n%10;
re=re*10+rem;
n=n/10;
}
M S C- 7 2 0 2- 3 2 0 2
if(ori==re)
printf("%d is a palindrome.\n",ori);
else
printf("%d is not a palindrome.\n",ori);
}
Test Case - 1
User Output
121
121 is a palindrome.
Test Case - 2
User Output
143
143 is not a palindrome.
Exp. Name: Write a C program to print the Pyramid
S.No: 23 Date: 2023-12-12
with numbers
Aim:
73 :oN egaP
Write a program to print a pyramid of numbers separated by spaces for the given number of rows.
At the time of execution, the program should print the message on the console as:
8 4 2 4 A 1 3 3 3 2 : DI
For example, if the user gives the input as :
1
1 2
1 2 3
Source Code:
M S C- 7 2 0 2- 3 2 0 2
PyramidDemo15.c
#include <stdio.h>
void main() {
int n, i, j, s;
printf("Enter number of rows : ");
Test Case - 1
User Output
User Output
83 :oN egaP
1
1 2
1 2 3
1 2 3 4
8 4 2 4 A 1 3 3 3 2 : DI
1 2 3 4 5
1 2 3 4 5 6
Test Case - 3
User Output
M S C- 7 2 0 2- 3 2 0 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
Aim:
93 :oN egaP
Write a C program to find the minimum and maximum in an array of integers.
Source Code:
ArrayElements.c
8 4 2 4 A 1 3 3 3 2 : DI
#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]);
}
min=max=arr[0];
for(int i=1;i<number;i++)
{
if(min>arr[i])
M S C- 7 2 0 2- 3 2 0 2
min=arr[i];
if(max<arr[i])
max=arr[i];
}
printf("Min an Max: %d and %d",min ,max);
}
Test Case - 1
User Output
5
Elements:
49682
Min an Max: 2 and 9
Test Case - 2
User Output
1
Elements:
216
Min an Max: 216 and 216
Exp. Name: Search for an element using Linear
S.No: 25 Date: 2023-12-12
search
Aim:
04 :oN egaP
Write a C program to check whether the given element is present or not in the array of elements using linear
search.
Source Code:
SearchEle.c
8 4 2 4 A 1 3 3 3 2 : DI
#include<stdio.h>
void main()
{
int key,i,n,arr[100];
printf("Enter size: ");
scanf("%d",&n);
printf("Enter %d element: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
M S C- 7 2 0 2- 3 2 0 2
printf("Enter search element: ");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(arr[i]==key)
{
printf("Found at position %d\n",i);
Test Case - 1
User Output
Enter size:
6
Enter 6 element:
248135
Enter search element:
6
6 is not found
Test Case - 2
User Output
14 :oN egaP 8 4 2 4 A 1 3 3 3 2 : DI M S C- 7 2 0 2- 3 2 0 2 )suomonotuA( gnireenignE fo egelloC RGVM
Enter search element:
Found at position 0
Enter 6 element:
Enter size:
248135
6
2
S.No: 26 Exp. Name: C program to reverse an array. Date: 2023-12-12
Aim:
Write a C program to reverse the elements an array of integers.
24 :oN egaP
Source Code:
reverseArray.c
#include<stdio.h>
8 4 2 4 A 1 3 3 3 2 : DI
void main()
{
int i,n,arr[100];
printf("Enter no of elements: ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("The reversed array: ");
for(i=n-1;i>=0;i--)
M S C- 7 2 0 2- 3 2 0 2
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:
8
Enter elements:
2 5 1 77 33 88 2 9
The reversed array: 9 2 88 33 77 1 5 2
S.No: 27 Exp. Name: 2’s complement of a given Binary number Date: 2023-12-12
Aim:
Write a C program to find 2’s complement of a given binary number.
34 :oN egaP
Note: The binary input should be separated by a space.
Source Code:
twosComplement.c
8 4 2 4 A 1 3 3 3 2 : DI
#include<stdio.h>
void main()
{
int ones[100],twos[100],bin[100],i,j=1,n;
printf("Enter size: ");
scanf("%d",&n);
printf("Enter %d bit binary number: ",n);
for(i=0;i<n;i++)
scanf("%d",&bin[i]);
for(i=0;i<n;i++)
{
M S C- 7 2 0 2- 3 2 0 2
if(bin[i]==0)
ones[i]=1;
else if(bin[i]==1)
ones[i]=0;
}
n--;
for(i=n;i>=0;i--)
Test Case - 1
User Output
Enter size:
5
Enter 5 bit binary number:
10010
2's complement: 0 1 1 1 0
44 :oN egaP
Test Case - 2
User Output
Enter size:
6
8 4 2 4 A 1 3 3 3 2 : DI
Enter 6 bit binary number:
100011
2's complement: 0 1 1 1 0 1
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
S.No: 28 Exp. Name: Eliminate Duplicate elements in an array Date: 2023-12-12
Aim:
Write a C program to eliminate duplicate elements of an array.
54 :oN egaP
Source Code:
eliminateDuplicates.c
#include<stdio.h>
8 4 2 4 A 1 3 3 3 2 : DI
void main()
{
int i,n,j,k,a[100];
printf("Enter size: ");
scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
M S C- 7 2 0 2- 3 2 0 2
if(a[i]==a[j])
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n--;
Test Case - 1
User Output
Enter size:
5
Enter 5 elements:
12123
After eliminating duplicates: 1 2 3
Test Case - 2
User Output
Enter size:
5
64 :oN egaP
Enter 5 elements:
11 13 11 12 13
After eliminating duplicates: 11 13 12
8 4 2 4 A 1 3 3 3 2 : DI
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
S.No: 29 Exp. Name: Addition of two matrices Date: 2023-12-18
Aim:
Write a C program to perform the addition of two matrices.
74 :oN egaP
Source Code:
addTwoMatrices.c
#include<stdio.h>
8 4 2 4 A 1 3 3 3 2 : DI
void main()
{
int i,j,a[5][5],b[5][5],c[5][5],m,n;
printf("Enter no of rows, columns: ");
scanf("%d%d",&m,&n);
printf("Elements of matrix 1: ");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
M S C- 7 2 0 2- 3 2 0 2
}
printf("Elements of matrix 2: ");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&b[i][j]);
Test Case - 1
User Output
84 :oN egaP
Addition of matrices:
10 10
Test Case - 2
8 4 2 4 A 1 3 3 3 2 : DI
User Output
M S C- 7 2 0 2- 3 2 0 2
10 10 10
Aim:
Write a C program to find the multiplication of two matrices
94 :oN egaP
Source Code:
matrixMul.c
8 4 2 4 A 1 3 3 3 2 : DI
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
#include<stdio.h>
void main()
{
int r,s;
05 :oN egaP
int i,j;
printf("no of rows, columns of matrix1: ");
scanf("%d %d", &r, &s);
int a[r][s];
printf("matrix1 elements:");
8 4 2 4 A 1 3 3 3 2 : DI
for(i=0;i<r;i++)
{
for(j=0;j<s;j++)
{
scanf("%d",&a[i][j]);
}
}
int ro,co;
printf("no of rows, columns of matrix2: ");
scanf("%d %d", &ro,&co);
int b[ro] [co];
int k;
M S C- 7 2 0 2- 3 2 0 2
printf("matrix2 elements:\n");
for(i=0;i<ro;i++)
{
for(j=0;j<co;j++)
{
scanf("%d",&b[i][j]);
printf("Given matrix2:\n");
for(i=0;i<ro;i++)
{
for(j=0;j<co;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
if(s!=ro)
{
printf("Multiplication not possible\n");
}
else{
{
for(j=0;j<co;j++)
{
ck[i][j]=0;
for(k=0;k<ro;k++)
15 :oN egaP
{
ck[i][j]=ck[i][j]+(a[i][k]*b[k][j]);
}
}
}
8 4 2 4 A 1 3 3 3 2 : DI
printf("Multiplication of two matrices:\n");
for(i=0;i<r;i++)
{
for(j=0;j<co;j++)
{
printf("%d ",ck[i][j]);
}
printf("\n");
}
}
}
M S C- 7 2 0 2- 3 2 0 2
Execution Results - All test cases have succeeded!
User Output
Test Case - 2
User Output
25 :oN egaP
456
789
no of rows, columns of matrix2:
23
8 4 2 4 A 1 3 3 3 2 : DI
matrix2 elements:
123
456
Given matrix1:
1 2 3
4 5 6
7 8 9
Given matrix2:
1 2 3
4 5 6
M S C- 7 2 0 2- 3 2 0 2
Multiplication not possible
Aim:
35 :oN egaP
Develop an algorithm, implement and execute a C program that reads n integer numbers and arrange them in
ascending order using Bubble Sort.
Source Code:
Lab7.c
8 4 2 4 A 1 3 3 3 2 : DI
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
#include<stdio.h>
void main()
45 :oN egaP
int s;
scanf("%d", &s);
8 4 2 4 A 1 3 3 3 2 : DI
int a[s];
int i,j;
printf("Elements: ");
for(i=0;i<s;i++)
scanf("%d", &a[i]);
printf("");
M S C- 7 2 0 2- 3 2 0 2
printf("Before sorting: ");
for(i=0;i<s;i++)
printf("%d ",a[i]);
for(i=0;i<s-1;i++){
for(j=0;j<s-1-i;j++){
if(a[j]>a[j+1]){
int t =a[j];
a[j]=a[j+1];
a[j+1]=t;
for(i=0;i<s;i++)
printf("%d ",a[i]);
printf("\n");
}
55 :oN egaP
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
8 4 2 4 A 1 3 3 3 2 : DI
4
Elements:
44 22 66 11
Before sorting: 44 22 66 11
After sorting: 11 22 44 66
Test Case - 2
User Output
M S C- 7 2 0 2- 3 2 0 2
5
Elements:
92716
Before sorting: 9 2 7 1 6
After sorting: 1 2 6 7 9
Aim:
65 :oN egaP
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 :
8 4 2 4 A 1 3 3 3 2 : DI
For example, if the user gives the input as:
string1 : ILove
Next, the program should print the message on the console as:
string2 :
string2 : Coding
M S C- 7 2 0 2- 3 2 0 2
then the program should print the result as:
Note: Do use the printf() function with a newline character ( \n ) at the end.
Program605.c
#include<stdio.h>
void main()
75 :oN egaP
char string1[20];
char string2[20];
8 4 2 4 A 1 3 3 3 2 : DI
printf("string1 : ");
scanf("%s", string1);
printf("string2 : ");
scanf("%s", string2);
int length=0;
int size=0;
M S C- 7 2 0 2- 3 2 0 2
while(string1[length]!= '\0')
length++;
while(string2[size]!= '\0')
int i;
for(i=0;i<size;i++)
string1[length+i]=string2[i];
string1[length+i]='\0';
printf("\n");
Test Case - 1
User Output
string1 :
ILove
string2 :
Coding
concatenated string = ILoveCoding
Test Case - 2
85 :oN egaP
User Output
string1 :
1234
string2 :
8 4 2 4 A 1 3 3 3 2 : DI
567
concatenated string = 1234567
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
Exp. Name: Write a C program to Reverse the given
S.No: 33 Date: 2023-12-19
string without using the Library Functions
Aim:
95 :oN egaP
Write a program to reverse the given string without using the library functions.
At the time of execution, the program should print the message on the console as:
Enter a string :
8 4 2 4 A 1 3 3 3 2 : DI
For example, if the user gives the input as:
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
M S C- 7 2 0 2- 3 2 0 2
Program609.c
#include<stdio.h>
void main()
{
char a[20];
Test Case - 1
User Output
Enter a string :
Dallas
Reverse string : sallaD
Exp. Name: Write a C program to find Sum of array
S.No: 34 elements by allocating memory using malloc() Date: 2023-12-26
function
06 :oN egaP
Aim:
Write a program to find the sum of n elements by allocating memory by using malloc() function.
8 4 2 4 A 1 3 3 3 2 : DI
Source Code:
SumOfArray1.c
#include <stdio.h>
#include <stdlib.h>
#include "UsingMalloc.c"
void main() {
int *p, n, i;
printf("Enter n value : ");
scanf("%d", &n);
p = allocateMemory(n);
printf("Enter %d values : ", n);
M S C- 7 2 0 2- 3 2 0 2
read1(p, n);
printf("The sum of given array elements : %d\n", sum(p, n));
}
UsingMalloc.c
16 :oN egaP
}
8 4 2 4 A 1 3 3 3 2 : DI
{
int i;
for(i=0;i<n;i++)
scanf("%d",p+i);
M S C- 7 2 0 2- 3 2 0 2
}
for(i=0;i<n;i++)
return total;
Test Case - 1
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
26 :oN egaP
Enter 4 values :
-5 -6 -4 -2
The sum of given array elements : -17
8 4 2 4 A 1 3 3 3 2 : DI
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
Exp. Name: Write a program to find Total and Average
S.No: 35 gained by Students in a Section using Array of Date: 2023-12-26
Structures
36 :oN egaP
Aim:
Write a C program to find out the total and average marks gained by the students in a section using array of
structures.
8 4 2 4 A 1 3 3 3 2 : DI
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
M S C- 7 2 0 2- 3 2 0 2
Source Code:
ArrayOfStructures2.c
struct student {
46 :oN egaP
int regdNo;
int marks[3];
8 4 2 4 A 1 3 3 3 2 : DI
int total;
float average;
};
void main() {
int i, n;
M S C- 7 2 0 2- 3 2 0 2
printf("Enter number of students : ");
scanf("%d", &n);
scanf("%d%d%d%d",&s[i].regdNo,&s[i].marks[0],&s[i].marks[1],&s[i].marks[2]);
s[i].average = (s[i].total)/(3.0);
}
}
User Output
56 :oN egaP
Enter regdno, three subjects marks of student-0:
101 56 78 76
Enter regdno, three subjects marks of student-1:
201 76 89 91
8 4 2 4 A 1 3 3 3 2 : DI
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
Test Case - 2
User Output
M S C- 7 2 0 2- 3 2 0 2
10
Enter regdno, three subjects marks of student-0:
501 23 45 67
Enter regdno, three subjects marks of student-1:
502 78 65 76
Test Case - 3
User Output
66 :oN egaP
Enter number of students :
5
Enter regdno, three subjects marks of student-0:
101 76 78 73
8 4 2 4 A 1 3 3 3 2 : DI
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:
105 88 47 69
Student-0 Regdno = 101 Total marks = 227 Average marks = 75.666664
Student-1 Regdno = 102 Total marks = 214 Average marks = 71.333336
M S C- 7 2 0 2- 3 2 0 2
Student-2 Regdno = 103 Total marks = 203 Average marks = 67.666664
Student-3 Regdno = 104 Total marks = 136 Average marks = 45.333332
Student-4 Regdno = 105 Total marks = 204 Average marks = 68.000000
Aim:
76 :oN egaP
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:
8 4 2 4 A 1 3 3 3 2 : DI
FailedList.c
#include <stdio.h>
#include <stdlib.h>
struct student {
int roll;
int marks[6], sum;
float avg;
};
#include "FailedList1.c"
void main() {
struct student *s;
M S C- 7 2 0 2- 3 2 0 2
int i, n;
printf("Enter the number of students : ");
scanf("%d", &n);
s = allocateMemory(s, n);
read1(s, n);
calculateMarks(s, n);
displayFailedList(s, n);
FailedList1.c
struct student* allocateMemory(struct student *s, int n) {
86 :oN egaP
return s;
8 4 2 4 A 1 3 3 3 2 : DI
void read1(struct student *s, int n) {
scanf("%d",&s[i].roll);
M S C- 7 2 0 2- 3 2 0 2
printf("Enter 6 subjects marks : ");
scanf("%d%d%d%d%d%d",&s[i].marks[0],&s[i].marks[1],&s[i].marks[2],&s[i].marks[3],&s[i].marks[4],&
}
}
96 :oN egaP
void calculateMarks(struct student *s, int n) {
8 4 2 4 A 1 3 3 3 2 : DI
for(int i=0;i<n;i++){
int total = 0;
s[i].sum = total;
M S C- 7 2 0 2- 3 2 0 2
s[i].avg = (float)total / 6.0;
int i;
printf("RollNo\tTotalMarks\tAverageMarks\tStatus\n");
printf("%d\t", s[i].roll);
// Fill the missingcode
printf("%f\t", s[i].avg);
int flag = 0;
// Fill the missing code
flag = 1;
break;
}
if (flag==1) // Fill the missing code
printf("Fail");
else
07 :oN egaP
printf("Pass");
printf("\n");
8 4 2 4 A 1 3 3 3 2 : DI
}
Test Case - 1
User Output
M S C- 7 2 0 2- 3 2 0 2
Enter the number of students :
3
Enter the details of student - 1
Enter the roll number :
101
Enter 6 subjects marks :
Test Case - 2
User Output
17 :oN egaP
1002
Enter 6 subjects marks :
58 67 58 89 87 76
RollNo TotalMarks AverageMarks Status
8 4 2 4 A 1 3 3 3 2 : DI
1001 350 58.333332 Fail
1002 435 72.500000 Pass
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
Exp. Name: Write a C program to find Total Marks of a
S.No: 37 Date: 2023-12-26
Student using Command-line arguments
Aim:
27 :oN egaP
Write a C program to read student name and 3 subjects marks from the command line and display the student
details along with total.
8 4 2 4 A 1 3 3 3 2 : DI
If the arguments passed as $./TotalMarksArgs.c Sachin 67 89 58 , then the program
should print the output as:
Cmd Args : Sachin 67 89 58
Student name : Sachin
Subject-1 marks : 67
Subject-1 marks : 89
Subject-1 marks : 58
Total marks : 214
M S C- 7 2 0 2- 3 2 0 2
should print the output as:
Cmd Args : Johny 45 86 57 48
Arguments passed through command line are not equal to 4
Hint : atoi() is a library function that converts string to integer. When program gets the input from command
line, string values transfer in the program, we have to convert them to integers. atoi() is used to return the
TotalMarksArgs.c
#include<stdio.h>
#include<stdlib.h>
37 :oN egaP
{
if(argc != 5)
8 4 2 4 A 1 3 3 3 2 : DI
{
return 1;
M S C- 7 2 0 2- 3 2 0 2
printf("Subject-2 marks : %s\n", argv[3]);
int sum=0;
Test Case - 1
User Output
Test Case - 2
User Output
Aim:
Write a C program to implement realloc() .
47 :oN egaP
The process is
0. Allocate memory of an array with size 2 by using malloc()
0. Assign the values 10 and 20 to the array
0. Reallocate the size of the array to 3 by using realloc()
8 4 2 4 A 1 3 3 3 2 : DI
0. Assign the value 30 to the newly allocated block
0. Display all the 3 values
Source Code:
ProgramOnRealloc.c
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int) * 2);
int i;
int *ptr_new;
M S C- 7 2 0 2- 3 2 0 2
*ptr = 10;
*(ptr + 1) = 20;
Test Case - 1
User Output
10 20 30
Exp. Name: Write a C Program to store information
S.No: 39 Date: 2024-01-23
using Structures with DMA
Aim:
57 :oN egaP
Write a program to create a list of nodes using self-referential structure and print that data.
At the time of execution, the program should print the message on the console as:
8 4 2 4 A 1 3 3 3 2 : DI
For example, if the user gives the input as:
Next, the program should print the message on the console as:
M S C- 7 2 0 2- 3 2 0 2
The input to the list is continued up to the user says n (No)
Finally, the program should print the result on the console as:
StructuresWithDma.c
#include <stdio.h>
#include <stdlib.h>
struct list {
int data;
struct list *next;
};
#include "CreateNodes.c"
void main() {
struct list *first = NULL;
first = create(first);
printf("The elements in the single linked lists are : ");
display(first);
}
67 :oN egaP 8 4 2 4 A 1 3 3 3 2 : DI M S C- 7 2 0 2- 3 2 0 2 )suomonotuA( gnireenignE fo egelloC RGVM
CreateNodes.c
struct list* create(struct list *first)
77 :oN egaP
char op;
8 4 2 4 A 1 3 3 3 2 : DI
struct list *q, *temp;
do {
M S C- 7 2 0 2- 3 2 0 2
printf("Enter an integer value : ");
if (first == NULL) {
}else{
}
printf("Do u want another list (y|n) : ");
87 :oN egaP
scanf(" %c", &op);
8 4 2 4 A 1 3 3 3 2 : DI
while(op == 'y' || op == 'Y');
return first;
M S C- 7 2 0 2- 3 2 0 2
void display(struct list *first) {
printf("%d-->", temp->data);
printf("NULL\n");
Test Case - 1
User Output
97 :oN egaP
Enter an integer value :
20
Do u want another list (y|n) :
y
8 4 2 4 A 1 3 3 3 2 : DI
Enter an integer value :
30
Do u want another list (y|n) :
n
The elements in the single linked lists are : 10-->20-->30-->NULL
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
Exp. Name: Write a C program to demonstrate the
S.No: 40 Date: 2024-01-06
differences between Structures and Unions
Aim:
08 :oN egaP
Write a C program to demonstrate the differences between structures and unions .
The process is
0. Create a structure student-1 with members rollno, m1, m2, m3, total of int type and avg of float type
0. Read rollno, m1, m2 and m3 of student-1
8 4 2 4 A 1 3 3 3 2 : DI
0. Find and display total and average marks of student-1
0. Display the size of struct student-1
0. Create a union student-2 with members rollno, m1, m2, m3, total of int type and avg of float type
0. Read rollno, m1, m2 and m3 of student-2
0. Find and display total and average marks of student-2
0. Display the size of union student-2
Sample Input and Output:
M S C- 7 2 0 2- 3 2 0 2
Enter first subject marks of student - 2 : 76
Enter second subject marks of student - 2 : 87
Enter third subject marks of student - 2 : 69
Total marks of student - 2 : 232
Average marks of student - 2 : 77.333336
Size of union student - 2 : 4
StructureAndUnion.c
#include<stdio.h>
#include<stdlib.h>
struct student1
18 :oN egaP
{
long rollno;
8 4 2 4 A 1 3 3 3 2 : DI
int sub1,sub2,sub3;
}s1;
union data
int rollno;
int sub1,sub2,sub3;
M S C- 7 2 0 2- 3 2 0 2
}s;
void main()
float avg;
scanf("%ld%d%d%d",&s1.rollno,&s1.sub1,&s1.sub2,&s1.sub3);
tot=s1.sub1+s1.sub2+s1.sub3;
avg=(float)tot/3;
scanf("%d",&s.rollno);
scanf("%d",&s1.sub1);
scanf("%d",&s1.sub2);
scanf("%d",&s1.sub3);
tot = s1.sub1+s1.sub2+s1.sub3;
avg=(float)tot/3;
28 :oN egaP
printf("Total marks of student - 2 : %d\n",tot);
8 4 2 4 A 1 3 3 3 2 : DI
printf("Size of union student - 2 : %lu\n",sizeof(union data));
Test Case - 1
User Output
M S C- 7 2 0 2- 3 2 0 2
Enter rollno and 3 subjects marks of student - 1 :
101 76 58 67
Total and average marks of student - 1 : 201 67.000000
Size of struct student - 1 : 24
Enter rollno of student - 2 :
Test Case - 2
User Output
38 :oN egaP
Size of union student - 2 : 4
Test Case - 3
8 4 2 4 A 1 3 3 3 2 : DI
User Output
M S C- 7 2 0 2- 3 2 0 2
57
Enter third subject marks of student - 2 :
69
Total marks of student - 2 : 225
Average marks of student - 2 : 75.000000
Size of union student - 2 : 4
User Output
Aim:
Write a C program to demonstrate left shift operation
48 :oN egaP
Source Code:
shift.c
8 4 2 4 A 1 3 3 3 2 : DI
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
#include<stdio.h>
58 :oN egaP
if(num==0)
8 4 2 4 A 1 3 3 3 2 : DI
printf("Binary: 0\n");
return ;
int binary[32];
int index;
while(num>0)
M S C- 7 2 0 2- 3 2 0 2
{
binary[index]=num%2;
num=num/2;
for(int i=index-1;i>=0;i--)
printf("%d",binary[i]);
printf("\n");
int main()
int n,shiftcount;
scanf("%d",&n);
printbinary(n);
printf("Binary representation:");
68 :oN egaP
printbinary(result);
return 0;
8 4 2 4 A 1 3 3 3 2 : DI
}
Test Case - 1
User Output
Enter an integer:
12
M S C- 7 2 0 2- 3 2 0 2
Original value: 1100
number of bits to left shift:
2
After left shift: 48
Binary representation:110000
User Output
Enter an integer:
5
Original value: 101
number of bits to left shift:
3
After left shift: 40
Binary representation:101000
Exp. Name: Copy the contents of one structure
S.No: 42 Date: 2024-01-23
variable to another structure variable
Aim:
78 :oN egaP
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
8 4 2 4 A 1 3 3 3 2 : DI
same to another structure variable, finally display the copied data.
Source Code:
CopyStructureMain.c
#include <stdio.h>
#include "CopyStructureFunctions.c"
void main() {
struct student s1, s2;
read(&s1);
s2 = copyStructureVariable(s1, s2);
M S C- 7 2 0 2- 3 2 0 2
display(s2);
}
CopyStructureFunctions.c
char name[10];
88 :oN egaP
int age;
float height;
8 4 2 4 A 1 3 3 3 2 : DI
}s1, s2;
M S C- 7 2 0 2- 3 2 0 2
// Write the code to take inputs to structure
s1.age=s2.age;
s1.height = s2.height;
return s2;
void display(struct student s) {
98 :oN egaP
//write your code here to display the structure data
8 4 2 4 A 1 3 3 3 2 : DI
printf("Student name: %s", s1.name);
printf("\nHeight: %f\n",s1.height);
M S C- 7 2 0 2- 3 2 0 2
}
User Output
Test Case - 2
User Output
Aim:
09 :oN egaP
Draw the flowchart and write a recursive C function to find the factorial of a number, n! , defined by fact(n) = 1, if
n = 0. Otherwise fact(n) = n * fact(n-1).
Using this function, write a C program to compute the binomial coefficient ncr . Tabulate the results for different
8 4 2 4 A 1 3 3 3 2 : DI
values of n and r with suitable messages.
At the time of execution, the program should print the message on the console as:
M S C- 7 2 0 2- 3 2 0 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 z)
{
int f=1,i;
if(z==0)
return(f);
else
{
for(i=1;i<=z;i++)
{
f=f*i;
}
}
return(f);
}
Lab14.c
#include <stdio.h>
#include "Lab14a.c"
void main() {
int n, r;
printf("Enter the values of n and r : ");
19 :oN egaP
scanf("%d %d", &n, &r);
if (n >= r)
printf("The value of %dc%d = %d\n", n, r, factorial(n) /
(factorial(r) * factorial(n - r)));
else
8 4 2 4 A 1 3 3 3 2 : DI
printf("Enter valid input data\n");
}
Test Case - 1
User Output
M S C- 7 2 0 2- 3 2 0 2
10 4
The value of 10c4 = 210
Test Case - 2
User Output
Test Case - 3
User Output
Aim:
29 :oN egaP
Write a C program to find the length of a given string.
8 4 2 4 A 1 3 3 3 2 : DI
Length of CodeTantra : 10
Source Code:
StrLength.c
#include <stdio.h>
#include "StrLength1.c"
void main() {
char str[30];
printf("Enter the string : ");
scanf("%s", str);
M S C- 7 2 0 2- 3 2 0 2
printf("Length of %s : %d\n", str, myStrLen(str));
}
StrLength1.c
return(strlen(str));
}
Test Case - 1
User Output
Test Case - 2
User Output
User Output
39 :oN egaP
Length of MalayalaM : 9
Test Case - 4
8 4 2 4 A 1 3 3 3 2 : DI
User Output
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
Exp. Name: Demonstrate numerical integration of
S.No: 46 Date: 2023-12-26
differential equations using Euler’s method
Aim:
49 :oN egaP
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.
8 4 2 4 A 1 3 3 3 2 : DI
The formula for Euler's Method:
Source Code:
euler.c
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
#include <stdio.h>
59 :oN egaP
double f(double y, double t) {
return y*t;
// Example: dy/dt = t*y
}
8 4 2 4 A 1 3 3 3 2 : DI
// Euler's method for numerical integration
void eulerIntegration(double *y,double t,double h ) {
*y=*y+h*(f(*y,t));
}
int main() {
double t,y,h,l;
scanf("%lf",&y);
M S C- 7 2 0 2- 3 2 0 2
printf("initial value of t (t0): ");
scanf("%lf",&t);
scanf("%lf",&l);
while (t<l){
eulerIntegration(&y,t,h);
t+=h;
Test Case - 1
User Output
69 :oN egaP
10
t = 1.00 y = 1.00
t = 4.00 y = 4.00
t = 7.00 y = 52.00
8 4 2 4 A 1 3 3 3 2 : DI
Test Case - 2
User Output
M S C- 7 2 0 2- 3 2 0 2
end value for t:
3
t = 1.00 y = 1.00
Aim:
79 :oN egaP
Write a program to display the fibonacci series up to the given number of terms using recursion process.
Source Code:
fibonacciSeries.c
8 4 2 4 A 1 3 3 3 2 : DI
#include <stdio.h>
#include "fibonacciSeriesa.c"
void main() {
int n, i;
printf("n: ");
scanf("%d", &n);
printf("%d terms: ", n);
for (i = 0; i < n; i++) {
printf("%d ", fib(i));
}
}
M S C- 7 2 0 2- 3 2 0 2
fibonacciSeriesa.c
int fib(int k)
{
if(k==0)
{
Test Case - 1
User Output
n:
4
4 terms: 0 1 1 2
Test Case - 2
89 :oN egaP 8 4 2 4 A 1 3 3 3 2 : DI M S C- 7 2 0 2- 3 2 0 2 )suomonotuA( gnireenignE fo egelloC RGVM
10 terms: 0 1 1 2 3 5 8 13 21 34
User Output
10
n:
Exp. Name: Write a C program to find the LCM of two
S.No: 48 Date: 2023-12-26
numbers using Recursion
Aim:
99 :oN egaP
Write a program to find the lcm (Least Common Multiple) of a given two numbers using recursion process.
The least common multiple ( lcm ) of two or more integers, is the smallest positive integer that is divisible by
both a and b.
8 4 2 4 A 1 3 3 3 2 : DI
At the time of execution, the program should print the message on the console as:
M S C- 7 2 0 2- 3 2 0 2
Note: Write the function lcm() and recursive function gcd() in Program907a.c .
Source Code:
Program907.c
#include <stdio.h>
Program907a.c
int lcm();
multiple += b;
8 4 2 4 A 1 3 3 3 2 : DI
return multiple;
else
return lcm(a,b);
M S C- 7 2 0 2- 3 2 0 2
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Test Case - 2
User Output
Test Case - 3
User Output
Test Case - 4
User Output
Test Case - 5
User Output
8 4 2 4 A 1 3 3 3 2 : DI
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
Exp. Name: Write a C program to find the Factorial of
S.No: 49 Date: 2023-12-26
a given number using Recursion
Aim:
8 4 2 4 A 1 3 3 3 2 : DI
Program901.c
#include <stdio.h>
#include "Program901a.c"
void main() {
long int n;
printf("Enter an integer : ");
scanf("%ld", &n);
printf("Factorial of %ld is : %ld\n", n ,factorial(n));
}
M S C- 7 2 0 2- 3 2 0 2
Program901a.c
return 1;
else
return n*factorial(n-1);
Test Case - 1
User Output
Enter an integer :
5
Factorial of 5 is : 120
Test Case - 2
User Output
8 4 2 4 A 1 3 3 3 2 : DI
Test Case - 3
User Output
Enter an integer :
8
Factorial of 8 is : 40320
Test Case - 4
User Output
M S C- 7 2 0 2- 3 2 0 2
Enter an integer :
0
Factorial of 0 is : 1
Aim:
At the time of execution, the program should print the message on the console as:
8 4 2 4 A 1 3 3 3 2 : DI
For example, if the user gives the input as:
A(2, 1) = 5
Source Code:
M S C- 7 2 0 2- 3 2 0 2
AckermannFunction.c
#include <stdio.h>
#include "AckermannFunction1.c"
void main() {
long long int m, n;
printf("Enter two numbers : ");
AckermannFunction1.c
#include<math.h>
return n+1;
8 4 2 4 A 1 3 3 3 2 : DI
}
else if (n==0&&m>0)
return ackermannFun(m-1,1);
M S C- 7 2 0 2- 3 2 0 2
else
return ackermannFun(m-1,ackermannFun(m,n-1));
Test Case - 1
User Output
Test Case - 2
User Output
Test Case - 3
User Output
Enter two numbers :
21
A(2, 1) = 5
User Output
8 4 2 4 A 1 3 3 3 2 : DI
A(1, 1) = 3
Test Case - 5
User Output
M S C- 7 2 0 2- 3 2 0 2
Test Case - 6
User Output
Aim:
Write a program to find the sum of n natural numbers using recursion process.
Enter value of n :
8 4 2 4 A 1 3 3 3 2 : DI
For example, if the user gives the input as:
Enter value of n : 6
Program903.c
M S C- 7 2 0 2- 3 2 0 2
#include <stdio.h>
#include "Program903a.c"
void main() {
int n;
printf("Enter value of n : ");
scanf("%d", &n);
Program903a.c
int sum(int n)
if(n<=1)
return n;
return n+sum(n-1);
Test Case - 1
User Output
Enter value of n :
5
Sum of 5 natural numbers = 15
Test Case - 2
Enter value of n :
9
Sum of 9 natural numbers = 45
8 4 2 4 A 1 3 3 3 2 : DI
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
Exp. Name: Write a C program to Swap two values by
S.No: 52 Date: 2023-12-29
using Call-by-Address method
Aim:
At the time of execution, the program should print the message on the console as:
8 4 2 4 A 1 3 3 3 2 : DI
For example, if the user gives the input as:
Note: Write the function swap() in Program1002a.c and do use the printf() function with a newline character
M S C- 7 2 0 2- 3 2 0 2
( \n ).
Source Code:
Program1002.c
#include <stdio.h>
Program1002a.c
void swap(int*p,int*q)
int temp;
*p=*q;
*q=temp;
8 4 2 4 A 1 3 3 3 2 : DI
printf("After swapping in swap : *p = %d *q = %d\n",*p,*q);
M S C- 7 2 0 2- 3 2 0 2
Test Case - 1
User Output
Test Case - 2
User Output
Test Case - 3
User Output
Test Case - 4
User Output
Test Case - 5
8 4 2 4 A 1 3 3 3 2 : DI
User Output
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
S.No: 53 Exp. Name: Dangling Pointers Date: 2023-12-29
Aim:
Demonstrate Dangling pointer problem using a C program.
danglingPointer.c
8 4 2 4 A 1 3 3 3 2 : DI
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr1 = NULL;
8 4 2 4 A 1 3 3 3 2 : DI
printf("Enter an integer value: ");
scanf("%d",&value);
ptr1=(int*)malloc(sizeof(int));
M S C- 7 2 0 2- 3 2 0 2
if (ptr1!=NULL)
{
*ptr1=value;
free(ptr1);
}
else
{
ptr1 = NULL;
ptr2 = NULL;
return 0;
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
8 4 2 4 A 1 3 3 3 2 : DI
Test Case - 2
User Output
M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
Exp. Name: Write a C program to Copy one String into
S.No: 54 Date: 2023-12-29
another using Pointers
Aim:
8 4 2 4 A 1 3 3 3 2 : DI
Target string : Robotic Tool
Source Code:
CopyStringPointers.c
#include <stdio.h>
#include "CopyStringPointers1.c"
void main() {
char source[100], target[100];
printf("Enter source string : ");
fgets(source, sizeof(source), stdin);
M S C- 7 2 0 2- 3 2 0 2
copyString(target, source);
printf("Target string : %s\n", target);
}
CopyStringPointers1.c
char *ptr;
int i=0;
ptr=source;
while(*ptr!='\0')
target[i++]=*ptr++;
target[i]='\0';
Test Case - 1
User Output
Test Case - 2
User Output
8 4 2 4 A 1 3 3 3 2 : DI
Test Case - 3
User Output
Test Case - 4
User Output
M S C- 7 2 0 2- 3 2 0 2
Enter source string :
Hyderabad London Losangels Weelington
Colombo
Target string : Hyderabad London Losangels Weelington Colombo
8 4 2 4 A 1 3 3 3 2 : DI
Enter a string : Indo Pak 125 143 *.$
Number of uppercase letters = 2
Number of lowercase letters = 5
Number of digits = 6
Number of other characters = 7
Source Code:
CountCharDigitOthers.c
#include <stdio.h>
M S C- 7 2 0 2- 3 2 0 2
#include "CountCharDigitOthers1.c"
void main() {
char str[80];
int upperCount = 0, lowerCount = 0, digitCount = 0, otherCount = 0;
printf("Enter a string : ");
gets(str);
CountCharDigitOthers1.c
void countCharDigitOthers(char *str,int *uppercount,int *lowercount,int
*digitcount,int *othercount)
{
while(*str!='\0'){
(*uppercount)++;
else if(*str>='a'&&*str<='z')
8 4 2 4 A 1 3 3 3 2 : DI
(*lowercount)++;
else if(*str>='0'&&*str<='9')
(*digitcount)++;
else
(*othercount)++;
*str++;
M S C- 7 2 0 2- 3 2 0 2
}
Test Case - 1
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
Number of other characters = 7
Test Case - 3
User Output
Enter a string :
12345
Number of uppercase letters = 0
Number of lowercase letters = 0
Test Case - 4
8 4 2 4 A 1 3 3 3 2 : DI
User Output
Enter a string :
USA@
Number of uppercase letters = 3
Number of lowercase letters = 0
Number of digits = 0
Number of other characters = 1
M S C- 7 2 0 2- 3 2 0 2
Test Case - 5
User Output
Enter a string :
Wellington@NZ I will Stay Here
Number of uppercase letters = 6