-->




C++ code to print right triangle shape using nested for loops

32 comments
Concept used: nested for loop
Post Contains: 


  • C++ code 
  • Line by line variable values and output
  • Image view of code
  • Image view of sample output of program

    C++ Code:

#include<iostream>
using namespace std;
int main()
{
   for(int i=0;i<=5;i++){

        for(int j=0;j<=i;j++)
            {
                cout<<j;
            }
       cout<<endl;
    }
 return 0;
}



Line by line variable values and output

i=0 
j=0
output
         0


i=1
j=0 to j=1
output 
          0
          0  1

i=2
j=0 to j=2
output 

          0
          0  1
          0  1  2 

i=3
j=0 to j=3
output


          0
          0  1
          0  1  2 
          0  1  2  3
   
i=4
j=0 to j=4

output 


          0
          0  1
          0  1  2 
          0  1  2  3
          0  1  2  3  4
   

i=5
j=0 to j=5

final output: 


          0
          0  1
          0  1  2
          0  1  2  3
          0  1  2  3  4
          0  1  2  3  4  5



Image view of program:
shape code to print right triangle using for loop
c++ shape code to print right triangle using for loop

Sample output image view: 
sample output cplusplus shape code to print right triangle using for loop
sample output shape code to print right triangle using for loop

32 comments:

  1. Can anybody help me to solve this program
    *******
    *
    *
    *
    *
    *
    *
    *
    *
    *******

    ReplyDelete
  2. plx giv me
    1
    2 2 2
    3 3 3 3 3
    4 4 4 4 4 4

    ReplyDelete
    Replies
    1. You Can Find Codes Like This Here.
      http://fahad-cprogramming.blogspot.com/p/c-shapes-code.html

      Delete
    2. #include
      using namespace std;
      int main()
      {
      for(int i=0;i<=5;i++){

      for(int j=0;j<=i;j++)
      {
      cout<<i;
      }
      cout<<endl;
      }
      return 0;
      }

      Delete
  3. 1. Write a program that prints the following pattern where n is taken as input:
    n = 2
    1
    21
    n = 3
    1
    21
    321
    n = 4
    1
    21
    321
    4321
    And so on

    ReplyDelete
    Replies
    1. Write this code frd
      For(I=0;I<5;I++)
      {
      For(j=I;j>=1;j++)
      {
      Printf("%d",j);
      }
      Printf("\n");
      }

      Delete
  4. dare make program that your output will look like this
    1
    2 6
    3 7 10
    4 8 11 13
    5 9 12 14 15

    ReplyDelete
  5. How can I write a program that will let the user inter a number (i,e 4) and outputs the followings using a nested for loop?
    1
    12
    123
    1234
    123
    12
    1
    another example say the user inters a number 2
    1
    12
    1

    ReplyDelete
  6. dndu can put the answer of your question .

    ReplyDelete
  7. A solution to Dndu's question

    #include
    using namespace std;

    int main()
    {
    int num, out = 0;
    cout << "Give a number: ";
    cin >> num;

    for (int i = 1; i <= 2*num-1; i++){

    cout << 1 << " ";

    for (int j = 2; j <= i && ((j <= num && i <= num) || (i > num && j <= 2*num-i)); j++)
    {
    out = j;
    cout << out;
    cout << " ";

    }

    cout << endl;
    }

    return 0;
    }

    ReplyDelete
  8. A solution for papeeu's "challenge"

    #include
    using namespace std;
    int main()
    {
    int rows, out = 0, temp;
    cout << "Give a number: ";
    cin >> rows;

    for (int i = 1; i <= rows; i++){
    temp = i;
    cout << i << " ";
    for (int j = 1; j < i; j++)
    {
    out = temp + rows - j;
    cout << out;
    cout << " ";
    temp = out;
    }
    cout << endl;
    }

    return 0;
    }

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Write a C++ program that will print the pattern as shown below:
    Output:
    *
    ***
    *****
    *******
    *********
    *********
    *******
    *****
    ***
    *

    ReplyDelete
    Replies

    1. using namespace std;
      int main()
      {

      char e=42;

      for(int i=0; i<=5;i++){
      for(int a=0;a<=i; a++)
      {cout<<" * ";}
      cout<<" "< 0; i--){
      for(int a=0;a<i; a++)
      {cout<<" * ";}
      cout<<" "<<endl;
      }

      cout <<"\n programmed by @kangemah ...tweeter.........................."<<endl;
      return 0;
      }

      Delete
  11. how to print like this

    1
    22
    333
    4444
    55555
    ??

    ReplyDelete
    Replies
    1. #include
      using namespace std;
      int main()
      {
      for(int i=1;i<=5;i++){

      for(int j=1;j<=i;j++)
      {
      cout<<i;
      }
      cout<<endl;
      }
      return 0;
      }

      Delete
  12. plz give the code for following output.

    1
    3 5
    7 9 11
    13 15 17 19

    ReplyDelete
  13. pls give code for the following output

    A
    A B
    A B C
    A B C D
    A B C D E

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. ****1
      ***21
      **321
      *4321
      54321
      can u declare space instead of star?

      Delete
  16. Hi can you answer a question from me? How do i do this by using for loop?

    Enter a first number
    Cin>>a;example is 1
    Enter second number
    Cin>>b;//example is 5

    The out put should be
    54321
    4321
    321
    32
    1

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. Let start a program
    In array you enter 5 array and you enter number
    What the number will be plus

    ReplyDelete