C++ Assesment

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

JIMMA UNIVERSITY

JIT INSTITUTE OF TECHNOLOGY PRE-ENGINERING

COMPUTER PROGRAMMING INDIVIDUAL ASSIGNMENT

SECTION 5
NAME: BEDASA ADMASU
ID NO RU 1116/14

1|Page
1.Write a statement (or comment) to accomplish each of the following:

State that a program calculates the product of three integers.

Declare the variables x, y, z and result to be of type int.

Prompt the user to enter three integers.

Read three integers from the keyboard and store them in the variables x, y
and z.

Compute the product of the three integers contained in variables x, y and z,


and assign the result to the variable result.

Print "The product is " followed by the value of the variable result.

Return a value from main indicating that the program terminated


successfully.

#include<iostream>

using namespace std;

int main(){

int x, y,z,result;

cout << "Please enter three integers: ";

cin >> x >> y >> z;

result = x * y * z;

cout << "The product is " << result << endl;

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>

using namespace std;

int main() {

int num1, num2;

cout << "Enter the first integer: ";

cin >> num1;

cout << "Enter the second integer: ";

cin >> num2;

int sum = num1 + num2;

int difference = num1 - num2;

int product = num1 * num2;

float division = static_cast<float>(num1) / num2;

cout << "Sum: " << sum << endl;

cout << "Difference: " << difference << endl;

cout << "Product: " << product << endl;

cout << "Division: " << division << endl;

3|Page
if (num1 > num2) {

cout << num1 << " is greater than " << num2 << endl;

cout << num2 << " is smaller than " << num1 << endl;

} else if (num1 < num2) {

cout << num1 << " is smaller than " << num2 << endl;

cout << num2 << " is greater than " << num1 << endl;

} else {

cout << num1 << " and " << num2 << " are equal" << endl;

return 0;

3. Write a program that calculate and display the circumference of a circle.


(C = 2∏r )

#include <iostream>

#include <cmath>

using namespace std;

int main() {

double radius ;

double circumference;

const double pi = 3.14159;

4|Page
cout << "Enter the radius of the circle: ";

cin >> radius;

circumference = 2 * pi * radius;

cout << "The circumference of the circle is: " << circumference << endl;

return 0;

4. Write a program to solve a quadratic equation.


Hint: y = ax2 + bx + c

root = (-b ± sqrt(b2 -4ac)) / 2a

Note: include math.h to use square root function(sqrt(double))

#include <iostream>

#include <cmath>

using namespace std;

int main() {

double a, b, c;

double discriminant, root1, root2;

5|Page
cout << "Enter the coefficients (a, b, c) of the quadratic equation: ";

cin >> a >> b >> c;

// Calculate discriminant

discriminant = b * b - 4 * a * c;

// Check if roots are real or complex

if (discriminant > 0) {

// Real and distinct roots

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

cout << "Root 1 = " << root1 << endl;

cout << "Root 2 = " << root2 << endl;

} else if (discriminant == 0) {

// Real and equal roots

root1 = -b / (2 * a);

cout << "Root 1 = Root 2 = " << root1 << endl;

} else {

// Complex roots

double realPart = -b / (2 * a);

double imaginaryPart = sqrt(-discriminant) / (2 * a);

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?

Draw a flowchart of the code.

if (x > 2) {
if (y > 2) {
int z = x + y;
cout << "z is " << z << endl;
}
}
else
cout << "x is " << x << endl;

Let's go through the code for each scenario:

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:

Output: "z is 7"

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:

Output: "x is 2"


Flow chart

8|Page
9|Page
6.Write a program to accept any character from keyboard and
display whether it is vowel or not.

#include <iostream>

using namespace std;

int main() {

char character;

cout << "Enter a character: ";

cin >> character;

// Convert the character to lowercase for easier comparison

character = tolower(character);

// Check if the character is a vowel

if (character == 'a' || character == 'e' || character == 'i' || character == 'o'


|| character == 'u') {

cout << "The character is a vowel." << endl;

} else {

cout << "The character is not a vowel." << endl;

return 0;

7.Write a program that gives grade based on the following scale


using if else statement :

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>

using namespace std;

int main() {

int marks;

cout << "Enter the marks: ";

cin >> marks;

if (marks > 95) {

cout << "Grade: A+" << endl;

} else if (marks >= 85 && marks <= 94) {

cout << "Grade: A" << endl;

} else if (marks >= 80 && marks <= 84) {

cout << "Grade: A-" << endl;

} else if (marks >= 75 && marks <= 79) {

cout << "Grade: B+" << endl;

} else if (marks >= 70 && marks <= 74) {

11 | P a g e
cout << "Grade: B" << endl;

} else if (marks >= 65 && marks <= 69) {

cout << "Grade: B-" << endl;

} else if (marks >= 60 && marks <= 64) {

cout << "Grade: C+" << endl;

} else if (marks >= 50 && marks <= 59) {

cout << "Grade: C" << endl;

} else {

cout << "Grade: F" << endl;

return 0;

8.Write a program that display greatest of three numbers using if


statement accept input from user .

#include <iostream>

using namespace std;

int main() {

int num1, num2, num3;

cout << "Enter three numbers: ";

cin >> num1 >> num2 >> num3;

if (num1 >= num2 && num1 >= num3) {

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;

9.Write a program that calculates sum of numbers from 1 to 100 .

#include <iostream>

using namespace std;

int main() {

int sum = 0;

for (int i = 1; i <= 100; ++i) {

sum += i;

cout << "The sum of numbers from 1 to 100 is: " << sum << endl;

13 | P a g e
return 0;

10.Write a program that displays numbers between 0 -

100 that are divisible by 2, 3, and 5. The numbers


displayed should be those that can be divided by 2, 3, and 5
without remainder

#include <iostream>

using namespace std;

int main() {

cout << "Numbers between 0 and 100 divisible by 2, 3, and 5: ";

for (int i = 0; i <= 100; ++i) {

if (i % 2 == 0 && i % 3 == 0 && i % 5 == 0) {

cout << i << " ";

cout << endl;

return 0;

14 | P a g e
}

11.Write a program that calculates factorial using for loop, while


loop and do while loops. The program should accept the number
and then perform the calculation of the factorial .

#include <iostream>

using namespace std;

int main() {

int num;

int factorial = 1;

cout << "Enter a positive integer: ";

cin >> num;

// Using for loop


for (int i = 1; i <= num; ++i) {

factorial *= i;

cout << "Factorial using for loop: " << factorial << endl;

// Reset factorial for next calculation

factorial = 1;

// Using while loop

int i = 1;

15 | P a g e
while (i <= num) {

factorial *= i;

++i;

cout << "Factorial using while loop: " << factorial << endl;

// Reset factorial for next calculation

factorial = 1;

// Using do-while loop


i = 1;

do {

factorial *= i;

++i;

} while (i <= num);

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>

using namespace std;

int main() {

16 | P a g e
int sum = 0;

int count = 0;

int num = 1;

while (num <= 10) {

sum += num;

++num;

++count;

double average = static_cast<double>(sum) / count;

cout << "The average of numbers from 1 to 10 is: " << average << endl;

return 0;

13 convert 25.625 in to binary numbers


To convert the decimal number 25.625 into binary, we need to split it into its
integer and fractional parts .

25 divided by 2 is 12 with a remainder of 1.


12 divided by 2 is 6 with a remainder of 0.
6 divided by 2 is 3 with a remainder of 0.
3 divided by 2 is 1 with a remainder of 1.
1 divided by 2 is 0 with a remainder of 1.

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.

Putting the binary representations of the integer and fractional parts


together, we get:
25.625 in binary is 11001.101 .
14 11.011convert to decimal numbers

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.

0/2^1 + 1/2^2 + 1/2^3 = 0/2 + 1/4 + 1/8 = 0 + 0.25 + 0.125 = 0.375

Adding the integer and fractional parts together, we get:


11.011 in decimal is equal to 3 + 0.375 = 3.375

18 | P a g e

You might also like