PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Basic C Program

Program to convert number of days into years, months and days

Input - Output

Input
Enter a number of days : 781

Output
No. of years = 2

No. of months= 1

No. of days= 21

Algorithm

Step 1: START

Step 2: input number of "days" as input.

Step 3: declare a third variable as years and months.

Step 4: calculate years=days/365

Step 5: calculate the remaining days=days%365

Step 6: calculate months=days/12

Step 7: calculate the remaining=days%12

Step 8: Print the years , months and days

Step 9: END

CODE


/* convert days into years ,months and days */

#include  <stdio.h>
int main()
{
      int days,years,months;
      printf("Enter number of days: ");
      scanf("%d",&days);
      years=days/365;
      days=days%365;
      months=days/30;
      days=days%30;
      printf("No.of years = %d\n",years);
      printf("No.of months = %d\n",months);
      printf("No.of days = %d\n",days);
    return 0;
}


/* convert days into years , months , days */

#include  <iostream>
using namespace std;
int main()
{
      int days,years,months;
      cout<<"Enter number of days: ";
      cin>>days;
      years=days/365;
      days=days%365;
      months=days/30;
      days=days%30;
      cout<<"No.of years = "<<years<<"\n";
      cout<<"No.of months = "<<months<<"\n";
      cout<<"No.of days = "<<days<<"\n";
    return 0;
}

OutPut

Enter a number of days : 781

No.of years = 2
No.of months= 1
No.of days= 21