Programming Lab 1
Programming Lab 1
A. If the cost price and selling price of an item is input through the keyboard, write a
program to determine whether the seller has made a profit or incurred a loss. Also,
determine how much profit he made or the loss he incurred. (if-else and ternary
operator)
B. Any integer is input through the keyboard. Write a program to find out whether it is an
odd number or even number. (if-else and ternary operator)
C. Any year is input through the keyboard. Write a program to determine whether the year
is a leap year or not. (Hint: Use the % (modulus) operator) (if-else and ternary
operator)
D. A five-digit number is entered through the keyboard. Write a program to obtain the
reversed number and to determine whether the original and reversed numbers are equal
or not. (if-else and ternary operator)
E. If the ages of Ram, Shyam, and Ajay are input through the keyboard, write a program to
determine the youngest of the three. (if-else and ternary operator)
F. Write a program to check whether a triangle is valid or not, when the three angles of the
triangle are entered through the keyboard. A triangle is valid if the sum of all three
angles is equal to 180 degrees. (if-else and ternary operator)
G. Find the absolute value of a number entered through the keyboard. (if-else and ternary
operator)
H. Given the length and breadth of a rectangle, write a program to find whether the area of
the rectangle is greater than its perimeter. For example, the area of the rectangle with
length = 5 and breadth = 4 is greater than its perimeter. (if-else and ternary operator)
I. Given three points (x1, y1), (x2, y2), and (x3, y3), write a program to check if all the three
points fall on one straight line. (if-else and ternary operator)
J. Given the coordinates (x, y) of a center of a circle and its radius, write a program that will
determine whether a point lies inside the circle, on the circle, or outside the circle. (Hint:
Use sqrt( ) and pow( ) functions) (if-else and ternary operator)
Switch Case:
K. Program to build a simple calculator with 6 functions using switch Statement. Functions
are Addition, Subtraction, Multiplication, division, square, and selecting the last one by
yourself.
L. Write a program which to find the grace marks for a student using a switch. The user
should enter the class obtained by the student and the number of subjects he has failed
in.
- If the student gets first-class and the number of subjects he failed in is greater
than 3, then he does not get any grace. If the number of subjects he failed in is less than
or equal to 3 then the grace is 5 marks per subject.
- If the student gets second class and the number of subjects he failed in is
greater than 2, then he does not get any grace. If the number of subjects he failed in is
less than or equal to 2 then the grace is 4 marks per subject.
- If the student gets third class and the number of subjects he failed in is greater
than 1, then he does not get any grace. If the number of subjects he failed in is
equal to 1 then the grace is 5 marks per subject.