Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that asks the user to enter an odd integer between 1 and 15 (bot

ID: 3691070 • Letter: W

Question

Write a program that asks the user to enter an odd integer between 1 and 15 (both inclusive). If the integer value was 3, then print out the following diamond pattern on screen. (20 points)

*

***

*

If the value was 5, then the pattern printed is as follows:

*

***

*****

***

*

And so on for other odd values less than or equal to 15. If the user enters an incorrect value, please print out an error statement and ask the user to enter another odd number between 1 and 15, and keep doing this until the user enters a correct input. Use nested control statements in your program to get the desired output. The only cout statements you should use to help you print the pattern are cout <<”*” ; cout <<” ” ; cout<< “ “ ;

Explanation / Answer

#include<iostream>

using namespace std;

int main(){
  
   int n;
   cout<<"Enter a number: ";
   cin>>n;
  
   for(int i=1; i<=n; i=i+2){
       for(int j=1; j<=i; j++)
           cout<<"*";
       cout<<endl;
   }
  
   for(int i=n-2; i>=1; i = i-2){
       for(int j=1; j<=i; j++)
           cout<<"*";
       cout<<endl;
   }
   return 0;
}

/*

sample run:

Enter a number: 5
*
***
*****
***
*

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote