Program to print a pattern of numbers in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to print a given pattern of numbers.

Our task is to make use of looping structure in the code and print the given pattern −

     1
    232
   34543
 4567654
567898765

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
int main(){
   int n = 5, i, j, num = 1, gap;
   gap = n - 1;
   for ( j = 1 ; j <= n ; j++ ){
      num = j;
      for ( i = 1 ; i <= gap ; i++ )
      cout << " ";
      gap --;
      for ( i = 1 ; i <= j ; i++ ){
         cout << num;
         num++;
      }
      num--;
      num--;
      for ( i = 1 ; i < j ; i++){
         cout << num;
         num--;
      }
      cout << "\n";
   }
   return 0;
}

Output

    1
   232
  34543
 4567654
567898765
raja
Published on 19-Dec-2019 14:56:54
Advertisements