Write a double loop over 0 i,j < 10 that prints the first pair where the product
ID: 3886716 • Letter: W
Question
Write a double loop over 0 i,j < 10 that prints the first pair where the product of indices satisfies i · j > N, where N is a number your read in. A good test case is N = 40.
Can you traverse the i,j indices such that they first enumerate all pairs i + j = 1, then i + j = 2, then i + j = 3 et cetera?
Write a program that prints out both pairs, each on a separate line, with the numbers separated with a comma, for instance 8,5.
The output should look like this:
[ajpye@isp02 Exercises]$ . /Exercise4 Enter upper bound: 40 [ajpye@isp02 Exercises]$ . /Exercise4 Enter upper bound: 80 9,9 9,9 [ajpye@isp02 Exercises]$ . /Exercise4 Enter upper bound: 20 3,7 3,7Explanation / Answer
#include<iostream>
using namespace std;
//Main method definition
int main()
{
//Variables for row counter, column counter and upper bound
int r, c, upperBound;
//Accept upper bound
cout<<" Enter the upper bound: ";
cin>>upperBound;
//Iterate 0 - 10
for(r = 0; r < 10; r++)
{
//Iterate 0 - 10
for(c = 0; c < 10; c++)
{
//Checks if r * c is greater than upper bound then display the pair with separated by comma
if((r * c) > upperBound && r < c)
{
cout<<r<<", "<<c<<endl;
//To stop duplicate pair such as 5, 9 and 9, 5
//Come out of the loop
break;
}//End of if
}//End of inner for loop
}//End of outer for loop
}//End of main
Sample Run:
Enter the upper bound: 40
5, 9
6, 7
7, 8
8, 9
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.