Computer >> 컴퓨터 >  >> 프로그램 작성 >> C 프로그래밍

3x3 2D 배열에서 아래쪽 삼각형 요소만 표시하는 C 프로그램

<시간/>

런타임에 키보드를 사용하여 2D 배열에서 총 9개의 요소를 의미하는 3x3 행렬을 입력해 보겠습니다.

그것과 for 루프의 도움으로 3X3 행렬에서 아래쪽 삼각형만 표시할 수 있습니다.

하단 삼각형 요소를 인쇄하는 논리 다음과 같습니다 -

for(i=0;i<3;i++){
   for(j=0;j<3;j++){
      if(i>=j) //lower triangle index b/s 1st index>=2nd index
         printf("%d",array[i][j]);
      else
         printf(" "); //display blank in non lower triangle places
   }
   printf("\n");
}

프로그램

다음은 3x3 2D 배열에서 아래쪽 삼각형 요소만 표시하는 C 프로그램입니다. -

#include<stdio.h>
int main(){
   int array[3][3],i,j;
   printf("enter 9 numbers:");
   for(i=0;i<3;i++){
      for(j=0;j<3;j++)
         scanf("%d",&array[i][j]);
   }
   for(i=0;i<3;i++){
      for(j=0;j<3;j++){
         if(i>=j) //lower triangle index b/s 1st index>=2nd index
            printf("%d",array[i][j]);
         else
            printf(" "); //display blank in non lower triangle places
      }
      printf("\n");
   }
   return 0;
}

출력

출력은 다음과 같습니다 -

enter 9 numbers:
1 2 3
1 3 4
4 5 6
1
13
456

주어진 3X3 행렬 형식에 대해 위쪽 삼각형을 인쇄할 수 있는 다른 프로그램을 고려하십시오.

예시

#include<stdio.h>
int main(){
   int array[3][3],i,j;
   printf("enter 9 numbers:");
   for(i=0;i<3;i++){
      for(j=0;j<3;j++)
         scanf("%d",&array[i][j]);
      }
      for(i=0;i<3;i++){
         for(j=0;j<3;j++){
            if(i<=j) //upper triangle
               printf("%d",array[i][j]);
            else
               printf(" "); //display blank in lower triangle places
         }
         printf("\n");
   }
   return 0;
}

출력

출력은 다음과 같습니다 -

enter 9 numbers:
2 3 4
8 9 6
1 2 3
2 3 4
  9 6
    3