I'm trying to solve this exercise. Write a program that print out the following, user will input the top number:
*****
****
***
**
*


i made a program similary to this , but user inputs the bottom number ,the output is like this:
*
**
***
****
*****


here is the code to this program

#include <cstdlib>
#include <iostream>
#include <conio.h>

using namespace std;
int main()
{
int b;
cout<<"Introdu un integer";
cin>>b;
for (int i = 1; i <=b;i++) 
{
	for (int j = 1; j <= b; j++) 
	{
		cout<<"*";
		}
		cout<<endl;
		
}
getch();
return 0;
}

so what i must edit to solve the first program?

Recommended Answers

All 2 Replies

Read the user input as a string.
Convert the string to an integer
Start your loop at the top number and "decrement" the loop.

yeah i solved it)

#include <cstdlib>
#include <iostream>
#include <conio.h>

using namespace std;
int main()
{
int b;
cout<<"Enter an integer: ";
cin>>b;
for (int i = 1; i <= b;i++) 
{
	for (int j = b; j >= i ; j--) 
	{
	cout<<"*";
		}
		cout<<endl;
		
}
getch();
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.