Skip to content
Solved Examples · Example 8

Q.The program shows the use of integer arithmetic to convert a given number of days into months 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);  
}  

Karnataka PUCTextbookLong· 5mImportance★★★★★est
25% · 39/154 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

[!TLDR]

Divide the number of days by 30 to get whole months and take the remainder (days % 30) to get the leftover days.

The program reads a number of days and converts it, treating 30 days as one month. Two integer operators do the work: integer division / truncates any fractional part, so days/30 is the number of complete months; the modulus operator % returns the remainder of that division, so days%30 is the number of days left over. A complete, compilable version is:

#include <stdio.h>

int main(void)
{
    int months, days;
    printf("Enter days\n");
    scanf("%d", &days);
    months = days / 30;
    days = days % 30;
    printf("months=%d days=%d", months, days);
    return 0;
}
``` …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.