# Does not show percentage calculations

3

I'm starting in C # and I have a problem that I can not solve ...

I need to take the percentage of sale of three products (1,2,3) of the total.

I do the corresponding operations and I return on screen a value 0 in each of the operations. Even changing the calculation returns the same value.

What could it be?

Here I attach the code

Greetings

``````        int codigo = 1;
double prom = 0;
int total = 0;

while (codigo != 0)
{
Console.WriteLine("Ingresar codigo: ");
Console.WriteLine("(Presione 0 para finalizar la venta)");
if (codigo == 1)
{
Console.WriteLine("Ud compro producto 1");
total = total + 100;
//Sumamos las ventas del producto 1 con sus totales

}
if (codigo == 2)
{
Console.WriteLine("Ud compro producto 2");

total = total + 150;

}
if (codigo == 3)
{
Console.WriteLine("Ud compro producto 3");
total = total + 200;

}
//Aca si el usuario ingresa un codigo distinto al cargado le devuelve un error
if (codigo > 3)
{
Console.WriteLine("Ingrese un codigo correcto (1,2,3)");
Console.Clear();
}
}
Console.WriteLine("La cantidad de ventas del Producto 1 fueron: " + contador1.ToString() + " El total de la venta del mismo: " + acumulador1);

Console.WriteLine("El total de ventas es: " + total);
Console.WriteLine("el Promdio gral. es: " + prom);
Console.WriteLine("------------------------------");

Console.WriteLine("Porcentaje 1: "+ porcentaje1);

Console.WriteLine(porcentaje2);

Console.WriteLine(porcentaje3);

``````

asked by Agus Veneziano 07.04.2018 в 21:50
source

4

You are dividing two integers and c # is taking it as a whole division. Therefore, since the dividend is less than the divisor, it is logical that 0 comes out.

You must convert them before to double, at least one of the two.

decimal percentage2 = (counter2 / (double) large counter) * 100;

Greetings

source
0

As David says the problem is with the integers and I would like to recommend some improvements to your code, such as using a switch, in addition to using string interpolation, so that the syntax is more fluid. Greetings: D int code = 1;             decimal prom = 0;              total decimal = 0;             decimal large counter = 0;             int counter1 = 0;             int counter2 = 0;             int contador3 = 0;

``````        int acumulador1 = 0;

while (codigo != 0)
{
Console.WriteLine("Ingresar codigo: ");
Console.WriteLine("(Presione 0 para finalizar la venta)");
**switch (codigo)**
{
**case 1:**
Console.WriteLine("Ud compro producto 1");
total += 100;
break;
**case 2:**
Console.WriteLine("Ud compro producto 2");
total += 150;
break;

**case 3:**
Console.WriteLine("Ud compro producto 3");
total += 200;
break;

**case 0: break;**

**default:**
Console.WriteLine("Ingrese un codigo correcto (1,2,3)");
Console.Clear();
break;
}
}
Console.WriteLine(**\$"La cantidad de ventas del Producto 1 fueron: {contador1.ToString()} \nEl total de la venta del mismo: {acumulador1}"**);

Console.WriteLine(\$"El total de ventas es: {total}");
Console.WriteLine(\$"el Promdio gral. es: {prom}");
Console.WriteLine("------------------------------");