KEMBAR78
CP Lab Programs | PDF | Fahrenheit | Mathematics
0% found this document useful (0 votes)
14 views120 pages

CP Lab Programs

The document contains a series of C programming exercises aimed at teaching various programming concepts, including displaying messages, scanning different data types, performing arithmetic operations, calculating sums and averages, temperature conversions, and calculating simple and compound interest. Each exercise includes source code, execution results, and test cases demonstrating the expected output. The exercises are structured to progressively build programming skills through practical applications.

Uploaded by

Gopi Tirumala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views120 pages

CP Lab Programs

The document contains a series of C programming exercises aimed at teaching various programming concepts, including displaying messages, scanning different data types, performing arithmetic operations, calculating sums and averages, temperature conversions, and calculating simple and compound interest. Each exercise includes source code, execution results, and test cases demonstrating the expected output. The exercises are structured to progressively build programming skills through practical applications.

Uploaded by

Gopi Tirumala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 120

Exp.

Name: Display hello world and a greeting to the


S.No: 1 Date: 2023-12-05
user.

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

)suomonotuA( gnireenignE fo egelloC RGVM


Enter your name:
Jack
Hello World
Hello Jack

Test Case - 2

User Output

Enter your name:


Jonathan
Hello World
Hello Jonathan
Exp. Name: Scan all data type variables and display
S.No: 2 Date: 2023-12-05
them

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

)suomonotuA( gnireenignE fo egelloC RGVM


printf("Float: %f\n",s);
printf("Character: %c\n",a);
printf("Double: %lf",d);
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

integer:
9
floating-point number:
12.0254
character:
C
double:
12.02543124
You entered:
Integer: 9
Float: 12.025400
Character: C
Double: 12.025431
Test Case - 2

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");

)suomonotuA( gnireenignE fo egelloC RGVM


printf("\nModulo by zero is not allowed\n");
}

Execution Results - All test cases have succeeded!

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

Execution Results - All test cases have succeeded!

)suomonotuA( gnireenignE fo egelloC RGVM


Test Case - 1

User Output

Enter three integers :


121 34 56
Sum of 121, 34 and 56 : 211
Average of 121, 34 and 56 : 70.333336

Test Case - 2

User Output

Enter three integers :


583
Sum of 5, 8 and 3 : 16
Average of 5, 8 and 3 : 5.333333

Test Case - 3

User Output

Enter three integers :


-1 5 -6
Sum of -1, 5 and -6 : -2
Average of -1, 5 and -6 : -0.666667
Exp. Name: Temperature conversions from
S.No: 5 Date: 2023-12-06
Centigrade to Fahrenheit and vice versa.

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)

)suomonotuA( gnireenignE fo egelloC RGVM


{
printf("Enter Temperature in Fahrenheit: ");
scanf("%f",&f);
c=(f-32)*5/9;
printf("Celsius Temperature: %.2f\n",c);
}
else//(n>2)
{
printf("Invalid choice\n");
}
}

Execution Results - All test cases have succeeded!

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

)suomonotuA( gnireenignE fo egelloC RGVM


S.No: 6 Exp. Name: Problem Solving Date: 2023-12-06

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:

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

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

then the program should print the result as:

Simple Interest = 2052.468018

Note: Do use the printf() function and ensure that there is a '\n' at the end after print the result.
Source Code:

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 : ");

)suomonotuA( gnireenignE fo egelloC RGVM


scanf("%d%d%d",&p,&r,&t);
s=(p*t*r)/100;
printf("Simple Interest = %f\n",s);
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

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


2500 5 2
Simple Interest = 250.000000
S.No: 7 Exp. Name: Calculate the square root of an integer Date: 2023-12-06

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.

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

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: ");

)suomonotuA( gnireenignE fo egelloC RGVM


scanf("%d",&n);
s=sqrt(n);
printf("Square root: %.3f\n",s);
}

Execution Results - All test cases have succeeded!

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 .

The formula to find compound interest is


compoundInterest = principal * pow(1 + (rate / 100), time) - principal .

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: ");

)suomonotuA( gnireenignE fo egelloC RGVM


scanf("%f%f%f",&p,&r,&t);
s=(p*t*r)/100;
c=p*(pow((1+r/100),t))-p;
printf("SI= %f\n",s);
printf("CI= %f\n",c);
}

Execution Results - All test cases have succeeded!

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

sides: 2.3 2.4 2.5

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.

Hint: Use sqrt function defined in math.h header file


Source Code:

)suomonotuA( gnireenignE fo egelloC RGVM


Program313.c

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

Execution Results - All test cases have succeeded!

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

2.6 2.7 2.8


sides:
Exp. Name: Write a C program to find the Distance
S.No: 10 Date: 2023-12-06
Travelled by an Object

Aim:

51 :oN egaP
Write a program to find the distance travelled by an object.

Sample Input and Output:

Enter the acceleration value : 2.5

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 - 1: Use the formula to find distance, distance = ut + (1/2) at2 .

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

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 : ");

)suomonotuA( gnireenignE fo egelloC RGVM


scanf("%f",&a);
printf("Enter the initial velocity : ");
scanf("%f",&u);
printf("Enter the time taken : ");
scanf("%f",&t);
s=u*t+0.5*a*t*t;
printf("Distance travelled : %f\n",s);

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Enter the acceleration value :


4
Enter the initial velocity :
5
Enter the time taken :
6
Distance travelled : 102.000000
Test Case - 2

User Output

Enter the acceleration value :


5

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

Enter the acceleration value :


2.5
Enter the initial velocity :
5.7
Enter the time taken :

M S C- 7 2 0 2- 3 2 0 2
20
Distance travelled : 614.000000

Test Case - 4

)suomonotuA( gnireenignE fo egelloC RGVM


User Output

Enter the acceleration value :


50
Enter the initial velocity :
34.67
Enter the time taken :
6
Distance travelled : 1108.020020

Test Case - 5

User Output

Enter the acceleration value :


125.6
Enter the initial velocity :
45.8
Enter the time taken :
4
Distance travelled : 1188.000000
S.No: 11 Exp. Name: Evaluate the expressions Date: 2023-12-06

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

)suomonotuA( gnireenignE fo egelloC RGVM


}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Enter values for A, B, C, D, E, F, G, i:


12345678
a.A+B*C+(D*E) + F*G = 69
b.A/B*C-B+A*D/3 = -1
c.A+++B---A = 3
d.J = (i++) + (++i) = 18

Test Case - 2

User Output

Enter values for A, B, C, D, E, F, G, i:


10 20 60 30 40 4 6 1
a.A+B*C+(D*E) + F*G = 2434
b.A/B*C-B+A*D/3 = 80
c.A+++B---A = 21
Exp. Name: Greatest of three numbers using a
S.No: 12 Date: 2023-12-06
conditional operator

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

Execution Results - All test cases have succeeded!

)suomonotuA( gnireenignE fo egelloC RGVM


Test Case - 1

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.

Sample Input and Output:

Enter 5 subjects marks : 55 56 57 54 55

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;

)suomonotuA( gnireenignE fo egelloC RGVM


printf("Average marks : %f\n",m);
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Enter 5 subjects marks :


45 67 89 57 49
Total marks : 307.000000
Average marks : 61.400002

Test Case - 2

User Output

Enter 5 subjects marks :


55 56 57 54 55
Total marks : 277.000000
Average marks : 55.400002

Test Case - 3
User Output

Enter 5 subjects marks :


90 97 95 92 91
Total marks : 465.000000
Average marks : 93.000000

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

Enter 5 subjects marks :

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

)suomonotuA( gnireenignE fo egelloC RGVM


User Output

Enter 5 subjects marks :


44 35 67 49 51
Total marks : 246.000000
Average marks : 49.200001
Exp. Name: Write a Program to find the Max and Min
S.No: 14 Date: 2023-12-07
of Four numbers

Aim:

12 :oN egaP
Write a program to find the max and min of four numbers.

Sample Input and Output :

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

)suomonotuA( gnireenignE fo egelloC RGVM


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

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Enter 4 numbers :
9852
Max value : 9
Min value : 2

Test Case - 2
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

)suomonotuA( gnireenignE fo egelloC RGVM


User Output

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

Test Case - 6

User Output

Enter 4 numbers :
564 547 574 563
Max value : 574
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;

)suomonotuA( gnireenignE fo egelloC RGVM


}
else if(a>400)
{
sc=0.15*a;
t=a+sc;
}
else

{
t=a;
}
printf("Amount charged: %.2f\n",a);
printf("Surcharges: %.2f\n",sc);
printf("Amount to be paid: %.2f\n",t);
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Enter customer name:


John
Units consumed:
78
Customer name: John
Units consumed: 78
Amount charged: 62.40
Surcharges: 0.00

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

Enter customer name:


Rosy
Units consumed:
325
Customer name: Rosy
Units consumed: 325
Amount charged: 325.00
Surcharges: 0.00
Amount to be paid: 325.00

M S C- 7 2 0 2- 3 2 0 2
Test Case - 3

User Output

Enter customer name:

)suomonotuA( gnireenignE fo egelloC RGVM


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

Test Case - 4

User Output

Enter customer name:


Raman
Units consumed:
300
Customer name: Raman
Units consumed: 300
Amount charged: 270.00
Surcharges: 0.00
Amount to be paid: 270.00
Exp. Name: C program to find roots and nature of
S.No: 16 Date: 2023-12-09
quadratic equation.

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

)suomonotuA( gnireenignE fo egelloC RGVM


Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Enter coefficients a, b and c:


379
root1 = -1.17+1.28i and root2 = -1.17-1.28i

Test Case - 2

User Output

Enter coefficients a, b and c:


886
root1 = -0.50+0.71i and root2 = -0.50-0.71i
Exp. Name: Write program to simulate a basic
S.No: 17 Date: 2023-12-10
calculator

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

)suomonotuA( gnireenignE fo egelloC RGVM


switch(i)
{
case '+':printf("%d",a+b);
break;
case '-':printf("%d\n",a-b);
break;
case '*':printf("%d\n",a*b);
break;
case '/':printf("%d\n",a/b);
break;

Execution Results - All test cases have succeeded!

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

)suomonotuA( gnireenignE fo egelloC RGVM


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

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

1900
1900 is not a leap year

Test Case - 2

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 is not a leap year


User Output

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

)suomonotuA( gnireenignE fo egelloC RGVM


User Output

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
{

)suomonotuA( gnireenignE fo egelloC RGVM


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

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Enter a number:
9
9 is not a prime number

Test Case - 2

User Output

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:

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

8 4 2 4 A 1 3 3 3 2 : DI
cos x = 1 - (x2/2!) + (x4/4!) - (x6/6!) + ....

Note: Print the result up to 4 decimal places.


Source Code:

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++)
{

)suomonotuA( gnireenignE fo egelloC RGVM


r=((-r)*(x*x))/((2*i)*(2*i+1));
sum1=sum1+r;
}
printf("Sine = %.4f\n",sum1);
sum2=t=1;
for(i=1;i<n;i++)
{
t=((-t)*(x*x))/((2*i)*(2*i-1));
sum2=sum2+t;
}
printf("Cosine = %.4f\n",sum2);
}

Execution Results - All test cases have succeeded!

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

)suomonotuA( gnireenignE fo egelloC RGVM


Execution Results - All test cases have succeeded!

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:

Enter number of rows :

8 4 2 4 A 1 3 3 3 2 : DI
For example, if the user gives the input as :

Enter number of rows : 3

then the program should print the result 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 : ");

)suomonotuA( gnireenignE fo egelloC RGVM


scanf("%d", &n);
for(i=1;i<=n;i++)
{
for(j=n-1;j>=i;j--)
{
printf(" ");
}
for(s=1;s<=i;s++)
{
printf("%d ",s);
}
printf("\n");
}
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Enter number of rows :


3
1
1 2
Test Case - 2

User Output

Enter number of rows :


6

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

Enter number of rows :


8
1
1 2

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

)suomonotuA( gnireenignE fo egelloC RGVM


Exp. Name: Write a C program to find the minimum
S.No: 24 Date: 2023-12-12
and maximum in an array of integers.

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

)suomonotuA( gnireenignE fo egelloC RGVM


Execution Results - All test cases have succeeded!

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

)suomonotuA( gnireenignE fo egelloC RGVM


break;
}
}
if(i==n)
printf("%d is not found\n",key);
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Enter size:
6
Enter 6 element:
248135
Enter search element:
6
6 is not found

Test Case - 2

User Output
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]);
}

Execution Results - All test cases have succeeded!

)suomonotuA( gnireenignE fo egelloC RGVM


Test Case - 1

User Output

Enter no of elements:
5
Enter elements:
34124
The reversed array: 4 2 1 4 3

Test Case - 2

User Output

Enter no of elements:
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--)

)suomonotuA( gnireenignE fo egelloC RGVM


{
if(ones[i]==1&&j==1)
twos[i]=0;
else if(ones[i]==0&&j==1)
{
twos[i]=1;
j=0;
}
else
twos[i]=ones[i];
}
printf("2's complement: ");
for(i=0;i<=n;i++)
printf("%d ",twos[i]);
printf("\n");
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Enter size:
5
Enter 5 bit binary number:
10010
2's complement: 0 1 1 1 0

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

)suomonotuA( gnireenignE fo egelloC RGVM


j++;
}}
}
printf("After eliminating duplicates: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}

Execution Results - All test cases have succeeded!

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]);

)suomonotuA( gnireenignE fo egelloC RGVM


}
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Addition of matrices:\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
printf("%d ",c[i][j]);
printf("\n");
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Enter no of rows, columns:


12
Elements of matrix 1:
12
Elements of matrix 2:
98

84 :oN egaP
Addition of matrices:
10 10

Test Case - 2

8 4 2 4 A 1 3 3 3 2 : DI
User Output

Enter no of rows, columns:


23
Elements of matrix 1:
123456
Elements of matrix 2:
987654
Addition of matrices:
10 10 10

M S C- 7 2 0 2- 3 2 0 2
10 10 10

)suomonotuA( gnireenignE fo egelloC RGVM


S.No: 30 Exp. Name: Multiplication of two matrices Date: 2023-12-20

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]);

)suomonotuA( gnireenignE fo egelloC RGVM


}
}
printf("Given matrix1:\n");
for(i=0;i<r;i++)
{
for(j=0;j<s;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}

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!

)suomonotuA( gnireenignE fo egelloC RGVM


Test Case - 1

User Output

no of rows, columns of matrix1:


22
matrix1 elements:
11 22
33 44
no of rows, columns of matrix2:
22
matrix2 elements:
11 22
33 44
Given matrix1:
11 22
33 44
Given matrix2:
11 22
33 44
Multiplication of two matrices:
847 1210
1815 2662

Test Case - 2
User Output

no of rows, columns of matrix1:


33
matrix1 elements:
123

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

)suomonotuA( gnireenignE fo egelloC RGVM


Exp. Name: Write a C program to Sort given elements
S.No: 31 Date: 2023-12-20
using Bubble sort

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]);

)suomonotuA( gnireenignE fo egelloC RGVM


printf("\n");

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;

printf("After sorting: ");

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

)suomonotuA( gnireenignE fo egelloC RGVM


Exp. Name: Write a C program to Concatenate two
S.No: 32 Date: 2023-12-19
given strings without using string library functions

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 :

For example, if the user gives the input as:

string2 : Coding

M S C- 7 2 0 2- 3 2 0 2
then the program should print the result as:

concatenated string = ILoveCoding

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

)suomonotuA( gnireenignE fo egelloC RGVM


Source Code:

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

)suomonotuA( gnireenignE fo egelloC RGVM


size++;

int i;

for(i=0;i<size;i++)

string1[length+i]=string2[i];

string1[length+i]='\0';

printf("concatenated string = %s", string1);

printf("\n");

Execution Results - All test cases have succeeded!

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:

Enter a string : Dallas

then the program should print the result as:

Reverse string : sallaD

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

)suomonotuA( gnireenignE fo egelloC RGVM


printf("Enter a string : ");
scanf("%s", a);
int i,j;
int l=0;
while(a[l]!='\0')
{
l++;
}
char r[20];
for(i=l-1,j=0;i>=0; i--,j++)
r[j]=a[i];
printf("Reverse string : %s\n",r);
}

Execution Results - All test cases have succeeded!

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.

Note: Write the functions allocateMemory(), read1() and sum() in UsingMalloc.c .

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

)suomonotuA( gnireenignE fo egelloC RGVM


int *allocateMemory(int n)

return (int *)malloc(n*sizeof(int));

16 :oN egaP
}

void read1(int *p,int n)

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
}

int sum(int *p, int n)

)suomonotuA( gnireenignE fo egelloC RGVM


int i,total=0;

for(i=0;i<n;i++)

total = total + *(p+i);

return total;

Execution Results - All test cases have succeeded!

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

Sample Input and Output:

Enter number of students : 3


Enter regdno, three subjects marks of student-0: 101 56 78 76
Enter regdno, three subjects marks of student-1: 201 76 89 91
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

M S C- 7 2 0 2- 3 2 0 2
Source Code:

ArrayOfStructures2.c

)suomonotuA( gnireenignE fo egelloC RGVM


#include <stdio.h>

struct student {

// Write the members of structure

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() {

struct student s[60];

int i, n;

M S C- 7 2 0 2- 3 2 0 2
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);

)suomonotuA( gnireenignE fo egelloC RGVM


// Read regdno and 3 subjects marks

scanf("%d%d%d%d",&s[i].regdNo,&s[i].marks[0],&s[i].marks[1],&s[i].marks[2]);

for (i = 0 ; i < n ; i++) { // Complete the code in for

// Find Total and Average

s[i].total = s[i].marks[0] + s[i].marks[1] +


s[i].marks[2];

s[i].average = (s[i].total)/(3.0);

printf("Student-%d Regdno = %d\tTotal marks = %d\tAverage marks =


%f\n",i,s[i].regdNo,s[i].total,s[i].average );

// Fill the code in printf()

}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output

Enter number of students :


3

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

Enter number of students :

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

)suomonotuA( gnireenignE fo egelloC RGVM


Enter regdno, three subjects marks of student-2:
503 99 87 67
Enter regdno, three subjects marks of student-3:
504 89 78 82
Enter regdno, three subjects marks of student-4:
505 37 59 76
Enter regdno, three subjects marks of student-5:
506 78 59 67
Enter regdno, three subjects marks of student-6:
507 92 72 82
Enter regdno, three subjects marks of student-7:
508 45 47 48
Enter regdno, three subjects marks of student-8:
509 55 52 59
Enter regdno, three subjects marks of student-9:
510 62 61 66
Student-0 Regdno = 501 Total marks = 135 Average marks = 45.000000
Student-1 Regdno = 502 Total marks = 219 Average marks = 73.000000
Student-2 Regdno = 503 Total marks = 253 Average marks = 84.333336
Student-3 Regdno = 504 Total marks = 249 Average marks = 83.000000
Student-4 Regdno = 505 Total marks = 172 Average marks = 57.333332
Student-5 Regdno = 506 Total marks = 204 Average marks = 68.000000
Student-6 Regdno = 507 Total marks = 246 Average marks = 82.000000
Student-7 Regdno = 508 Total marks = 140 Average marks = 46.666668
Student-9 Regdno = 510 Total marks = 189 Average marks = 63.000000

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

)suomonotuA( gnireenignE fo egelloC RGVM


Exp. Name: Write a Program to enter n students data
S.No: 36 Date: 2023-12-26
using calloc() and display Failed Students List

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

)suomonotuA( gnireenignE fo egelloC RGVM


}

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

// Write the code

s = (struct student *)malloc(n * sizeof(struct student));

86 :oN egaP
return s;

8 4 2 4 A 1 3 3 3 2 : DI
void read1(struct student *s, int n) {

// write the code

for(int i=0; i<n ; i++){

printf("Enter the details of student - %d\n",i+1);

printf("Enter the roll number : ");

scanf("%d",&s[i].roll);

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],&

)suomonotuA( gnireenignE fo egelloC RGVM


arks[5]);

}
}

96 :oN egaP
void calculateMarks(struct student *s, int n) {

// write the code

8 4 2 4 A 1 3 3 3 2 : DI
for(int i=0;i<n;i++){

int total = 0;

for(int j=0; j<6 ; j++){

total = total +s[i].marks[j];

s[i].sum = total;

M S C- 7 2 0 2- 3 2 0 2
s[i].avg = (float)total / 6.0;

)suomonotuA( gnireenignE fo egelloC RGVM


void displayFailedList(struct student *s, int n) {

int i;

printf("RollNo\tTotalMarks\tAverageMarks\tStatus\n");

for (i = 0; i < n; i++) {

printf("%d\t", s[i].roll);
// Fill the missingcode

printf("%d\t", s[i].sum); // Fill the missing code

printf("%f\t", s[i].avg);

int flag = 0;
// Fill the missing code

for(int j=0; j<6; j++){

if(s[i].marks[j] < 35){

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
}

Execution Results - All test cases have succeeded!

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 :

)suomonotuA( gnireenignE fo egelloC RGVM


45 67 58 36 59 63
Enter the details of student - 2
Enter the roll number :
102
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
101 328 54.666668 Pass
102 394 65.666664 Fail
103 421 70.166664 Pass

Test Case - 2

User Output

Enter the number of students :


2
Enter the details of student - 1
Enter the roll number :
1001
Enter 6 subjects marks :
26 57 68 67 67 65
Enter the details of student - 2
Enter the roll number :

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.

Sample Input and Output - 1:

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

Sample Input and Output - 2:

If the arguments passed as $./TotalMarksArgs.c Johny 45 86 57 48 , then the program

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

)suomonotuA( gnireenignE fo egelloC RGVM


integer of the string arguments.
Source Code:

TotalMarksArgs.c
#include<stdio.h>

#include<stdlib.h>

int main(int argc, char *argv[])

37 :oN egaP
{

if(argc != 5)

8 4 2 4 A 1 3 3 3 2 : DI
{

printf("Arguments passed through command line are not equal to


4\n");

return 1;

printf("Student name : %s\n", argv[1]);

printf("Subject-1 marks : %s\n", argv[2]);

M S C- 7 2 0 2- 3 2 0 2
printf("Subject-2 marks : %s\n", argv[3]);

printf("Subject-3 marks : %s\n", argv[4]);

int sum=0;

)suomonotuA( gnireenignE fo egelloC RGVM


sum = atoi(argv[2]) + atoi(argv[3]) + atoi(argv[4]);

printf("Total marks : %d\n",sum);

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Student name : Sachin


Subject-1 marks : 67
Subject-2 marks : 89
Subject-3 marks : 58
Total marks : 214

Test Case - 2

User Output

Arguments passed through command line are not equal to 4


S.No: 38 Exp. Name: Write a C program to implement realloc() Date: 2023-12-26

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;

ptr_new = (int *)realloc(ptr , sizeof(int) * 3);


// Reallocate the *//Assign the value 30 to newly allocated memory
*(ptr_new + 2) = 30;

)suomonotuA( gnireenignE fo egelloC RGVM


for (i = 0; i < 3; i++)
printf("%d ", *(ptr_new + i));
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

10 20 30
Exp. Name: Write a C Program to store 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:

Enter an integer value :

8 4 2 4 A 1 3 3 3 2 : DI
For example, if the user gives the input as:

Enter an integer value : 10

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

Do u want another list (y|n) :

if the user gives the input as:

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

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)

For example, if the user gives the input as:

Enter an integer value : 20

)suomonotuA( gnireenignE fo egelloC RGVM


Do u want another list (y|n) : y
Enter an integer value : 30
Do u want another list (y|n) : n

Finally, the program should print the result on the console as:

The elements in the single linked lists are : 10-->20-->30-->NULL

Note: Write the functions create() and display() in CreateNodes.c .


Source Code:

StructuresWithDma.c

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

temp=(struct list *)malloc(sizeof(struct list));// Allocate


memory

M S C- 7 2 0 2- 3 2 0 2
printf("Enter an integer value : ");

)suomonotuA( gnireenignE fo egelloC RGVM


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

}
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) {

struct list *temp=first;

)suomonotuA( gnireenignE fo egelloC RGVM


while (temp!=NULL) { // Stop the loop where temp is NULL}

printf("%d-->", temp->data);

temp = temp->next; // Assign next of temp to temp

printf("NULL\n");

Execution Results - All test cases have succeeded!

Test Case - 1
User Output

Enter an integer value :


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

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:

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

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

)suomonotuA( gnireenignE fo egelloC RGVM


Source Code:

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

)suomonotuA( gnireenignE fo egelloC RGVM


int tot;

float avg;

printf("Enter rollno and 3 subjects marks of student - 1 : ");

scanf("%ld%d%d%d",&s1.rollno,&s1.sub1,&s1.sub2,&s1.sub3);

tot=s1.sub1+s1.sub2+s1.sub3;

avg=(float)tot/3;

printf("Total and average marks of student - 1 : %d %f\n",tot,avg);

printf("Size of struct student - 1 : %lu\n",sizeof(struct student1));

printf("Enter rollno of student - 2 : ");

scanf("%d",&s.rollno);

printf("Enter first subject marks of student - 2 : ");

scanf("%d",&s1.sub1);

printf("Enter second subject marks of student - 2 : ");

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

printf("Average marks of student - 2 : %f\n",avg);

8 4 2 4 A 1 3 3 3 2 : DI
printf("Size of union student - 2 : %lu\n",sizeof(union data));

Execution Results - All test cases have succeeded!

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 :

)suomonotuA( gnireenignE fo egelloC RGVM


102
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

Test Case - 2

User Output

Enter rollno and 3 subjects marks of student - 1 :


105 66 65 68
Total and average marks of student - 1 : 199 66.333336
Size of struct student - 1 : 24
Enter rollno of student - 2 :
106
Enter first subject marks of student - 2 :
88
Enter second subject marks of student - 2 :
89
Enter third subject marks of student - 2 :
79
Total marks of student - 2 : 256
Average marks of student - 2 : 85.333336

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

Enter rollno and 3 subjects marks of student - 1 :


501 76 85 84
Total and average marks of student - 1 : 245 81.666664
Size of struct student - 1 : 24
Enter rollno of student - 2 :
502
Enter first subject marks of student - 2 :
99
Enter second subject marks of student - 2 :

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

)suomonotuA( gnireenignE fo egelloC RGVM


Test Case - 4

User Output

Enter rollno and 3 subjects marks of student - 1 :


201 75 46 59
Total and average marks of student - 1 : 180 60.000000
Size of struct student - 1 : 24
Enter rollno of student - 2 :
201
Enter first subject marks of student - 2 :
66
Enter second subject marks of student - 2 :
57
Enter third subject marks of student - 2 :
61
Total marks of student - 2 : 184
Average marks of student - 2 : 61.333332
Size of union student - 2 : 4
S.No: 41 Exp. Name: Demonstrate left shift operation Date: 2024-01-06

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>

void printbinary(int num)

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;

)suomonotuA( gnireenignE fo egelloC RGVM


index++;

for(int i=index-1;i>=0;i--)

printf("%d",binary[i]);

printf("\n");

int main()

int n,shiftcount;

printf("Enter an integer: ");

scanf("%d",&n);

printf("Original value: ");

printbinary(n);

printf("number of bits to left shift: ");


int result=n<<shiftcount;

printf("After left shift: %d\n",result);

printf("Binary representation:");

68 :oN egaP
printbinary(result);

return 0;

8 4 2 4 A 1 3 3 3 2 : DI
}

Execution Results - All test cases have succeeded!

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

)suomonotuA( gnireenignE fo egelloC RGVM


Test Case - 2

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

)suomonotuA( gnireenignE fo egelloC RGVM


struct student {

char name[10];

88 :oN egaP
int age;

float height;

8 4 2 4 A 1 3 3 3 2 : DI
}s1, s2;

void read(struct student *p) {

printf("Enter student name, age and height: ");

M S C- 7 2 0 2- 3 2 0 2
// Write the code to take inputs to structure

)suomonotuA( gnireenignE fo egelloC RGVM


scanf("%s%d%f",&*s1.name,&s1.age,&s1.height);

struct student copyStructureVariable(struct student s1, struct student s2) {

//write your code here to copy the 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("\nAge: %d", s1.age);

printf("\nHeight: %f\n",s1.height);

M S C- 7 2 0 2- 3 2 0 2
}

Execution Results - All test cases have succeeded!

)suomonotuA( gnireenignE fo egelloC RGVM


Test Case - 1

User Output

Enter student name, age and height:


Yamuna 19 5.2
Student name: Yamuna
Age: 19
Height: 5.200000

Test Case - 2

User Output

Enter student name, age and height:


Kohli 21 5.11
Student name: Kohli
Age: 21
Height: 5.110000
Exp. Name: Write a C Program to find ncr using
S.No: 43 Date: 2023-12-20
Factorial recursive function

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:

Enter the values of n and r :

For example, if the user gives the input as:

Enter the values of n and r : 4 2

then the program should print the result as:

The value of 4c2 = 6

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:

Enter valid input data

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

)suomonotuA( gnireenignE fo egelloC RGVM


Source Code:

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

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Enter the values of n and r :

M S C- 7 2 0 2- 3 2 0 2
10 4
The value of 10c4 = 210

Test Case - 2

User Output

)suomonotuA( gnireenignE fo egelloC RGVM


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

Test Case - 3

User Output

Enter the values of n and r :


52
The value of 5c2 = 10
Exp. Name: Write a Program to find the Length of a
S.No: 44 Date: 2023-12-20
String

Aim:

29 :oN egaP
Write a C program to find the length of a given string.

Sample Input and Output - 1:

Enter the string : CodeTantra

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

)suomonotuA( gnireenignE fo egelloC RGVM


#include<string.h>
int myStrLen(char str[100])
{

return(strlen(str));
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Enter the string :


CodeTantra
Length of CodeTantra : 10

Test Case - 2

User Output

Enter the string :


IndoUsUk
Length of IndoUsUk : 8
Test Case - 3

User Output

Enter the string :


MalayalaM

39 :oN egaP
Length of MalayalaM : 9

Test Case - 4

8 4 2 4 A 1 3 3 3 2 : DI
User Output

Enter the string :


Oh!MyGod
Length of Oh!MyGod : 8

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:

ynext= y + h * f(y ,t)


wheref(y, t) is the derivative function representing dy/dtin the given Ordinary differential equation.

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

Source Code:

euler.c

M S C- 7 2 0 2- 3 2 0 2
)suomonotuA( gnireenignE fo egelloC RGVM
#include <stdio.h>

//write your code...

// Define the differential equation function f(y, t)

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;

printf("initial value of y (y0): ");

scanf("%lf",&y);

M S C- 7 2 0 2- 3 2 0 2
printf("initial value of t (t0): ");

scanf("%lf",&t);

printf("step size (h): ");

)suomonotuA( gnireenignE fo egelloC RGVM


scanf("%lf",&h);

printf("end value for t: ");

scanf("%lf",&l);

while (t<l){

printf("t = %.2f y = %.2f\n",t,y);

eulerIntegration(&y,t,h);

t+=h;

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

initial value of y (y0):


1
initial value of t (t0):
1
step size (h):
3
end value for t:

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

initial value of y (y0):


1
initial value of t (t0):
1
step size (h):
3

M S C- 7 2 0 2- 3 2 0 2
end value for t:
3
t = 1.00 y = 1.00

)suomonotuA( gnireenignE fo egelloC RGVM


Exp. Name: Fibonacci series up to the given number
S.No: 47 Date: 2023-12-26
of terms using Recursion

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

)suomonotuA( gnireenignE fo egelloC RGVM


return 0;
}
int i;
int next=1,first=0,second=1;
for(i=2;i<=k;i++)
{
next=first+second;
first=second;
second=next;
}
return next;
}

Execution Results - All test cases have succeeded!

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:

Enter two integer values :

For example, if the user gives the input as:

Enter two integer values : 25 15

then the program should print the result as:

The lcm of two numbers 25 and 15 = 75

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>

)suomonotuA( gnireenignE fo egelloC RGVM


#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 lcm(int a,int b)

001 :oN egaP


static int multiple=0;

multiple += b;

if(multiple %a ==0 && multiple %b ==0)

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

)suomonotuA( gnireenignE fo egelloC RGVM


Enter two integer values :
34 24
The lcm of two numbers 34 and 24 = 408

Test Case - 2

User Output

Enter two integer values :


69
The lcm of two numbers 6 and 9 = 18

Test Case - 3

User Output

Enter two integer values :


345 467
The lcm of two numbers 345 and 467 = 161115

Test Case - 4

User Output

Enter two integer values :


100 88
The lcm of two numbers 100 and 88 = 2200

Test Case - 5

User Output

101 :oN egaP


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

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:

201 :oN egaP


Write a program to find the factorial of a given number using recursion process.

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


Source Code:

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

long int factorial(long int n)

)suomonotuA( gnireenignE fo egelloC RGVM


if(n<=1)

return 1;

else

return n*factorial(n-1);

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Enter an integer :
5
Factorial of 5 is : 120

Test Case - 2

User Output

301 :oN egaP


Enter an integer :
4
Factorial of 4 is : 24

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

)suomonotuA( gnireenignE fo egelloC RGVM


Exp. Name: Write a program to implement Ackermann
S.No: 50 Date: 2023-12-26
function using Recursion

Aim:

401 :oN egaP


Write a program to implement Ackermann function using recursion process.

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

Enter two numbers :

8 4 2 4 A 1 3 3 3 2 : DI
For example, if the user gives the input as:

Enter two numbers : 2 1

then the program should print the result 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 : ");

)suomonotuA( gnireenignE fo egelloC RGVM


scanf("%lli %lli", &m, &n);
printf("A(%lli, %lli) = %lli\n", m, n, ackermannFun(m, n));
}

AckermannFunction1.c
#include<math.h>

long long int ackermannFun(long long int m,long long int n)

501 :oN egaP


if(m==0)

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

)suomonotuA( gnireenignE fo egelloC RGVM


}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Enter two numbers :


01
A(0, 1) = 2

Test Case - 2

User Output

Enter two numbers :


22
A(2, 2) = 7

Test Case - 3

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

601 :oN egaP


Test Case - 4

User Output

Enter two numbers :


11

8 4 2 4 A 1 3 3 3 2 : DI
A(1, 1) = 3

Test Case - 5

User Output

Enter two numbers :


10
A(1, 0) = 2

M S C- 7 2 0 2- 3 2 0 2
Test Case - 6

User Output

Enter two numbers :


23
A(2, 3) = 9

)suomonotuA( gnireenignE fo egelloC RGVM


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

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

701 :oN egaP


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

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

then the program should print the result as:

Sum of 6 natural numbers = 21

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


Source Code:

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

)suomonotuA( gnireenignE fo egelloC RGVM


printf("Sum of %d natural numbers = %d\n", n, sum(n));
}

Program903a.c

int sum(int n)

if(n<=1)

return n;

return n+sum(n-1);

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

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

Test Case - 2

801 :oN egaP


User Output

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:

901 :oN egaP


Write a program to swap two values by using call by address method.

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

Enter two integer values :

8 4 2 4 A 1 3 3 3 2 : DI
For example, if the user gives the input as:

Enter two integer values : 12 13

then the program should print the result as:

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

M S C- 7 2 0 2- 3 2 0 2
( \n ).
Source Code:

Program1002.c

#include <stdio.h>

)suomonotuA( gnireenignE fo egelloC RGVM


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

int temp;

011 :oN egaP


temp=*p;

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

Execution Results - All test cases have succeeded!

M S C- 7 2 0 2- 3 2 0 2
Test Case - 1

User Output

Enter two integer values :


121 131

)suomonotuA( gnireenignE fo egelloC RGVM


Before swapping in main : a = 121 b = 131
After swapping in swap : *p = 131 *q = 121
After swapping in main : a = 131 b = 121

Test Case - 2

User Output

Enter two integer values :


555 999
Before swapping in main : a = 555 b = 999
After swapping in swap : *p = 999 *q = 555
After swapping in main : a = 999 b = 555

Test Case - 3

User Output

Enter two integer values :


1001 101
Before swapping in main : a = 1001 b = 101
After swapping in swap : *p = 101 *q = 1001
After swapping in main : a = 101 b = 1001

Test Case - 4
User Output

Enter two integer values :


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

111 :oN egaP


After swapping in main : a = 2999 b = 9999

Test Case - 5

8 4 2 4 A 1 3 3 3 2 : DI
User Output

Enter two integer values :


10101 11010
Before swapping in main : a = 10101 b = 11010
After swapping in swap : *p = 11010 *q = 10101
After swapping in main : a = 11010 b = 10101

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.

211 :oN egaP


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

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;

311 :oN egaP


int *ptr2 = NULL;
int value;
// Allocate memory for an integer

// Input the integer value

8 4 2 4 A 1 3 3 3 2 : DI
printf("Enter an integer value: ");

scanf("%d",&value);

ptr1=(int*)malloc(sizeof(int));

// Assign the input value to the allocated memory

// Point ptr2 to the same memory location as ptr1

// Check if ptr2 is a valid pointer before accessing

M S C- 7 2 0 2- 3 2 0 2
if (ptr1!=NULL)
{

*ptr1=value;

)suomonotuA( gnireenignE fo egelloC RGVM


ptr2=ptr1;

printf("Value through ptr2: %d\n",*ptr2);

free(ptr1);

}
else
{

printf("ptr2 is a dangling pointer (invalid)\n");

// Deallocate the memory pointed to by ptr1

// Set ptr1 and ptr2 to NULL to avoid dangling pointers

ptr1 = NULL;

ptr2 = NULL;

return 0;

}
Execution Results - All test cases have succeeded!

Test Case - 1

User Output

411 :oN egaP


Enter an integer value:
54
Value through ptr2: 54

8 4 2 4 A 1 3 3 3 2 : DI
Test Case - 2

User Output

Enter an integer value:


10
Value through ptr2: 10

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:

511 :oN egaP


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

Sample Input and Output:

Enter source string : Robotic Tool

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

)suomonotuA( gnireenignE fo egelloC RGVM


void copyString(char*target,char*source)

char *ptr;

int i=0;

ptr=source;

while(*ptr!='\0')

target[i++]=*ptr++;

target[i]='\0';

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

Enter source string :


CodeTantra
Target string : CodeTantra

Test Case - 2

User Output

611 :oN egaP


Enter source string :
Robotic Tool
Target string : Robotic Tool

8 4 2 4 A 1 3 3 3 2 : DI
Test Case - 3

User Output

Enter source string :


Virat Pointer Sachin Ganguly Dravid Warne
Target string : Virat Pointer Sachin Ganguly Dravid Warne

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

)suomonotuA( gnireenignE fo egelloC RGVM


Exp. Name: Write a C program to Count number of
S.No: 55 Lowercase, Uppercase, digits and Other Characters Date: 2023-12-29
using Pointers

711 :oN egaP


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

Sample Input and Output:

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

)suomonotuA( gnireenignE fo egelloC RGVM


countCharDigitOthers(str, &upperCount, &lowerCount, &digitCount,
&otherCount);
printf("Number of uppercase letters = %d\n", upperCount);
printf("Number of lowercase letters = %d\n", lowerCount);
printf("Number of digits = %d\n", digitCount);
printf("Number of other characters = %d\n", otherCount);
}

CountCharDigitOthers1.c
void countCharDigitOthers(char *str,int *uppercount,int *lowercount,int
*digitcount,int *othercount)

{
while(*str!='\0'){

811 :oN egaP


if(*str>='A'&&*str<='Z')

(*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
}

)suomonotuA( gnireenignE fo egelloC RGVM


Execution Results - All test cases have succeeded!

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

911 :oN egaP


Number of digits = 5
Number of other characters = 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

)suomonotuA( gnireenignE fo egelloC RGVM


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

You might also like