Question

IN C++

Write a program using for loops to produce the following output. Please read carefully the entire specification! Your program must work for any odd number entered by the user, not just 13. (NOTE that there is no space after the colon on the Hill, Triangle, and Diamond labels.) Enter an odd number width: 13 Hill: Triangle no spaces Diamond

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

// C++ code to demonstrate star pattern

#include <iostream>

#include <limits>

using namespace std;

void triangle(int n)

{

for (int i = 0; i <= n / 2; i++)

{

int j = 0;

// space

for (j = 0; j < n / 2 - i; j++)

cout << " ";

for (int k = 0; k <= 2 * i; k++)

{

cout << "*";

j++;

}

cout << endl;

}

}

void diamond(int n)

{

int c, k, space = 1;

space = n / 2;

for (k = 1; k <= n / 2 + 1; k++)

{

for (c = 1; c <= space; c++)

cout << " ";

space--;

for (c = 1; c <= 2 * k - 1; c++)

cout << "*";

cout << "\n";

}

space = 1;

for (k = n / 2; k < n; k++)

{

for (c = 0; c < space; c++)

cout << " ";

space++;

for (c = 1; c <= 2 * (n - k) - 3; c++)

cout << "*";

cout << "\n";

}

}

void hill(int n)

{

for (int i = 0; i < n / 2 + 1; i++)

{

for (int j = 0; j <= i; j++)

{

cout << "*";

}

cout << endl;

}

}

int main()

{

int num;

cout << "Enter positive odd number : ";

while (!(cin >> num) || (num < 0 || num % 2 == 0))

{

// flush the cin data

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << "Invalid entry, Try again : ";

}

cout << "Hill:" << endl;

hill(num);

cout << "Triangle:" << endl;

triangle(num);

cout << "Diamond:" << endl;

diamond(num);

return 0;

}

Output:

Enter positive odd number:13 Hill: Triangle: Diamond:

Add a comment
Know the answer?
Add Answer to:
IN C++ Write a program using for loops to produce the following output. Please read carefully...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program that will produce the EXACT output shown below. Write a program to...

    Write a C program that will produce the EXACT output shown below. Write a program to find out the number of words, spaces, vowels, consonants, along with special characters in a sentence. Take the data below as an example. Your C program should take the data in the arrays and produce the output below, neatly formatted as shown Enter a sentence: My cat’s name is Bella. You sentence includes:             Number of words: 5             Number of spaces: 4            ...

  • Today you are to write a Java program that will prompt for and read 2 words...

    Today you are to write a Java program that will prompt for and read 2 words of equal length entered by the user, and create a new word which contains the last letter of the 1st word, the last letter of the 2nd word, followed by the second to last character of the 1st word, followed by the second to last character of the 2nd word and so on. Be sure to use the same format and wording as in...

  • Use Java program Material Covered : Loops & Methods Question 1: Write a program to calculate...

    Use Java program Material Covered : Loops & Methods Question 1: Write a program to calculate rectangle area. Some requirements: 1. User Scanner to collect user input for length & width 2. The formula is area = length * width 3. You must implement methods getLength, getWidth, getArea and displayData ▪ getLength – This method should ask the user to enter the rectangle’s length and then return that value as a double ▪ getWidth – This method should ask the...

  • read it carefully C++ Question: Write a program that dynamically allocates an array large enough to...

    read it carefully C++ Question: Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible. Input Validation: Do not...

  • Plz use nested loops , read the question carefully, i need help step by step solution...

    Plz use nested loops , read the question carefully, i need help step by step solution using ( spyder).. Exercise 2 Write a Python program (Lab7-ex2.py) that reads from the user an integer n, displays the triangular number sequence up to the n term (ie. 1,3, 6, 10,. n"term). The triangular of a number n is calculated as "n(n+1)/2" where n is a nonzero positive number. Note that your program should display a suitable error message if n is entered...

  • 1. Write a C++ program to output a framed greeting. The program will produce five lines...

    1. Write a C++ program to output a framed greeting. The program will produce five lines of output. The first line begins the frame. It is a sequence of * characters as long as the person's name, plus some characters to match the salutation ("Hello, "), plus a space and an * at each end. The line after that will be an appropriate number of spaces with an * at each end. The third line is an *, a space,...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • Using loops, write a C# program that asks the user to enter repeatedly an integer number,...

    Using loops, write a C# program that asks the user to enter repeatedly an integer number, it stops when the user enters -1, then the program should display how many numbers were entered, their the sum and their average

  • In C++ using For Loops; "Write a program that displays number facts for a number provided...

    In C++ using For Loops; "Write a program that displays number facts for a number provided by the end user between 1-100 in a table format that includes the number itself, whether the number is a prime number, whether the number is even or odd, whether the number is a perfect square and all of its factors. " It should also print this information.

  • I have to write a program in java which uses while loops. 1.prompt the user to...

    I have to write a program in java which uses while loops. 1.prompt the user to input two intergers: firstnum ans second numnumber. (first number must be smaller that the second number). 2. Output all the even numbers between first number and second number inclusive. 3. Output the sum of all the even number between first number and second number inclusive. 4 Output all the odd numbers between first number and second number inclusive. 5. Output the sum of all...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT