0% found this document useful (0 votes)
5 views9 pages

C++ Programming Exercises and Solutions

Uploaded by

Abanoub ehab
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

C++ Programming Exercises and Solutions

Uploaded by

Abanoub ehab
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Structural Programming (C++)

Q1) Write a C++ Program that get a number from the user then
print whether the number is positive, negative or zero in a
proper form.

Answer:

In case that the statements in if condition is only one line ->


no need to write {}, as following:
Q2) Write a C++ Program that get the gender and specialty
from the user then print the salary corresponding to the
gender and specialty as following in the table:

Gender Specialty Salary


IT 40k
Front end 30k
Male
Back end 30k
AI 50k
IT 38k
Front end 35k
Female
Back end 35k
AI 45k

Answer:

We combined Specialty (Front end & Back end) in same condition


since they have same salary.
We use getline in case of getting input string with blank spaces or in
multi-string inputs.
We can optimize the previous code using nested if as
following:
Q3) Write a C++ Program that get the grade as a character
from the user (A, B, C, D, or F) then print the message
corresponding to his grade.

Answer:

We combined case 'A' and 'a' by not using break, so if the user enters
'A' or 'a', they will return same result.
Q4) Write a C++ program that asks the user to enter a month
(1–12) and then uses a switch statement to print how many
days are in that month.

Answer:
Q5) Write a C++ program that asks the user to enter a grade
(A, B, C, D, F and + or – or empty) and then uses a switch
statement to print his GPA.
 “For grades with a plus (+), add 0.3 to the GPA, except for
grades ‘A’ and ‘F.’
 For grades with a minus (–), subtract 0.3 from the GPA,
except for ‘F’.”
Answer:
Q1) Write a C++ Program that get a number from the user then print whether
the number is even or odd in a proper form.

Q2) Write a C++ Program that ask the user to enter a score from the user then
print its corresponding grade according to the following table:
Score range Grade
< 50 Fail
50 - 64 Pass
65 - 74 Good
75 - 84 Very Good
> 84 Excellent

Q3) Write a C++ Program that get a price of a product from the user then print
the price after discount that equals 10% if the price is larger than 1000, else
it will print the same price.

Q4) Write a C++ program that asks the user to enter a letter grade (such as
A, B, C, D, or F) and a sign (+, -, or none).
- The program should first assign the base GPA value for the letter grade,
then adjust it according to the sign as follows:
- If the sign is ‘+’, add 0.3 to the GPA.
- If the sign is ‘–’, subtract 0.3 from the GPA.
- If there is no sign, keep the GPA as it is.
- For the grades A and F, the GPA does not change regardless of the sign.
(i.e., A+ = A = 4.0, and F+, F, F– = 0.0).
- Finally, the program should display the calculated GPA value.
- If the user enters an invalid grade or sign, print an appropriate error
message.
- Example, if the input is (B +) then output is (3.3).
Q5) Write a C++ program that asks the user to enter a number from 1 to 7,
then uses a switch statement to print the corresponding day of the week.

Q6) Write a C++ program that asks the user to enter two numbers and an
operator (+, -, *, /), then uses a switch statement to perform the
corresponding operation and display the result.

Q7) Repeat Question 2 (the simple calculator), but this time use if statements
instead of a switch statement to perform the operations.

You might also like