percentage calculation someone help!

LucasNiko

New member
Joined
Feb 7, 2021
Messages
1
Programming Experience
Beginner
C#:
// Name: Luka Nikolaisvili
// Student Number: xxxxxxx
// Lab 4 Part 2
// Program Description: This program uses a loop and if statement to input marks
//    between 0 and 100) from a user, determines if the mark is pass or fail
//    and outputs the percentage of passing and failing marks. The user then
//    terminates the program by entering a negative value for a mark.

using System;
public static class Lab4_2
{
    public static void Main()
    {
        const int PASS = 50;
        int numPass = 0, numFail = 0, totalMarks = 0;
        double mark, perPass = 0, perFail = 0;

        // loop to read in a valid mark or the sentinel value
        do
        {
            // Read initial mark (seed the loop)
            Console.Write("Enter a mark between 0 and 100 (-ve value to stop): ");
            mark = Convert.ToDouble(Console.ReadLine());
        } while (mark > 100);
        // if the inputted mark is not the sentinel value, process it

        while (mark >= 0)
        {

            // increment the counter for the total number of data values
            totalMarks++; numFail++; numPass++;

            // Determine if the mark is a pass or fail (If statement)
            if (mark >= PASS)
                Console.WriteLine("You have Succesfully passed the class");
            else
                Console.WriteLine("You have Failed the class");


            // Read next mark
            Console.Write("Enter a mark between 0 and 100 (-ve value to stop): ");
            mark = Convert.ToDouble(Console.ReadLine());
        }

        // Calculate the percentage of marks that were passes and fails
?????????????????????????????????????????????????????????????????????????????????
      

            // Print results
            Console.WriteLine("Total number of marks = {0}", totalMarks);
        Console.WriteLine("Percentage of passing marks = {0:p1}", perPass);
        Console.WriteLine("Percentage of failing marks = {0:p1}", perFail);
        Console.ReadLine();
    }
}
 
Last edited by a moderator:
First of all, a title and some code is not a proper question. You need to explain that actual problem, not expect us to trawl through the code and work it out.

Secondly, this is a pure mathematics question, not programming. How to calculate a percentage is something you should have learned in maths class at a young age. If you can't remember how, do a web search to remind yourself.

That said, you probably don't even need to actually calculate the percentage. You just need to calculate the proportion, because you are using the "p" format specifier. For instance, if use the "p" format specifier with the number 0.5 then you'll get "50.00%" or the like. The result is implicitly multiplied by 100. As such, you're basically asking us how to perform a division.
 
Back
Top Bottom