본문 바로가기
C and C++/C and C++ Examples

[C Examples] 입력한 온도 값에 따라 온도 상태 구분하기 예제 코드: if(), else if(), while()

by Henry Cho 2023. 3. 22.
728x90

입력한 온도 값에 따라 온도 상태 구분하기 예제 코드: if(), else if(), while()

포스트 난이도: HOO_Junior


# Example codes

#include <stdio.h>

int main(){
    
    int temperature[50];
    int n,i,hot=0,pleasant=0,cold=0;
    float average=0;
    
    printf("Enter number of temperatures:");
    scanf("%d",&n);
    
    
    printf("Enter Temperatures: ");
    
    while(i<n){
        scanf("%d",&temperature[i]);
        i++;
    }
    
    i=0;
    while(i<n) {
    
        if(temperature[i]>=85)
        hot++;
        
        else if(temperature[i]>=60&&temperature[i]<85)
        pleasant++;
        
        else if(temperature[i]<60)
        cold++;
        
        i++;
    }
    
    printf("Number of Hot Days are: %d\n",hot);
    i=0;
    while(i<n){
    
        if(temperature[i]>=85)
        printf("%d ",temperature[i]);
        i++;
    }
    
    printf("\nNumber of Pleasant Days are: %d\n",pleasant);
    i=0;
    while(i<n){
    
        if(temperature[i]>=60 && temperature[i]<85)
        printf("%d ",temperature[i]);
        i++;
    }
    
    printf("\nNumber of Cold Days are: %d\n",cold);
    i=0;
    while(i<n){
    
        if(temperature[i]<60)
        printf("%d ",temperature[i]);
        i++;
    }
    printf("\n Coded by HOO.");
    return 0;

}

# Results

Enter number of temperatures:3
Enter Temperatures: 40 60 80
Number of Hot Days are: 0

Number of Pleasant Days are: 2
60 80 
Number of Cold Days are: 1
40 
 Coded by HOO.

 

Enter number of temperatures:5
Enter Temperatures: 20 90 100 78 74 65
Number of Hot Days are: 2
90 100 
Number of Pleasant Days are: 3
78 74 65 
Number of Cold Days are: 0

 Coded by HOO.

 

728x90

댓글