j33vansh

Answer 3 Looping CPP

Jun 27th, 2021 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.   int n,i,j;;
  6.  
  7.   cout<<"Enter number of rows\n";
  8.   cin>>n;
  9.   int n1=n/2+1;
  10.   for (i=1;i<=n1;i++)
  11.   {
  12.     for (j=1;j<=n1-i;j++)
  13.         {cout<<" ";}
  14.  
  15.     for (j=1;j<=2*i-1;j++)
  16.         {cout<<"*";}
  17.  
  18.     cout<<"\n";
  19.   }
  20.  
  21.   for (i= 1;i<=n1-1;i++)
  22.   {
  23.     for (j=1;j<=i;j++)
  24.         {cout<<" ";}
  25.  
  26.     for (j=1;j<=2*(n1-i)-1;j++)
  27.         {cout<<"*";}
  28.  
  29.     cout<<"\n";
  30.   }
  31.  
  32.   return 0;
  33. }
  34.  
  35.  
  36. /*
  37. #include <bits/stdc++.h>
  38. using namespace std;
  39. #define FIO                           \
  40.     ios_base::sync_with_stdio(false); \
  41.     cin.tie(NULL);                    \
  42.     cout.tie(NULL);
  43.  
  44. int main()
  45. {
  46.     FIO;
  47.     int n;
  48.     cin >> n;
  49.     int n1 = (n + 1) / 2;
  50.     int n2 = n1 - 1;
  51.     int i = 1;
  52.     while (i <= n1)
  53.     {
  54.         int spaces = 1;
  55.         while (spaces <= n1 - i)
  56.         {
  57.             cout << " ";
  58.             spaces++;
  59.         }
  60.         int stars = 1;
  61.         while (stars <= 2 * i - 1)
  62.         {
  63.             cout << "*";
  64.             stars++;
  65.         }
  66.         cout << endl;
  67.         i++;
  68.  
  69.     }
  70.     i = i - 2;
  71.     while (i >= 1)
  72.     {
  73.  
  74.         int spaces = 1;
  75.         while (spaces <= n2 - i + 1)
  76.         {
  77.             cout << " ";
  78.             spaces++;
  79.         }
  80.         int stars = 1;
  81.         while (stars <= 2 * i - 1)
  82.         {
  83.             cout << "*";
  84.             stars++;
  85.         }
  86.         cout << endl;
  87.         i--;
  88.     }
  89. }
  90. */
Add Comment
Please, Sign In to add comment