Q25 Write a program to print 24 hours of day with suitable suffixes like AM, PM, Noon and Midnight.

Write a program to print 24 hours of day with suitable suffixes like AM, PM, Noon and Midnight.

Program: 118

Write a c program to print 24 hours of day with suitable suffixes like AM, PM, Noon and Midnight.

#include<stdio.h>
#include<conio.h>
int main()
{
    int h;

    //for-loop for 24 hours
    for(h=0;h<=23;h++)
    {
        if(h==0)
        {
            printf("12 Midnight\n");
            continue;
        }

        if(h<12)
        {
            printf("%d AM\n",h);
        }


        if(h==12)
        {
            printf("%d Noon\n",h);
        }


        if(h>12)
        {
            //h=h%12;
            printf("%d PM\n",h%12);
        }

    }
}

Output:

12 Midnight
1 AM
2 AM
3 AM
4 AM
5 AM
6 AM
7 AM
8 AM
9 AM
10 AM
11 AM
12 Noon
1 PM
2 PM
3 PM
4 PM
5 PM
6 PM
7 PM
8 PM
9 PM
10 PM
11 PM

Lokesh Kumar: Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.

View Comments (1)

  • Can be optimize as:

    #include <stdio.h>
    int main()
    {
    int i,j;
    printf("12 Midnight\n");
    for(i=1; i<=11; i++)
    printf("%d AM\n",i);
    printf("12 NOON\n");
    for(j=1; j<=11; j++)
    printf("%d PM\n", j);
    }

Related Post
Leave a Comment

This website uses cookies.