can anyone tell me the code for this?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vp1
    New Member
    • Oct 2006
    • 4

    can anyone tell me the code for this?

    write nested loops (while or for) to print a trun-
    cated (missing a few lines at the top) triangular figure using
    integers m and k. This figure is made up of stars and spaces. First
    choose smaller of m and k, and call it top. Call the other number
    height. For this truncated triangle, top tells you how many stars
    (`*') are there on the top line. Height tells you, how many lines are
    there. For the above example, m = 3 and k = 6 So top is 3 and height
    is 6. Now print a truncated triangular figure of stars (*) as shown.
    Here top line has 3 stars and truncated triangle has 6 lines.

    m = 3, k = 6, top = 3, height = 6

    ***
    *****
    *******
    *********
    ***********
    *************
  • smartway
    New Member
    • Oct 2006
    • 24

    #2
    try the code given below and let me know the result
    for (i=1;i<=k;i++)
    {
    for (j=1;j<=m;j++)
    cout<<"*";
    cout<<endl;
    m+=2;
    }

    Comment

    • vp1
      New Member
      • Oct 2006
      • 4

      #3
      thanks.but its not giving the truncated form.
      **
      ****
      like the above one.with every line two spaces are decreasing and a stars are increasing.

      Comment

      • smartway
        New Member
        • Oct 2006
        • 24

        #4
        it is working fine on my side can you please give the code

        Comment

        • D_C
          Contributor
          • Jun 2006
          • 293

          #5
          Code:
          for (i=1;i<=k;i++)
          {
            for (j=1;j<=m;j++)
            {
              cout<<"*";
            }
            cout<<endl;
            m+=2;
          }
          is how it should look. Maybe the whitespace was lost and you put more into the nested for loop than there should be?
          Code:
          for(int i = 0; i < k; i++)
          {
            cout << string(m,'*') << endl;
            m += 2;
          }
          should do the same thing.

          Comment

          Working...