0% found this document useful (0 votes)
68 views105 pages

Lab Record Download-Codethantra

Uploaded by

joshua Gregory
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)
68 views105 pages

Lab Record Download-Codethantra

Uploaded by

joshua Gregory
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/ 105

Exp.

Name: Write a C program to find Area and


S.No: 1 Date: 2023-10-21
Circumference of a Circle

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:
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>
#define PI 3.14
int main() {

2023-2050-Faculty
float radius;
printf("radius : ");
scanf("%f", &radius);
float area = PI * radius * radius;
float circumference = 2 * PI * radius;
printf("area = %.6f\n", area);
printf("circumference = %.6f\n", circumference);
return 0;

K.Ramakrishnan College of Technology


}

Execution Results - All test cases have succeeded!


Test Case - 1

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
User Output
radius :
14.69
area = 677.599731
circumference = 92.253197

Page No: 2
ID:
2023-2050-Faculty
K.Ramakrishnan College of Technology
S.No: 2 Exp. Name: Basic Calculator implementation Date: 2023-10-20

Aim:
Writing a C program to implement Basic Calculator

Page No: 3
Source Code:

calculatior.c

#include<stdio.h>

ID:
void main()
{
float a,b;
printf("enter number1: ");
scanf("%f",&a);
printf("enter non zero number2: ");
scanf("%ff",&b);
printf("%.2f + %.2f = %.2f\n",a,b,a+b);
printf("%.2f - %.2f = %.2f\n",a,b,a-b);
printf("%.2f * %.2f = %.2f\n",a,b,a*b);
printf("%.2f / %.2f = %.2f\n",a,b,a/b);

2023-2050-Faculty
}

Execution Results - All test cases have succeeded!


Test Case - 1

K.Ramakrishnan College of Technology


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 using C
S.No: 3 Date: 2023-10-21
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.

Sample Input and Output:

ID:
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-2050-Faculty
void main()
{
int a,b,c,d,temp;
printf("Enter values of a, b, c and d: ");
scanf(" %d %d %d %d",&a,&b,&c,&d);
temp=a;
a=b;

K.Ramakrishnan College of Technology


b=c;
c=d;
d=temp;
printf("After swapping\na = %d\nb = %d\nc = %d\nd = %d\n",a,b,c,d);
}

Execution Results - All test cases have succeeded!


Test Case - 1

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:
d = 98
c = 36
b = 21
a = 74
98 74 21 36
After swapping

K.Ramakrishnan College of Technology 2023-2050-Faculty ID: Page No: 5


Exp. Name: Conversion of one datatype to another
S.No: 4 Date: 2023-10-21
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.

Note: Print the floating point value up to 2 decimal places.


Source Code:

ID:
typeconversion.c

#include <stdio.h>

int main() {
double input;
printf("Enter a value: ");
scanf("%lf", &input);

int intValue = (int)input;


float floatValue = (float)input;

2023-2050-Faculty
printf("Value as an integer: %d\n", intValue);
printf("Value as a float: %.2f\n", floatValue);

return 0;
}

K.Ramakrishnan College of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

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-10-21

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:
int main() {
double result;
// Evaluate the expression
result = 3*2/3+5;// write your code here
// Display the result
printf("Result: %.2lf\n",result); // write your code here
return 0;
}

Execution Results - All test cases have succeeded!

2023-2050-Faculty
Test Case - 1

User Output
Result: 7.00

K.Ramakrishnan College of Technology


Exp. Name: Write the code to find addition of two
S.No: 6 Date: 2023-10-21
numbers

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:
#include<stdio.h>
void main()
{
int num1,num2;
scanf("%d %d",&num1,&num2);
printf("The addition of two numbers is: %d\n",num1+num2);
}

Execution Results - All test cases have succeeded!

2023-2050-Faculty
Test Case - 1

User Output
62
The addition of two numbers is: 8

K.Ramakrishnan College of Technology


Test Case - 2

User Output
759 624
The addition of two numbers is: 1383
Exp. Name: Write a program to find the total number of
S.No: 7 Date: 2023-10-21
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:
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-2050-Faculty
#include <stdio.h>

int main() {
int n;
scanf("%d", &n);

int totalSquares = (n * (n + 1) * (2 * n + 1)) / 6;

K.Ramakrishnan College of Technology


printf("%d\n",totalSquares);

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
5
55

Test Case - 2

User Output
17
1785
Exp. Name: Write a program to find the quotient and
S.No: 8 Date: 2023-10-21
remainder

Aim:

Page No: 10
Problem Description:
Nancy bought apples in a fruit shop.The shop keeper specified the bill amount. Nancy also given some amount to
the shop keeper for paying the bill.But she likes to know the quotient and remainder after dividing the amount
given by her by the bill amount specified by shop keeper.

ID:
Can you help Nancy in finding it?

Constraint :
5 ≤ amount_given≤ 2500
5 ≤ bill_amount ≤ 2500

Input Format:
First Line: Integer value of amount_given representing the amount given by nancy.
Second Line: Integer value of bill_amountrepresenting the amountspecified by the shop keeper

Output Format:
First Line: Print the Quotient in integer format.

2023-2050-Faculty
Second Line: Print the Remainder in integer format.
Source Code:

billAmount.c

#include <stdio.h>

int main() {

K.Ramakrishnan College of Technology


int amount_given, bill_amount;

scanf("%d", &amount_given);
scanf("%d", &bill_amount);

int quotient = amount_given / bill_amount;


int remainder = amount_given % bill_amount;

printf("%d\n", quotient);
printf("%d\n", remainder);

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
1800
8
8

50
32

150
1250
User Output
Test Case - 2

K.Ramakrishnan College of Technology 2023-2050-Faculty ID: Page No: 11


Exp. Name: Write a program to find the volume of the
S.No: 9 Date: 2023-10-21
ball

Aim:

Page No: 12
Problem Description:
Laasya bought a new volleyball in the sports shop. It looks medium size.She somehow found the radius of the
sphere. But she would like to know the volume of that ball.

Can you help him in finding the Volume of the ball?

ID:
Functional Description:
Volume = (4.0/3.0) × π × r^3
π = 3.14

Constraint:
1.00 ≤ r ≤ 5.00

Input Format :
The only line of input has a single value of type float representing the radius of the ball.

Output Format:

2023-2050-Faculty
Print the volume of the ball in a single line.
Source Code:

volumeOfBall.c

#include <stdio.h>
#include <math.h>

K.Ramakrishnan College of Technology


int main() {
float radius;

scanf("%f", &radius);

float volume = (4.0 / 3.0) * 3.14 * pow(radius, 3);

printf("%.6f\n", volume);

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
4.6
407.513367

Test Case - 2
3.7
212.067230

K.Ramakrishnan College of Technology 2023-2050-Faculty ID: Page No: 13


Exp. Name: Write a program to find the matrix
S.No: 10 Date: 2023-10-21
multiplication

Aim:

Page No: 14
Problem Description:
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

ID:
mathematics problem of multiplying two numbers of type float.

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:

2023-2050-Faculty
In the only line of output print the result of the multiplication with 4 values after the decimal point.
Source Code:

multiplyTwoNums.c
#include <stdio.h>

int main() {

K.Ramakrishnan College of Technology


float var1, var2;

scanf("%f %f",&var1,&var2);

float result=var1*var2;

printf("%.4f\n",result);

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
412.23165 154.2136
63571.7266

Test Case - 2

User Output
749941.5000
784.2536 956.2487

K.Ramakrishnan College of Technology 2023-2050-Faculty ID: Page No: 15


Exp. Name: Write a program to calculate the area of the
S.No: 11 Date: 2023-10-21
Pyramid

Aim:

Page No: 16
Problem Description:
Jannu and Preethi both went to Egypt for visiting Pyramids.

On seeing the Pyramids they were in discussion.

ID:
During the discussion Jannu asked Preethi, what will be the area of this Pyramid.

Preethi have no idea about it.

Can you help Preethi in calculating the area of this Pyramid?

Functional Description:
Area = ( height * base )/2

Constraints:
1 <= height <= 500
1 <= base <= 500

2023-2050-Faculty
Input Format:
The only line of input has two floating point values representing height and base respectively separated by a
space.

Output Format:
In the only line of output print the area of the pyramid with only three values after decimal point.

K.Ramakrishnan College of Technology


Source Code:

areaOfPyramid.c

#include <stdio.h>
int main() {
float height, base;
scanf("%f %f", &height, &base);
float area = (height * base) / 2;
printf("%.3f\n", area);
return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
31.5 43.7
688.275

Test Case - 2
10630.890
176.3 120.6

K.Ramakrishnan College of Technology 2023-2050-Faculty ID: Page No: 17


Exp. Name: Print months in a year using switch case
S.No: 12 Date: 2023-10-21
statement

Aim:

Page No: 18
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:
DisplayMonth.c

2023-2050-Faculty
K.Ramakrishnan College of Technology
#include <stdio.h>
void main() {
int monthNumber;
printf("Enter month number : ");
scanf("%d", &monthNumber);

Page No: 19
switch (monthNumber) {
case 1:
printf("January\n");
break;
case 2:

ID:
printf("February\n");
break;
case 3:
printf("March\n");
break;
case 4:
printf("April\n");
break;
case 5:
printf("May\n");
break;
case 6:

2023-2050-Faculty
printf("June\n");
break;
case 7:
printf("July\n");
break;
case 8:
printf("August\n");
break;

K.Ramakrishnan College of Technology


case 9:
printf("September\n");
break;
case 10:
printf("October\n");
break;
case 11:
printf("November\n");
break;
case 12:
printf("December\n");
break;
default:
printf("Invalid month number\n");
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter month number :
1
January

Test Case - 2

User Output

Page No: 20
Enter month number :
11
November

ID:
Test Case - 3

User Output
Enter month number :
13
Invalid month number

Test Case - 4

User Output

2023-2050-Faculty
Enter month number :
9
September

Test Case - 5

K.Ramakrishnan College of Technology


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
Enter month number :
6
June

Test Case - 9

Page No: 21
User Output
Enter month number :
12

ID:
December

Test Case - 10

User Output
Enter month number :
0
Invalid month number

2023-2050-Faculty
Test Case - 11

User Output
Enter month number :
7
July

K.Ramakrishnan College of Technology


S.No: 13 Exp. Name: sum of n non-negative numbers Date: 2023-10-21

Aim:
Write a C program to find the sum of n non-negative numbers entered by the user.

Page No: 22
Source Code:

sum_nonnegative.c
#include <stdio.h>

ID:
int main() {
int n;
int sum = 0;
scanf("%d", &n);

if (n < 1) {
printf("Invalid input. Please enter a positive number of values.\n");
return 1;
}

printf("Enter %d Numbers: ", n);

2023-2050-Faculty
for (int i = 0; i < n; i++) {
int value;
scanf("%d", &value);

if (value >= 0) {
sum += value;
}
}

K.Ramakrishnan College of Technology


// Step 3: Print the sum
printf("Sum = %d\n", sum);

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
5
Enter 5 Numbers:
-1 2 3 4 -7
Sum = 9

Test Case - 2

User Output
7
Enter 7 Numbers:
Sum = 33
-3 -2 -4 -5 10 11 12

K.Ramakrishnan College of Technology 2023-2050-Faculty ID: Page No: 23


Exp. Name: Write a C program to calculate whether a
S.No: 14 Date: 2023-10-21
given number is Palindrome or not

Aim:

Page No: 24
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:
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 :

For example, if the user gives the input as:

Enter an integer : 2014

then the program should print the result as:

The reverse of a given number : 4102

2023-2050-Faculty
2014 is not a palindrome

If the input is given as 1221 then the result should be:

The reverse of a given number : 1221


1221 is a palindrome

K.Ramakrishnan College of Technology


Source Code:

Lab2.c
#include <stdio.h>

int main() {
int num, originalNum, reverseNum = 0, remainder;

Page No: 25
printf("Enter an integer : ");
scanf("%d", &num);

originalNum = num;

ID:
while (num != 0) {
remainder = num % 10;
reverseNum = reverseNum * 10 + remainder;
num /= 10;
}

if (originalNum == reverseNum) {
printf("The reverse of a given number : %d\n", reverseNum);
printf("%d is a palindrome\n", originalNum);
} else {
printf("The reverse of a given number : %d\n", reverseNum);
printf("%d is not a palindrome\n", originalNum);

2023-2050-Faculty
}

return 0;
}

Execution Results - All test cases have succeeded!

K.Ramakrishnan College of Technology


Test Case - 1

User Output
Enter an integer :
2014
The reverse of a given number : 4102
2014 is not a palindrome

Test Case - 2

User Output
Enter an integer :
1221
The reverse of a given number : 1221
1221 is a palindrome
Exp. Name: Write a C program to find whether the given
S.No: 16 Date: 2023-10-21
number is Armstrong or not

Aim:

Page No: 26
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:
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:

Enter any number :

For example, if the user gives the input as:

Enter any number : 153

2023-2050-Faculty
then the program should print the result as:

The given number 153 is an armstrong number

Similarly, if the input is given as 121 then the output should be "The given number 121 is not an armstrong
number".

K.Ramakrishnan College of Technology


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

Program410.c
#include <stdio.h>
#include <math.h>

int main() {
int number, originalNumber, remainder, n, result = 0;

Page No: 27
printf("Enter any number : ");
scanf("%d", &number);

ID:
originalNumber = number;

n = (int) log10(number) + 1;

while (originalNumber != 0) {
remainder = originalNumber % 10;
result += pow(remainder, n);
originalNumber /= 10;
}

2023-2050-Faculty
if (result == number) {
printf("The given number %d is an armstrong number\n", number);
} else {
printf("The given number %d is not an armstrong number\n", number);
}

return 0;
}

K.Ramakrishnan College of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

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

Page No: 28
User Output
Enter any number :
1634
The given number 1634 is an armstrong number

ID:
2023-2050-Faculty
K.Ramakrishnan College of Technology
Exp. Name: Write the C program to find the Factorial of
S.No: 17 Date: 2023-10-21
a number.

Aim:

Page No: 29
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:
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 n;

2023-2050-Faculty
long long factorial = 1;

printf("Num: ");
scanf("%d", &n);

if (n < 0) {

K.Ramakrishnan College of Technology


printf("Invalid\n");
}
else {

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


factorial *= i;
}

printf("Factorial: %lld\n", factorial);


}

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Num:
9
Factorial: 362880
Test Case - 2

User Output
Num:
0

Page No: 30
Factorial: 1

Test Case - 3

ID:
User Output
Num:
-10
Invalid

2023-2050-Faculty
K.Ramakrishnan College of Technology
S.No: 18 Exp. Name: Triple of numbers Date: 2023-10-21

Aim:
Problem Description:

Page No: 31
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.

You will be given three integers A, B, and C.

ID:
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-2050-Faculty
#include <stdio.h>

int main() {
int A, B, C;

scanf("%d %d %d", &A, &B, &C);

K.Ramakrishnan College of Technology


if ((A == B && B != C) || (A == C && A != B) || (B == C && B != A)) {
printf("Yes\n");
} else {
printf("No\n");
}

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
23 18 34
No

Test Case - 2

User Output
39 28 39
Yes
S.No: 19 Exp. Name: Write a program to find the profit and loss Date: 2023-10-21

Aim:
Problem Description:

Page No: 32
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

Can you kindly automate the same?

ID:
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.
If Cost Price < Selling Price Print as "Profit" along with the amount of profit.

2023-2050-Faculty
If Cost Price = Selling Price Print “No Profit No Loss”
Source Code:

profitLoss.c
#include <stdio.h>

int main() {

K.Ramakrishnan College of Technology


int cp, sp;

scanf("%d", &cp);
scanf("%d", &sp);

int profitOrLoss = sp - cp;

if (profitOrLoss > 0) {
printf("Profit: %d\n", profitOrLoss);
} else if (profitOrLoss < 0) {
printf("Loss: %d\n", -profitOrLoss);
} else {
printf("No Profit No Loss\n");
}

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
15945
13785
Loss: 2160

Test Case - 2

Page No: 33
User Output
26832
28513
Profit: 1681

ID:
Test Case - 3

User Output
100
100
No Profit No Loss

2023-2050-Faculty
K.Ramakrishnan College of Technology
S.No: 20 Exp. Name: Write a program to find the N pages Date: 2023-10-21

Aim:
Problem Description:

Page No: 34
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 to tell him for printing N pages at least how many sheets of paper he needs?

ID:
Constraints:
N is an integer.
1 ≤ N ≤ 1000

Input Format:
The only line of input has a single integer N representing the number of pages that need to be printed.

Output Format:
Print the output in a single line
Source Code:

print.c

2023-2050-Faculty
#include <stdio.h>

int main() {
int N;

scanf("%d", &N);

K.Ramakrishnan College of Technology


int sheets = (N + 1) / 2;

printf("%d\n", sheets);

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

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-10-21
between speeds

Aim:

Page No: 35
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:
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-2050-Faculty
Print difference between the driving speed of two participants in a single line.
Source Code:

speed.c
#include <stdio.h>
#include <stdlib.h>

K.Ramakrishnan College of Technology


int main() {
int aravspeed, aaronspeed;

scanf("%d", &aravspeed);
scanf("%d", &aaronspeed);

int speedDifference = abs(aravspeed - aaronspeed);

printf("%d\n", speedDifference);

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
74
51
23
13
89
76
User Output
Test Case - 2

K.Ramakrishnan College of Technology 2023-2050-Faculty ID: Page No: 36


S.No: 22 Exp. Name: Represent the currency value. Date: 2023-10-21

Aim:
Problem Description:

Page No: 37
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:
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:
Print either as “Even Currency” or “Odd Currency”.
Source Code:

2023-2050-Faculty
currency.c
#include <stdio.h>

int main() {
int currency;

K.Ramakrishnan College of Technology


scanf("%d", ¤cy);

if (currency % 2 == 0) {
printf("Even Currency\n");
} else {
printf("Odd Currency\n");
}

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
1985
Odd Currency

Test Case - 2

User Output
Even Currency

K.Ramakrishnan College of Technology 2023-2050-Faculty ID: Page No: 38


S.No: 23 Exp. Name: Store and print numbers using array Date: 2023-10-23

Aim:
Write a program to accept n numbers in an array and display it.

Page No: 39
Source Code:

printNumbers.c
#include <stdio.h>

ID:
int main() {
int n;

printf("How many numbers do you want to enter: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d numbers: ", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);

2023-2050-Faculty
}

printf("Entered numbers are: ");


for (int i = 0; i < n; i++) {
printf("%d", arr[i]);

if (i < n - 1) {
printf(" ");

K.Ramakrishnan College of Technology


}
}

printf("\n");

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

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: 40
ID:
2023-2050-Faculty
K.Ramakrishnan College of Technology
Exp. Name: C program to find the largest and the
S.No: 24 smallest of some numbers given by the user using Date: 2023-10-21
arrays.

Page No: 41
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:
#include <stdio.h>

int main() {
int n;

printf("Enter no.of elements: ");


scanf("%d", &n);

int elements[n];

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

2023-2050-Faculty
printf("Enter element %d: ", i + 1);
scanf("%d", &elements[i]);
}

int largest = elements[0];


int smallest = elements[0];

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

K.Ramakrishnan College of Technology


if (elements[i] > largest) {
largest = elements[i];
}
if (elements[i] < smallest) {
smallest = elements[i];
}
}

printf("Largest element: %d\n", largest);


printf("Smallest element: %d\n", smallest);

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter no.of elements:
6
Enter element 1:
48
Enter element 2:
59
Enter element 3:
68
Enter element 4:

Page No: 42
85
Enter element 5:
69
Enter element 6:

ID:
75
Largest element: 85
Smallest element: 48

Test Case - 2

User Output
Enter no.of elements:
5
Enter element 1:

2023-2050-Faculty
15
Enter element 2:
26
Enter element 3:
35
Enter element 4:

K.Ramakrishnan College of Technology


47
Enter element 5:
59
Largest element: 59
Smallest element: 15
Exp. Name: Write a program to count the number of
S.No: 25 Date: 2023-10-21
increasing subsequences

Aim:

Page No: 43
Given an array arr[] containing n integers. Write a C program to count the number of increasing subsequences in
the array of size k.
Source Code:

subsequences.c

ID:
#include <stdio.h>

int main() {
int n, k;
printf("Enter the size: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

2023-2050-Faculty
printf("value of k: ");
scanf("%d", &k);

int dp[n + 1][k + 1];


for (int i = 0; i <= n; i++) {
for (int j = 0; j <= k; j++) {
dp[i][j] = 0;
}

K.Ramakrishnan College of Technology


}

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


dp[i][1] = 1;
}

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


for (int j = 2; j <= k; j++) {
for (int l = i - 1; l >= 1; l--) {
if (arr[i - 1] > arr[l - 1]) {
dp[i][j] += dp[l][j - 1];
}
}
}
}

int result = 0;
for (int i = 1; i <= n; i++) {
result += dp[i][k];
}

printf("Result: %d\n", result);


return 0;
}
Execution Results - All test cases have succeeded!
Test Case - 1

User Output

Page No: 44
Enter the size:
5
Enter the elements:
1 2 3 0 -1

ID:
value of k:
3
Result: 1

Test Case - 2

User Output
Enter the size:
5
Enter the elements:

2023-2050-Faculty
10 11 12 13 15
value of k:
3
Result: 10

K.Ramakrishnan College of Technology


S.No: 26 Exp. Name: Split the string Date: 2023-10-21

Aim:
Write a C program to split and print the string based on blank space.

Page No: 45
Source Code:

splitstring.c
#include <stdio.h>

ID:
#include <string.h>

int main() {
char input[1000];

fgets(input, sizeof(input), stdin);

char *token = strtok(input, " ");

while (token != NULL) {


printf("%s\n", token);
token = strtok(NULL, " ");

2023-2050-Faculty
}

return 0;
}

Execution Results - All test cases have succeeded!

K.Ramakrishnan College of Technology


Test Case - 1

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-10-21

Aim:
Write a C Program to perform the following operations on a given paragraph using built-in functions

Page No: 46
• Find the total number of words
Source Code:

total_words.c

ID:
#include <stdio.h>
#include <string.h>

int main() {
char paragraph[1000];
printf("Enter a paragraph: ");
fgets(paragraph, sizeof(paragraph), stdin);

int wordCount = 0;
char *token = strtok(paragraph, " \t\n");

while (token != NULL) {

2023-2050-Faculty
wordCount++;
token = strtok(NULL, " \t\n");
}

printf("Total words: %d\n", wordCount);

return 0;
}

K.Ramakrishnan College of Technology


Execution Results - All test cases have succeeded!
Test Case - 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-10-21

Aim:
Write a C Program to perform the following operations on a given paragraph using built-in functions

Page No: 47
• Capitalize the first word of each sentence
Source Code:

capitalize.c

ID:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
char paragraph[1000];
printf("Enter a paragraph: ");
fgets(paragraph, sizeof(paragraph), stdin);

int len = strlen(paragraph);

if (len > 0) {

2023-2050-Faculty
paragraph[0] = toupper(paragraph[0]);

for (int i = 1; i < len; i++) {


if (paragraph[i - 2] == '.' || paragraph[i - 2] == '!' || paragraph[i - 2] ==
'?') {
paragraph[i] = toupper(paragraph[i]);
}
}

K.Ramakrishnan College of Technology


}

printf("Capitalizing first word:\n%s\n", paragraph);

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter a paragraph:
this is C programming
Capitalizing first word:
This is C programming

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

K.Ramakrishnan College of Technology 2023-2050-Faculty ID: Page No: 48


S.No: 29 Exp. Name: Replace a given word with another word Date: 2023-10-23

Aim:
Write a C Program to perform the following operations on a given paragraph using built-in functions

Page No: 49
• Replace a given word with another word
Source Code:

replace.c

ID:
2023-2050-Faculty
K.Ramakrishnan College of Technology
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Function to replace a word with another word in a paragraph

Page No: 50
char* replaceWord(const char* paragraph, const char* oldWord, const char* newWord)
{
char* result;
int i, cnt = 0;
int newWordLen = strlen(newWord);

ID:
int oldWordLen = strlen(oldWord);

// Counting the number of times the old word occurs in the paragraph
for (i = 0; paragraph[i] != '\0'; i++) {
if (strstr(¶graph[i], oldWord) == ¶graph[i]) {
cnt++;

// Jump to the index after the old word.


i += oldWordLen - 1;
}
}

2023-2050-Faculty
// Allocate memory for the new string
result = (char*)malloc(i + cnt * (newWordLen - oldWordLen) + 1);

i = 0;
while (*paragraph) {
// Compare the substring with the result
if (strstr(paragraph, oldWord) == paragraph) {
strcpy(&result[i], newWord);

K.Ramakrishnan College of Technology


i += newWordLen;
paragraph += oldWordLen;
}
else
result[i++] = *paragraph++;
}

result[i] = '\0';
return result;
}

int main()
{
char paragraph[1000]; // Assuming a maximum input size of 1000 characters
char oldWord[100]; // Assuming a maximum word length of 100 characters
char newWord[100]; // Assuming a maximum word length of 100 characters

printf("Enter a paragraph: ");


fgets(paragraph, sizeof(paragraph), stdin);
paragraph[strcspn(paragraph, "\n")] = '\0'; // Remove the newline character.

printf("word to replace: ");


scanf("%s", oldWord);

printf("new word: ");


printf("Original Paragraph: %s\n", paragraph);

char* result = replaceWord(paragraph, oldWord, newWord);

printf("After replacing: %s\n", result);

Page No: 51
free(result);
return 0;
}

ID:
Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Enter a paragraph:
Roses are pink,violets are blue.
word to replace:
pink

2023-2050-Faculty
new word:
red
Original Paragraph: Roses are pink,violets are blue.
After replacing: Roses are red,violets are blue.

Test Case - 2

K.Ramakrishnan College of Technology


User Output
Enter a paragraph:
This is a C programming structure.
word to replace:
structure
new word:
language
Original Paragraph: This is a C programming structure.
After replacing: This is a C programming language.
S.No: 31 Exp. Name: Array Right Rotation by K Steps Date: 2023-10-21

Aim:
Fazil loves to preform different operations on arrays, and so being the Head of the higher education institution,

Page No: 52
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:
Constraints:
• 1 <= N <= 105
• 0 <= K <= 106
• 0 <= A[i] <= 109

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-2050-Faculty
• Print the array after rotation in the single line.

Sample test case:


52
21 96 85 74 32
74 32 21 96 85

Brief explanation:

K.Ramakrishnan College of Technology


N = 5 and K = 2, So when we rotate the 5 elements of the array i.e. 21 96 85 74 32 by given value 2 in right
direction, the resultant array will be 74 32 21 96 85.

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 reverseArray(int arr[], int start, int end) {


while (start < end) {
int temp = arr[start];

Page No: 53
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}

ID:
}

int main() {
int N, K;
scanf("%d %d", &N, &K);
int arr[N];

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


scanf("%d", &arr[i]);
}

K = K % N;

2023-2050-Faculty
reverseArray(arr, 0, N - K - 1);

reverseArray(arr, N - K, N - 1);

reverseArray(arr, 0, N - 1);

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


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

K.Ramakrishnan College of Technology


if (i != N - 1) {
printf(" ");
}
}
printf(" ");
return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

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

K.Ramakrishnan College of Technology 2023-2050-Faculty ID: Page No: 54


S.No: 32 Exp. Name: Addition on two-dimensional 3*3 array Date: 2023-10-21

Aim:
Mahesh has given a two-dimensional 3*3 array starting from A [0][0].You should add the alternate elements of

Page No: 55
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:
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-2050-Faculty
sum.c

#include <stdio.h>

int main() {
int A[3][3];

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

K.Ramakrishnan College of Technology


for (int j = 0; j < 3; j++) {
scanf("%d", &A[i][j]);
}
}

int sum1 = A[0][0] + A[0][2] + A[1][1] + A[2][0] + A[2][2];


int sum2 = A[0][1] + A[1][0] + A[1][2] + A[2][1];

printf("%d\n%d\n", sum1, sum2);

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
11 12 15 36 95 84 74 115 172
367
247
81
58
User Output
9 5 8 47 6 2 31 27 4
Test Case - 2

K.Ramakrishnan College of Technology 2023-2050-Faculty ID: Page No: 56


Exp. Name: Write the code to identify the element of
S.No: 33 Date: 2023-10-21
array which occurs most frequently.

Aim:

Page No: 57
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:
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-2050-Faculty
• 1 occurs 4 times
• 2 occurs 3 times
• 4 occurs 1 time
• 5 occurs 1 time
So, the output is 1.

Constraints:

K.Ramakrishnan College of Technology


0 < n < 100

0 < arr < 1000

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: 58
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

ID:
int maxElement = arr[0];
int maxCount = 1;

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

2023-2050-Faculty
}

if (currentCount > maxCount) {


maxElement = currentElement;
maxCount = currentCount;
}
}

K.Ramakrishnan College of Technology


printf("%d\n", maxElement);

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
9
436975412
4

Test Case - 2

User Output
10
3369852143
3
S.No: 34 Exp. Name: Write the code to solve the given question. Date: 2023-10-21

Aim:
Question description:

Page No: 59
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.

The computer teacher has given homework on the topic of Arrays andwhere he needs to do Array Rotation as per

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

0 < arr < 1000

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.
• The fourth line contains the character‘L’or‘R’that defines what type of rotation needs to be done.

2023-2050-Faculty
Output Format:
• The output contains a line that is only the resultant output.
Source Code:

arrayRotation.c

K.Ramakrishnan College of Technology


#include <stdio.h>

int main() {
int n, d;
char rotationType;

Page No: 60
scanf("%d", &n);
int arr[n];

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

ID:
scanf("%d", &arr[i]);
}

scanf("%d", &d);
scanf(" %c", &rotationType);

if (rotationType == 'L') {
// Left rotation
for (int i = 0; i < d; i++) {
int temp = arr[0];
for (int j = 0; j < n - 1; j++) {
arr[j] = arr[j + 1];

2023-2050-Faculty
}
arr[n - 1] = temp;
}
} else if (rotationType == 'R') {
// Right rotation
for (int i = 0; i < d; i++) {
int temp = arr[n - 1];
for (int j = n - 1; j > 0; j--) {

K.Ramakrishnan College of Technology


arr[j] = arr[j - 1];
}
arr[0] = temp;
}
}

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


printf("%d", arr[i]);
if (i != n - 1) {
printf(" ");
}
}
printf(" ");
return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
5
12345
4
L
5 1 2 3 4

Page No: 61
Test Case - 2

User Output
7
10 11 12 13 14 15 16

ID:
4
R
13 14 15 16 10 11 12

2023-2050-Faculty
K.Ramakrishnan College of Technology
S.No: 35 Exp. Name: Problem Solving Date: 2023-10-21

Aim:
Write a C program to find the maximum of 3 numbers using functions.

Page No: 62
Input and Output Format:
Input consists of 3 integers.
Refer sample output for formatting details.

ID:
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-2050-Faculty
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));
}

K.Ramakrishnan College of Technology


maximumOfNum1.c

// maximumOfNum1.c
int largest(int x, int y, int z) {
if (x >= y && x >= z) {
return x;
} else if (y >= x && y >= z) {
return y;
} else {
return z;
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

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: 63
ID:
2023-2050-Faculty
K.Ramakrishnan College of Technology
S.No: 36 Exp. Name: sum of digits of the largest number Date: 2023-10-21

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: 64
Source Code:

sum_of_digits.c
#include <stdio.h>

ID:
int main() {
int count, max, sum = 0;

printf("count of 4-digit numbers: ");


scanf("%d", &count);

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


int num;
printf("Enter number %d: ", i + 1);
scanf("%d", &num);

2023-2050-Faculty
if (num >= 1000 && num <= 9999) {
if (i == 0 || num > max) {
max = num;
}
} else {
printf("Enter a valid 4-digit number\n");
i--;
}

K.Ramakrishnan College of Technology


}

printf("largest number: %d\n", max);

while (max > 0) {


sum += max % 10;
max /= 10;
}

printf("sum of its digits: %d\n", sum);

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
count of 4-digit numbers:
3
Enter number 1:
1234
3456
Enter number 3:
4000
largest number: 4000
sum of its digits: 4

Page No: 65
Test Case - 2

User Output

ID:
count of 4-digit numbers:
4
Enter number 1:
1100
Enter number 2:
200
Enter a valid 4-digit number
Enter number 2:
2000
Enter number 3:

2023-2050-Faculty
3000
Enter number 4:
99999
Enter a valid 4-digit number
Enter number 4:
9999

K.Ramakrishnan College of Technology


largest number: 9999
sum of its digits: 36
S.No: 37 Exp. Name: Area of the circle Date: 2023-10-21

Aim:
Write a C program to find the area of the circle using call-by value and call-by reference.

Page No: 66
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:
Source Code:

area_circle.c

//write your code here...

#include <stdio.h>

double areaCallByValue(double r) {

2023-2050-Faculty
const double pi = 3.14;
return pi * r * r;
}

void areaCallByReference(double r, double *area) {


const double pi = 3.14;
*area = pi * r * r;
}

K.Ramakrishnan College of Technology


int main() {
double radius, area;

printf("radius: ");
scanf("%lf", &radius);

// Calculate the area using call by value


double areaByValue = areaCallByValue(radius);
printf("Area(Call by value): %.2f\n", areaByValue);

// Calculate the area using call by reference


areaCallByReference(radius, &area);
printf("Area(Call by reference): %.2f\n", area);

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
radius:
7
Area(Call by value): 153.86
Area(Call by reference): 153.86

Page No: 67
Test Case - 2

User Output
radius:

ID:
8.2
Area(Call by value): 211.13
Area(Call by reference): 211.13

2023-2050-Faculty
K.Ramakrishnan College of Technology
Exp. Name: Write a C program to find the Factorial of a
S.No: 38 Date: 2023-10-21
given number using Recursion

Aim:

Page No: 68
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:

ID:
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-2050-Faculty
Program901a.c
#include <stdio.h>

long int factorial(int n);

K.Ramakrishnan College of Technology


long int factorial(int n) {
if (n == 0) {
return 1; // Base case
} else {
return n * factorial(n - 1); // Recursive case
}
}

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
Enter an integer :
4
Factorial of 4 is : 24
Test Case - 3

User Output
Enter an integer :
8

Page No: 69
Factorial of 8 is : 40320

Test Case - 4

ID:
User Output
Enter an integer :
0
Factorial of 0 is : 1

2023-2050-Faculty
K.Ramakrishnan College of Technology
Exp. Name: Write a C program to display the Fibonacci
S.No: 39 Date: 2023-10-21
series up to the given number of terms using Recursion

Aim:

Page No: 70
Write a program to display the fibonacci series up to the given number of terms using recursion process.

The fibonacci series is 0 1 1 2 3 5 8 13 21 34...... .

ID:
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);
printf("The fibonacci series of %d terms are : ", n);
for (i = 0; i < n; i++) {

2023-2050-Faculty
printf(" %d ", fib(i));
}
}

fibonacci1.c

K.Ramakrishnan College of Technology


int fib(int n) {
if (n <= 1) {
return n;
} else {
return (fib(n - 1) + fib(n - 2));
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

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

User Output
Enter value of n :
14

Page No: 71
The fibonacci series of 14 terms are : 0 1 1 2 3 5 8 13 21 34 55 89 144 233

Test Case - 4

ID:
User Output
Enter value of n :
3
The fibonacci series of 3 terms are : 0 1 1

2023-2050-Faculty
K.Ramakrishnan College of Technology
S.No: 40 Exp. Name: To access elements using a pointer Date: 2023-10-21

Aim:
Write a program to access the array elements using a pointer.

Page No: 72
Source Code:

pyramid.c

#include <stdio.h>

ID:
int main() {
int size;
printf("Enter the size of elements:");
scanf("%d", &size);

int arr[size];
int *ptr = arr;
printf("Enter elements: ");
for (int i = 0; i < size; i++) {
scanf("%d", ptr + i);
}

2023-2050-Faculty
printf("Accessing the elements using the pointer: \n");
for (int i = 0; i < size; i++) {
printf("%d\n", *(ptr + i));
}

return 0;
}

K.Ramakrishnan College of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

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

User Output
Enter the size of elements:
Enter elements:
40 30 10 20
Accessing the elements using the pointer:
40
30

Page No: 73
10
20

ID:
2023-2050-Faculty
K.Ramakrishnan College of Technology
Exp. Name: Write the code to print its elements in
S.No: 41 Date: 2023-10-21
reverse order

Aim:

Page No: 74
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:
• 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

2023-2050-Faculty
K.Ramakrishnan College of Technology
#include <stdio.h>
#include <stdlib.h>

int main() {
int n;

Page No: 75
scanf("%d", &n);

// Dynamically allocate memory for the array


int *arr = (int *)malloc(n * sizeof(int));

ID:
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}

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


scanf("%d", &arr[i]);
}

// Reverse the array

2023-2050-Faculty
for (int i = 0, j = n - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

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

K.Ramakrishnan College of Technology


printf("%d", arr[i]);
if (i < n - 1) {
printf(" "); // Use · as a separator
}
}

// Free the dynamically allocated memory


free(arr);
printf(" ");

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
5
11 22 33 44 55
55 44 33 22 11
Test Case - 2

User Output
9
459 964 2225 631 82 3118 5202 9852 235

Page No: 76
235 9852 5202 3118 82 631 2225 964 459

ID:
2023-2050-Faculty
K.Ramakrishnan College of Technology
Exp. Name: Write the code to find whether the given
S.No: 42 Date: 2023-10-21
string is lucky or not

Aim:

Page No: 77
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:
and the maximum length of the string is 100. Refer sample input and output for formatting specifications.
Source Code:

luckyString.c
#include <stdio.h>

int isLucky(char *str) {


int sum = 0;

for (int i = 0; str[i] != '\0'; i++) {

2023-2050-Faculty
sum += str[i];
}

if (sum % 2 == 0) {
return 1; // Lucky
} else {
return 0; // Not lucky

K.Ramakrishnan College of Technology


}
}

int main() {
char str[101];

scanf("%s", str);

int result = isLucky(str);

if (result == 1) {
printf("%s is lucky\n", str);
} else {
printf("%s is not lucky\n", str);
}

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1
User Output
anitha
anitha is not lucky

Page No: 78
Test Case - 2

User Output
ram

ID:
ram is lucky

2023-2050-Faculty
K.Ramakrishnan College of Technology
Exp. Name: Write a program to calculate array some
S.No: 43 Date: 2023-10-21
using pointers

Aim:

Page No: 79
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:
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>
#include <stdlib.h>

2023-2050-Faculty
int main() {
int n;
scanf("%d", &n);

int *arr = (int *)malloc(n * sizeof(int));


if (arr == NULL) {
return 1;

K.Ramakrishnan College of Technology


}

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


scanf("%d", &arr[i]);
}

int sum = 0;
int *ptr = arr;

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


sum += *ptr;
ptr++;
}

printf("%d\n", sum);

free(arr);
return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1
0
5
4

88
22 22 22 22

User Output
User Output

0 -22 -44 22 44
Test Case - 2

K.Ramakrishnan College of Technology 2023-2050-Faculty ID: Page No: 80


S.No: 44 Exp. Name: Menu driven C program Date: 2023-10-23

Aim:
Write a menu-driven C program that allows a user to enter n numbers and then choose between finding the

Page No: 81
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:
ProgramMenu.c

2023-2050-Faculty
K.Ramakrishnan College of Technology
#include <stdio.h>
void smallest(int[], int);
void largest(int[], int);
void sum(int[], int);
void average(int[], int);

Page No: 82
#include <stdio.h>

void smallest(int[], int);


void largest(int[], int);
void sum(int[], int);

ID:
void average(int[], int);

void printMenu() {
printf("The menu driven is:\n");
printf("1.smallest\n");
printf("2.largest\n");
printf("3.sum\n");
printf("4.average\n");
}

int main() {
int n, option;

2023-2050-Faculty
printf("Enter number : ");
scanf("%d", &n);

int numbers[n];

printf("Enter %d numbers : ", n);


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

K.Ramakrishnan College of Technology


scanf("%d", &numbers[i]);
}

printMenu();

printf("Enter an option : ");


scanf("%d", &option);

switch (option) {
case 1:
smallest(numbers, n);
break;
case 2:
largest(numbers, n);
break;
case 3:
sum(numbers, n);
break;
case 4:
average(numbers, n);
break;
default:
printf("Invalid choice\n");
break;
}
}

void smallest(int arr[], int n) {


int smallest = arr[0];
for (int i = 1; i < n; i++) {

Page No: 83
if (arr[i] < smallest) {
smallest = arr[i];
}
}
printf("The smallest element is %d\n", smallest);

ID:
}

void largest(int arr[], int n) {


int largest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
printf("The largest element is %d\n", largest);
}

2023-2050-Faculty
void sum(int arr[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
printf("The sum of all elements is %d\n", sum);
}

K.Ramakrishnan College of Technology


void average(int arr[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
double avg = (double)sum / n;
printf("The average of all elements is %.6lf\n", avg);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter number :
5
Enter 5 numbers :
6 7 8 9 12
The menu driven is:
1.smallest
2.largest
3.sum
4.average
Enter an option :
5
Invalid choice

Test Case - 2

Page No: 84
User Output
Enter number :
4

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

2023-2050-Faculty
K.Ramakrishnan College of Technology
Exp. Name: Write a function to compute mean, variance,
S.No: 45 Standard Deviation, sorting of n elements in single Date: 2023-10-21
dimension array.

Page No: 85
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:
MeanVariance.c

2023-2050-Faculty
K.Ramakrishnan College of Technology
#include <stdio.h>
#include <math.h>
void calculateMean(int [], int);
float calculateVariance(int [], int);
float calculateStandardDeviation(int [], int);

Page No: 86
void calculateSort(int [], int);
void main() {
int arr[20], number;
float variance = 0, standardDeviation = 0;
printf("Enter size of the array : ");

ID:
scanf("%d", &number);
printf("Enter array elements : ", number);
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-2050-Faculty
for (int i = 0; i < number; i++) {
printf(" %d", arr[i]);
}
printf("\n");
}

// Function to calculate the mean

K.Ramakrishnan College of Technology


void calculateMean(int arr[], int n) {
float sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
float mean = sum / n;
printf("The mean of elements of the array : %.6f\n", mean);
}

// Function to calculate the variance


float calculateVariance(int arr[], int n) {
float mean = 0;
for (int i = 0; i < n; i++) {
mean += arr[i];
}
mean /= n;

float variance = 0;
for (int i = 0; i < n; i++) {
variance += pow(arr[i] - mean, 2);
}
variance /= n;

return variance;
}
float calculateStandardDeviation(int arr[], int n) {
float variance = calculateVariance(arr, n);
float standardDeviation = sqrt(variance);
return standardDeviation;
}

Page No: 87
// Function to sort the array
void calculateSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {

ID:
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

2023-2050-Faculty
Execution Results - All test cases have succeeded!
Test Case - 1

K.Ramakrishnan College of Technology


User Output
Enter size of the array :
5
Enter array elements :
24680
The mean of elements of the array : 4.000000
The variance of elements of the array : 8.000000
The Standard Deviation of elements of the array : 2.828427
The elements in array after sorting : 0 2 4 6 8

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
S.No: 46 Exp. Name: Student Information Management System Date: 2023-10-23

Aim:
Write a C program to build a simple Application Software for Student Information Management System which can

Page No: 88
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.
• Store the CGPA of every student.

ID:
• Store the Courses Registered by the student.
Source Code:

management_sys.c

2023-2050-Faculty
K.Ramakrishnan College of Technology
#include <stdio.h>
#include <string.h>
struct Student {
char firstName[50];
char lastName[50];

Page No: 89
int rollNumber;
float cgpa;
char courses[5][50];
int numCourses;
};

ID:
int main() {
struct Student student;

printf("Student Information Management System\n");

// Input student information


printf("First Name: ");
scanf("%s", student.firstName);

printf("Last Name: ");


scanf("%s", student.lastName);

2023-2050-Faculty
printf("Roll Number: ");
scanf("%d", &student.rollNumber);

printf("CGPA: ");
scanf("%f", &student.cgpa);

printf("number of courses(up to 5): ");

K.Ramakrishnan College of Technology


scanf("%d",&student.numCourses);

if (student.numCourses>5)
{
printf("Maximum 5 courses can be registered\n");
return 0;
}

printf("names of courses:\n");
for (int i = 0; i<student.numCourses; i++) {
// printf("Course %d:",i+1);
scanf("%s",student.courses[i]);
}
printf("Student Information:\n");
printf("First Name: %s\n",student.firstName);
printf("Last Name: %s\n",student.lastName);
printf("Roll Number: %d\n",student.rollNumber);
printf("CGPA: %.2f\n",student.cgpa);
printf("Courses Registered:\n");
for (int i=0;i<student.numCourses;i++) {
printf("%s\n",student.courses[i]);
}

return 0;
}
Execution Results - All test cases have succeeded!
Test Case - 1

User Output

Page No: 90
Student Information Management System
First Name:
william
Last Name:
Kare

ID:
Roll Number:
23
CGPA:
9.21
number of courses(up to 5):
3
names of courses:
Economics
Mathematics
Commerce

2023-2050-Faculty
Student Information:
First Name: william
Last Name: Kare
Roll Number: 23
CGPA: 9.21
Courses Registered:

K.Ramakrishnan College of Technology


Economics
Mathematics
Commerce

Test Case - 2

User Output
Student Information Management System
First Name:
Peter
Last Name:
Zion
Roll Number:
12
CGPA:
7.9
number of courses(up to 5):
7
Maximum 5 courses can be registered
S.No: 47 Exp. Name: Expense manager Date: 2023-10-21

Aim:
Write a C program to develop an expense manager that reads date, product, price, and product category. The

Page No: 91
program should display the total expense amount based on product category or date as per the user's selection
using structures.

Note: The driver function is provided to you in the editor.


Source Code:

ID:
expense_manager.c

2023-2050-Faculty
K.Ramakrishnan College of Technology
#include <stdio.h>
#include <string.h>
#define MAX_EXPENSES 100

Page No: 92
struct Expense {
char date[11];
char product[50];

ID:
float price;
char category[50];
};

float totalExpenseByCategory(struct Expense expenses[], int numExpenses, char category[50])


{
float total = 0.0;
for (int i = 0; i < numExpenses; i++) {
if (strcmp(expenses[i].category, category) == 0) {
total += expenses[i].price;
}
}

2023-2050-Faculty
return total;
}

float totalExpenseByDate(struct Expense expenses[], int numExpenses, char date[11]) {


float total = 0.0;
for (int i = 0; i < numExpenses; i++) {
if (strcmp(expenses[i].date, date) == 0) {
total += expenses[i].price;

K.Ramakrishnan College of Technology


}
}
return total;
}

int main() {
struct Expense expenses[MAX_EXPENSES];
int numExpenses = 0;

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: ");
if (choice == 1) {
if (numExpenses < MAX_EXPENSES) {
printf("Enter date (YYYY-MM-DD): ");
scanf("%s", expenses[numExpenses].date);
printf("Enter product name: ");

Page No: 93
scanf("%s", expenses[numExpenses].product);
printf("Enter price: ");
scanf("%f", &expenses[numExpenses].price);
printf("Enter category: ");
scanf("%s", expenses[numExpenses].category);

ID:
numExpenses++;
} else {
printf("Expense list is full!\n");
}
} else if (choice == 2) {
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) {
printf("Enter date (YYYY-MM-DD): ");
scanf("%s", date);

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

K.Ramakrishnan College of Technology


return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

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:
Enter price:
250
Enter category:
Skincare
Options:

Page No: 94
1. Add Expense
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit

ID:
Enter your choice:
1
Enter date (YYYY-MM-DD):
2023-10-12
Enter product name:
Riceflour
Enter price:
100
Enter category:
Food
Options:

2023-2050-Faculty
1. Add Expense
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
1

K.Ramakrishnan College of Technology


Enter date (YYYY-MM-DD):
2023-11-14
Enter product name:
Facewash
Enter price:
300
Enter category:
Skincare
Options:
1. Add Expense
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
2
Enter category:
Skincare
Total expense in category 'Skincare' is 550.00
Options:
1. Add Expense
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
Enter date (YYYY-MM-DD):
2023-10-12
Total expense on date '2023-10-12' is 350.00
Options:
1. Add Expense

Page No: 95
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
5

ID:
Invalid choice
Options:
1. Add Expense
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
2
Enter category:
Groceries

2023-2050-Faculty
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:

K.Ramakrishnan College of Technology


3
Enter date (YYYY-MM-DD):
2024-07-09
Total expense on date '2024-07-09' is 0.00
Options:
1. Add Expense
2. Calculate Total Expense by Category
3. Calculate Total Expense by Date
4. Exit
Enter your choice:
4
Exp. Name: Write the code to find no of characters in a
S.No: 48 Date: 2023-10-21
string

Aim:

Page No: 96
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:
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-2050-Faculty
char name[100];
};

int main() {
struct name myFriend;

K.Ramakrishnan College of Technology


scanf("%s", myFriend.name);

int numCharacters = strlen(myFriend.name);


printf("%d\n", numCharacters);

return 0;

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
raja
4

Test Case - 2

User Output
13
srmuniversity

K.Ramakrishnan College of Technology 2023-2050-Faculty ID: Page No: 97


S.No: 49 Exp. Name: Write the code to calculate EMI Date: 2023-10-21

Aim:
Your task is to:

Page No: 98
• 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:
• 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

#include <stdio.h>
#include <math.h>
struct EMI {
float principal;
float rate;

2023-2050-Faculty
float time;

};

int main() {
struct EMI e;

K.Ramakrishnan College of Technology


// Input principal, rate, and time

scanf("%f", &e.principal);

scanf("%f", &e.rate);

scanf("%f", &e.time);

// Calculate EMI
float rate = e.rate / (12 * 100); // Monthly interest rate
float time = e.time * 12; // Total number of monthly payments
float emi = (e.principal * rate * pow(1 + rate, time)) / (pow(1 + rate, time) - 1);

printf("Monthly EMI is: %.2f\n", emi);

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
200000
10
2
Monthly EMI is: 9228.99

Page No: 99
Test Case - 2

User Output
5614500
20

ID:
6
Monthly EMI is: 134483.22

2023-2050-Faculty
K.Ramakrishnan College of Technology
S.No: 50 Exp. Name: Write the code to display student details Date: 2023-10-21

Aim:
Define a structure named Student with the following members:

Page No: 100


• name (char array of size 100)
• city (char array of size 100)
• establishmentYear (integer)
• passPercentage (floating-point number)

Write a C program that allows the user to input details of multiple students and then sorts and displays these

ID:
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-2050-Faculty
K.Ramakrishnan College of Technology
#include <stdio.h>
struct Student {
char name[100];
char city[100];
int establishmentYear;

Page No: 101


float passPercentage;
};

void printStudent(struct Student student) {


printf("Name:%s\n", student.name);

ID:
printf("Department:%s\n", student.city);
printf("Year of study:%d\n", student.establishmentYear);
printf("CGPA:%.1f\n", student.passPercentage);
}

int main() {
int n;
scanf("%d", &n);

struct Student students[n];

2023-2050-Faculty
for (int i = 0; i < n; i++) {
scanf("%s %s %d %f", students[i].name, students[i].city,
&students[i].establishmentYear, &students[i].passPercentage);
}

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


for (int j = 0; j < n - i - 1; j++) {
if (strcmp(students[j].name, students[j + 1].name) > 0) {

K.Ramakrishnan College of Technology


struct Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}

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


printStudent(students[i]);
}

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
3
raju cse 1 7.8
somu IT 2 8.2
Jagan swe 3 8.6
Name:Jagan
Department:swe
Year of study:3
CGPA:8.6
Name:raju

Page No: 102


Department:cse
Year of study:1
CGPA:7.8
Name:somu
Department:IT

ID:
Year of study:2
CGPA:8.2

Test Case - 2

User Output
5
Rani IT 5 9.2
Meghana cse 4 6.5

2023-2050-Faculty
bhuvana ece 6 5.6
Swathi eee 3 6.4
ravi mech 7 9.6
Name:Meghana
Department:cse
Year of study:4
CGPA:6.5

K.Ramakrishnan College of Technology


Name:Rani
Department:IT
Year of study:5
CGPA:9.2
Name:Swathi
Department:eee
Year of study:3
CGPA:6.4
Name:bhuvana
Department:ece
Year of study:6
CGPA:5.6
Name:ravi
Department:mech
Year of study:7
CGPA:9.6
Exp. Name: Write the code to find sum of digits of a
S.No: 51 Date: 2023-10-23
number

Aim:

Page No: 103


Jagan played a maths game with his friend Sanjay to find the sum of digits of the number he said.

Help Jagan and Sanjay to solve it by your code using union.

Input Format

ID:
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 {

2023-2050-Faculty
int n;
};
int main()
{
union Data num;

// printf("Enter an integer (1 to 999): ");


scanf("%d", &num.n);

K.Ramakrishnan College of Technology


int number = num.n;
int sum = 0;

//if (number >= 1 && number <= 999)


//{
while (number > 0) {
sum += number % 10;
number /= 10;
}
printf("%d\n", sum);
//} else {
// printf("Input out of range (1 to 999).\n");
// }

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
123
6

14
1193
User Output
Test Case - 2

K.Ramakrishnan College of Technology 2023-2050-Faculty ID: Page No: 104

You might also like