Sequence

10:14 0 Comments A+ a-

Problem :
Write a C++ program which inputs two integers from user and display the following sequence in the specified format. [Hint: Use nested loops]
Sample Runs are given for text messages to be used for input and output. (Input is underlined in sample run, only to distinguish from display messages)
Sample Run 1:
Enter starting integer: 1
Enter ending integer: 5
(1,1)(1,2)(1,3)(1,4)(1,5)
(2,2)(2,3)(2,4)
(3,3)
Solution:
# include <iostream>
using namespace std;
int main()
{
int a; //integer a intialized for starting integer
int b; //integer b intialized for ending integer
int m; //m used as a counter
int n; //n used as a counter
cout<<"Enter Starting Integer:"<<endl;
cin>>a; //a declared with input
cout<<"Enter Ending Integer:"<<endl;
cin>>b; //b declared with input
m=a;
n=a;

for(m ; m<=b ; m++) //for loop to control the number of rows to be printed i.e ((b-a)/2)+1
{
for(n ; n<=b ; n++) //for loop to print integers in ()
{
cout<<"("<<m<<","<<n<<")";
}
cout<<endl;
n=a+1; //increment in n by a+1
a++; //increment in a to print 2 in 2nd line for 1st variable of ()
b=b-1; //decrement in b to print b one to print one less in 2nd and then third row for 2nd variable
}

system("pause");
return 0;

}