This is a very difficult problem with basic arithmetic and C # casting.
Decision
First save the result of integer division in a double variable. And then enter cast for the integer.
int x = 5, y = 10, answer; double ansDouble; answer = (int)(x / y) * 100; //percentage calculation Console.WriteLine("percentage={0}", answer); //>output percentage=0 answer = (int)((double)x / y) * 100; //percentage calculation Console.WriteLine("percentage={0}", answer); //>output percentage=0 answer = (int)((double)x / (double)y) * 100; //percentage calculation Console.WriteLine("x={0}", answer); //>output percentage=0 answer = (int)(x/(double)y) * 100; //percentage calculation Console.WriteLine("x={0}", answer); //>output percentage=0 ansDouble = ((double)x / y) * 100; answer = (int)ansDouble; Console.WriteLine("percentage={0}", answer); //>output percentage=50
Notes for notes
It turns out that x / y = 0, for any values ββof x and y, if they are integers. We cannot solve this in one line using any combination of casting
Sunil the techie
source share