Ad

Wednesday, February 20, 2013

Age Calculator by using C program


Assalamu Alaikum, how are you everyone ? Hope all are well. I'm back here again. Today I'm introducing a useful program. The task of this program is to calculate age from a date to another date. Hope, it would be helpful for new programmers.. Let's see..................


#include<stdio.h>

main()
{

    int current_date, current_month, current_year, birth_date, birth_month, birth_year, day, month, year;

    start:;
    printf("\nEnter Current date, month and year(date month year) : ");
    scanf("%d%d%d", &current_date, &current_month, &current_year);

    if(current_date < 1 || current_date > 31 || current_month < 1 || current_month > 12 || ((current_month==4 || current_month==6 || current_month==9 || current_month==11) && (current_date > 30)) || ((current_month==2 && ((current_year % 4 == 0 && current_year % 100 != 0) || (current_year % 400 == 0)) && current_date>29)) || (current_month==2 && ((current_year % 4 != 0 && current_year % 100 == 0) || (current_year % 400 != 0)) && (current_date>28)))
    {
        printf("\n\nSomething is Wrong !! Enter Correct Date or Month or Year.\n\n\n");
        goto start;
    }
    else
    {
        start2:;
        printf("\nEnter the Birth date, month and year(date month year) : ");
        scanf("%d%d%d", &birth_date, &birth_month, &birth_year);

        if(birth_date < 1 || birth_date > 31 || birth_month < 1 || birth_month > 12 || ((birth_month==4 || birth_month==6 || birth_month==9 || birth_month==11) && (birth_date>30)) || ((birth_month==2 && ((birth_year % 4 == 0 && birth_year % 100 != 0) || (birth_year % 400 == 0)) && birth_date>29)) || ((birth_month==2 && ((birth_year % 4 != 0 && birth_year % 100 == 0) || (birth_year % 400 != 0)) && birth_date>28)) || birth_year > current_year)
        {
            printf("\n\nSomething is Wrong !! Enter Correct Date or Month or Year.\n\n\n");
            goto start2;
        }
        else
        {
            if((current_date < birth_date) && (birth_month == 1 || birth_month == 3 || birth_month == 5 || birth_month == 7 || birth_month == 8 || birth_month == 10 || birth_month == 12))
            {
                day = (current_date + 31) - birth_date;
                birth_month++;
            }
            else if((current_date < birth_date) && (birth_month == 4 || birth_month == 6 || birth_month == 9 || birth_month == 11))
            {
                day = (current_date + 30) - birth_date;
                birth_month++;
            }
            else if((current_date < birth_date) && (birth_month == 2))
            {
                if((birth_year % 4 == 0 && birth_year % 100 != 0) || (birth_year % 400 == 0))
                {
                    day = (current_date + 29) - birth_date;
                    birth_month++;
                }
                else
                {
                    day = (current_date + 28) - birth_date;
                    birth_month++;
                }
            }
            else
            {
                day = current_date - birth_date;
            }


            if(current_month < birth_month)
            {
                month = (current_month + 12) - birth_month;
                birth_year++;
            }
            else
            {
                month = current_month - birth_month;
            }
            year = current_year - birth_year;


        printf("\n\nThe Age is : %d year(s), %d month(s), %d day(s).\n\n", year, month, day);
        }
    }

    printf("\n\nPress any key to stay or Press \'0\' (zero) to leave.");
    char ch = getch();
    printf("\n\n");
    if(ch!= '0')
    {
        goto start;
    }
    else
    {
        printf("\n\t\t\tThanks.\n");
    }
getch();
return 0;
}

Just copy and paste it into your compiler and save it with .c extension....
If you have any problem, feel free to comment here. Thanks to All.

0 comments:

Post a Comment