Question

Write a program that prints the following pattern to the screen. (no blank lines between lines)...

Write a program that prints the following pattern to the screen. (no blank lines between lines) You must use nested for loops.

1

212

32123

4321234

543212345


c++
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int main() {
    int n = 5;  // you can change it to any number you want
    for (int i = 1; i <= n; ++i) {  // print n rows
        // print (n-i) spaces first
        for (int j = 0; j < n - i; ++j) {
            cout << " ";
        }
        // then print from i to 1
        for (int j = i; j >= 1; --j) {
            cout << j;
        }
        // and then print from 2 to i
        for (int j = 2; j <= i; ++j) {
            cout << j;
        }
        // after each row, print a new line
        cout << endl;
    }
    return 0;
}

1 212 32123 4321234 543212345

Add a comment
Know the answer?
Add Answer to:
Write a program that prints the following pattern to the screen. (no blank lines between lines)...
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
  • Using java Sample Outputs Write a program which asks the user for an integer and then...

    Using java Sample Outputs Write a program which asks the user for an integer and then prints the following patterns based on that integer. Input Validation For Pattern 1, the input must be a value between 1 and 999 For Pattern 2, the input must be a value between 1 and 26. Requirements: For Pattern 1: must be able to work with up to three digit numbers. You will want to use printf to get the spacing correct. For Pattern...

  • C++ Write a program that asks for a number and then prints as many lines as...

    C++ Write a program that asks for a number and then prints as many lines as the user inputs. Each line contains as many pairs of characters ("*#") as the number of this line. It should look like the right half of a pyramid. Your version of the program must print the same result as the expected output. To to this lab, you must use two do-while loops. Two exceptions: When the user inputs a number less than 1, then...

  • Parallelogram Program Write a program that prints the following parallelogram pattern given the the following two...

    Parallelogram Program Write a program that prints the following parallelogram pattern given the the following two inputs. Here are the rules: Your program must work with any length greater than 1. Your program must use the character the user inputs to draw the parallelogram. Your program must not use global variables or global code other than a call to main(). Your program output must match my out exactly. I have provided you with the strings you need for the output...

  • 5) Write a program that prints the following diamond shape. may use (printf) statement that print...

    5) Write a program that prints the following diamond shape. may use (printf) statement that print either a single asterisk ( * ) or a single blank. Maximize your use of iteration ( with nested for statements) and minimize the number of (printf) statements. answer should be in simple C language please and it copyable.

  • Write a program that reads in a text file, infile.txt, and prints out all the lines...

    Write a program that reads in a text file, infile.txt, and prints out all the lines in the file to the screen until it encounters a line with fewer than 4 characters. Once it finds a short line (one with fewer than 4 characters), the program stops. For your testing you should create a file named infile.txt. Only upload your Python program, I will create my own infile.txt. Please use a while-loop BUT do not use break, Exit or Quit...

  • Write a python nested for loop that prints out the following pattern 100 99 98 97...

    Write a python nested for loop that prints out the following pattern 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33...

  • Java: makes sure program compiles before posting. Write a program that uses nested for loops to...

    Java: makes sure program compiles before posting. Write a program that uses nested for loops to create the pattern of Xs and Os, in which on every line each letter is displayed one additional space to the right. Use a toggle variable ( a boolean flag) to alternate between X and O to produce output as shown in the sample run below: Enter the number of lines: 7 X O X O X O X

  • Write a program which: 1. Prints out the Multiplication Table for a range of numbers (positive...

    Write a program which: 1. Prints out the Multiplication Table for a range of numbers (positive integers). • Prompts the user for a starting number: say 'x' • Prompts the user for an ending number: say 'y' • Prints out the multiplication table of 'x' up to the number 'y' SPECIFIC REQUIREMENTS 1. You must use the following method to load the array: public static void loadArray(int table[][], int x, int y) 2. You must use the following method to...

  • Write a Python program (using a nested loop) to print out the following pattern (there is...

    Write a Python program (using a nested loop) to print out the following pattern (there is a space between the numbers next to each other). 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1

  • Write a PYTHON program that allows the user to navigate the lines of text in a...

    Write a PYTHON program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the...

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