-2

I need to calculate years and months from a given number. how can I do it? eg: I am giving: 26 I need to get result: 2 years 2months please help

techlk
  • 1
  • 1

1 Answers1

1

Unless you have some more specific requirements, it should be as easy as Integer Division and the Remainder operator %

var input = 26;
var years = input / 12;
var months = input % 12;

Console.WriteLine($"{years} years and {months} months");

Output

2 years and 2 months

or

private static (int Years, int Months) GetYearsAndMonths(int input) 
   => (input / 12, input % 12);

...

var result = GetYearsAndMonths(26);

Console.WriteLine($"{result.Years} years and {result.Months} months");

or the little known method Math.DivRem Method as supplied by @Charlieface

Calculates the quotient of two numbers and also returns the remainder in an output parameter.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141