Class Exercise on IF … else…
Part I
1. Write a program to enter a number and check if it is digit or not.
2. Write a program to enter a number and check if it is two digited or not.
3. Write a program to enter a number and check if it is odd or even
4. Write a program to enter two numbers a and b and check if a is divisible by b or not.
5. Write a program to enter two numbers and print the larger number
6. Write a program to enter any character and check if it is digit or not.
7. Write a program to enter age of a student and check if he/she is eligible to vote or not.
8. Write a program to enter an year and check if it is leap year or not. (condition year should be
divisible by 4 and not by 100 , or the year should be divisible by 400)
9. Write a program to enter marks of a student in five subject ( assume each marks are out of
100) and check if the percentage obtained is more than 80 or not.
10. Write a program to enter a two digited number and check if sum of digits is equal to product
of the digits or not. ( for eg. If n= 25 then check if 2+5 = 2*5 or not)
11. Write a program to enter radius of circle and check if its area is equal to its perimeter or not.
12. Write a program to enter a character and check if it is in uppercase or lower case.
13. Enter three angles of a triangle and check if they form a triangle or not.
Part II
14. Write a program to calculate simple interest, ask user to enter amount and time, if time
>=10 then take rate of interest as 20% otherwise take rate of interest 15%
15. Write a program to enter a character and check if the character is alphabet, digit or special
character.
16. Write a program to enter two number a and b. If a is larger print “a larger”, if b is larger print
“b larger”, if both are equal print “Equal”
17. Write a program to enter the values of a,b and c and calculate the roots of quadratic
equation.
18. Write a program to enter a string and print equal if the first and last character of the string
are equal otherwise print “not equal”.
19. Write a program to enter name of a student and check if the name starts with capital vowel
or not.
20. Write a program to enter a string and print if the len of the string is odd or even. (length of
string is calculated using len(“string”) function, means if st=”Hello” then len(st) will give
length of string.
21. Write a program to enter a number, if the number is odd print its square, if number is even
print its cube.
22. Write a program to enter a three digited number , reverse the number and check if it is
palindrome (reverse of number is same as original number ) or not.
for eg if num = 323 rev_num = 323 , so it is palindrome
if num = 123 rev_num = 321 , so it is not palindrome
23. Write a program to enter any number and Print “ends with 7” if it end’s with seven, or print
“divisible by 7” if it isdivisibleby 7, or print “None”, if none of the two is true.
Gor eg. : if num = 127 it prints “Ends with 7”
If num = 49 it print “Divisible by 7”
If num = 423 it print “None”
Part III
24. Write a program to enter three numbers and find the largest and display.
25. Write a program to enter a month number from 1-12 and print it in words, if month number
is not in the range 1 to 12 print “invalid month”.
For eg. (If input num= 12 output is December, if input = 15 output is invalid
26. Write a program to enter a week number from 1 to 7 and print the equivalent week day , 1-
Monday and 7 for Sunday and print “Invalid” if number is not between 1 to 7.
27. Write a program to enter a character and print if it is uppercase , lower case, digit or special
character.
28. Write a program to enter your date of birth and check if it is a valid date or not.
29. Write a program to enter three numbers and print them in ascending and descending order.
30. Write a program to enter five numbers and print the sum of all unique numbers from the
five numbers.
For eg. : if a,b,c,d,e = 1,2,1,3,2 then output will be 6 (since 1 and 2 are duplicates they will
be considered only once.
31. Write a program to enter your date of birth and print it in the following format. (if data of
birth is dd = 12, mm= 2 and yy= 2003, then output should be 12th February 2003.
32. Write a program to enter marks of a student in 3 subjects, find percentage assuming each
marks are out of 100. Now find the grade of student using following criteria:
If marks >=90 grade =”A”
If marks <90 and marks >=80 grade =”B”
If marks <80 and marks >=65 grade =”c”
If marks <65 and marks >=50 grade =”D”
If marks <50 and marks >=35 grade = “E”
If marks <35 grade =”F”
33. Write a program to calculate commission of a salesman after entering the sales of the sales
considering following criteria
Sales >=30000 comm= 30 %
Sales <30000 and >= 20000 comm =20%
Sales<20000 and >=10000 comm=10%
Sales<10000 Nil
34. Write a program to caluculate BMI of a person after inputting its weight in kgs and height in
meters. The nutritional status is
BMI Result
<=18.5 “underweight”
18.5 - 24.9 “Normal”
25 – 29.9 “Overweight”
>=30 “Obese
wt
Formula = BMI = 2
h
Where wt – weight in kg and h – height in meter
35. Write a program to enter a number from 1-9 and print in words , print invalid otherwise.
36. Write a program to enter a two digited number and print it in words, print invalid if not a
two digited number.
For eg. If n= 12 o/p is Twelve
If n= 23 o/p is Twenty Three
37. Write a program to enter a three digited words and print It in words , print invalid if not a
three digited number.
For eg. If n= 234 o/p is Two Hundred and thirty four
If n= 106 o/p is One hundred and five
Part IV
Book questions using If
For CS Students :
Type C 1-7, 12,13
For IP Students :
Type C 1-6
38. Write a program for simple calculator.
39. Write a menu driven program to find area of circle, area of rectangle and area of square.
40. Write a menu driven program for temperature conversion (F to C and C to F)
41. Write a menu driven program for money conversion(rupees to pounds, dollar, dinar)
42. Write a program to enter a character and check if the entered character is vowel or
consonant.
43. Ask user to enter age, sex ( M or F ), marital status ( Y or N ) and then using following rules
print their place of service.
if employee is female, then she will work only in urban areas.
if employee is a male and age is in between 20 to 40 then he may work in anywhere
if employee is male and age is in between 40 t0 60 then he will work in urban areas only.
And any other input of age should print "ERROR".
44. Write a program to enter a number and check if it divisible by 3 and 5 both or not.
45. Write a program to enter a date in three variables d, m and y and enter any number
n(assume as number of days), now add this number n to this date and display the new date.
(Tricky one)