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

[C Examples] 24시간을 12시간 표기로 바꾸기 예제코드: if(), else

by Henry Cho 2023. 2. 18.
728x90

24시간을 12시간 표기로 바꾸기 예제코드: if(), else

포스트 난이도: HOO_Intern


# Example Codes

 

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int userHour, userMinute, hour;

    printf("Time(e.g., 23:15):");
    scanf("%d:%d", &userHour, &userMinute);


    if (userHour > 11)
    {
       hour = userHour - 12;
       if(hour == 12){
           printf("Time is  00:%d AM\n", userMinute);
       }
       else{
           printf("Time is %d:%d PM\n", hour, userMinute);
       }
    }
   else{
        printf("Time is %d:%d AM\n", userHour, userMinute);
    }
    printf("Coded by HOO.\n");
    return 0;
}

Time(e.g., 23:15):24:15
Time is  00:15 AM
Coded by HOO.

Time(e.g., 23:15):15:36
Time is 3:36 PM
Coded by HOO.

 


 

728x90

댓글