7/29/2019 CS106 PS3
http://slidepdf.com/reader/full/cs106-ps3 1/4
CS106 Programming Fundamentals
Submission Instructions• Submission by email to [email protected]
– Subject of the email should be
– CS106-PS3-YourName-RegNo
– No collaboration allowed.
– Due Date : 12th Nov 9 am
– You should send cpp files with names Task1.cpp, Task2.cpp … in onezip file
Problem Set 3 (Loops and Iterations)
Task 1
Suppose that there is a class of 30 students. You are required to make a program to
calculate the number of As, Bs,Cs and Fs where A,B,C and F are the only grade
options. You must remember that you cannot declare thirty different variables for
the grades of the students, rather you have to use loops to take input from the user.
The output should be properly formatted using setw etc.
Task 2
Make a program with the ability to print first 20 odd numbers, even numbers or
prime numbers based on the input from the user. Your program should have the
following menu1) Press 1 to generate first 20 even numbers
2) Press 2 to generate first 20 odd numbers
3) Press 3 to generate first 20 prime numbers
Note that you have to use loops in the program. Furthermore, knowledge of
arithmetic series can help in generating odd and even numbers
Task 3
Given a number find all the factors of the number. Factors of a number x are the
numbers which one multiplied by some other number result in x. For example the
factors of 12 include 1,2,3,4,6,12. ( Hint: if y is a factor of x then x remainder y is
equal to zero)
Task 4
Make a program that repeatedly asks the user to enter three different values and
checks whether the values can represent length of sides of triangle. If hyp
represents length of hypotenuse of the triangle, base represents length of base of the
triangle and alt represents the length of altitude of the triangle then hyp2=base2 +
alt 2
7/29/2019 CS106 PS3
http://slidepdf.com/reader/full/cs106-ps3 2/4
You have to take care of two more things in this program that the user may not
enter the values in any order implying that in one case the user may first enter the
value of base first while in the other case he may enter the value of altitude first.
Furthermore, after every check you have to ask the user whether he wants to enter
another value or not. If the user enters y then you should again take the input and
apply the checks.
Task 5
Make a program that calculates the sum of n numbers where n and the numbers are
all going to be entered by the user
Bonus Questions
Make a program to generate first n numbers of the Fibonacci Series using loops. Nth
number of the Fibonacci Series can be generated as per the following formula
() { ( ) ( )
7/29/2019 CS106 PS3
http://slidepdf.com/reader/full/cs106-ps3 3/4
Tips &Tricks/ Advice/Recommendations/Additional References
Advice (taken from Section 4.10 of Bjarne Stroustrup, “C++ Programming
Language,” 3rd Edition, 2000.)
1. Keep scopes small.
2. Don’t use the same name in both a scope and an enclosing scope.
3. Declare one name (only) per declaration.
4. Keep common and local names short, and keep uncommon and nonlocal
names longer.
5. Avoid similar-looking names.
6. Maintain a consistent naming style.
7. Choose names carefully to reflect meaning rather than implementation.
8. Use a typedef to define a meaningful name for a built-in type in cases in
which the built-in type used to represent a value might change.
9. Use typedef s to define synonyms for types; use enumerations and classes to
define new types.
10. Remember that every declaration must specify a type (there is no ‘‘implicit
int ”)
11. Avoid unnecessary assumptions about the numeric value of characters.
12. Avoid unnecessary assumptions about the size of integers.
13. Avoid unnecessary assumptions about the range of floating-point types.
14. Prefer a plain int over a short int or short or a long int or a long.
15. Prefer a double over a float or a long double.
16. Prefer plain char over signed char and unsigned char .
17. Avoid making unnecessary assumptions about the sizes of objects.
18. Avoid unsigned arithmetic.
19. View signed to unsigned and unsigned to signed conversions with suspicion.
20. View floating-point to integer conversions with suspicion.
21. View conversions to a smaller type, such as int to char , with suspicion.
For detail on these advices, please read the relevant sections in the above
mentioned book.
7/29/2019 CS106 PS3
http://slidepdf.com/reader/full/cs106-ps3 4/4
Another important thing, develop habit to declare and initialize the variable
at the same time.