-->





C++ code to display filled isosceles triangle using for loop and asterisk character

Write a program which draw isosceles Triangle Shape with asterisk(*) and using nested for loop
 Program required output:

display  isosceles triangle using for loop and asterisk character




C++ Code:


#include<iostream>
using namespace std;

int main()
{

  int e=1;

  cout<<"\" Triangle Shape SHAPE \":\n\n";

  for(int a=1;a<=5;a++)
 {
    for(int b=4;b>=a;b--)
   {
    cout<<" ";  // Printing Space Here
   }
      for(int c=0;c<e;c++)
     {
       cout<<"*";  // Printing asterisk here
     }
      cout<<endl;   // new line
      e=e+2;
 }
}



Program Output:
sample output filled isosceles triangle asterisk character




More C++ Shapes Here


These kind of programs are very helpful in the understanding of nested for loop.

9 comments:

  1. #include
    using namespace std;
    int main()
    {
    for(int i=1;i<=5;i++)
    {
    for(int j=4;j>=i;j--)
    cout<<" ";
    for(int k=1;k<=2*i-1;K++)
    cout<<"*";
    cout<<endl;
    }
    return 0;
    }

    ReplyDelete
  2. I use this code and the output is just a half triangle. Whatto do with that?

    ReplyDelete
  3. I use this code and the output is just a half triangle. Whatto do with that?

    ReplyDelete
  4. so what does it mean e=e+2; in this example above and why cstart 0 and e starts 1

    for(int c=0;c<e;c++)
    {
    cout<<"*"; // Printing asterisk here
    }
    cout<<endl; // new line
    e=e+2;

    ReplyDelete
    Replies
    1. e increments by two (e=e+2) because if you notice, on line #1 there is one "*", then on line #2 there are 3 "*", then on line #3 there are 5 "*" and so on... (if you want to count the odds numbers, start at one and increment of 2 every time (1,3,5,7,9,...))

      c starts at 0 because there is step between 0 and 1 (e starts at one) and then one line #2, e = 3 and there is 3 steps between 0 and 3 and so on... So this is juste a way to always do the right amount of recursion loop.

      Delete
  5. plz reply to me i have an assigment coz ididnot understood nested loop for writng the shapes plzzzz clarify to me each and every thing and each for loop and its function

    ReplyDelete
    Replies
    1. At this point, I would just recommand to you that you copy this code and use the debugger to see what each line does...

      Basically the loop using the int a and the int b are there to control the number of spaces and then the for(int c=0;c<e;c++) is there to control the amount of "*" that you need for each line

      because there are 5 lines, you know the last line is 9 times "*" (the 5th odd number is 9). so if you what to have an isoscele triangle,
      on line #1 : 4 spaces + 1 "*"
      on line #2 : 3 spaces + 3 "*"
      on line #3 : 2 spaces + 5 "*"
      on line #4 : 1 space + 7 "*"
      on line #5 : 0 space + 9 "*"

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. /* This one ask the user how many lines he wants and print them in the console*/
    #include
    using namespace std;

    int main() {
    int e=1;
    int n;
    cout<< "How many lines ?" << endl;
    cin >> n;

    for(int a=1;a<=n;a++) {
    for(int b=(n-1);b>=a;b--) {
    cout<<" ";
    }
    for(int c=0;c<e;c++) {
    cout<<"*";
    }
    cout<<endl;
    e=e+2;
    }
    }

    ReplyDelete