Question #39153

use for loop only to display the following display
123456
54321
1234
321
12
1

Expert's answer

Answer on Question#39153- Programming, C++

1. use for loop only to display the following display


123456
54321
1234
321
12
1


Solution.


#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
    for(int i=1, n=7; i<=n; i++)
    {
        if(i==n)
        {
            i=1;
            n=n-2;
            cout<<endl;
            for(int j=1; j<=n; j++){cout<<j;}
            for(int j=n-1; j>=1; j--){cout<<j;}
            cout<<endl;
        }
        if(n==1){break;}
        cout<<i;
    }
    return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS