212k views
0 votes
Given input of a number, display that number of spaces followed by an asterisk. (1 point) Write your solution within the existing supplied function called problem2() and run the Zybooks test for it before continuing on to the next problem. Hint: Use the setw() command to set the field width. Alternatively, you could again use a loop.

User Bogy
by
6.1k points

1 Answer

1 vote

Answer:

Following is the program in C++ language

#include <iostream> // header file

#include <iomanip> // header file

using namespace std; // namespace std;

void problem2() // function definition

{

int num ; // variable declaration

cout << "Enter the Number: ";

cin >> num; // Read the input by user

cout << "Resulatant is:";

for(int k = 0; k <num; k++) // iterarting the loop

{

std::cout << std::setw(num); // set width

std::cout <<"*"; // print *

}

}

int main() // main function

{

problem2();// function calling

return 0;

}

Output:

Enter the Number: 3

Resulatant is: * * *

Step-by-step explanation:

Following is the description of program

  • Create a function problem2().
  • Declared a variable "num" of "int" type .
  • Read the input in "num" by the user .
  • Iterating the for loop and set the width by using setw() and print the asterisk.
  • In the main function calling the problem2().
User Berlinguyinca
by
5.9k points