1
NAME – HRADESH
KRISHANAN
CLASS - AE
Subject- cs
Roll no.- 29
Question1.
You are working on a library
system. Implement a C code
to calculate the late fee
for a borrowed book based on
the number of days book is
2
borrowed. Consider different
fee
structures for different types
of books.
Type of book Days Late Fees
A
7 No Late fees
8-10 Rs. 2 per day
More than 10 Rs. 5 per day
B
5 No Late fees
6-10 Rs. 5 per day
3
More than 10 Rs. 10 per day
Answer.
#include <stdio.h>
int main () {
char book_type;
int days_late;
float late_fee;
printf ("Enter the type of
book (A/B): ");
4
scanf("%c", &book_type);
printf("Enter the number of
days the book is late: ");
scanf("%d", &days_late);
if (book_type == 'A') {
if (days_late <= 7) {
late_fee = 0;
} else if (days_late >= 8
&& days_late <= 10) {
5
late_fee = 2 *
(days_late - 7);
} else {
late_fee = 5 *
(days_late - 7);
}
} else if (book_type == 'B') {
if (days_late <= 5) {
late_fee = 0;
} else if (days_late >= 6
&& days_late <= 10) {
6
late_fee = 5 (days_late -
5);
} else {
late_fee = 10 *
(days_late - 5);
}
}
printf("The late fee for the
borrowed book is Rs. %.2f\n",
late_fee);
7
return 0;
}
Question 2.
You are developing a scientific
calculator program in C.
Implement a C code that
calculates the real roots of a
quadratic equation `ax2 + bx +
c = 0` based on user-provided
coefficients `a`, `b`, and `c`.
Handle cases with complex
roots as well.
8
Answer.
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, discriminant,
root1, root2, realPart,
imaginaryPart;
printf("Enter coefficients a,
b and c: ");
9
scanf("%lf %lf %lf", &a, &b,
&c);
discriminant = b * b - 4 * a *
c;
// Condition for real and
different roots
if (discriminant > 0) {
root1 = (-b +
sqrt(discriminant)) / (2 * a);
10
root2 = (-b -
sqrt(discriminant)) / (2 * a);
printf("The roots are real
and different.\n");
printf("root1 = %.2lf and
root2 = %.2lf", root1, root2);
}
// Condition for real and
equal roots
else if (discriminant == 0) {
11
root1 = root2 = -b / (2 *
a);
printf("The roots are real
and equal.\n");
printf("root1 = root2 =
%.2lf;", root1);
}
// Condition for complex
roots
else {
realPart = -b / (2 * a);
12
imaginaryPart = sqrt(-
discriminant) / (2 * a);
printf("The roots are
complex and different.\n");
printf("root1 = %.2lf+
%.2lfi and root2 = %.2f-%.2fi",
realPart, imaginaryPart,
realPart, imaginaryPart);
}
return 0;
}
13
Question 3.
You are developing a program
to find the Fibonacci
sequence. The Fibonacci
sequence is a sequence of
numbers where each number
is the sum of the two previous
numbers. The first two
numbers in the Fibonacci
sequence are 0 and 1. Write a
C program to find the
14
Fibonacci sequence up to a
given number of terms.
Answer.
#include <stdio.h>
int main() {
int i, num_terms, t1 = 0, t2 =
1, nextTerm;
printf("Enter the number of
terms: ");
15
scanf("%d", &num_terms);
printf("Fibonacci Series: ");
for (i = 1; i <= num_terms; +
+i) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
16
return 0;
}
Question 4. You are
developing a program to find
the A.P. series. An Arithmetic
Progression series is a series
of number having the
common difference between
any two consecutive terms of
series. Write a C program to
17
find the A.P. series and the
sum of an A.P. series.
Answer .
#include <stdio.h>
int main() {
int a, d, n;
printf("Enter the first term
of the A.P. series: ");
scanf("%d", &a);
18
printf("Enter the common
difference: ");
scanf("%d", &d);
printf("Enter the number of
terms in the A.P. series: ");
scanf("%d", &n);
int sum = 0;
printf("A.P. Series: ");
for (int i = 0; i < n; i++) {
int term = a + i * d;
19
printf("%d ", term);
sum += term;
}
printf("\nSum of the A.P.
series: %d\n", sum);
return 0;
}
1. It takes input for the first
term (a), common
difference (d), and the
20
number of terms (n) in the
A.P. series from the user.
2. It calculates each term in
the A.P. series using the
formula term = a + i * d and
prints each term in the
series.
3. It calculates the sum of
the A.P. series by adding up
all the terms and prints the
result.
Question 5.
21
You are to develop a User
defined Program to find the
Leap Year Finder program.
Write a C code to implement
the program to run the code
for user defined inputs for
year and continuation of
program. (Use Do while for
User Defined Program.)
Answer.
#include <stdio.h>
22
int main() {
int year;
char choice;
do {
printf("Enter a year to
check if it's a leap year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year
% 100 != 0) || (year % 400 ==
0)) {
23
printf("%d is a leap
year.\n", year);
} else {
printf("%d is not a leap
year.\n", year);
}
printf("Do you want to
check another year? (Y/N): ");
scanf(" %c", &choice); //
Note the space before %c to
consume any whitespace
characters
24
} while (choice == 'Y' ||
choice == 'y');
return 0;
}
1. It takes input for a year
from the user.
2. It checks whether the
entered year is a leap year
or not based on the leap
year rule.
25
3. It asks the user if they
want to check another year
(Y/N).
4. If the user enters 'Y' or
'y', the program continues
to the next iteration;
otherwise, it exits the loop.
Question 6.
You want to develop a Strong
Number Calculator. A Strong
number is a number whose
sum of factorial of digits is
26
equal to the original number.
Write a C code to develop the
Calculator for a given input N.
Answer .
#include <stdio.h>
// Function to calculate the
factorial of a number
int factorial(int num) {
int fact = 1;
27
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
// Function to check if a
number is a Strong number
int isStrongNumber(int num) {
int originalNum = num;
int sum = 0;
28
while (num > 0) {
int digit = num % 10;
sum += factorial(digit);
num /= 10;
}
return (sum ==
originalNum);
}
int main() {
int n;
29
printf("Enter a number to
check if it's a Strong number:
");
scanf("%d", &n);
if (isStrongNumber(n)) {
printf("%d is a Strong
number.\n", n);
} else {
printf("%d is not a Strong
number.\n", n);
}
30
return 0;
}
Question 7.
You are developing a program
to find the least common
multiple (LCM) of two
numbers. The least common
multiple of two numbers is the
smallest positive integer that
is a multiple of both numbers.
31
Write a C program to find the
least common multiple of two
numbers, given two positive
integers.
Answer.
#include <stdio.h>
// Function to calculate the
greatest common divisor
32
(GCD) using Euclidean
algorithm
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
// Function to calculate the
least common multiple (LCM)
int lcm(int a, int b) {
33
return (a * b) / gcd(a, b);
}
int main() {
int num1, num2;
printf("Enter the first
positive integer: ");
scanf("%d", &num1);
printf("Enter the second
positive integer: ");
scanf("%d", &num2);
34
int result = lcm(num1,
num2);
printf("The least common
multiple of %d and %d is: %d\
n", num1, num2, result);
return 0;
}
Question 8.
You are developing a calendar
Calculator. Write a C code to
35
convert the given number of
Days into Years, week and
days, using arithmetic
operations.
Answer .
#include <stdio.h>
int main() {
int days, years, weeks,
remainingDays;
36
printf("Enter the number of
days: ");
scanf("%d", &days);
// Calculate years, weeks,
and remaining days
years = days / 365;
days = days % 365;
weeks = days / 7;
remainingDays = days % 7;
printf("Years: %d\n", years);
37
printf("Weeks: %d\n",
weeks);
printf("Remaining Days: %d\
n", remainingDays);
return 0;
}
Question 9.
38
You are developing a program
to calculate the volume of a
sphere. The volume of a
sphere can be calculated using
the following formula: Volume
of a sphere = 4/3 * π r3 where
π is a mathematical constant
with an approximate value of
3.14. Write a C program to
calculate the volume of a
sphere, given the radius.
39
Answer.
#include <stdio.h>
#include <math.h>
int main() {
double radius, volume;
const double pi =
3.14159265358979323846; //
Value of pi (π)
printf("Enter the radius of
the sphere: ");
40
scanf("%lf", &radius);
// Calculate the volume of
the sphere
volume = (4.0 / 3.0) * pi *
pow(radius, 3);
printf("The volume of the
sphere with radius %.2lf is
%.2lf cubic units\n", radius,
volume);
41
return 0;
}
Question 10.
You are developing a program
to find the greatest common
divisor (GCD) of two numbers.
The greatest common divisor
of two numbers is the largest
positive integer that divides
both numbers. Write a C
program to find the greatest
common divisor of two
42
numbers, given two positive
integers.
Answer.
#include <stdio.h>
// Function to calculate the
greatest common divisor
(GCD) using the Euclidean
algorithm
int gcd(int a, int b) {
43
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int num1, num2;
44
printf("Enter the first
positive integer: ");
scanf("%d", &num1);
printf("Enter the second
positive integer: ");
scanf("%d", &num2);
int result = gcd(num1,
num2);
45
printf("The greatest
common divisor (GCD) of %d
and %d is: %d\n", num1,
num2, result);
return 0;
}