Search code examples
cpalindrome

Write a program to output all palindromic times displayed by a 24 hour digital clock (e.g. 13:31)


I am attempting to write a program that will output all palindromic times show below using C-Programming:

00:00
01:10
02:20
03:30
04:40
05:50
10:01
11:11
12:21
13:31
14:41
15:51
20:02
21:12
22:22
23:32

I know to treat the hours and the minutes as two separate variables rather than as an actual time. Also there will be a few counters to increment the hours and minutes but not sure exactly where to head any ideas is much appreciated?


Solution

  • This is the perfect answer you want

        #include <stdio.h>
    
    int pallindrome(int loop)
    {
        if (loop >= 10)
        {
            int temp, temp1 = 0, sum = 0;
            for(temp = 0; temp < 2;temp++)
            {
                temp1 = loop % 10;
                loop = loop / 10;
                sum = sum * 10 + temp1;
            }
            return(sum);
        }
        else
        {
            return(loop * 10);
        }
    }
    int main(void) {
        // your code goes here
        int a=0;
        int loop=0;
        for (loop=0; loop<=59; loop++)
        {
            a = pallindrome(loop);
            if (a < 60)
            printf("%.2d:%.2d\n", loop,a);  
        }
        return 0;
    }
    

    See the output here: http://ideone.com/54ouHv