Lab Record Download
Lab Record Download
Aim:
Page No: 1
Write a program to find the area and circumference of a circle.
Note: Use the printf() function with a newline character ( \n ) at the end.
ID: 927623BCS011
area = PI * radius * radius
circumference = 2 * PI * radius
Define PI value as 3.14 using #define and consider float data type for radius
Source Code:
circle.c
#include<stdio.h>
#include<math.h>
#define PI 3.14
2023-2027-B8
int main ()
{
float radius,area,circumference;
printf("radius : ");
scanf("%f",&radius);
area=PI*radius*radius;
User Output
radius :
2.67
area = 22.384747
circumference = 16.767601
Test Case - 2
User Output
radius :
30.05
area = 2835.427734
circumference = 188.713989
Test Case - 3
User Output
radius :
14.69
Page No: 2
area = 677.599731
circumference = 92.253197
ID: 927623BCS011
2023-2027-B8
M.Kumarasamy College of Engineering
S.No: 2 Exp. Name: Basic Calculator implementation Date: 2023-11-03
Aim:
Writing a C program to implement Basic Calculator
Page No: 3
Source Code:
calculatior.c
#include<stdio.h>
ID: 927623BCS011
int main(){
float num1,num2,sum,dif,pro,div;
printf("enter number1: ");
scanf("%f",&num1);
printf("enter non zero number2: ");
scanf("%f",&num2);
sum = num1+num2;
dif = num1-num2;
pro = num1*num2;
div = num1/num2;
printf("%.2f + %.2f = %.2f\n", num1, num2, sum);
printf("%.2f - %.2f = %.2f\n", num1, num2, dif);
2023-2027-B8
printf("%.2f * %.2f = %.2f\n", num1, num2, pro);
printf("%.2f / %.2f = %.2f\n", num1, num2, div);
}
User Output
enter number1:
2
enter non zero number2:
3
2.00 + 3.00 = 5.00
2.00 - 3.00 = -1.00
2.00 * 3.00 = 6.00
2.00 / 3.00 = 0.67
Exp. Name: Design an algorithm and implement
S.No: 3 Date: 2023-11-03
using C language the following exchanges
Aim:
Page No: 4
Design an algorithm and implement using C language the following exchanges a ← b ← c ← d ← a and print the
result as shown in the example.
ID: 927623BCS011
Enter values of a, b, c and d: 98 74 21 36
After swapping
a = 74
b = 21
c = 36
d = 98
Source Code:
exchange.c
#include<stdio.h>
2023-2027-B8
int main()
{
int a,b,c,d,temp;
printf("Enter values of a, b, c and d: ");
scanf("%d %d %d %d\n",&a, &b, &c, &d);
printf("After swapping\n");
User Output
Enter values of a, b, c and d:
1234
After swapping
a = 2
b = 3
c = 4
d = 1
Test Case - 2
User Output
Enter values of a, b, c and d:
98 74 21 36
Page No: 5
After swapping
a = 74
b = 21
c = 36
ID: 927623BCS011
d = 98
2023-2027-B8
M.Kumarasamy College of Engineering
Exp. Name: Conversion of one datatype to another
S.No: 4 Date: 2023-11-03
datatype
Aim:
Page No: 6
Write a simple program that converts one given datatype to another using auto conversion. Take a double value
from the user and convert it into integer and floating point datatypes.
ID: 927623BCS011
typeconversion.c
#include<stdio.h>
int main()
{
double inpvalue;
int integer;
printf("Enter a value: ");
scanf("%lf",&inpvalue);
integer=(int)inpvalue;
printf("Value as an integer: %d\n",integer);
2023-2027-B8
printf("Value as a float: %.2f\n",inpvalue);
}
User Output
Enter a value:
12345.67890
Value as an integer: 12345
Value as a float: 12345.68
Test Case - 2
User Output
Enter a value:
12
Value as an integer: 12
Value as a float: 12.00
S.No: 5 Exp. Name: Solving Expression Date: 2023-11-03
Aim:
Write a C code to solve the expression as 3*2/3+5
Page No: 7
Source Code:
evaluate.c
#include <stdio.h>
ID: 927623BCS011
int main() {
double result;
// Evaluate the expression
result = 3*2/3+5;
printf("Result: %.2lf\n",result);
return 0;
}
2023-2027-B8
Test Case - 1
User Output
Result: 7.00
Aim:
Page No: 8
Jenny's homework for the first day is to find the addition of two numbers, help jenny to solve the problem.
Source Code:
add.c
ID: 927623BCS011
#include<stdio.h>
int main()
{
int value1,value2,addition;
scanf("%d %d",&value1,&value2);
addition=value1+value2;
printf("The addition of two numbers is: %d\n",addition);
}
2023-2027-B8
Test Case - 1
User Output
62
Test Case - 2
User Output
759 624
The addition of two numbers is: 1383
Exp. Name: Write a program to find the total
S.No: 7 Date: 2023-11-03
number of squares
Aim:
Page No: 9
Problem Description:
Tina's brother gave her a friendly task of calculating the number of squares in a board that has n*n squares of
dimensions 1cm *1cm each.
Help her to find the number of total squares including all small and big ones.
ID: 927623BCS011
Constraints:
• 2 ≤ n ≤ 20
Input Format:
The only line of the input represents a value of "n"
Output Format:
Print the number of squares in the n*n board."
Source Code:
squares.c
2023-2027-B8
#include<stdio.h>
int main()
{
int i=1,n,sq=0;
scanf("%d",&n);
if((n>=2)&&(n<=20))
User Output
5
55
Test Case - 2
User Output
17
1785
Exp. Name: Write a program to find the quotient
S.No: 8 Date: 2023-11-03
and remainder
Aim:
Page No: 10
Problem Description:
Nancy bought apples from a fruit shop, where the shopkeeper specified the bill amount. Nancy provided some
money to the shopkeeper to pay the bill. Now, she would like to know the quotient and remainder after dividing
the amount she gave by the bill amount specified by the shopkeeper. Can you help Nancy in finding it?
ID: 927623BCS011
Constraint :
5 ≤ amount_given≤ 2500
5 ≤ bill_amount ≤ 2500
Input Format:
The first line of the input consists of an integer representing the amount given by Nancy
The second line of the input consists of an integer representing the amount specified by the shopkeeper
Output Format:
The first line of the output represents the quotient in integer format
The second line of the output represents the remainder in integer format
Source Code:
2023-2027-B8
billAmount.c
#include<stdio.h>
int main(){
int amount_given,bill_amount;
User Output
1800
56
32
8
Test Case - 2
User Output
1250
150
8
50
Exp. Name: Write a program to find the volume of
S.No: 9 Date: 2023-11-03
the ball
Aim:
Page No: 11
Problem Description:
Laasya bought a new volleyball in the sports shop. It looks medium-sized. She somehow found the radius of the
sphere. But she would like to know the volume of that ball.
ID: 927623BCS011
Functional Description:
Volume = (4.0/3.0) × π × r^3
π = 3.14
Constraint:
1.00 ≤ radius ≤ 5.00
Input Format :
The input consists of a single float number representing the radius of the ball
Output Format:
2023-2027-B8
The output consists of the volume of the ball
Source Code:
volumeOfBall.c
#include<stdio.h>
User Output
4.6
407.513367
Test Case - 2
User Output
3.7
212.067230
S.No: 10 Exp. Name: Multiplication of two float values Date: 2023-11-03
Aim:
Problem Description:
Page No: 12
Arif came from a very low-income family. However, his father Irfan sent him abroad for the purpose of studying.
Arif also concentrated well on his learning keeping in mind his father’s poverty. Arif was excellent in mathematics.
One day Arif had a computer class and his computer teacher asked him to create a programming logic for the
mathematics problem of multiplying two numbers of type float.
ID: 927623BCS011
Can you help Arif with the logic?
Constraints:
• 1.00 ≤ var1 ≤ 1000.00
• 1.00 ≤ var2 ≤ 1000.00
Input Format:
The only line of input has two floating point numbers separated by space
Output Format:
In the only line of output print the result of the multiplication with 4 values after the decimal point.
2023-2027-B8
Source Code:
multiplyTwoNums.c
#include<stdio.h>
int main(){
User Output
412.23165 154.2136
63571.7266
Test Case - 2
User Output
784.2536 956.2487
749941.5000
Exp. Name: Write a program to calculate the area
S.No: 11 Date: 2023-11-03
of the Pyramid
Aim:
Page No: 13
Problem Description:
Jannu and Preethi both went to Egypt to visit the Pyramids. On seeing the Pyramids they were in
discussion.During the discussion Jannu asked Preethi, what will be the area of this Pyramid.Preethi has no idea
about it.
ID: 927623BCS011
Can you help Preethi in calculating the area of this Pyramid?
Functional Description:
Area = ( height * base )/2
Constraints:
1 <= height <= 500
1 <= base <= 500
Input Format:
The only line of input has two floating point values representing height and base respectively separated by a
space.
2023-2027-B8
Output Format:
In the only line of output print the area of the pyramid with only three values after the decimal point.
Source Code:
areaOfPyramid.c
User Output
31.5 43.7
688.275
Test Case - 2
User Output
176.3 120.6
10630.890
Exp. Name: Print months in a year using switch
S.No: 12 Date: 2023-11-03
case statement
Aim:
Page No: 14
Write a C program to display month name according to the month number using switch case statement.
Note: If the given input number is not in the range i.e., other than 1 to 12, the output should be as "Invalid
month number"
Source Code:
ID: 927623BCS011
DisplayMonth.c
2023-2027-B8
M.Kumarasamy College of Engineering
#include<stdio.h>
int main()
{
int month;
printf("Enter month number : ");
Page No: 15
scanf("%d",&month);
switch(month)
{
case(1):
printf("January\n");
ID: 927623BCS011
break;
case(2):
printf("February\n");
break;
case(3):
printf("March\n");
break;
case(4):
printf("April\n");
break;
case(5):
printf("May\n");
2023-2027-B8
break;
case(6):
printf("June\n");
break;
case(7):
printf("July\n");
Page No: 16
Test Case - 2
User Output
ID: 927623BCS011
Enter month number :
11
November
Test Case - 3
User Output
Enter month number :
13
Invalid month number
2023-2027-B8
Test Case - 4
User Output
Enter month number :
Test Case - 5
User Output
Enter month number :
4
April
Test Case - 6
User Output
Enter month number :
2
February
Test Case - 7
User Output
Enter month number :
8
August
Test Case - 8
User Output
Enter month number :
6
Page No: 17
June
Test Case - 9
ID: 927623BCS011
User Output
Enter month number :
12
December
Test Case - 10
User Output
Enter month number :
0
2023-2027-B8
Invalid month number
Test Case - 11
Aim:
Write a C program to find the sum of n non-negative numbers entered by the user.
Page No: 18
Source Code:
sum_nonnegative.c
#include<stdio.h>
ID: 927623BCS011
int main()
{
int i,n,input,sum=0;
scanf("%d",&n);
printf("Enter %d Numbers: ",n);
for(i=1;i<=n;i++)
{
scanf("%d",&input);
if(input>=0)
{
sum=sum+input;
}
2023-2027-B8
}
printf("Sum = %d\n",sum);
}
User Output
5
Enter 5 Numbers:
-1 2 3 4 -7
Sum = 9
Test Case - 2
User Output
7
Enter 7 Numbers:
-3 -2 -4 -5 10 11 12
Sum = 33
Exp. Name: Write a C program to calculate whether
S.No: 14 Date: 2023-11-05
a given number is Palindrome or not
Aim:
Page No: 19
Design and develop an algorithm to find the reverse of an integer number and check whether it is Palindrome or
not.
Implement a C program for the developed algorithm that takes an integer number as input and output the
ID: 927623BCS011
reverse of the same with suitable messages.
At the time of execution, the program should print the message on the console as:
Enter an integer :
2023-2027-B8
2014 is not a palindrome
Lab2.c
#include<stdio.h>
int main()
{
int n,originalnum,remainder,reversednum;
printf("Enter an integer : ");
scanf("%d",&n);
originalnum=n;
while(n>0)
{
remainder=n%10;
reversednum=reversednum*10+remainder;
n/=10;
}
printf("The reverse of a given number : %d\n",reversednum);
if(originalnum==reversednum)
printf("%d is a palindrome\n",originalnum);
else
printf("%d is not a palindrome\n",originalnum);
}
User Output
Enter an integer :
2014
Page No: 20
The reverse of a given number : 4102
2014 is not a palindrome
Test Case - 2
ID: 927623BCS011
User Output
Enter an integer :
1221
The reverse of a given number : 1221
1221 is a palindrome
2023-2027-B8
M.Kumarasamy College of Engineering
S.No: 15 Exp. Name: Fibonacci series Date: 2023-11-07
Aim:
Write a C program to print the Fibonacci series i.e., 0 1 1 2 3 5 8 13 21....., up to the limit given by the user
Page No: 21
Hint: By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is
the sum of the previous two.
Source Code:
ID: 927623BCS011
Program14.c
#include<stdio.h>
int main()
{
int n,a=0,b=1,c,i;
printf("Enter the maximum limit to generate the Fibonacci series : ");
scanf("%d",&n);
printf("The Fibonacci series is : ");
printf("%d %d",a,b);
c=a+b;
for(i=0;i<n;i++)
2023-2027-B8
{
printf(" %d",c);
a=b;
b=c;
c=a+b;
}
}
User Output
Enter the maximum limit to generate the Fibonacci series :
10
The Fibonacci series is : 0 1 1 2 3 5 8
Test Case - 2
User Output
Enter the maximum limit to generate the Fibonacci series :
6
The Fibonacci series is : 0 1 1 2 3 5
Exp. Name: Write a C program to find whether the
S.No: 16 Date: 2023-11-07
given number is Armstrong or not
Aim:
Page No: 22
Write a sample code to check whether the given number is an armstrong number or not.
[Hint: An armstrong number is a number that is the sum of its own digits each raised to the power of the
number of digits.
ID: 927623BCS011
For example,
9 = 91 = 9
371 = 33 + 73 + 13 = 27 + 343 +1 = 371
8208 = 84 + 24+04 + 84 = 4096 + 16 + 0 + 4096 = 8028]
At the time of execution, the program should print the message on the console as:
2023-2027-B8
then the program should print the result as:
Similarly, if the input is given as 121 then the output should be "The given number 121 is not an armstrong
Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
Program410.c
#include<stdio.h>
int main()
{
int input,remainder,sum=0,temp,count=0;
printf("Enter any number : ");
Page No: 23
scanf("%d",&input);
temp=input;
while(input>0)
{
input=input/10;
ID: 927623BCS011
count++;
}
input=temp;
while(input>0)
{
remainder=input%10;
sum=sum+pow(remainder,count);
input/=10;
}
if(temp==sum)
printf("The given number %d is an armstrong number\n",sum);
else
2023-2027-B8
printf("The given number %d is not an armstrong number\n",temp);
}
User Output
Enter any number :
370
The given number 370 is an armstrong number
Test Case - 2
User Output
Enter any number :
1824
The given number 1824 is not an armstrong number
Test Case - 3
User Output
Enter any number :
5
The given number 5 is an armstrong number
Test Case - 4
User Output
Enter any number :
1634
The given number 1634 is an armstrong number
Page No: 24
ID: 927623BCS011
2023-2027-B8
M.Kumarasamy College of Engineering
Exp. Name: Write the C program to find the
S.No: 17 Date: 2023-11-04
Factorial of a number.
Aim:
Page No: 25
Write a program to find the Factorial of a non-zero positive integer n, denoted by n!, where factorial is the
product of all positive integers less than or equal to n. For example, 5! = 5 * 4 * 3 * 2 * 1 = 120.
Note:
• For negative values factorial cannot be calculated, so print "Invalid".
ID: 927623BCS011
Constraints: -10 <= n <= 10
Instruction: To run your custom test cases strictly map your input and output layout with the visible test cases.
Source Code:
FactorialDoWhile.c
#include<stdio.h>
int main()
{
int i,input,factorial=1;
2023-2027-B8
printf("Num: ");
scanf("%d",&input);
if(input<0)
{
printf("Invalid");
}
User Output
Num:
9
Factorial: 362880
Test Case - 2
User Output
Num:
0
-10
Num:
Invalid
User Output
Factorial: 1
Test Case - 3
Aim:
Problem Description:
Page No: 27
Johny is very enthusiastic about programming languages. One day he tried to write a program to find a triple of
numbers. A triple of numbers is said to be poor when two of those numbers are equal but the other number is
different from those two numbers. Help Johny to write the code.
ID: 927623BCS011
Constraints:
1 <= A, B, and C <= 50
Input Format:
Only line of input has three integers A B C separated by a space.
Output Format:
Print the output in a single line, If the given triple is poor, print Yes; otherwise, print No.
Source Code:
triple.c
2023-2027-B8
#include<stdio.h>
int main()
{
int int1,int2,int3;
scanf("%d %d %d",&int1,&int2,&int3);
else
printf("No\n");
}
User Output
23 18 34
No
Test Case - 2
User Output
39 28 39
Yes
Exp. Name: Write a program to find the profit and
S.No: 19 Date: 2023-11-04
loss
Aim:
Page No: 28
Problem Description:
In primary mathematics classes, you all have learned about profit and loss.
If cost price is greater than selling price, then there is a loss otherwise profit
ID: 927623BCS011
Can you kindly automate the same?
Constraints:
1000≤cp≤45000
1000≤sp≤45000
Input Format:
First Line : Integer representing the Cost price
Second Line: Integer representing Selling Price
Output Format:
If Cost Price > Selling Price Print a “Loss” along with the amount of loss.
2023-2027-B8
If Cost Price < Selling Price Print as "Profit" along with the amount of profit.
If Cost Price = Selling Price Print “No Profit No Loss”
Source Code:
profitLoss.c
else if(cp<sp)
printf("Profit: %d",sp-cp);
else
printf("No Profit No Loss");
User Output
15945
13785
Loss: 2160
Test Case - 2
User Output
26832
28513
Page No: 29
Profit: 1681
Test Case - 3
ID: 927623BCS011
User Output
100
100
No Profit No Loss
2023-2027-B8
M.Kumarasamy College of Engineering
S.No: 20 Exp. Name: Write a program to find the N pages Date: 2023-11-04
Aim:
Problem Description:
Page No: 30
Tharun wants to print a document with "N" pages double-sided, where two pages of data can be printed on one
sheet of paper.
Can you tell him how many sheets of paper he needs to print N pages?
ID: 927623BCS011
Constraints:
• 1 ≤ N ≤ 1000
Input Format:
The only line of input contains a single integer N representing the number of pages that need to be printed
Output Format:
The output is an integer representing the minimum number of sheets of paper needed in a single line
Source Code:
print.c
2023-2027-B8
#include<stdio.h>
#include<math.h>
int main()
{
int pages,papers;
scanf("%d",&pages);
User Output
189
95
Test Case - 2
User Output
567
284
Exp. Name: Write a program to find the difference
S.No: 21 Date: 2023-11-04
between speeds
Aim:
Page No: 31
Problem Description:
Arav and Aaron are participating in the Bike racing. Arav crossed some milestones earlier and Aaron crossed some
milestones earlier during their racing because they have changed their speeds at different times.
Both of them like to know the difference in speeds between them at different stages of racing.
ID: 927623BCS011
Can you help find the speed difference between Arav and Aaron?
Constraints:
20≤ aravspeed ≤100
20≤ aaronspeed ≤100
Input Format:
The first line of input represents the speed of Arav.
The second line of input represents speed of Aaron.
Output Format:
2023-2027-B8
Print difference between the driving speed of two participants in a single line.
Source Code:
speed.c
#include<stdio.h>
User Output
74
51
23
Test Case - 2
User Output
76
13
89
Aim:
Problem Description:
Page No: 33
Paytm announced a Cashback offer for the people of Tamil Nadu which is a one-time offer for the new year.But to
avail of the Cashback users need to pass the simple tasks given by Paytm. One such task given by Paytm is to
check the nature of the currency value provided by Paytm.
Constraint:
ID: 927623BCS011
1 <= currency <= 20000
Functional Description:
One more condition imposed by Paytm is that participants need to do this checking using the concept of
Operators.
Input Format:
Only the Line of input has a single value of type integer representing the currency value.
Output Format:
The output format will be a string indicating whether the input currency is even or odd.
Source Code:
2023-2027-B8
currency.c
#include<stdio.h>
int main()
{
User Output
1985
1985 is Odd Currency
Test Case - 2
1642
User Output
Aim:
Write a program to accept n numbers in an array and display it.
Page No: 35
Source Code:
printNumbers.c
#include<stdio.h>
ID: 927623BCS011
int main()
{
int n,i;
printf("How many numbers do you want to enter: ");
scanf("%d",&n);
int array[n];
printf("Enter %d numbers: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
printf("Entered numbers are: ");
2023-2027-B8
for(i=0;i<n;i++)
{
if(i==0)
printf("%d",array[i]);
else
printf(" %d",array[i]);
User Output
How many numbers do you want to enter:
5
Enter 5 numbers:
2 4 6 8 10
Entered numbers are: 2 4 6 8 10
Test Case - 2
User Output
How many numbers do you want to enter:
10
Enter 10 numbers:
1 2 3 4 5 6 7 8 9 10
Entered numbers are: 1 2 3 4 5 6 7 8 9 10
Page No: 37
Aim:
Write the c program to find the largest and the smallest of some numbers given by the user using arrays.
Source Code:
SmallLarge.c
ID: 927623BCS011
#include<stdio.h>
int main()
{
int i=0,n=0,small,large,arr[50];
printf("Enter no.of elements: ");
scanf("%d",&n);
printf("Enter element 1: ");
scanf("%d",&arr[0]);
large=arr[0];
small=arr[0];
for(i=1;i<n;i++)
2023-2027-B8
{
printf("Enter element %d: ",i+1);
scanf("%d",&arr[i]);
if(large<arr[i])
{
large=arr[i];
}
}
printf("Largest element: %d\n",large);
printf("Smallest element: %d\n",small);
}
User Output
Enter no.of elements:
6
Enter element 1:
48
Enter element 2:
59
Enter element 3:
68
Enter element 4:
Page No: 38
85
Enter element 5:
69
Enter element 6:
ID: 927623BCS011
75
Largest element: 85
Smallest element: 48
Test Case - 2
User Output
Enter no.of elements:
5
Enter element 1:
2023-2027-B8
15
Enter element 2:
26
Enter element 3:
35
Aim:
Page No: 39
Given an array arr[] containing n integers. Write a C program to count the number of increasing subsequences in
the array of size k.
Constraints:
0 < size < 50
ID: 927623BCS011
-1000 < element < 1000
Input Format:
The first line reads an integer representing the size of the array.
The next line reads space-separated integers representing the elements of the array.
The next line reads an integer representing the value of k.
Output Format:
The output is an integer representing the count of increasing subsequences in the array.
2023-2027-B8
Source Code:
subsequences.c
Page No: 40
{
for(int j=0;j<k;j++)
{
dp[i][j]=0;
}
ID: 927623BCS011
}
for(int i=0;i<n;i++)
{
dp[i][0]=1;
}
for(int i=1;i<n;i++)
{
for(int j=1;j<k;j++)
{
for(int m=0;m<i;m++)
{
if(arr[i]>arr[m])
2023-2027-B8
dp[i][j]+=dp[m][j-1];
}
}
}
int result=0;
for(int i=0;i<n;i++)
Page No: 41
value of k:
3
Result: 1
ID: 927623BCS011
Test Case - 2
User Output
Enter the size:
5
Enter the elements:
10 11 12 13 15
value of k:
3
Result: 10
2023-2027-B8
M.Kumarasamy College of Engineering
S.No: 26 Exp. Name: Split the string Date: 2023-11-14
Aim:
Write a C program to split and print the string based on blank space.
Page No: 42
Source Code:
splitstring.c
#include<stdio.h>
ID: 927623BCS011
#include<string.h>
#define MAX_STRING_LENGTH 100
int main()
{
char inputstring[MAX_STRING_LENGTH];
scanf("%[^\n]",&inputstring);
char *token = strtok(inputstring," ");
while(token!=NULL)
{
printf("%s\n",token);
token=strtok(NULL," ");
2023-2027-B8
}
}
User Output
Hello world!!
Hello
world!!
Test Case - 2
User Output
This is a C program
This
is
a
C
program
S.No: 27 Exp. Name: Find the total number of words Date: 2023-11-20
Aim:
Write a C Program to perform the following operations on a given paragraph using built-in functions
Page No: 43
• Find the total number of words
Source Code:
total_words.c
ID: 927623BCS011
#include <stdio.h>
int main()
{
char a[100];
int count = 0, i;
2023-2027-B8
}
printf("Total words: %d\n",count+1);
}
User Output
Enter a paragraph:
Hi Hello good morning!!
Total words: 4
Test Case - 2
User Output
Enter a paragraph:
Twinkle Twinkle little star, how i wonder what
you are, up above the world so high.
Total words: 16
S.No: 28 Exp. Name: Capitalize the first word Date: 2023-11-20
Aim:
Write a C Program to perform the following operations on a given paragraph using built-in functions
Page No: 44
• Capitalize the first word of each sentence
Source Code:
capitalize.c
ID: 927623BCS011
#include<stdio.h>
int main()
{
char str[50]={0};
printf("Enter a paragraph: ");
scanf("%[^\n]s",str);
str[0]=str[0]-32;
printf("Capitalizing first word:\n%s\n",str);
}
2023-2027-B8
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Enter a paragraph:
Test Case - 2
User Output
Enter a paragraph:
roses are red and violets are blue
Capitalizing first word:
Roses are red and violets are blue
Exp. Name: Replace a given word with another
S.No: 29 Date: 2023-12-04
word
Aim:
Page No: 45
Write a C Program to perform the following operations on a given paragraph using built-in functions
• Replace a given word with another word
Source Code:
replace.c
ID: 927623BCS011
#include <stdio.h>
#include <string.h>
void replaceWord(char *paragraph, const char *oldWord, const char *newWord) {
char *pos, temp[1000];
int index = 0, oldWordLen = strlen(oldWord);
while ((pos = strstr(paragraph, oldWord)) != NULL) {
strncpy(temp, paragraph, pos - paragraph);
temp[pos - paragraph] = '\0';
strcat(temp, newWord);
strcat(temp, pos + oldWordLen);
strcpy(paragraph, temp);
2023-2027-B8
index += pos - paragraph + strlen(newWord);
paragraph += index;
}
}
int main() {
User Output
Enter a paragraph:
Roses are pink,violets are blue.
word to replace:
pink
new word:
red
Original Paragraph: Roses are pink,violets are blue.
After replacing: Roses are red,violets are blue.
Test Case - 2
Page No: 46
User Output
Enter a paragraph:
This is a C programming structure.
word to replace:
ID: 927623BCS011
structure
new word:
language
Original Paragraph: This is a C programming structure.
After replacing: This is a C programming language.
2023-2027-B8
M.Kumarasamy College of Engineering
Exp. Name: Absolute difference between any two
S.No: 30 Date: 2023-12-11
consecutive integer
Aim:
Page No: 47
Elon Musk has given an array of integers of size N and he has to find out if the absolute difference of values of
any two consecutive array integers is at most D.
Constraints:
• 1 <= N <= 103
ID: 927623BCS011
• 1 <= D <= 100
• 1 <= integer[i] (elements of the array) <= 106
Input Format:
• The first line contains two integers N and D separated by a space, denoting the size of the array and value
of D respectively.
• The second line contains N integers separated by a space
Output Format:
• Print YES if the absolute difference between any two consecutive integer is at-most D, otherwise print NO.
Source Code:
2023-2027-B8
absDiff.c
#include <stdio.h>
char*check_absolute_difference(int arr[],int n,int d)
{
Page No: 48
Test Case - 2
User Output
ID: 927623BCS011
5 11
19 23 45 65 95
YES
Test Case - 3
User Output
5 61
221 412 326 951 548
NO
2023-2027-B8
M.Kumarasamy College of Engineering
S.No: 31 Exp. Name: Array Right Rotation by K Steps Date: 2023-12-08
Aim:
Fazil loves to preform different operations on arrays, and so being the Head of the higher education institution,
Page No: 49
he assigned a task to his new student Bazil.
Bazil will be provided with an integer array A of size N and an integer K , where she needs to rotate the array in
the right direction by K steps and then print the resultant array.
As she is new to the school, please help her to complete the given task.
ID: 927623BCS011
Constraints:
• 1 ≤ N ≤ 10 5
• 0 ≤ K ≤ 10 6
• 0 ≤ A [i] ≤ 10 9
Input Format:
• The first line consists of two integers N and K , N being the number of elements in the array and K
denotes the number of steps of rotation.
• The next line consists of N space separated integers , denoting the elements of the array A.
Output Format:
2023-2027-B8
• Print the array after rotation in the single line.
Instruction: To run your custom test cases strictly map your input and output layout with the visible test cases.
Source Code:
arrayRotation.c
#include <stdio.h>
void right(int arr[], int n, int k) {
k = k % n;
for (int i = 0, j = n - k - 1; i < j; i++, j--) {
int temp = arr[i];
Page No: 50
arr[i] = arr[j];
arr[j] = temp;
}
for (int i = n - k, j = n - 1; i < j; i++, j--) {
int temp = arr[i];
ID: 927623BCS011
arr[i] = arr[j];
arr[j] = temp;
}
for (int i = 0, j = n - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
void printarr(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
2023-2027-B8
}
printf("\n");
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
User Output
52
21 96 85 74 32
74 32 21 96 85
Test Case - 2
User Output
10 4
52 14 69 32 84 75 91 11 21 25
91 11 21 25 52 14 69 32 84 75
S.No: 32 Exp. Name: Addition on two-dimensional 3*3 array Date: 2023-12-08
Aim:
Mahesh has given a two-dimensional 3*3 array starting from A [0][0].You should add the alternate elements of
Page No: 51
the array and print their sum.
It should print two different numbers. The first is the sum ofA 0 0, A 0 2, A 1 1, A 2 0, A 2 2and A 0 1, A 1 0, A 1
2, A 2 1.
Constraints:
ID: 927623BCS011
1 <= values of array <= 200
Input Format:
• First and only line contains the value of the array separated by a space.
Output Format:
• The first line should print the sum of A 0 0, A 0 2, A 1 1, A 2 0, A 2 2
• Second line should print sum of A 0 1, A 1 0, A 1 2, A 2 1
Instruction: To run your custom test cases strictly map your input and output layout with the visible test cases.
Source Code:
2023-2027-B8
sum.c
#include <stdio.h>
int main() {
int A[3][3];
for (int i = 0; i < 3; i++) {
User Output
11 12 15 36 95 84 74 115 172
367
247
Test Case - 2
User Output
9 5 8 47 6 2 31 27 4
58
81
Aim:
Page No: 53
Question description:
Simon is studying B.Tech-Mechanical Engineering. He's going to attend a computer science-based subject exam
this semester. Due to the less preparation in the previous monthly tests,his internal marks are decreased.His
computer science Professor made an offer one more chance to boost up his internal marks.
ID: 927623BCS011
Professor assigns a program to Simon for the internal mark boost up.So Simon wants to identify the element of
the array which occurs most time in the array. Can you help him?
Function Description:
Sample Input:
9
121214512
Sample Output: 1
Explanation:
2023-2027-B8
• 1 occurs 4 times
• 2 occurs 3 times
• 4 occurs 1 time
• 5 occurs 1 time
So, the output is 1.
Input Format:
• The first line will contain the number of elements present in the array.
• The second line will contain the elements of the array.
Output Format:
• The output contains only the element which occurs most times in the array.
Source Code:
frequentElementOfArray.c
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
Page No: 54
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int maxCount = 0;
ID: 927623BCS011
int mostFrequentElement = arr[0];
for (int i = 0; i < n; i++) {
int currentElement = arr[i];
int currentCount = 1;
for (int j = i + 1; j < n; j++) {
if (arr[j] == currentElement) {
currentCount++;
}
}
if (currentCount > maxCount) {
maxCount = currentCount;
mostFrequentElement = currentElement;
2023-2027-B8
}
}
printf("%d\n", mostFrequentElement);
return 0;
}
User Output
9
436975412
4
Test Case - 2
User Output
10
3369852143
3
Exp. Name: Write the code to solve the given
S.No: 34 Date: 2023-12-08
question.
Aim:
Page No: 55
Question description:
Issac is a Language teacher at a high school in Madurai.Sabari is a student, he is studying programming during
Language class.Issac tells Sabari to leave the other subject and learn Tamil.Sabari asked permission for 10
minutes, Finally, Sabri got permission to solve the program.
ID: 927623BCS011
The computer teacher has given homework on the topic of Arrays andwhere he needs to do Array Rotation as per
the input of the question. But Sabari is not good at C Programming.Can you help him to solve the programming
problem?
Constraints:
0 < n < 100
Input Format:
• The first line predicts the total number of elements present in the array.
• The second line contains the elements of an array.
• The third line contains the number of rotations needs to be done.
2023-2027-B8
• The fourth line contains the character‘L’or‘R’that defines what type of rotation needs to be done.
Output Format:
• The output contains a line that is only the resultant output.
Source Code:
Page No: 56
}
arr[n - 1] = temp;
}
void rotateRight(int arr[], int n) {
int temp = arr[n - 1];
ID: 927623BCS011
for (int i = n - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = temp;
}
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
2023-2027-B8
}
int rotations;
scanf("%d", &rotations);
char direction;
scanf(" %c", &direction);
User Output
5
12345
4
L
5 1 2 3 4
4
7
R
User Output
10 11 12 13 14 15 16
13 14 15 16 10 11 12
Test Case - 2
Aim:
Write a C program to find the maximum of 3 numbers using functions.
Page No: 58
Input and Output Format:
Input consists of 3 integers.
Refer sample output for formatting details.
ID: 927623BCS011
Sample Input and Output:
Enter three numbers : 9 99 49
99 is the largest among three numbers
Source Code:
maximumOfNum.c
#include <stdio.h>
#include "maximumOfNum1.c"
int largest(int x, int y, int z);
void main() {
2023-2027-B8
int x, y, z, max;
printf("Enter three numbers : ");
scanf("%d%d%d", &x, &y, &z);
printf("%d is the largest among three numbers\n", largest(x, y, z));
}
User Output
Enter three numbers :
111 10 99
111 is the largest among three numbers
Test Case - 2
User Output
Enter three numbers :
25 37 48
48 is the largest among three numbers
Page No: 59
ID: 927623BCS011
2023-2027-B8
M.Kumarasamy College of Engineering
S.No: 36 Exp. Name: sum of digits of the largest number Date: 2023-12-11
Aim:
Write a C program to print the sum of digits of the largest number from the given set of 4-digit numbers.
Page No: 60
Source Code:
sum_of_digits.c
ID: 927623BCS011
2023-2027-B8
M.Kumarasamy College of Engineering
#include <stdio.h>
Page No: 61
int sum = 0;
while (number != 0) {
ID: 927623BCS011
number /= 10;
return sum;
int main() {
2023-2027-B8
printf("count of 4-digit numbers: ");
scanf("%d", &n);
scanf("%d",& currentNumber);
largestNumber = currentNumber;
Page No: 62
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
ID: 927623BCS011
count of 4-digit numbers:
3
Enter number 1:
1234
Enter number 2:
3456
Enter number 3:
4000
largest number: 4000
sum of its digits: 4
2023-2027-B8
Test Case - 2
User Output
count of 4-digit numbers:
Aim:
Write a C program to find the area of the circle using call-by value and call-by reference.
Page No: 63
Note:
• Take the pi value as 3.14.
• Print the result up to 2 decimal places.
• The driver function is provided to you in the editor.
ID: 927623BCS011
Source Code:
area_circle.c
#include <stdio.h>
#define PI 3.14
double areaCallByValue(double radius) {
return PI * radius * radius;
}
void areaCallByReference(double radius, double *result) {
*result = PI * radius * radius;
}
2023-2027-B8
int main() {
double radius, area;
printf("radius: ");
return 0;
}
User Output
radius:
7
Area(Call by value): 153.86
Area(Call by reference): 153.86
Test Case - 2
User Output
radius:
8.2
Area(Call by value): 211.13
Area(Call by reference): 211.13
Page No: 64
ID: 927623BCS011
2023-2027-B8
M.Kumarasamy College of Engineering
Exp. Name: Write a C program to find the Factorial
S.No: 38 Date: 2023-12-29
of a given number using Recursion
Aim:
Page No: 65
Write a program to find the factorial of a given number using recursion process.
ID: 927623BCS011
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));
}
2023-2027-B8
Program901a.c
#include <stdio.h>
int factorial(num)
if (num==0 || num==1)
return 1;
else
return
num*factorial(num-1);
Page No: 66
Test Case - 2
User Output
ID: 927623BCS011
Enter an integer :
4
Factorial of 4 is : 24
Test Case - 3
User Output
Enter an integer :
8
Factorial of 8 is : 40320
2023-2027-B8
Test Case - 4
User Output
Enter an integer :
Page No: 67
Aim:
Write a program to display the fibonacci series up to the given number of terms using recursion process.
ID: 927623BCS011
Note: Write the recursive function fib() in fibonacci1.c .
Source Code:
fibonacci.c
#include <stdio.h>
#include "fibonacci1.c"
void main() {
int n, i;
printf("Enter value of n : ");
scanf("%d", &n);
2023-2027-B8
printf("The fibonacci series of %d terms are : ", n);
for (i = 0; i < n; i++) {
printf(" %d ", fib(i));
}
}
int fib(int n)
Page No: 68
int a=0, b=1,c;
if(n==0)
ID: 927623BCS011
return a;
else if(n==1)
return b;
else
for(int i=0;
2023-2027-B8
i<=n;i++){
c=a+b;
if(i==n-2) {
a=b;
b=c;
User Output
Enter value of n :
4
The fibonacci series of 4 terms are : 0 1 1 2
Test Case - 2
User Output
Enter value of n :
8
Page No: 69
The fibonacci series of 8 terms are : 0 1 1 2 3 5 8 13
Test Case - 3
ID: 927623BCS011
User Output
Enter value of n :
14
The fibonacci series of 14 terms are : 0 1 1 2 3 5 8 13 21 34 55 89 144 233
Test Case - 4
User Output
Enter value of n :
3
2023-2027-B8
The fibonacci series of 3 terms are : 0 1 1
Aim:
Write a program to access the array elements using a pointer.
Page No: 70
Source Code:
pyramid.c
#include <stdio.h>
ID: 927623BCS011
int main() {
int size;
scanf("%d", &size);
int array1[size];
2023-2027-B8
for (int i = 0; i < size; i++) {
scanf("%d", &array1[i]);
return 0;
User Output
Enter the size of elements:
5
Enter elements:
12345
Accessing the elements using the pointer:
1
2
3
4
5
Test Case - 2
Page No: 71
User Output
Enter the size of elements:
4
ID: 927623BCS011
Enter elements:
40 30 10 20
Accessing the elements using the pointer:
40
30
10
20
2023-2027-B8
M.Kumarasamy College of Engineering
Exp. Name: Write the code to print its elements in
S.No: 41 Date: 2023-12-29
reverse order
Aim:
Page No: 72
John is learning about dynamic memory allocation in C. He wants to create an array of integers, dynamically
allocate memory, and then reverse the elements of the array. Write a C program to help John perform this
operation using dynamic memory allocation.
Input Format:
ID: 927623BCS011
• A single integer 'n' (1 <= n <= 100) represents the number of integers in the array.
• 'n' integers separated by spaces, providing the initial values of the array.
Output Format:
• A single line containing the reversed array.
Source Code:
reverse1.c
#include<stdio.h>
int main()
2023-2027-B8
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=n-1;i>=0;i--)
printf("%d ",a[i]);
Page No: 73
Test Case - 2
User Output
ID: 927623BCS011
9
459 964 2225 631 82 3118 5202 9852 235
235 9852 5202 3118 82 631 2225 964 459
2023-2027-B8
M.Kumarasamy College of Engineering
Exp. Name: Write the code to find whether the
S.No: 42 Date: 2023-12-29
given string is lucky or not
Aim:
Page No: 74
Write a program to find whether the given string is Lucky or not. A string is said to be lucky if the sum of the
ASCII values of the characters in the string is even. Refer function specifications for the function details. The
function accepts a pointer to a string and returns an int. The return value is 1 if the string is lucky and 0 otherwise.
Input and Output Format: Input consists of a string. Assume that all characters in the string are lowercase letters
ID: 927623BCS011
and the maximum length of the string is 100. Refer sample input and output for formatting specifications.
Source Code:
luckyString.c
2023-2027-B8
M.Kumarasamy College of Engineering
#include<stdio.h>
#include<string.h>
Page No: 75
{
int sum=0;
ID: 927623BCS011
for(int i=0;i<strlen(str);i++){
sum+=str[i];
if(sum%2==0){
return 1;
2023-2027-B8
else
return 0;
char str[100];
scanf("%s",&str);
if(lucky(str)==1)
printf("%s is lucky",str);
else
User Output
anitha
anitha is not lucky
ram
User Output
ram is lucky
Test Case - 2
Aim:
Page No: 77
You are working on a project that requires you to develop a program in C that calculates the sum of elements in a
one-dimensional array using dynamic memory allocation. The array will be provided as input by the user. Your
task is to write a C program that can read the array elements, calculate their sum using pointers, and print the
result.
ID: 927623BCS011
Input format:
The first line contains an integer n, representing the number of elements in that array.
The next line contains an integer array arr[] with n space-separated values.
Output format:
Print the sum of the array of elements using pointers.
Source Code:
Pointerssum.c
#include<stdio.h>
2023-2027-B8
int main()
int n,sum=0;
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
p=&a;
for(int i=0;i<n;i++)
sum+=(*p)[i];
printf("%d\n",sum);
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Page No: 78
4
22 22 22 22
88
ID: 927623BCS011
Test Case - 2
User Output
5
0 -22 -44 22 44
0
2023-2027-B8
M.Kumarasamy College of Engineering
S.No: 44 Exp. Name: Menu driven C program Date: 2023-12-30
Aim:
Write a menu-driven C program that allows a user to enter n numbers and then choose between finding the
Page No: 79
smallest, largest, sum, or average. The menu and all the choices are to be functions. Use a switch statement to
determine what action to take. Display an error message if an invalid choice is entered.
Source Code:
ID: 927623BCS011
ProgramMenu.c
2023-2027-B8
M.Kumarasamy College of Engineering
#include <stdio.h>
void smallest(int[], int);
void largest(int[], int);
void sum(int[], int);
void average(int[], int);
Page No: 80
int main()
{
int n;
printf("Enter number : ");
scanf("%d",&n);
ID: 927623BCS011
int a[n],ch;
printf("Enter %d numbers : ",n);
for(int i = 0; i<n;i++){
scanf("%d",&a[i]);
}
printf("The menu driven is:\n");
printf("1.smallest\n");
printf("2.largest\n");
printf("3.sum\n");
printf("4.average\n");
printf("Enter an option : ");
scanf("%d",&ch);
2023-2027-B8
switch(ch)
{
case 1:
smallest (a,n);
break;
case 2:
Page No: 81
printf("The sum of all elements is %d\n", sum); }
void average(int arr[], int n) {
float sum=0;
for(int i=0;i<n;i++){
sum+=arr[i];
ID: 927623BCS011
}
printf("The average of all elements is %f\n",
(float)sum/n);
}
2023-2027-B8
User Output
Enter number :
5
Enter 5 numbers :
6 7 8 9 12
Test Case - 2
User Output
Enter number :
4
Enter 4 numbers :
6897
The menu driven is:
1.smallest
2.largest
3.sum
4.average
Enter an option :
4
The average of all elements is 7.500000
Exp. Name: Write a function to compute mean,
S.No: 45 variance, Standard Deviation, sorting of n Date: 2023-12-30
elements in single dimension array.
Page No: 82
Aim:
Write a C program to compute the mean, variance, Standard Deviation, and sorting of n elements in a single-
dimension array using functions.
Source Code:
ID: 927623BCS011
MeanVariance.c
2023-2027-B8
M.Kumarasamy College of Engineering
#include <stdio.h>
#include <math.h>
void calculateMean(int [], int);
float calculateVariance(int [], int);
float calculateStandardDeviation(int [], int);
Page No: 83
void calculateSort(int [], int);
void main() {
int arr[20], number;
float variance = 0, standardDeviation = 0;
printf("Enter size of the array : ");
ID: 927623BCS011
scanf("%d", &number);
printf("Enter array elements : ");
for (int i = 0; i < number; i++) {
scanf("%d", &arr[i]);
}
calculateMean(arr, number);
variance = calculateVariance(arr, number);
printf("The variance of elements of the array : %f\n", variance);
standardDeviation = calculateStandardDeviation(arr, number);
printf("The Standard Deviation of elements of the array : %f\n", standardDeviation);
calculateSort(arr, number);
printf("The elements in array after sorting :");
2023-2027-B8
for (int i = 0; i < number; i++) {
printf(" %d", arr[i]);
}
printf("\n");
}
mean /= n;
mean /= n;
variance /= n;
float calculateStandardDeviation(int arr[], int n) {
float variance = calculateVariance(arr, n);
return sqrt(variance);
}
Page No: 84
void calculateSort(int arr[], int n) {
int temp;
ID: 927623BCS011
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements if they are in the wrong order
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}}
2023-2027-B8
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Test Case - 2
User Output
Enter size of the array :
6
Enter array elements :
357268
The mean of elements of the array : 5.166667
The variance of elements of the array : 4.472222
The Standard Deviation of elements of the array : 2.114763
The elements in array after sorting : 2 3 5 6 7 8
Exp. Name: Student Information Management
S.No: 46 Date: 2023-12-30
System
Aim:
Page No: 85
Write a C program to build a simple Application Software for Student Information Management System which can
perform the following operations:
• Store the First name of the student.
• Store the Last name of the student.
• Store the Unique Roll number for every student.
ID: 927623BCS011
• Store the CGPA of every student.
• Store the Courses Registered by the student.
Source Code:
management_sys.c
2023-2027-B8
M.Kumarasamy College of Engineering
#include <stdio.h>
#include <string.h>
struct Student {
char firstname[50];
char lastname[50];
Page No: 86
int rollnumber;
float cgpa;
char courses[5][50];
int a;
};
ID: 927623BCS011
int main(){
struct Student student;
printf("Student Information Management System\n");
printf("First Name: ");
scanf("%s",student.firstname);
printf("Last Name: ");
scanf("%s",student.lastname);
printf("Roll Number: ");
scanf("%d",&student.rollnumber);
printf("CGPA: ");
scanf("%f",&student.cgpa);
printf("number of courses(up to 5): ");
2023-2027-B8
scanf("%d",&student.a);
if(student.a>5){
printf("Maximum 5 courses can be registered\n");
return 1;
}
User Output
Student Information Management System
First Name:
william
Last Name:
Kare
Roll Number:
23
CGPA:
9.21
Page No: 87
number of courses(up to 5):
3
names of courses:
Economics
ID: 927623BCS011
Mathematics
Commerce
Student Information:
First Name: william
Last Name: Kare
Roll Number: 23
CGPA: 9.21
Courses Registered:
Economics
Mathematics
Commerce
2023-2027-B8
Test Case - 2
User Output
Aim:
Write a C program to develop an expense manager that reads date, product, price, and product category. The
Page No: 88
program should display the total expense amount based on product category or date as per the user's selection
using structures.
ID: 927623BCS011
expense_manager.c
2023-2027-B8
M.Kumarasamy College of Engineering
#include <stdio.h>
#include <string.h>
#define MAX_EXPENSES 100
struct Expense {
char date[11];
Page No: 89
char product[100];
float price;
char category[50];
};
float totalExpenseByCategory(struct Expense expenses[], int numExpenses, char category[]){
ID: 927623BCS011
float total=0;
for(int i=0;i<numExpenses;++i){
if(strcmp(expenses[i].category,category)==0){
total += expenses[i].price;
}
}
return total;
}
float totalExpenseByDate(struct Expense expenses[],int numExpenses, char date[]){
float total=0;
for (int i=0;i<numExpenses;++i){
if(strcmp(expenses[i].date,date)==0){
2023-2027-B8
total += expenses[i].price;
}
}
return total;
}
int choice;
char category[50];
char date[11];
printf("Expense Manager\n");
while (1) {
printf("Options:\n");
printf("1. Add Expense\n");
printf("2. Calculate Total Expense by Category\n");
printf("3. Calculate Total Expense by Date\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 1) {
if (numExpenses < MAX_EXPENSES) {
printf("Enter date (YYYY-MM-DD): ");
scanf("%s", expenses[numExpenses].date);
printf("Enter product name: ");
scanf("%s", expenses[numExpenses].product);
printf("Enter price: ");
scanf("%f", &expenses[numExpenses].price);
numExpenses++;
} else {
printf("Expense list is full!\n");
}
} else if (choice == 2) {
Page No: 90
printf("Enter category: ");
scanf("%s", category);
float total = totalExpenseByCategory(expenses, numExpenses, category);
printf("Total expense in category '%s' is %.2f\n", category, total);
} else if (choice == 3) {
ID: 927623BCS011
printf("Enter date (YYYY-MM-DD): ");
scanf("%s", date);
float total = totalExpenseByDate(expenses, numExpenses, date);
printf("Total expense on date '%s' is %.2f\n", date, total);
} else if (choice == 4) {
break;
} else {
printf("Invalid choice\n");
}
}
return 0;
2023-2027-B8
}
User Output
Expense Manager
Options:
1. Add Expense
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
1
Enter date (YYYY-MM-DD):
2023-10-12
Enter product name:
Moisturizer
Enter price:
250
Enter category:
Skincare
Options:
1. Add Expense
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
1
Enter date (YYYY-MM-DD):
2023-10-12
Enter product name:
Riceflour
Page No: 91
Enter price:
100
Enter category:
Food
ID: 927623BCS011
Options:
1. Add Expense
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
1
Enter date (YYYY-MM-DD):
2023-11-14
Enter product name:
Facewash
2023-2027-B8
Enter price:
300
Enter category:
Skincare
Page No: 92
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
2
ID: 927623BCS011
Enter category:
Groceries
Total expense in category 'Groceries' is 0.00
Options:
1. Add Expense
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
3
Enter date (YYYY-MM-DD):
2023-2027-B8
2024-07-09
Total expense on date '2024-07-09' is 0.00
Options:
1. Add Expense
2. Calculate Total Expense by Category
Aim:
Page No: 93
Manoj arranged one event to find the number of characters in his friend's name, Your idea is to give your friend's
name, and for that, Manoj has to answer the no of characters present in it, with the help of the structure concept
to accomplish it.
Input Format
ID: 927623BCS011
The input contains the name of a friend.
Output Format
The output displays the No of characters in the given string.
Source Code:
noOfCharacters.c
#include <stdio.h>
#include <string.h>
struct name {
2023-2027-B8
char name[50];
};
int main() {
int length;
scanf("%s", friendName.name);
length = strlen(friendName.name);
printf("%d\n", length);
return 0;
User Output
raja
4
13
User Output
srmuniversity
Test Case - 2
Aim:
Your task is to:
Page No: 95
• Create a Structure called EMI and declare three variables as principal(float), rate(float), and time(float).
• Create a structure variable as "e".
• Input the principal, rate, and time.
• Calculate the EMI to be paid and the formula is as follows:
• One Month interest = rate=rate/(12*100)
ID: 927623BCS011
• One Month Period = time=time*12
• totalemi= (principal*rate*pow(1+rate,time))/(pow(1+rate,time)-1)
• Print the final EMI
Source Code:
emiCalculator.c
2023-2027-B8
M.Kumarasamy College of Engineering
#include <stdio.h>
#include <math.h>
struct EMI {
float principal;
Page No: 96
float rate;
float time;
ID: 927623BCS011
};
e.time) - 1);
2023-2027-B8
return emi;
int main() {
scanf("%f", &e.principal);
scanf("%f", &e.rate);
scanf("%f", &e.time);
return 0;
User Output
200000
10
2
Monthly EMI is: 9228.99
Test Case - 2
User Output
Page No: 97
5614500
20
6
Monthly EMI is: 134483.22
ID: 927623BCS011
2023-2027-B8
M.Kumarasamy College of Engineering
Exp. Name: Write the code to display student
S.No: 50 Date: 2023-12-29
details
Aim:
Page No: 98
Define a structure named Student with the following members:
• name (char array of size 100)
• city (char array of size 100)
• establishmentYear (integer)
• passPercentage (floating-point number)
ID: 927623BCS011
Write a C program that allows the user to input details of multiple students and then sorts and displays these
details based on the students' names in ascending order.
Note:
• The structure variables, data members, and structure names are CASE Sensitive.
• Your output should match with the
Source Code:
studentDetails.c
2023-2027-B8
M.Kumarasamy College of Engineering
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct Student {
char name[100];
Page No: 99
char department[100];
int yearofstudy;
float passcgpa;
};
ID: 927623BCS011
int compareName(const void *a, const void *b) {
return strcmp(((struct Student *)a)->name, ((struct Student *)b)->name);
}
int main() {
int numstudents;
if (scanf("%d", &numstudents) != 1) {
printf("Invalid input for the number of students.\n");
return 1;
}
2023-2027-B8
struct Student *students = (struct Student *)malloc(numstudents * sizeof(struct
Student));
if (students == NULL) {
printf("Memory allocation error.\n");
free(students);
return 0;
};
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
ID: 927623BCS011
Name:Jagan
Department:swe
Year of study:3
CGPA:8.6
Name:raju
Department:cse
Year of study:1
CGPA:7.8
Name:somu
Department:IT
Year of study:2
2023-2027-B8
CGPA:8.2
Test Case - 2
User Output
Aim:
Input Format
ID: 927623BCS011
The input integer ranges from 1 to 999
Output Format
The sum of digits of the number
Source Code:
sumOfDigits.c
#include <stdio.h>
union Data {
int number;
2023-2027-B8
};
int sum = 0;
num /= 10;
return sum;
int main() {
int sum;
scanf("%d", &input.number);
sum = sumOfDigits(input.number);
printf("%d\n", sum);
return 0;
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Test Case - 2
ID: 927623BCS011
User Output
1193
14
2023-2027-B8
M.Kumarasamy College of Engineering