Author Topic: X pattern of numbers with given input  (Read 1049 times)

administrator

  • Administrator
  • Full Member
  • *****
  • Posts: 69
X pattern of numbers with given input
« on: October 26, 2011, 02:40:49 pm »
Write the program to get the following output:

Enter a number: 5
1               1
  2           2
    3       3
      4   4
        5
      4   4
    3       3
  2           2
1               1



Source Code:


int main()
{
   int n = 0;
   printf("Enter a number: ");
   scanf("%d", &n);

   for(int i = 1; i <= n; i++)
   {
      for(int j = 0; j < (i - 1) * 2; j++)
         std::cout << " ";
      std::cout << i;
      for(int j = 0; j < ((n - i) * 4) - 1; j++)
      {
         std::cout << " ";
      }
      if(i < n)
         std::cout << i;
      std::cout << "\n";
   }

   for(int i = n - 1; i >= 1; i--)
   {
      for(int j = 0; j < (i - 1) * 2; j++)
         std::cout << " ";
      std::cout << i;
      for(int j = 0; j < ((n - i) * 4) - 1; j++)
      {
         std::cout << " ";
      }
      if(i < n)
         std::cout << i;
      std::cout << "\n";
   }

   std::cout << "\n\n";

   return 0;
}

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1