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

Need help by tonight! Write a C++ program that: Request a positive number \"N\"

ID: 3543171 • Letter: N

Question

Need help by tonight!

Write a C++ program that: Request a positive number "N" from the user (display an error message if not). Using a nested loop, display a table showing what numbers are divisible by what number in the range 1 to N. Make sure the output is formatted. For example: Enter max number to display: 20 Puts a "1" whenever the number on the row above the horizontal line is divisible the number on the left side of the vertical line and a "0" otherwise. For instance: Every number is divisible by "1" and therefore the first row is all ones. A number is also divisible by itself, so there is a single one at the bottom row. For any other row, the result will depend on the number entered. For example, for the second row, all even numbers have a "1" and all odd numbers have a "0". Tips: Use a nested loop to build the table (e.g., one "for" inside another "for" statement Take advantage of integer division to test number divisibility. For example for two integers A and B. A is divisible by B if (A/B)*B is exactly equal to A.

Explanation / Answer

please rate - thanks

any question - ask


#include <iostream>
#include <iomanip>
using namespace std;
int main()
{int i,j,n;
cout<<"Enter max number to display: ";
cin>>n;
while(n<=0)
{cout<<"must be positive ";
cout<<"Enter max number to display: ";
cin>>n;
}
cout<<" "; //4 spaces before top row of numbers
for(i=1;i<=n;i++) //print headings usinf 3 spaces
cout<<setw(3)<<i;
cout<<endl;
for(i=0;i<n*3+4;i++) //3 underline per number and 4 for the spaces
cout<<"-";
cout<<endl;
for(i=1;i<=n;i++) //print number and bar
{ cout<<setw(2)<<i<<" |";
for(j=1;j<=n;j++)
if(j/i*i==j) //if divisible print 1
cout<<setw(3)<<1;
else
cout<<setw(3)<<0; //otherwise 0
cout<<endl;
}
system("pause");
return 0;
}

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