Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab05: EECS 138 Submission Guidelines: Implement all tasks in single main file a

ID: 3753049 • Letter: L

Question

Lab05: EECS 138 Submission Guidelines: Implement all tasks in single main file and ask user to choose which task output to show (2 points) (Hint: Use if conditions. Be careful with variable declaration) Problems: 1. Write a C++ program to find SUM of all natural numbers between 1 to n. (2 points) 2. Write a C++ program to find PRODUCT of all Odd numbers between 1 to n. (2 points) . Write C++ program to print multiplication table (from 1 to 10) for any number. (2 points) Example Output for number 3: 3 *1 3 * 2 3 6 3 10 30 4. Write C+ program to count number of digits in a number. (2 points)

Explanation / Answer

Here is code:

#include <iostream>

using namespace std;

int main()

{

int op;

long int product = 1;

int sum = 0;

int counter = 0;

int num, result;

while (true)

{

cout << " Main Menu "

<< "1: Sum of all Natural numbers "

<< "2: Product of odd numbers "

<< "3: Multiplication table "

<< "4: Number count "

<< "5: Exit ";

// read choice

cout << "Enter a Choice: ";

cin >> op;

if (op == 5) // if user choice is 5 then exit while loop

{

break;

}

// read two numbers

cout << "Enter number : ";

cin >> num;

switch (op)

{

case 1:

sum = 0;

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

{

sum += i;

}

cout << "The Answer is: " << sum << endl;

break;

case 2:

product = 1;

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

{

if (i % 2 == 1)

product *= i;

}

cout << "The Answer is: " << product << endl;

break;

case 3:

for (int i = 1; i <= 10; i++)

{

cout << num << " * " << i << " = " << num * i << endl;

}

break;

case 4:

counter = 0;

while (num != 0)

{

num = num / 10;

counter++;

}

cout << "The Answer is: " << counter << endl;

break;

case 5:

break;

default:

// If the operator is other than 1-5 error message is shown

cout << "Error! operator is not correct";

break;

}

}

return 0;

}

Output:

Main Menu
1: Sum of all Natural numbers
2: Product of odd numbers
3: Multiplication table
4: Number count
5: Exit

Enter a Choice: 1
Enter number : 5
The Answer is: 15

Main Menu
1: Sum of all Natural numbers
2: Product of odd numbers
3: Multiplication table
4: Number count
5: Exit

Enter a Choice: 2
Enter number : 5
The Answer is: 15

Main Menu
1: Sum of all Natural numbers
2: Product of odd numbers
3: Multiplication table
4: Number count
5: Exit

Enter a Choice: 3
Enter number : 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Main Menu
1: Sum of all Natural numbers
2: Product of odd numbers
3: Multiplication table
4: Number count
5: Exit

Enter a Choice: 4
Enter number : 12345
The Answer is: 5

Main Menu
1: Sum of all Natural numbers
2: Product of odd numbers
3: Multiplication table
4: Number count
5: Exit

Enter a Choice: 5