Thursday 16 July 2020

19. 5.23 (Diamond-Printing Program) Write an application that prints the following diamond shape. You may use output statements that print a single asterisk (*), a single space or a single new-line character. Maximize your use of iteration (with nested for statements), and minimize the number of output statements. * *** ***** ******* ********* ******* ***** *** *

19. 5.23 (Diamond-Printing Program) Write an application that prints the following diamond shape. You may use output statements that print a single asterisk (*), a single space or a single new-line character. Maximize your use of iteration (with nested for statements), and minimize the number of output statements.

Code:


#include <iostream> 
using namespace std; 
int main(){

int n=5;
int space = n-1 ; 

// run loop (parent loop) 
// till number of rows 
for (int i = 0; i < n; i++) 

// loop for initially space, 
// before star printing 
for (int j = 0;j < space; j++) 
cout << " "; 

// Print i+1 stars 
for (int j = 0; j <= i; j++) 
cout << "* "; 

cout << endl; 
space--; 


// Repeat again in reverse order 
space = 0; 

// run loop (parent loop) 
// till number of rows 
for (int i = n; i > 0; i--) 

// loop for initially space, 
// before star printing 
for (int j = 0; j < space; j++) 
cout << " "; 

// Print i stars 
for (int j = 0;j < i;j++) 
cout << "* "; 

cout << endl; 
space++; 



return 0; 

}

   

No comments:

Post a Comment