Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am new to programming and trying to understand why the void method works as it does. In the below example, obtainedMarks is displayed in the console while percentage does not.
C#
class Student
    {
        string name;
        int age;
        int marksOfMath;
        int marksOfEnglish;
        int marksOfScience;
        int totalMarks = 300;
        int obtainedMarks;
        double percentage;

        void CalculateTotalMarks()
        {
            obtainedMarks = marksOfMath + marksOfEnglish + marksOfScience;
        }

        void CalculatePercentage()
        {
            percentage = obtainedMarks / totalMarks * 100;
        }

        static void Main()
        {
            Student st1 = new Student();
            st1.name = "x";
            st1.age = 20;
            st1.marksOfEnglish = 80;
            st1.marksOfMath = 99;
            st1.marksOfScience = 96;
            st1.CalculateTotalMarks();
            st1.CalculatePercentage();

            Console.WriteLine("{0} of {1} years age got {2}% marks, {3}", st1.name, st1.age, st1.percentage, st1.obtainedMarks);
        }


If I change the "void CalculatePercentage()" to this:
C#
void CalculatePercentage()
        {
            percentage = obtainedMarks + totalMarks + 100;
        }

percentage is now displayed in the console. Could anybody enlighten me as to why the first calculation gives me 0 when it should give me 91.67%?

Thanks in advance for your help.
Posted
Updated 16-Dec-10 10:03am
v2
Comments
Toli Cuturicu 17-Dec-10 15:12pm    
Never post fake answers (I deleted yours this time).

The answer is that the calculation you are performing is not the calculation you think you are performing. Here's a question for you - what do you get when you divide 275 by 300? The answer is not 0.91666 as you are expecting - it's actually 0, so 0 * 100 = 0.

I know this seems wrong, but I've divided two integers here, and the result is 0. What you are trying to do is divide 275 as a floating point number. Fortunately there's a quick fix. Change this calculation to be
C#
percentage = (double)obtainedMarks / totalMarks * 100;
Now when you run it, you get the answer you are expecting.
 
Share this answer
 
try this...

percentage = ((double)obtainedMarks / (double)totalMarks) * 100.0;
 
Share this answer
 
Comments
Marc A. Brown 16-Dec-10 15:52pm    
Good answer. Just beat me to it.
Pete O'Hanlon 16-Dec-10 16:04pm    
Good answer Marcus, but the second (double) is redundant. You only need to cast obtainedMarks to double.
fjdiewornncalwe 16-Dec-10 16:31pm    
Pete: Agreed!!! I just put both and the brackets in to make the order of precedence and the logic clearer. I was trying to say the same thing you did in your answer, but in a shorter(lazier) way.
Or in C#
percentage = (Convert.ToDouble(obtainedMarks) / totalMarks) * 100.0;
 
Share this answer
 
Comments
fjdiewornncalwe 17-Dec-10 10:34am    
Ummmm... The previous answers were in C# already, weren't they?
Toli Cuturicu 17-Dec-10 15:11pm    
Already answered... and way better.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900