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;
        int contadorgral = 0;
        int contador1 = 0;
        int contador2 = 0;
        int contador3 = 0;

        int acumulador1 = 0;

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


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

                total = total + 150;
                contador2 = contador2 + 1;
                contadorgral = contadorgral + 1;

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

                contadorgral = contadorgral + 1;



            }
            //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.ReadLine();
                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);
        prom = total / contadorgral;
        Console.WriteLine("el Promdio gral. es: " + prom);
        Console.WriteLine("------------------------------");

        decimal porcentaje1 = (contador1 / contadorgral) * 100;
        decimal porcentaje2 = (contador2 / contadorgral) * 100;
        decimal porcentaje3 = (contador3 / contadorgral) * 100;


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


        Console.WriteLine(porcentaje2);

        Console.WriteLine(porcentaje3);




        Console.ReadLine();
    
asked by Agus Veneziano 07.04.2018 в 21:50
source

2 answers

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

    
answered by 07.04.2018 / 22:11
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)");
            codigo = Convert.ToInt32(Console.ReadLine());
            **switch (codigo)**
            {
                **case 1:**
                    Console.WriteLine("Ud compro producto 1");
                    total += 100;
                    contador1 += 1;
                    acumulador1 += 100;
                    contadorgral += 1;
                    break;
                **case 2:**
                    Console.WriteLine("Ud compro producto 2");
                    total += 150;
                    contador2 += 1;
                    contadorgral += 1;
                    break;

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

                **case 0: break;**

                **default:**
                    Console.WriteLine("Ingrese un codigo correcto (1,2,3)");
                    Console.ReadLine();
                    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}");
        prom = total / contadorgral;
        Console.WriteLine($"el Promdio gral. es: {prom}");
        Console.WriteLine("------------------------------");

        decimal porcentaje1 = (contador1 / contadorgral) * 100;
        decimal porcentaje2 = (contador2 / contadorgral) * 100;
        decimal porcentaje3 = (contador3 / contadorgral) * 100;


        Console.WriteLine($"Porcentaje 1: {porcentaje1}");

        Console.WriteLine(porcentaje2);

        Console.WriteLine(porcentaje3);

        Console.ReadLine();
    
answered by 12.04.2018 в 04:05