C
C#8mo ago
BabySid

❔ I am facing a doubt in this question.

I am not getting the desired output by my code for this question
No description
16 Replies
BabySid
BabySid8mo ago
// Name: xxxxxxxxxxxxx
// 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
// *** Insert code

// Determine if the mark is a pass or fail (If statement)
// *** Insert code

// Read next mark
// *** Insert code
}

// Calculate the percentage of marks that were passes and fails
// *** Insert code

// 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();
}
}
// Name: xxxxxxxxxxxxx
// 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
// *** Insert code

// Determine if the mark is a pass or fail (If statement)
// *** Insert code

// Read next mark
// *** Insert code
}

// Calculate the percentage of marks that were passes and fails
// *** Insert code

// 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();
}
}
cs This is the Question
csusing 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++;

// Determine if the mark is a pass or fail (If statement)
if (mark >= PASS)
{
numPass++;
}
else
{
numFail++;
}

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

// Calculate the percentage of marks that were passes and fails
if (totalMarks > 0)
{
perPass = (double)numPass / totalMarks * 100;
perFail = (double)numFail / totalMarks * 100;
}

// 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();
}
}
csusing 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++;

// Determine if the mark is a pass or fail (If statement)
if (mark >= PASS)
{
numPass++;
}
else
{
numFail++;
}

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

// Calculate the percentage of marks that were passes and fails
if (totalMarks > 0)
{
perPass = (double)numPass / totalMarks * 100;
perFail = (double)numFail / totalMarks * 100;
}

// 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();
}
}
this is what I've done
JakenVeina
JakenVeina8mo ago
what in particular are you having trouble with here?
BabySid
BabySid8mo ago
I am not getting the desired output the output seems to be wrong
JakenVeina
JakenVeina8mo ago
yes and what is the problem what is the desired output
BabySid
BabySid8mo ago
I dunno why that's happening
JakenVeina
JakenVeina8mo ago
what is the output you're actually getting
BabySid
BabySid8mo ago
No description
BabySid
BabySid8mo ago
I should be getting these but
BabySid
BabySid8mo ago
I'm getting this instead
No description
BabySid
BabySid8mo ago
what is it that I'm doin wrong
SinFluxx
SinFluxx8mo ago
You don't want to be multiplying your perPass / perFail by 100 before formatting them in the output
JakenVeina
JakenVeina8mo ago
per docs, the "P" format specifier already includes that logic
JakenVeina
JakenVeina8mo ago
Standard numeric format strings - .NET
In this article, learn to use standard numeric format strings to format common numeric types into text representations in .NET.
BabySid
BabySid8mo ago
I dont understand what you mean
JakenVeina
JakenVeina8mo ago
what part?
Accord
Accord8mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.