Monday 4 July 2016

Print Diamond Shape in C++ | Pattern Programs

Print Diamond Shape in C++ | Pattern Programs


Diamond Shape in C++





#include <iostream>
using namespace std;

int main()
{
     int input_base, line = 1, stars = 1, no_of_stars = 1, spaces = 1, no_of_spaces;

     cout << "Enter the base of Diamond: ";
     cin >> input_base;

     no_of_spaces = input_base;
     for (line = 1; line <= input_base; line++){

           for (spaces = 1; spaces<no_of_spaces; spaces++)
                cout << " ";
          
           for (stars = 1; stars <= no_of_stars; stars++)
                cout << "*";
          
           cout << endl;
           no_of_spaces--;
           no_of_stars += 2;
     }

     no_of_stars = 1;
     no_of_spaces = 1;
    
     for (int counter = 1; counter<(input_base - 1); counter++)
           no_of_stars = no_of_stars += 2;
    
     for (line = 1; line<input_base; line++){

           for (spaces = 1; spaces <= no_of_spaces; spaces++)
                cout << " ";
          
           for (stars = 1; stars <= no_of_stars; stars++)
                cout << "*";
          
           cout << endl;
           no_of_spaces++;
           no_of_stars -= 2;
     }

     system("pause");
     return 0;
}

No comments

Post a Comment

Recent Posts