Problems On Control Flow Statements
Problems On Control Flow Statements
Problem statement:
User will be prompted to provide coefficients a, b, c.
The program will give solution of the given equation along with the comment on type of equation /
roots
Possible types:
Abnormal cases :
1. Linear equation (a = 0, b, c != 0)
2. Equation with infinite roots ( a= b = c = 0)
3. Inconsistent equation (a, b = 0, c!=0)
Normal cases:
Hints:
1. sqrt() function is available in ‘math’ library, you may have to use it.
2. You may use ‘multiple ifs ladder’ or ‘if else if ladder’
3. In main you are allowed to use ‘return’ statement at multiple places and program immediately
stops when program flow arrives at any of the return statements.
4. Try to bring output in exactly same format as specified in the documentation
Test cases:
=============================================================================
This program solves quadratic equation aX^2 + bX - C = 0
Give values of a, b, c : 0 0 0
Infinite Solutions.
=============================================================================
This program solves quadratic equation aX^2 + bX - C = 0
Give values of a, b, c : 0 0 5
Inconsistent equation.
=============================================================================
This program solves quadratic equation aX^2 + bX - C = 0
Give values of a, b, c : 0 4 5
=============================================================================
This program solves quadratic equation aX^2 + bX - C = 0
Give values of a, b, c : 1 5 2
=============================================================================
This program solves quadratic equation aX^2 + bX - C = 0
Give values of a, b, c : 2 4 2
=============================================================================
This program solves quadratic equation aX^2 + bX - C = 0
Give values of a, b, c : 2 1 2
Complex roots :
x1 = -0.250000 + 0.968246i
x2 = -0.250000 - 0.968246i
=============================================================================
Assignment 2: Guess my number
Problem statement:
In this game, computer holds a random number in its memory (say between 1 to 100) .
The user attempts to guess the number.
Every time the user submits his guess, computer will give feedback to user, by stating
Your guess is larger than the number you are predicting
OR
Your guess is smaller than the number you are predicting
This will continue till the user’s guess does not match with the actual number.
When the users guess is correct, program will terminate with congratulations message.
Computer will also mention number of attempts user took to guess the correct number.
Hints:
1) Computer can generate random number. Find out the library function in C that helps this.
2) How do we generate random number within a given range (Say 1 to 100)
Sample run:
Welcome! I have a number between 1 to 100, can you guess it?
Hints:
1) You will have to decide a strategy to guess the next number. You may base it on the strategy you
used while acting as user in the previous assignment.
2) Response from the user has to be considered while guessing the next number
3) How will you ensure that user will always respond L / S / E? You may have to trap him in a loop till
he gives a valid reply.
Sample run: (Number in my mind 64)
Hold a number in your mind. Number should be between 1 to 100 (both included).
Hints:
1) Nested loops will have to be used
2) In nested loops the inner look works faster and outer loop moves slower
3) Use printf formatting to ensure proper alignments of numbers in the table
Sample run:
11 12 13 14 15
22 24 26 28 30
33 36 39 42 45
44 48 52 56 60
55 60 65 70 75
66 72 78 84 90
77 84 91 98 105
88 96 104 112 120
99 108 117 126 135
110 120 130 140 150
171 172 173 174 175 176 177 178 179 180
342 344 346 348 350 352 354 356 358 360
513 516 519 522 525 528 531 534 537 540
684 688 692 696 700 704 708 712 716 720
855 860 865 870 875 880 885 890 895 900
1026 1032 1038 1044 1050 1056 1062 1068 1074 1080
1197 1204 1211 1218 1225 1232 1239 1246 1253 1260
1368 1376 1384 1392 1400 1408 1416 1424 1432 1440
1539 1548 1557 1566 1575 1584 1593 1602 1611 1620
1710 1720 1730 1740 1750 1760 1770 1780 1790 1800