Write a program in c++ that reads in a number n and prints out n rows each containing n star characters, '*'.
Thus if n is 3, then your program should print
***
***
***
Answers
Prints star characters in C++
Output:
Enter value of n: 3
***
***
***
Explanation:
The program to print star characters as follows:
Program:
#include <iostream> //defining header file
using namespace std;
int main() //defining main method
{
int i,j, n;//defining integer variable
cout<<"Enter value of n: "; //print message
cin>>n; //input value by user
for(i=0;i<n;i++) //loop for columns printing
{
for(j=0;j<n;j++) //loop for rows printing
{
cout<<"*"; //prints star character
}
cout<<endl; //print value in new line
}
return 0;
}
Explanation of the above program as follows:
- In the above C++ program, firstly header file is included then defined, the main method inside this three integer variable "i,j, and n" is defined, in which variable "n" is used to input value by the user.
- In the next line, two for loop are defined, which uses the integer variable n for where the value to be printed and (i and j) for print value.
- The outer loop is used for a print column and the inner loop print rows, inside this loop the print function is used, which user star character for print its value.
Learn more:
- Pattern program in C: https://brainly.in/question/11287149
- Print star characters program: https://brainly.in/question/13027846
C++ program: -
//importing the header file
#include <iostream>
//using namespace std for standard I/O
using namespace std;
//defining the main function
int main()
{
//declaring the variable
int n;
//prompts the user to enter the value of n
cout<<"Enter value of n: ";
//storing the value enterred by the user in n
cin>>n;
//nested for-loop is used to print the pattern
//outer loop for row
for(int row=0;row<n;row++)
{
//inner loop for column
for(int col=0;col<n;col++)
{
//print the character
cout<<"*";
}
//to enter the new line character
cout<<"\n";
}
}
Explanation: -
- Program Start.
- Importing the essential headers and using the namespace std.
- Defining the main() function.
- Declaring the variable n.
- Prompts the user to enter the value of n and storing the value.
- Using the nested for-loop to iterate over rows and columns.
- The outer loop is used for row and inner loop is used for column.
- Both the loops will terminate when the value of variable "row" and "col" becomes equal to n.
- In the inner loop print "*".
- The inner loop will execute a total of 9 times and the outer loop will execute 3 times.
- Print the new line character.
- Program End.
Output: -
Enter value of n: 3
***
***
***
Learn more: - C++ program to print the pattern:
https://brainly.in/question/1628993
C program to print the pattern:
https://brainly.in/question/636377