Skip to search.

Breaking News Visit Yahoo! News for the latest.

×Close this window

  1. Home >
  2. All Categories >
  3. Computers & Internet >
  4. Programming & Design >
  5. Resolved Question
eman911 eman911
Member since:
February 09, 2007
Total points:
116 (Level 1)

Resolved Question

Show me another »

C++ program that makes an equilateral triangle using stars??????

i tried my best bu ti could not find the right answer fo r that question..
i want to write a c++ programm that will print the following on the console:
*
***
*****
it should have the equilateral triangle pattren
please help me????????/

Additional Details

i tried your progrm <jman> well,it didn't have the equilateral triangle pattren

5 years ago

cja by cja
Member since:
January 28, 2008
Total points:
33,928 (Level 7)

Best Answer - Chosen by Voters

See my code below for a C++ solution to the problem. Only one 'for' loop is needed, if you take advantage of what iomanip can do for you. If you consider the spaces between the lines as contributing a unit of length, then it's an equilateral triangle. Otherwise, it's isosceles.

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
int n;
const char star('*');
const char space(' ');

if ((argc < 2) || (sscanf(argv[1],"%d",&n) != 1)) {
cout << "usage : " << argv[0] << " <n>" << endl;
cout << " where n is an integer" << endl;
return -1;
}
n = abs(n);
for (int i = 0; i < n; i++) {
cout << setfill(space) << setw(n-i) << space;
cout << setfill(star) << setw(i*2+1) << star << endl;
}
return 0;
}
100% 1 Vote
  • 1 person rated this as good

There are currently no comments for this question.

Other Answers (2)

  • jman by jman
    Member since:
    March 03, 2008
    Total points:
    1,555 (Level 3)
    You are right, the previous code didnt work....didnt test it before i wrote it. Here is some revised code

    for (i=0; i<6; i++)
    {
    if (i mod 2 = 0)
    {
    for (n = 0; n < i +1; i++)
    {print ("*");}
    }
    print line method
    }

    I tested this in vb.net and it gave me the exact printout you have above. I just took out the not equals and updated the second loop to have a different index variable.....the two i's wont work well. Update the code you tried and it will work.

    Source(s):

    your momma
    0% 0 Votes
  • Badlass by Badlass
    Member since:
    February 27, 2008
    Total points:
    803 (Level 2)
    It looks like you want to print an extra 2 stars every line. Based on that, the following works, where lines is the number of lines you want to print.

    #include <iostream>
    int main() {
    int lines = 3;

    for(int i = 0; i < lines; i++) {
    for(int j = 0; j < (2*i+1); j++) {
    std::cout << '*';
    }
    std::cout << std::endl;
    }
    }
    0% 0 Votes

Answers International

Yahoo! does not evaluate or guarantee the accuracy of any Yahoo! Answers content. Click here for the Full Disclaimer.

Help us improve Yahoo! Answers. Send Feedback