Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

c programming triangles?

this is oneof the four triangles i have to make. i cant come up with with any logical set of steps that would lead to this. as far as i can tell there are 3 variables, but nobody gives them actual names for me to work off of. i need to use a for loop for each. help

*

**

***

****

*****

3 Answers

Relevance
  • Anonymous
    8 years ago
    Favorite Answer

    #include<stdio.h>

    #include <math.h>

    int main()

    {

    unsigned int nh = 7; // set height (7 as an example)

    unsigned int nw = 16; // set width (16 as an example)

    float theta = atan(nw/nh);

    unsigned int i,j,c;

    for (i = 0; i < nh; i) {

    if (i == 0)

    c = 1;

    else

    c = (i 1)*round(tan(theta));

    for (j = 0; j < c; j)

    printf("%c",'*');

    printf("%c",'\n');

    } // i

    return 0;

    }

  • 8 years ago

    sure, first you need the number of rows, let's call it "n", then you need a "for" loop to create each of the rows. You may use a variable called "i" to iterate, like for (i=1; i<=n; i++).

    Then you need an inner for loop to write the "*"s, but you need to iterate from 1 to i this time, so you may use another variable, call it "j", like for (j=1; j<=i; j++)

    The rest sould be easy.

  • Chris
    Lv 7
    8 years ago

    The idea is two use two nested for loops.

    Like this (pseudo code):

    size = 5;

    for (i = 1; i <= size; i++) {

    for (j = 1; j <= i; j++) {

    print "*";

    }

    print "\n"; // end line

    }

Still have questions? Get your answers by asking now.