문제
피라미드, 직각 삼각형과 같은 다른 형식으로 숫자를 인쇄하는 C 언어의 논리는 무엇입니까?
해결책
다른 모델의 숫자나 기호를 인쇄하려면 코드에서 for 루프를 사용할 수 있습니다.
예시 1
다음은 피라미드를 인쇄하는 C 프로그램입니다 -
#include<stdio.h>
int main(){
int n;
printf("Enter number of lines: ");
scanf("%d", &n);
printf("\n");
// loop for line number of lines
for(int i = 1; i <= n; i++){
// loop to print leading spaces in each line
for(int space = 0; space <= n - i; space++){
printf(" ");
}
// loop to print *
for(int j = 1; j <= i * 2 - 1; j++){
printf(" * ");
}
printf("\n");
}
return 0;
} 출력
Enter number of lines: 8 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
예시 2
다음은 직각삼각형(패턴)의 형태로 숫자를 표시하는 프로그램입니다 -
#include <stdio.h>
void main(){
int i,j,rows;
printf("Input number of rows : ");
scanf("%d",&rows);
for(i=1;i<=rows;i++){
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
} 출력
Input number of rows : 10 1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910