Printing triangle without nested conditionals

Viewed 126
#include<iostream>

using namespace std;
int main() {
    int height, star{ 0 };
    cout << "Enter height of triangle";
    cin >> height;
    for(int i=1; i<=height; i++)
    {
        if(star<i)
        {
            cout << "*";
            ++star;
            continue;
        }
        cout << endl;
        star = 0;
    }
}

This is printing stars in a line I want to print one star in 1st line then 2 in second line and so forth.

Example:

*
**
***
****

Image:

enter image description here

7 Answers

You should be able to simply do this:

#include <iostream>

using namespace std;

int main()
{
    int height;
    cout << "Enter height of triangle";
    cin >> height;
    for(int i=1; i<=height; i++)
    {
        cout << string(i, '*') << endl;            
    }
}

You could do this. This uses the length of the string directly instead of a separate counter i.

#include <iostream>
#include <string>

int main() {
    int height;
    std::cout << "Enter height of triangle";
    std::cin >> height;
    
    for(std::string stars = "*"; stars.length() <= height; stars += "*")
    {
        std::cout << stars << std::endl;
    }
    return 0;
}

I don't really understand the reason for your question, but hey, why not:

#include <iostream>
#include <string>

int main() {
    int height;
    std::cout << "Enter height of triangle";
    std::cin >> height;
    std::string stars = "*";

    for(int i=1; i<=height; i++)
    {
        std::cout << stars << std::endl;
        stars += "*";
    }

    return 0;
}

No loops:

void stars(int height) {
  if (!height) return;
  stars(height-1);
  std::cout << std::string(height, '*') << '\n';
}

int main() {
  int height;
  std::cout << "Enter height of triangle";
  std::cin >> height;

  stars(height);
}

Or to not use the loop in std::string(height, '*') as idz suggested:

void stars(int height, std::string &s) {
  if (!height) return;
  std::cout << s << '\n';
  stars(height-1, s+='*');
}

int main() {
  int height;
  std::cout << "Enter height of triangle\n";
  std::cin >> height;

  std::string s = "*";
  stars(height, s);
}

Here is a C inspired solution. Don't write C++ like this. It's essentially just to show another way to do it than the already posted solutions:

#include <iostream>

int main() {
  int height;
  std::cout << "Enter height of triangle";
  std::cin >> height;

  char *arr = new char[height];
  std::fill(arr, arr+height+1, '\0');

  for(int i=0; i<height; i++) {
      arr[i] = '*';
      std::cout << arr << std::endl;
  }

  delete arr;
}

Not necessarily a good solution, but it's an option:

#include <iomanip>
#include <iostream>

int main() {
    int height;

    std::cout << "Enter height of triangle";
    std::cin >> height;
    
    std::cout << std::setfill('*'); // fill the default blank spaces with * instead

    ++height;
    for(int i = 2; i <= height; ++i) {
        std::cout << std::setw(i) << '\n'; // fill width i until the newline
    }
}

first of all it's not a half triangle it's a triangle or for you we can count that a half rectangle but leaving that one here what you are looking for is most likely this way

#include <bits/stdc++.h>

for making it easier

    using namespace std ;
    /*you can always use std::"command" when you ain't using absolute standard name spaces but leaving that one there*/

    int main ()
{     

   
        for ( string s = "Symbol" ; s.length() <= h ;s += "Symbol" )
    {
        cout << s << "\n" ;
    }
    return 0 ;
}  
    
Related