C++ Assesment
C++ Assesment
C++ Assesment
SECTION 5
NAME: BEDASA ADMASU
ID NO RU 1116/14
1|Page
1.Write a statement (or comment) to accomplish each of the following:
Read three integers from the keyboard and store them in the variables x, y
and z.
Print "The product is " followed by the value of the variable result.
#include<iostream>
int main(){
int x, y,z,result;
result = x * y * z;
return 0;
2|Page
2.Write a program that accepts two integers and display the sum, difference,
product and division of the two numbers. The program should also state the
greater and smaller number.
#include <iostream>
int main() {
3|Page
if (num1 > num2) {
cout << num1 << " is greater than " << num2 << endl;
cout << num2 << " is smaller than " << num1 << endl;
cout << num1 << " is smaller than " << num2 << endl;
cout << num2 << " is greater than " << num1 << endl;
} else {
cout << num1 << " and " << num2 << " are equal" << endl;
return 0;
#include <iostream>
#include <cmath>
int main() {
double radius ;
double circumference;
4|Page
cout << "Enter the radius of the circle: ";
circumference = 2 * pi * radius;
cout << "The circumference of the circle is: " << circumference << endl;
return 0;
#include <iostream>
#include <cmath>
int main() {
double a, b, c;
5|Page
cout << "Enter the coefficients (a, b, c) of the quadratic equation: ";
// Calculate discriminant
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
} else if (discriminant == 0) {
root1 = -b / (2 * a);
} else {
// Complex roots
cout << "Root 1 = " << realPart << " + " << imaginaryPart << "i" << endl;
cout << "Root 2 = " << realPart << " - " << imaginaryPart << "i" << endl;
return 0;
6|Page
}
5.. Suppose x = 3 and y = 2; show the output, if any, of the following code.
What is the output if x = 3 and y = 4? What is the output if x = 2 and y = 2?
if (x > 2) {
if (y > 2) {
int z = x + y;
cout << "z is " << z << endl;
}
}
else
cout << "x is " << x << endl;
Scenario 1: x = 3 and y = 2
In this case, the condition (x > 2) evaluates to true since 3 is greater than 2.
Inside the first if statement, the condition (y > 2) evaluates to false since 2 is
not greater than 2. Therefore, the code inside the second if statement is not
executed. Since there is no code inside the else block, nothing is outputted.
Output: No output.
Scenario 2: x = 3 and y = 4
Again, the condition (x > 2) evaluates to true. Inside the first if statement,
the condition (y > 2) evaluates to true since 4 is greater than 2. Therefore,
the code inside the if statement is executed. The variable z is assigned the
value of x + y, which is 3 + 4 = 7. Then, the output statement cout << "z is " <<
z << endl; is executed, resulting in the output:
7|Page
Scenario 3: x = 2 and y = 2
This time, the condition (x > 2) evaluates to false since 2 is not greater than
2. Therefore, the code inside the first if statement is not executed. Instead,
the code inside the else block is executed. The output statement cout << "x is
" << x << endl; is executed, resulting in the output:
8|Page
9|Page
6.Write a program to accept any character from keyboard and
display whether it is vowel or not.
#include <iostream>
int main() {
char character;
character = tolower(character);
} else {
return 0;
10 | P a g e
> 95 –> A+
85 - 94 –> A
80 - 84 –> A-
75 - 79 –> B+
70 - 74 –> B
65 - 69 –> B-
60 - 64 –> C+
50 - 59 –> C
<50 – F
#include <iostream>
int main() {
int marks;
11 | P a g e
cout << "Grade: B" << endl;
} else {
return 0;
#include <iostream>
int main() {
cout << "The greatest number is: " << num1 << endl;
12 | P a g e
} else if (num2 >= num1 && num2 >= num3) {
cout << "The greatest number is: " << num2 << endl;
} else {
cout << "The greatest number is: " << num3 << endl;
return 0;
#include <iostream>
int main() {
int sum = 0;
sum += i;
cout << "The sum of numbers from 1 to 100 is: " << sum << endl;
13 | P a g e
return 0;
#include <iostream>
int main() {
if (i % 2 == 0 && i % 3 == 0 && i % 5 == 0) {
return 0;
14 | P a g e
}
#include <iostream>
int main() {
int num;
int factorial = 1;
factorial *= i;
cout << "Factorial using for loop: " << factorial << endl;
factorial = 1;
int i = 1;
15 | P a g e
while (i <= num) {
factorial *= i;
++i;
cout << "Factorial using while loop: " << factorial << endl;
factorial = 1;
do {
factorial *= i;
++i;
cout << "Factorial using do-while loop: " << factorial << endl;
return 0;
12. Write a while loop that prints the average of numbers from 1
to 10 .
#include <iostream>
int main() {
16 | P a g e
int sum = 0;
int count = 0;
int num = 1;
sum += num;
++num;
++count;
cout << "The average of numbers from 1 to 10 is: " << average << endl;
return 0;
Reading the remainders from the last division upwards, the integer part of
25 in binary is 11001
To convert the fractional part, we can repeatedly multiply the fraction by 2 and
take the integer part of the result until we reach 0 or the desired level of
precision.
17 | P a g e
0.625 * 2 = 1.25 (integer part: 1)
0.25 * 2 = 0.5 (integer part: 0)
0.5 * 2 = 1.0 (integer part: 1)
Reading the integer parts from the first multiplication upwards, the fractional
part of 0.625 in binary is 101.
To convert the binary number 11.011 to decimal, we need to consider both the
integer and fractional parts.
: The leftmost digits before the decimal point represent the integer part. In this
case, it's 11. The binary value of 2+1=3
: The digits after the decimal point represent the fractional part. In this case, it's
011. To convert this to decimal, we divide each digit by the corresponding power
of 2.
18 | P a g e