The percentage returns me zero in c#

Asked

Viewed 2,356 times

1

When the results are printed, the percentage of men and women interviewed and people with income up to 500 kingdoms equals zero.

        `string nome;
        char sexo, estado_civil;
        int idade, cont = 0, cont_m = 0, cont_f = 0, cont_media = 0, cont_renda_500 = 0, cont_renda_f = 0;
        double renda, menor_renda_m = double.MaxValue, maior_renda_f = double.MinValue, media = 0, rendamedia = 0, porce_m, porce_f, porce_dois;

        while (cont < 5)
        {
            ++cont;
            Console.Write("Digite o seu nome: (FLAG = 0)");
            nome = Console.ReadLine();
            Console.Write("Digite o seu sexo:");
            sexo = char.Parse(Console.ReadLine().ToUpper());
            Console.Write("Digite o seu estado civil:(S = solteiro / C = Casado / D = Divorciado)");
            estado_civil = char.Parse(Console.ReadLine().ToUpper());
            Console.Write("Digite a seu idade:");
            idade = int.Parse(Console.ReadLine());
            Console.Write("Digite a sua renda:");
            renda = double.Parse(Console.ReadLine());
            if (sexo == 'F') {
                cont_f++;
                if (renda > maior_renda_f && estado_civil == 'S')
                {
                    maior_renda_f = renda;
                }
                if (estado_civil == 'S' && idade < 25 && renda < 3200)
                {
                    cont_renda_f++;
                }
            }
            else if (sexo == 'M') {
                cont_m++;
                if (renda < menor_renda_m && estado_civil == 'C')
                {
                    menor_renda_m = renda;
                }
            }
            if (idade >= 30 && idade <= 40)
            {
                rendamedia += renda;
                cont_media++;
            }
            if (renda <= 500)
            {
                cont_renda_500++;
            }
        }

            porce_f = (cont_f / cont) * 100;
            porce_m = (cont_m / cont) * 100;
            media = rendamedia / cont_media;
            porce_dois = (cont_renda_500 / cont) * 100;
            Console.WriteLine("Porcentagem de mulheres entrevistadas: " + porce_f.ToString("0.00") + "%");
            Console.WriteLine("Porcentagem de homens entrevistados: " + porce_m.ToString("0.00") + "%");
            Console.WriteLine("Maior renda entre mulheres solteiras: " + maior_renda_f.ToString("C"));
            Console.WriteLine("Menor renda entre homens casados " + menor_renda_m.ToString("C"));
            Console.WriteLine("Renda média das pessoas com a idade entre 30 e 40 anos: " + media.ToString("0.00"));
            Console.WriteLine("Mulheres solteiras com idade inferior a 25 anos e ganham até 3200 reais: " + cont_renda_f);
            Console.WriteLine("Porcentagem de pessoas com renda até 500 reais: " + porce_dois.ToString("0.00" + "%"));
            Console.ReadKey();`
  • 1

    Guess: both variables cont_renda_500 and cont are integer, so I believe that the decimal part of the division is disregarded so that the result is also integer. Ever tried to convert a value to double before the division?

  • What are the input data, and the expected results? Is there any way you can make a [mcve]? In addition to what @Andersoncarloswoss said, there are other things that can influence since some things are very naive in the code, works if everything is right. Help create confusion that tangle of variables at the beginning of the code.

  • @bigown entries can be random, the rest of the results are correct but when asking for the percentage it returns me 0.

  • @Maremads is not what I said.

1 answer

4


What happens is that you are doing the operation with integers. To get the percentage always do the division by one double or float.

Example:

(cont_f / (double)cont) * 100

If you want to do the rounding use the Math class with one of the rounding methods like Math.Round() or Math.Ceiling() or Math.Floor()

  • Thank you very much!

  • Thank you, if the answer was helpful, please, an upvote and mark as accepted ;)

  • Nice negative, but please indicate me the error so I can correct or if so until remove my answer if it is completely wrong

Browser other questions tagged

You are not signed in. Login or sign up in order to post.