Computer >> Máy Tính >  >> Lập trình >> lập trình C

Chương trình in các cột số khôn ngoan trong C

Mô tả chương trình

In các cột số tự nhiên như hình dưới đây

1
2 6
3 7 10
4 8 11 13
5 9 12 14 15

Thuật toán

i stands for rows and j stands for columns.
5 stands for making pattern for 5 Rows and Columns
Loop for each Row (i)
K is initialized to i
Loop for each Column (j)
Do the Pattern for the current Column (j)
Display the Value of K
Reinitialize the Value of K = k + 5 - j

Ví dụ

/* Program to print the Natural Numbers in Columns wise */
#include<stdio.h>
int main(){
   int i,j,k;
   printf("Printing the Numbers in Columns wise: ");
   printf("\n");
   printf("\n");
   for(i=1;i<=5;i++){
      k = i;
      for(j=1;j<=i;j++){
         printf("%d ", k);
         k += 5-j;
      }
      printf("\n");
   }
   getch();
   return 0;
}

Đầu ra

Chương trình in các cột số khôn ngoan trong C