How do i do this?
Hi, how can i write a program that prints out this..
*****
****
***
**
*
with the user inputting the top number to start off from.
Thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include<iostream>
#include<Windows.h>
using namesapce std;
int main()
{
for(int i=5;i>1;i--)
{
for(int j=0;j<i;j++)
cout<<"*";
cout<<endl;
}
system("pause");
return 0;
}
| |
hope it can help you.
1 2 3 4 5 6 7 8 9 10 11
|
#include <iostream>
#include <string>
int main() {
unsigned int initialNumberOfStars = 0;
std::cin >> initialNumberOfStars;
for ( int i = initialNumberOfStars; i > 0; --i ) {
std::string stars( i, '*' );
std::cout << stars << std::endl;
}
}
| |
Enjoy!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <iostream>
#include <iomanip>
int main()
{
while ( true )
{
std::cout << "Enter a non-negative number (0 - exit): ";
size_t n = 0;
std::cin >> n;
if ( !n ) break;
std::cout << std::endl;
std::cout << std::setfill( '*' );
while ( n ) std::cout << std::setw( n-- + 1 ) << '\n';
std::cout << std::endl;
}
}
| |
Also try the following
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream>
#include <iomanip>
int main()
{
while ( true )
{
std::cout << "Enter a non-negative number (0 - exit): ";
size_t n = 0;
std::cin >> n;
if ( !n ) break;
std::cout << std::endl;
std::cout << std::setfill( '*' );
size_t i = 0;
while ( i < n ) std::cout << std::setw( i++ + 1 ) << '\n';
while ( i ) std::cout << std::setw( i-- + 1 ) << '\n';
std::cout << std::endl;
}
}
| |
Another:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
#include <iomanip>
void print_stars( const unsigned int amount )
{
if ( amount == 0 ) return;
std::cout << std::setfill('*') << std::setw( amount + 1 ) << '\n';
print_stars( amount - 1 );
}
int main(void)
{
print_stars(10);
return 0;
}
| |
Last edited on
Topic archived. No new replies allowed.