A rational number is of the form a/b where a and b are integers with b not equal
ID: 3890000 • Letter: A
Question
A rational number is of the form a/b where a and b are integers with b not equal to zero. Develop a class for processing rational numbers. The class should have a numerator data member and a denominator data member. It should read and display all rational numbers in the format a/b; for output, just display a if the denominator is 1. The following examples illustrate the operations that should be provided:
A rational number x can be entered in the form
a or a/b. (e.g. 4, 3/4, 5/2)
x + y addition
x - y subtraction
x * y multiplication
x / y division
Explanation / Answer
using System;
public class Test
{
public static void Main()
{
double numarator;
double denominator;
int choice;
Console.WriteLine("Select your choice for x value 1->Fraction 2.Integer");
choice=Convert.ToInt32(Console.ReadLine());
if(choice==1)
{
Console.WriteLine("Enter x");
var x=Console.ReadLine();
Console.WriteLine("Enter y");
int y=Convert.ToInt32(Console.ReadLine());
if(x.Contains("/"))
{
string[] val=new string[2];
val=x.Split('/');
numarator=Convert.ToInt32(val[0]);
denominator=Convert.ToInt32(val[1]);
double xval=(double)(numarator/denominator);
Console.WriteLine(" Sum of X+Y is "+(xval+y));
Console.WriteLine("Mul of X-Y is "+(xval-y));
Console.WriteLine(" Mul of X+Y is "+(xval*y));
Console.WriteLine(" Div of X/Y is "+(xval/y));
}
else
{
Console.WriteLine("Hii");
}
}
else
{
Console.WriteLine("Enter x");
int x=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter y");
int y=Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" Sum of X+Y is "+(x+y));
Console.WriteLine("Mul of X-Y is "+(x-y));
Console.WriteLine(" Mul of X+Y is "+(x*y));
Console.WriteLine(" Div of X/Y is "+(x/y));
}
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.