Maha

Maha

  • NA
  • 0
  • 326k

/ and %

May 20 2012 10:38 AM
In this program inches is converted into yard, feet and inches. Even though "n" is in yard and divided by 12 inches, giving output similar to the manual arithmetic but there is no logic because yard is divided by inches. Please explain the reason.

Manual arithmetic

n = 67/36 = 1.86 yard(36 inches = 1 yard)

Converting remainder 0.86 yard into feet & inches

feet = 36 x 0.86 inches (converting 0.86 yard into inches. 36 inches = 1 yard)
= 30.96 inches
= 2.58 feet
= 2feet 7inches


using System;
public class ConvertInches
{
public static void Main()
{
int inches = 67;

YardsFeetInches(inches);

Console.ReadKey();
}
public static void YardsFeetInches(int number)
{
int yard, n, feet, inches;
yard = number / 36;
n = number % 36; //n - is in yard, 36 inches = 1 yard

feet = n / 12; //n is not converted into inches before divided by 12 (12 inches = 1 feet)
inches = n % 12; //n is not converted into inches before applying remainder operator (12 inches = 1 feet)

Console.WriteLine("{0} yard {1} feet {2} inches", yard, feet, inches);
}
}
//1 yard 2 feet 7 inches


Answers (3)