Write a program that prompts the user for a positive number

Write a program that prompts the user for a positive number. The program then prints the pattern given below. Sample Run: \"E\\\\WORK\\Sharjah\\TEACHING\\116 C++ programming\\hw2_ex1-Copyexe\" Enter number of rows: 5 The number pattern is: 0 1 2 3 4 1 0 1 2 3 2 1 0 1 2 3 2 1 0 1 4 3 2 1 0 process returned 0(Times 0) execution time: 1.390 s press any key to continue \"E\\\\WORK\\Sharjah\\TEACHING\\116 C++ programming\\hw2_ex1-Copyexe\" Enter number of rows: 7 The number pattern is: 0 1 2 3 4 5 6 1 0 1 2 3 4 5 2 1 0 1 2 3 4 3 2 1 0 1 2 3 4 3 2 1 0 1 2 5 4 3 2 1 0 1 6 5 4 3 2 1 1 process returned 0(Times 0) execution time: 0.646 a press any key to continue

Solution

#include <iostream>
using namespace std;
int main()
{
int n,i,j,t; // \'n\' is for positive number , \'i\',\'j\' is for loop, \'t\' is temp variable
cout << \"Enter the number of rows : \";
cin>>n;
if(n<1){
cout<< \"Please enter positive number\" <<endl;
return 0;
}
cout<<\"The number pattern is : \"<<endl;   
for(i=0; i<n; i++){
t=i;
for(j=0; j<n; j++)
{
if(j == 0)
{
cout<<t<<\"\\t\";
}
else
{
t = t-1;
cout<<abs(t)<<\"\\t\";
}
}
cout<<endl;
}
return 0;
}

Here\'s the explaination:
* Prepare two loop, one for rows and one for columns
* keep the row\'s number in temporary variable \'t\' and iterate the column\'s
* In each column\'s iteration, check if its the first column then print the
temp variable else decrement temp variable by 1 and prints its absolute value

 Write a program that prompts the user for a positive number. The program then prints the pattern given below. Sample Run: \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site