w3resource

C++ Exercises: Print a single digit number and print a rectangular form of 4 columns and 6 rows

C++ Basic: Exercise-31 with Solution

Write a C++ program to input a single-digit number and print it in a rectangular form of 4 columns and 6 rows.

Visual Presentation:

C++ Exercises: Input a single digit number and print in such a rectangular form that it gots 4 columns and 6 rows

Sample Solution:

C++ Code :

#include <iostream> // Including input-output stream header file

using namespace std; // Using the standard namespace

int main() // Start of the main function
{
    int x; // Declaring an integer variable x

    cout << "\n\n Make a rectangular shape by a single digit number :\n"; // Outputting a message indicating the purpose of the program
    cout << "--------------------------------------------------------\n"; // Outputting a separator line

    cout << " Input the number : "; // Prompting the user to input a number
    cin >> x; // Taking input for the variable x from the user

    // Outputting the rectangular shape using the input number
    cout << " " << x << x << x << x << endl; // Outputting a row of the input number repeated four times
    cout << " " << x << " " << " " << x << endl; // Outputting a row with the input number separated by spaces
    cout << " " << x << " " << " " << x << endl; // Outputting a row with the input number separated by spaces
    cout << " " << x << " " << " " << x << endl; // Outputting a row with the input number separated by spaces
    cout << " " << x << " " << " " << x << endl; // Outputting a row with the input number separated by spaces
    cout << " " << x << x << x << x << endl; // Outputting a row of the input number repeated four times

    cout << endl; // Outputting a blank line for better readability

    return 0; // Returning 0 to indicate successful program execution
} // End of the main function

Sample Output:

 Make a rectangular shape by a single digit number :                   
--------------------------------------------------------               
 Input the number : 5                                                  
 5555                                                                  
 5  5                                                                  
 5  5                                                                  
 5  5                                                                  
 5  5                                                                  
 5555     

Flowchart:

Flowchart: Print a single digit number and print in such a rectangular form that it gots 4 columns and 6 rows

C++ Code Editor:

Previous: Write a program in C++ to compute the total and average of four numbers.
Next: Write a program in C++ to check whether a number is positive, negative or zero.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.