Simple math problem in C #

I have this program, which takes 3 ratings out of 200 possible, and then should get the average value and display the percentage. but when I enter the numbers, I get 00.0 as an answer. What can i do wrong?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int Score1;
            int Score2;
            int Score3;

            Console.Write("Enter your score (out of 200 possible) on the first test: ");

            Score1 = int.Parse(Console.ReadLine());
            Console.Write("Enter your score (out of 200 possible) on the second test: ");

            Score2 = int.Parse(Console.ReadLine());
            Console.Write("Enter your score (out of 200 possible on the third test: ");

            Score3 = int.Parse(Console.ReadLine());
            Console.WriteLine("\n");

            float percent = (( Score1+ Score2+ Score3) / 600);

            Console.WriteLine("Your percentage to date is: {0:00.0}", percent);
            Console.ReadLine();
        }
    }
}
+5
source share
4 answers

You divide the integer by an integer that always uses integer arithmetic, even if you assign the result float. The easiest way to fix is ​​to make one of the operands a float, for example

float percent = (Score1 + Score2 + Score3) / 600f;

Note that this will not actually give you a percentage - it will give you a number from 0 to 1 (assuming the inputs are between 0 and 200).

, 100 - 6:

float percent = (Score1 + Score2 + Score3) / 6f;
+17

. , : 200 + 200 + 200 = 600, 600 = 1. - 200, 1 0. ( ) 100.

+3

This is a data type problem, I think. You should output one of the indicators for float, since your variable percentage is float, and all points are int.

+2
source
using System;

namespace stackOverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            int Score1;
            int Score2;
            int Score3;

            Console.Write("Enter your score (out of 200 possible) on the first test: ");
            Score1 = int.Parse(Console.ReadLine());
            Console.Write("Enter your score (out of 200 possible) on the second test: ");
            Score2 = int.Parse(Console.ReadLine());
            Console.Write("Enter your score (out of 200 possible on the third test: ");
            Score3 = int.Parse(Console.ReadLine());
            Console.WriteLine("\n");
            var percent = ((Score1 + Score2 + Score3) / 6D);
            Console.WriteLine("Your percentage to date is: {0:00.0}", percent);
            Console.ReadLine();

        }
    } 

}
0
source

All Articles