Programming Exercises 1: Basic Programming
1. Write a program that prints ‘Welcome to C++ programming course’.
2. Write a program that prints
‘*************************************’
‘Faculty of Electrical Engineering’
‘**************************************’.
3. Given the integer variables a=2, b=5, c=7 and floating point variables x=10.0, y=10.5, z=20.0. Use type cast to obtain accurate answers for each of the following expressions:
a) y+a/b b) a+b(z/a) c) x+(b/a)c
4. Write a program that reads positive distance (km) and time (s), then print out the speed in km/s.
5. Write a program that reads voltage and resistance values, then print out the resulting current using Ohm’s law.
6. Write a program that convert the number read as degrees to radians using 1 radian = 57.295779 degrees, correct to three decimal places.
7. Write a program that convert the number (float) read as degrees Fahrenheit (F) into Celsius (C) using C = (100/180)*(F-32)
8. Write a program that calculates the area and perimeter of a right angle triangle with length and width supplied by the user.
9. Write a program that reads positive integer m and n and then prints m to the power of n .
10. Write a program that reads an integer and then prints its square root.
Programming Exercises 2: Selection/Decision Making
1. Write a program that reads five integer numbers from the keyboard, and display the largest and smallest of the numbers entered.
2. Write a program that reads a list of test scores and prints the test scores together with the letter grades according to the following scale:
90 – 100 A, 70 – 79 C, 00 – 59 F,
80 – 89 B, 60 – 69 D,
3. Write a program that generates two random numbers between 0 and 100 and ask user to provide the correct answer if the numbers are added together. If the answer given is wrong, print ‘sorry, better luck next time’ and exit, if correct, congratulates the user and exit. Create the random number using rand( ). Hint: To make the random number change randomly, use the srand(time(0)) function to create a new seed for the rand( ) function.
4. Write a program to compute the real roots of a quadratic equation of the form ax2 + bx + c = 0. The roots x1 and x2 are calculated using:-
x1= (-b+sqrt((bb)-4ac))/2a and x2=(-b-sqrt((bb)-4ac))/2a
The program should prompt the user to enter the constants a, b, c. The roots will be displayed according to the following rules:-
a) If both a and b are zero, there is no solution. Display “No solution”.
b) If a is zero, there is only one root, i.e. –c/b. Display the root.
c) If (b2 – 4ac) is negative, there are no real roots. Display “Roots are imaginary”.
d) For all other combinations, there are two real roots. Display the roots x1 and x2.
Test the program by using a, b, c as follows:-
i) 3, 8, 5 ii) -6, 7, 8 iii) 0, 9, -10 iv) 0, 0, 11
5. Write a program using switch statement that will display the value of the colour used in the resistor colour code. The user will enter a single character representing the colour. Use the following codes:
B for black - 0 G for green - 5
W for brown - 1 L for blue - 6
R for red - 2 V for violet - 7
O for orange - 3 E for grey - 8
Y for yellow - 4 H for white - 9
6. A triangle of sides a, b, and c can be categorized as ‘equilateral’, isosceles’ and ‘scalene’. A triangle can be drawn only if a+b > c and b+c > a and a+c > b.
A triangle is equilateral when a = b and b = c.
A triangle is isosceles when a = b or b = c or c = a.
A triangle is scalene if a != b and b != c and c != a.
Write a program to accept an input a, b, c as the triangle sides, and then prints out whether the triangle is ‘equilateral’, isosceles’ or ‘scalene’.
Programming Exercises 3: Loops/Iteration
1. Write a program that displays all the multiplication table for numbers 1 to 10.
2. Write a program that asks the user for the multiplication table that he wants, and then the program will display the multiplication table. The program will terminate if the user enter ‘0’.
3. Write a program to find the sum of digits of a number given by the user. That is, if the user enters 456, then the sum is 15. The program will terminate if the user enter ‘0’.
4. Write a program to read 10 integers from user, and print the sum and average of the numbers.
5. Generate and print 20 random numbers in the range of 6 to 15. To seed the random number, use srand(time(0)), which is available in libraries cstdlib and ctime. The random number can then be generated using rand().
6. Write a program to read n integers from user, and print the sum and average of the numbers. The program will stop reading input when user enters -99.
7. A certain system measure the force P expressed as a function of time t by the formula P = 4 (t – 3) + 20. Write a program to tabulate P for t = 0 to 10.
8. Write a program to graph the function y= 40x/(1+x^2) on the interval from 0 to 2 in increments of 0.1. Print a number of stars proportional to y for the requested values of x.
9. Write a program to graph the function y = sin (x) on the interval from 0 to π in increments of π/20. Print a number of stars proportional to y for the various values of x. The function sin (x) is available as a library function in cmath.
10. Write a program that finds the equivalent series and parallel resistance for a collection of resistor values. The program should read all the resistor values and then compute the equivalent total series and parallel resistance values. Use value ‘0’ to signal the end of program data.
11. Write a program that displays all the prime numbers between 0 and 1000. Prime numbers are integers that are divisible only by 1 and itself. A few prime numbers are 2, 3, 5, 7, 11, ...
12. Write a program that convert a decimal number into any base of the user choice, as an example octal, hexadecimal or binary.