Problem 1

A movie run in a theatre is documented in days. Write a C program to help the theatre owner represent the days into number of years, weeks and days respectively. Assume there is no leap year. (Input/output days)

Example: 1329 days is split into 3 years, 33 weeks and 3 days respectively.

Solution
Problem 2

Read any four digit number from the keyboard. Print the number whose digits are in the reverse order to that of the input number. Print the absolute difference between the reversed and the input numbers.

Example: If the number entered is 1234, then it will print the result 4321 and 3087.

Solution
Problem 3

Write a C program to find the entered character through the keyboard is digit, alphabet or special character

Solution
Problem 4

Write a C program to implement a simple calculator. Input from the user will be two values and the operation the user wants to carry out. Use Switch case.

Solution
Problem 5

Write a C program to swap two integers using the following logics

a) Using a temporary variable.
b) Without using temporary variable using arithmetic operators.
c) Without using temporary variable and arithmetic operators.
(hint: Use bitwise operators)
Input: x = 10 y = 5
Output: x = 5, y = 10

Solution
Problem 6

Consider a triangle, whose two sides and the angle between them in degree are provided as a, b and theta (to be read from keyboard). Compute the perimeter and the area of the triangle.
Generate output with the following input data set:
(i) a=10, b=7, theta=40
(ii) a=3.7, b=8.9, theta= 130
(iii) a=18.0, b=2.48, theta= 90

Solution
Problem 7

Compute the simple and compound interests of a principal amount p (to be read) at an interest rate of r% (to be read) for n (to be read) years.

Generate output with the following input data set:
(i) p=1000, r=3.5, n=5
(ii) p=100, r=2.5, n=25
(iii) a=100000, r=5, n=3

Solution
Problem 8

Suppose a stone of mass m Kg is thrown vertically with a velocity of v m/s in some planet. The time taken the stone to hit the ground is t sec. Compute the gravitational acceleration (g) of the planet, given m, v and t (to be read). Given the radius of the planet R m, compute its mass M in Kg. Take the value of gravitational constant as 6.67408 × 10-11 m3 kg-1 s-2 .
Generate output with the following input data set:
(i) m=40, v=100, t=.04, R= 64000
(ii) m=4, v=30, t=.4, R= 6400
(iii) m=.4, v=10, t=4, R= 640

Solution
Problem 9

Write a C-program which evaluates the value of the following
function as given below for an input real number x (to be read):
f(x)= 8x^3+4x-3
The program also computes the sign of the derivative of the function (f’(x)). If the derivative is positive, it prints ‘1’.
If it is negative, prints ‘-1’.
Otherwise, it prints ‘0’. Run your program to provide results for the following input numbers.
-5.4, 2.4, 3.0, 5.0, and 38

Solution
Problem 10

Write a C-program which takes three distinct points (in 2-D coordinates) as inputs and checks whether they form a triangle or straight line. If they form a triangle, it prints “Form triangle.” If they form a straight line, it prints “Form Straight Line”. If any pair of points are non-distinct, it prints coordinates of that point, and notifies “Non-distinct input point”.
Provide results for the following coordinate points:
(i) (2,3), (4,5), (10,23)
(ii) (1,2), (4,8), (9,18)
(iii) (0,0), (1,3), (1,3)

Solution
Problem 11

Write a c program that will do the following:
•Read a temperature value in Fahrenheit scale from the keyboard in a variable F
•Convert the temperature value to its corresponding value in Centigrade scale and store it in a variable C
•Print the value of C in the display
Note that the temperature values are real numbers. Also, they can be positive, negative, or 0.

Solution
Problem 12

Write a c program that will do the following:
•Read the length L and width W of a rectangle (using a single scanf statement)
•Compute the area A and perimeter P of the rectangle
•Print the values of A and P with a suitable message (using a single printf statement).
You can assume that L and W are integers.

Solution
Problem 13

A ball is released from the top of a tower of height h metres. It takes T seconds to reach the ground.
Write a c program which will -
•Read the value of h
•And print the position of the ball in T/3 seconds?
Assume all values to be floating point numbers.

Solution
Problem 14

A rectangle with sides parallel to the X and Y-axis can be fully specified by specifying the (x,y) coordinates of the bottom-left corner (a, b) and the top-right corner (c, d) as shown below:
In this assignment, given a rectangle R specified as above and another point P with coordinates (p, q), you will find whether P is inside R or not (If P is on one of the boundaries of the rectangle, you should still consider it outside the rectangle).
For this assignment, assume that all coordinates will be integers.

Write the program to do the following (exactly in this order):
 Read the coordinates (a, b) of the bottom-left corner of the rectangle (read both a and b in a single scanf statement)
 Read the coordinates (c, d) of the top-right corner of the rectangle (read both c and d in a single scanf statement)
 Compute the other two corners of the rectangle and store them in variables.
 Print all four corners of the rectangle nicely using a single printf statement. Print the corners starting from the bottom left corner (the point (a,b)) and going in the clock-wise direction (so bottom-left, then top-left, then top-right, then bottom right).

Your print should look something like “The four corners of the rectangle are (.. , ..), (.. , ..), (.. , ..), (.. , ..)” where the brackets will show the actual x,y coordinates of the corners
 Read in the coordinates (p,q) of the point to be checked using a single scanf statement.
 Check if the point is inside the rectangle or not and print out a suitable message like “The point (..,..) is inside the rectangle” or “The point (..,..) is not inside the rectangle”, where the (..,..) should show the actual x,y coordinates of the point
 Test your program with the rectangle specified by bottom left corner (2,1) and top right corner (7,5) for the points (4,3), (8,2), (5,8), and (3,7)

Solution
Problem 15

Read in the coefficients a, b, c of the quadratic equation ax2 + bx + c = 0.
Assume that a, b, c are real numbers. Then
 Print the values of a, b, and c read with the message “The coefficients of the equation are ….”
 If the roots are complex, print a message saying so, nothing else to be done
 Else, compute and print the roots of the equation with a message “The roots of the equation are…”. In addition, if the roots are equal, also print a message after printing the roots “The roots are equal”


Test with the following sets of data
 a = 16.0, b = 24, c = 9
 a = 4, b = 10, c = 4
 a = 4, b = 5, c = 4

 To do this assignment, you will need to also find square root. C provides a whole set of mathematical functions including square root (you will see other examples in theory)
 Here’s a small sample C statements to find square root of a real number x

double s;
s = sqrt(x);

 You may get a warning about x being not double if you have declared it as a float, ignore it for now, we will take care of it later

Solution
Problem 16

Read in the coordinates of three points (x1, y1), (x2, y2), (x3, y3)
 Name the variables as x1, y1, x2, y2, x3, y3

 Assume all coordinates are integers

 Check if the three points are collinear (lies on the same line)

 If yes,
Print a message saying “The points (..,..), (.., ..), (..,..) are collinear”, showing the three points If no (else part of the if statement),

 Compute the lengths of the three sides of the triangle formed by the points and store them in variables named side1, side2, side3

 Print the lengths of the three sides in a single printf statement with a message like “The lengths of the three sides are …”

 Compute the area of the triangle formed and store in a variable called area. Print the area with a nice message like “The area of the triangle is …”

 Print if the triangle formed is equilateral (all three sides have same length), isosceles (exactly two sides have same length), or neither equilateral nor isosceles (all three sides have different lengths)

 So print messages like “The triangle formed is equilateral” or “ The triangle formed is isosceles” or “ The triangle formed is neither equilateral nor isosceles”

Solution
Problem 17

Consider an integer X. Let Y be the set of all integers that divide X, including 1 but not including X. We want to check whether X is equal to the sum of the numbers in Y.
For example, if X = 6, then Y = {1, 2, 3}, and 1 + 2 + 3 = 6. But if X = 12, then Y = {1, 2, 3, 4, 6} and 1 + 2 + 3 + 4 + 6 = 16 ≠ 12

 Read in an integer X

 Print if X is equal to the sum of the numbers in Y as defined above or not (print a message in either case)

 Approach: In a for loop (till what?), find the numbers that divide X one by one starting from 1, and add to a running sum when you find one. After you come out of the for loop, check in an if-else statement for equality and print accordingly

 Do NOT use arrays even if you know it. You will get 0 if you use arrays

Solution
Problem 18

Consider a triangle ABC. Input lengths of the sides AB, BC, and CA from the user, using scanf statements. Compute and print

(i) the perimeter of the triangle ABC,
(ii) the square of the area of the triangle.
Use Heron’s formula to compute the square of the area

Solution
Problem 19

In this problem, you need to evaluate trigonometric functions like sin(x) and cos(x), using their respective series expansion formulas. For the series expansion formulas, refer to http://mathworld.wolfram.com/SeriesExpansion.html.
Input an angle x (in radians) from the user.

(a) Compute approximate values of sin(x) and cos(x) using the first four terms of their series expansion formulae

(b) Compute sin(2*x) = 2 * sin(x) * cos(x)

(c) Compute sin(2*x) directly using the first four terms of the series expansion formula for sin, and compare the value with that obtained in (b).

Your program should take x (in radians) as input, and then print out the values of sin(x), cos(x), and sin(2*x) computed by the two methods described above.

Solution
Problem 20

In this problem, you need to compute the derivative of a degree-5 polynomial of one variable x: a5x^5 + a4x^4 + a3x^3 + a2x^2 + a1x + a0

Assume that the coefficients (ai) of the polynomial are integers. Input the 6 coefficients from the user.
Print the polynomial, and then compute and print the derivative polynomial.
A sample input and output are given below; stick to the format shown below.

Enter coefficient of 1: 5
Enter coefficient of x: 2
Enter coefficient of x^2: -2
Enter coefficient of x^3: 7
Enter coefficient of x^4: 0
Enter coefficient of x^5:3
Polynomial: 3*x^5 + 7*x^3 - 2*x^2 + 2*x + 5
Derivative: 15*x^4 + 21*x^2 - 4*x + 2

Solution
Problem 21

Write a program in C to print your name, roll number, and department in three consecutive lines each.(You will be lucky to have this question in lab)

Solution
Problem 22

Write a program in C to take two integer numbers from the keyboard and print successively the following --
their sum, subtraction, multiplication, and division.

In case of division, print both the quotient and remainder. For convenience, take input the bigger number as the first integer and the smaller number as the second integer.

Solution
Problem 23

Write a program in C to compute the area and perimeter of a triangle whose sides a, b, and c are given by user as inputs.

Hint: You may use math.h header file and sqrt function, sqrt(9) = 3

Click Me
Problem 24

Write a program in C to input a 3 digit number x (100<= x <= 999) from the user and print the sum of its digits.

Solution
Problem 25

The sun rises at 6 AM in the morning and sets at 6 PM in the evening in a “Y” country throughout the year.
In semester breaks during the month of May 2019, your friend visited “Y”. During his stay at “Y”, he conducted an experiment, in which he placed a “X” ft pole in the middle of a football field nearer to his hotel.
Thereafter, he measured and recorded the length of the pole’s shadow, casted by sunlight, at 8 AM, 9 AM, 10 AM, 2PM, and 4PM. Your friend asked you to write a C program to find the length of the pole’s shadow for all the aforementioned time based on the given input: length of the pole (X).

Hint: tan(0)=0, tan(30)=0.577, tan(45)=1, tan(60)=1.732, tan(90)=undefined.
Draw a pen-and-paper diagram of the question to have a clear understanding.

Solution
Problem 26

Write a program to input the x and y coordinates of three vertices of a triangle.
Then, check and print if it is scalene, isosceles, or equilateral.
Take the three points as (x1, y1), (x2, y2) and (x3, y3).

Hint: You can assume the three points are valid points to form a triangle

Solution
Problem 27

Write a program to take as input three integers and find out the second largest among them.

Solution
Problem 28

Write a program to compute and print the taxi fare based on the following chart.
Total number of Kilometers traveled will be taken as input by the user as a floating point number.

(a) First 12 KM: Rs. 100/-
(b) Next 4 KM: Rs. 8 / KM
(c) Next 4 KM: Rs 6 / KM
(d) Above 20 KM: Rs 5 / KM

Solution
Problem 29

e wish to calculate the total cost of a flat. Flats available for sale are classified accordingly as follows, depending on their square feet,

ˆ A: 5000 sq. ft.
ˆ B: 4500 sq. ft.
ˆ C: 4000 sq. ft.
ˆ D: 3500 sq. ft.
ˆ E: 3000 sq. ft.
ˆ F: 2500 sq. ft.
The price per sq. ft is Rs 2000 if the distance of the flat is within 500 m of the city centre, else it is Rs. 1500. Write a program to find out the total cost of a flat by taking the distance of the flat from the city centre and the class of the flat as inputs.
Hint: You can use both IF/ELSE and SWITCH CASE statements

Solution
Problem 30

There is a course with two components: Theory and Lab. There are 70% and 30% weightage in the theory and lab evaluation respectively. A grade has to be prepared based on the total marks say X, which is the total marks obtained in theory and lab tests with the following break up.

ˆ EX: X ≥ 90
ˆ A: 80 ≤ X < 90
ˆ B: 70 ≤ X < 80
ˆ C: 60 ≤ X < 70
ˆ D: 50 ≤ X < 60
ˆ P: 40 ≤ X < 50
ˆ F: X < 40

Further, it is decided to award grace marks to some students based on their attendance, say Y, in the theory and lab classes, which is as follows.
(a) If Y ≥ 85% and marks in theory is ≤ 70%, then add 15% of the obtained theory marks to theory evaluation.
(b) If 75% ≥ Y ≥ 85% and marks in theory is ≤ 60%, then add 10% of the obtained theory marks to theory evaluation.
(c) If 60% ≥ Y ≥ 75% and marks in theory is ≤ 50%, then add 5% of the obtained theory marks to theory evaluation.

A total of 60 classes were held for theory and lab. There are two tests each of 100 marks. Input the marks of a student in theory and lab evaluation, and attendance count of the student. Calculate the grade that the student should deserve.

Write a program in C for the above. You should use only switch-case statements to implement condition and branching logic in your program.

Solution
Problem 31

Write a C program that takes as input two integer elements from the keyboard, adds them, and prints the sum on the terminal.

Solution
Problem 32

Write a program that takes as input the values of five floating-point variables: a, b, c, d, e, calculates the value of the expression ((a-b)*c)/(d+e), and prints the result on the terminal. Assume that d+e is non-zero.

Solution
Problem 33

Write a program that takes as input the (x,y) coordinates of two points in floating point, calculates the distance between them, and prints the result on the terminal.

You should include math library for square root function as follows:
#include
Example Runs:
Enter the coordinates of 1st point: 0.5 -0.5
Enter the coordinates of 2nd point: 3.5 3.5
Distance = 5.000000
Enter the coordinates of 1st point: -5 7
Enter the coordinates of 2nd point: 9 -11
Distance = 22.803509

Solution
Problem 34

User supplies a 3-digit number. Your program should reverse the digits and print the resultant number on the terminal.

Example runs:

Enter a 3-digit number: 567
Reverse number = 765

Enter a 3-digit number: 333
Reverse number = 333

Enter a 3-digit number: 120
Reverse number = 21

Solution
Problem 35

Write a program to perform the following for a user who wants to purchase wood:
Ask the user for the type of wood ('n' for natural wood, 's' for synthetic wood).
Ask for the weight he wants.
Compute and print the base price.
Compute and print the GST.
Compute and print the total price.
All prices should have two digits in the decimal place (use %10.2f format for this).
Assume that price of natural wood is Rs. 1100/kg, that of synthetic one is Rs. 780/kg, and the respective GSTs are 20% and 15% on the base price.
Enter the type of wood (n for natural wood, s for synthetic wood): n
Enter the weight of wood (in Kg.) you want: 7.25
-------------------------
Base price = 7975.00
GST = 1595.00
Total price = 9570.00
-------------------------
Enter the type of wood (n for natural wood, s for synthetic wood): n
Enter the weight of wood (in Kg.) you want: 13.12
-------------------------
Base price = 14432.00
GST = 2886.40
Total price = 17318.40
-------------------------

Solution
Problem 36

Write a program to find the real or complex roots of ax2 + bx + c = 0, where a, b, c are real and given as input by the user.
You can use sqrt or sqrtf function of math library (invoke #include math.h) for computing the determinant.

Enter a, b, c in order: 2 0 1
Complex roots = -0.000000+0.707107i, -0.000000-0.707107i

Enter a, b, c in order: 1 0 -1
Real roots = 1.000000, -1.000000

Enter a, b, c in order: 1 4 2
Real roots = -0.585786, -3.414214

Enter a, b, c in order: 2 0 -2
Real roots = 1.000000, -1.000000

Enter a, b, c in order: 3.5 7.21 1.9
Real roots = -0.310248, -1.749752

Enter a, b, c in order: 7.21 3.5 1.9
Complex roots = -0.242718+0.452339i, -0.242718-0.452339i

Enter a, b, c in order: 1.9 3.5 7.21
Complex roots = -0.921053+1.716508i, -0.921053-1.716508i

Solution
Problem 37

Given a year as input, your program should print whether it is a leap year. You can use if, else-if, etc. in your program.
(Note: A year is a leap year if it is divisible by 4 but not by 100. However, if it is divisible by 400, then also it is a leap year.)

Enter the year: 2021
Year 2021 is not a leap year.

Enter the year: 2020
Year 2020 is a leap year.

Enter the year: 2000
Year 2000 is a leap year.

Enter the year: 2100
Year 2100 is not a leap year.

Solution
Problem 38

In an automobile company, the number of cars manufactured on a day depends on which day of the week it is.
Monday means Day 1, Tuesday means Day 2, ..., Sunday means Day 7.
Let n be the number of cars manufactured on a day.
For Day 1, n is given by the number of available machines (say m, which is a user input).
For Day 2, n is 7m/4, rounded off to the nearest integer.
For Day 3 and Day 6, k machines (k is user input) are kept out for maintenance, and so n becomes 7(m-k)/4, rounded off to the nearest integer.
For the other days, n is m plus a boosting factor f of m, rounded off to the nearest integer; assume that f is a positive real number less than 1 and taken as user input.
Given the week day (1 to 7) as input, find the number of manufactured cars (n) on that day.
You should use switch-case statements.

Enter the number of machines: 127
Enter the day number (1-7): 3
Enter no. of machines under maintenance: 25
Number of manufactured cars = 179.

Enter the number of machines: 127
Enter the day number (1-7): 6
Enter no. of machines under maintenance: 14
Number of manufactured cars = 198.

Enter the number of machines: 127
Enter the day number (1-7): 5
Enter boosting factor: .35
Number of manufactured cars = 171.

Enter the number of machines: 95
Enter the day number (1-7): 4
Enter boosting factor: 0.35
Number of manufactured cars = 128.

Solution
Problem 39

Write a program to do the following.
- Declare three integer variables.
- Take their values as input from the user during execution.
- Add the first two and print the result on the terminal.
- Divide the last result by the third and print the quotient on the terminal.
- Print also the remainder of the division.

Solution
Problem 40

Write a C program that takes as input the radius and height of a right-circular cone (in the two-dimensional real plane) and prints its volume. (Define pi as 3.1416).

Solution
Problem 41

Write a C program that takes the marks of a student in English, Mathematics, Physics, Chemistry, and Computer Science as input. Then it computes the total and the percentage of marks, and prints them on the terminal. Consider full marks of each subject to be 50.

Solution
Problem 42

Suppose you deposited some amount of money in a bank and you want to know the amount of interest that will be incurred on the money and also the total amount that you will receive from the bank after a certain amount of time. Write a C code that first reads the principal amount of money you deposited initially, the rate of interest the bank gives, and the period of time over which you want to keep the money in the bank. Based on this input, it computes and prints the incurred interest and the total amount. (use proper units).

Solution
Problem 43

Write a C program that takes two integer numbers as input in two variables A and B, and prints their contents. Next, it exchanges (swaps) the contents of A and B and then again prints their contents.

Solution
Problem 44

Write a C program which reads 4 integers from the user and finds the maximum integer and second maximum integer among them using only "if-else" statements.
Example •
(Note: do not use any loop)
-Output----------------
Give four integer: 30 10 20 40
Maximum Integer: 40
Second maximum: 30

Solution
Problem 45

Write a C program that reads two positive integers, a and b.
Then it finds which one is larger, and decides whether the larger is divisible by the smaller.
It should finally print in the terminal which one divides (or not) which one .

Example 1
Enter two positive integers: 18 12
Result: 12 does not divide 18.

Example 2
Enter two
positive integers:6 18
Result: 6 divides 18.

Solution
Problem 46

Write a C program that asks user for an arithmetic operation (I-addition, 2-subtraction, 3-multiplication, 4-division) and two operands.
Then it performs the corresponding calculations on the operands and prints the result.
[Hint: Use switch case statements]
---------Output---
Add-I
Sub-2
Mult-3
Div-4
Enter the choice: 1
Enter the two operands: 4 6
Result=10

Solution
Problem 47

Write a program to perform the following for a user who wants to purchase wood:
i) Ask the user for the type of wood ('n' for natural wood, 's' for synthetic wood).
ii) Ask for the weight he wants.
iii) Compute and print the base price.
iv) Compute and print the GST.
v) Compute and print the total price.
All prices should have two digits in the decimal place (use %10.2f format for this).
Assume that price of natural wood is Rs. 1100/kg, that of synthetic one is Rs. 780/kg, and the respective GSTs are 20% and 15% on the base price.
Enter the type of wood (n for natural wood, s for synthetic wood): n
Enter the weight of wood (in Kg.) you want: 7.25
Base price = 7975.00
GST= 1595.00
Total price = 9570.00

Enter the type of wood (n for natural wood, s for synthetic wood): n
Enter the weight of wood (in Kg.) you want: 13.12
Base price = 14432.00
GST= 2886.40
Total price = 17318.40

Solution