Answer to Question #150495 in C++ for Maryeeah

Question #150495
Write programs that will accept an integer input and will display asterisks correspondingly. See examples below:

Program 1:
Enter integer: 5
Output:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
//the output is a 5 x 5 set of asterisks

Program 2:
Enter integer: 4
Output:
*
* *
* * *
* * * *
//the output is a triangle-shaped set of asterisks that start with 1 then ends with 4

Program 3:
Enter integer: 5
Output:
* * * * *
* *
* *
* *
* * * * *
1
Expert's answer
2020-12-11T16:12:27-0500

Program 1:

#include <iostream>
int main()
{
    int value;
    std::cout << "Enter integer: ";
    std::cin >> value;
  
    for(int i=0; i<value; ++i)
    {
        std::cout << std::string(value, '*') << "\n";
    }

    return 0;
}


Program 2:

#include <iostream>
int main()
{
    int value;
    std::cout << "Enter integer: ";
    std::cin >> value;
  
    for(int i=0; i<value; ++i)
    {
        std::cout << std::string(i + 1, '*') << "\n";
    }

    return 0;
}


Program 3:

#include <iostream>
int main()
{
    int value;
    std::cout << "Enter integer: ";
    std::cin >> value;
  
    for(int i=0; i<value; ++i)
    {
        if(i == 0 || i == (value - 1))
        {
            std::cout << std::string(value, '*') << "\n";
        }
        else
        {
            std::cout << std::string(2, '*') << "\n";
        }
    }

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment