Program 7) This program shows the use of integer airthmetic to convert a given number of days into month and days.
PROGRAM
main( )
{
int months, days ;
printf("Enter days\n") ;
scanf("%d", &days) ;
months = days/30 ;
days = days % 30 ;
printf("Months = %d Days = %d, months, days) ;
getch( ) ;
}
Output: Enter days
265
months = 8 days = 25
Enter days
364
Months = 12 Days = 4
Enter days
45
Months = 1 days = 15
The variables months and days are declared as integers. Therefore, the statement
months = days/30;
truncates the decimal part and assigns the integer part to months. Similarly, the statement
days = days%30 ;
assigns the remainder part of the divison to days. Thus the given numberof days in converted into an equivalent number of months and days and the result is printed as shown in the output.
No comments:
Post a Comment