-->





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

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

40 comments:

  1. Replies
    1. Aslam-U-Alaikum bhai.
      how can I print M with stearic (*).
      can u help me?

      Delete
  2. Can anybody help me to solve this program
    *******
    *
    *
    *
    *
    *
    *
    *
    *
    *******

    ReplyDelete
  3. 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
  4. 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
  5. 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
  6. 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
  7. dndu can put the answer of your question .

    ReplyDelete
  8. 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
  9. 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
  10. This comment has been removed by the author.

    ReplyDelete
  11. 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
  12. 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
  13. plz give the code for following output.

    1
    3 5
    7 9 11
    13 15 17 19

    ReplyDelete
  14. pls give code for the following output

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

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

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

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

      Delete
    2. import java.util.Scanner;
      class Task8{
      public static void main(String[] args)
      {

      Scanner user_input=new Scanner(System.in);
      System.out.println("enter number row :");
      int col;
      col = user_input.nextInt();
      for(int i = 1; i <= col; ++i) {
      for(int j = (col-1); j >= i; --j)
      System.out.print(" ");

      for(int k = 1; k <= i; ++k)
      System.out.print(k);

      System.out.println();
      }
      }
      }

      Delete
  17. How to get this pattern
    1 1 1
    2 2
    3

    ReplyDelete
  18. How to get this pattern
    1 1 1
    2 2
    3

    ReplyDelete
  19. 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
  20. This comment has been removed by the author.

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

    ReplyDelete
  22. hello,

    can someone help me with this:

    ^^^^*
    ^^^**
    ^^a**
    ^*b**
    **c**
    ^*d**
    ^^e**
    ^^^**
    ^^^^*

    (^ is represented as space)

    and can you recommend any websites that shows more example on adjusting the star pattern instead of just simple pattern.

    thank you!

    ReplyDelete
  23. how to
    1
    12
    123
    1234
    1234
    and
    1234321
    123 321
    12 21
    1 1

    ReplyDelete
  24. Design a complete C++ program to print out the outputs as shown below. The program must use
    nested for loop and if else statement.
    * * * *
    # # #
    * *
    #

    ReplyDelete
  25. i need this please urgent within 6 hours

    12345
    2345
    345
    45
    5

    ReplyDelete
  26. plxxxxxxx give me codd for this......

    1 2 3
    5 10 15
    25 50 76

    ReplyDelete