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

Write a C++ program that: 1. Requests a positive number “N” from the user that i

ID: 672203 • Letter: W

Question

Write a C++ program that:

1. Requests a positive number “N” from the user that is in the range [1, 20]. If the user enters a number outside this range, display an error message and request the number again.

2. Using a nested loop, display a table similar to the one below (the example is for the case N=20). Each of the table entries indicates with ‘y’ that the corresponding column can divide exactly the corresponding row. Otherwise, ‘n’.

3. For example, only 1 and 2 can divide exactly 2, so the second row has two ‘y’ entries and the rest are ‘n’.

• The program must include comments explaining the steps

Explanation / Answer

#include <iostream> // std::cout, std::cin
using namespace std;
int main()
{
// The user input
int input;
// Ask for a number
cout << "Please, enter a number: ";
// Get the user input
cin >> input;
// While the user is not inputting a correct number...
while (cin.fail())
{
// Ask again for input
cout << "Please, enter a valid number: ";
// Clear the input we got before
cin.clear();
// Ignore the new line character
cin.ignore(256, ' ');
// Get the input again
cin >> input;
}
// If the input is [1, 10], You are done
if (input >= 1 && input <= 10)
cout << endl << "You are Done" << endl;
else //else Sorry enter again
cout << endl << "soryy enter again" << endl;
return 0;
}

3.

int add(int x, int y)
{
while(x)
{
int t = (x & y) <<1;
y ^= x;
x = t;
}
return y;
}
int divideby2 (int num)
{
int sum = 0;
while (num > 3)
{
sum = add(num >> 2, sum);
num = add(num >> 2, num & 3);
}
if (num == 3)
sum = add(sum, 1);
return sum;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote