58 : C Program to display half triangle/pyramid of number sequence 6

Check out the complete list of c-programs : C Program List    





We have seen how to make a half pyramid (a right angle triangle) using asterisk's now we have to use numbers instead. Rows are numbered from 1 to 9, to achive this we require two for loops and instead of * we print out the row number (i)

To get this, we need to have 2 nested loops. One for row and second to print the numbers for the row.

To achive this you need to have 3 nested for loops.

1 

2 2 

3 3 3 

4 4 4 4 

5 5 5 5 5 

6 6 6 6 6 6 

7 7 7 7 7 7 7 

8 8 8 8 8 8 8 8 

9 9 9 9 9 9 9 9 9 




Variables :

i : Datatype int : Used in for loop1.

j : Datatype int : Used in for loop2.



Logic :


  for(i=1;i<10;++i)
    {
        for(j=1;j<=i;++j)
        {
           printf("%d ",i);
        }
        printf("\n");
    }







/*
 * 1000+ C programs + tutorials
 *
 * 
 * 57_c_program_to_display_half_pyramid_of_numbers_sequence_4
 *  
 *
 *
 *  Created on: Oct 22, 2014
 *  Author: Code2care.org
 */

#include 
//#include <conio.h>

void main() {


//    clrscr();

    int i = 0; //for loop1
    
    int j = 0; //for loop2

  
    for(i=1;i<10;++i)
    {
        for(j=1;j<=i;++j)
        {
           printf("%d ",i);
        }
        printf("\n");
    }
    

//	getch();


}









The best way to learn C programming is to practice more and more of programs . Code2care C Programming tutorials provide 1000+ programs in C that you can study and become an expert in the language. Programs are divided into categories depending upon type and complexity.

BSc. IT, BSc. Computer Science, BSc. IT, MSc. IT, MSc. Computer Science, MCA, BTech IT & BTech Engineers may find these programs very useful.