berikut adalah program penghitung umur sederhana dengan bahasa C, program ini akan menanyakan nama user, lalu meminta input tanggal lahir dan sekarang, sekaligus memberi komentar
#include <stdio.h> #include <stdlib.h> #include <string.h> /*age counter program*/
int main() { /*variable declaration*/ char name[32]; int bday, bmonth, byear; int tday, tmonth, tyear; int tdate, bdate; int day, month, year; /*user input*/ printf ("who are you \n"); scanf ("%s[^\n]", name); /*input birth day*/ printf ("input birth day \n"); scanf ("%d", &bday); printf ("input birth month \n"); scanf ("%d", &bmonth); printf ("input birth year \n"); scanf ("%d", &byear); /*input recent day*/ printf ("input recent day \n"); scanf ("%d", &tday); printf ("input recent month \n"); scanf ("%d", &tmonth); printf ("input recent year \n"); scanf ("%d", &tyear); /*algorhytm processing*/ bdate=bday+(bmonth*30)+(byear*365); tdate=tday+(tmonth*30)+(tyear*365); /*result processing*/ year=(tdate-bdate)/365; month=(tdate-bdate)%365/30; day=(tdate-tmonth)%365%30; /*result*/ printf("%s %d %d %d", name, day, month, year); /*comment about age*/ if (year <= 5) { printf ("\n you're still baby \n"); } else if (year <= 17) { printf ("\n you're still young\n"); } else if (year <= 40) { printf ("\n you're an adult \n"); } else if (year <= 99) { printf ("\n you're old enough \n"); } else { printf ("\n are you serious? \n"); } getchar(); return 0; }
|
0 komentar