Do you want to write a program in C or C++ that'll ask the user to input his age in years and months and the programs gives him his age in days? This is how you do it!

Steps

  1. 1
    Plan your program. To calculate the age of the program-user in days, you'll first have to know his age in years and months. So, you'll have to ask the user to input is age in years and the remaining months. Try using the cin function in C++ or scanf function in C for this step.
  2. 2
    Calculate the age in days. You will have to convert both, the years and months into days.
    • One non-leap year has 365 days. Leap year has an additional day (total = 366 days). For the sake of simplicity of the program, we will take one year as 365 days. Therefore, to convert years into days, the conversion formula is:
      Days = Years x 365
      • For an accurate result, you can use 1 year = 365.25 days
    • One month has 30, or 31 or 28 days (in case of February). February can have 29 days, if the year is a leap year. Again, for the sake of simplicity, we take 1 month = 30 days. Therefore, to convert months into days, the conversion formula is:
      Days = Months x 30
    Advertisement
  3. 3
    Display the result to the user. Once the calculation is complete, the result has to be displayed to the user. Try using the cout function in C++ or printf function in C for this step.
  4. Advertisement

Community Q&A

  • Question
    How can I find out how many days I have lived in this world?
    Community Answer
    Community Answer
    Google your name, then go on your profile, and it will tell you.
Advertisement

Code

C++

using namespace std;

int main (){                      
   int age, year, month; //Declaring variables as integer
   cout<<"Enter Your Age in Years and Months"; //Asking user to input his age
   cin>>year>>month; //Storing user's age in two different variables
   age=(year*365)+(month*30); //Calculating age in days
   cout<<"Your Age in Days is "<<age; //Displaying output
   return 0;
   }

C

void main (){                      
   int age, year, month; //Declaring variables as integer
   printf("Enter Your Age in Years and Months"); //Asking user to input his age
   scanf("%d %d", &year, &month); //Storing user's age in two different variables
   age=(year*365)+(month*30); //Calculating age in days
   printf("Your Age in Days is %d", age); //Displaying output   
   }

About This Article

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, volunteer authors worked to edit and improve it over time. This article has been viewed 58,520 times.
How helpful is this?
Co-authors: 4
Updated: June 1, 2018
Views: 58,520
Advertisement