Write a user defined method named getMark that can be used to get a valid mark from the user. Your method must continuously ask the user for a mark, until a valid mark is entered (valid marks are between 0 and 100). If a user enters an invalid mark, display an error message before asking for a valid mark to be entered. The method must return the valid value to the calling program.
Write a user defined method named isPass that can be used to determine whether a mark is a pass mark (greater or equal to 50). The method must take as input a mark, and return a boolean, indicating whether the mark was a pass mark.
Write a program that reads in 5 marks by using the getMark method to ensure that valid marks are processed. You need to calculate the sum and the average of the marks. Your program must also use the isPass method to determine the number of pass marks entered. When the 5 marks have been processed you need to display the sum, average and number of passes (from these marks).
using System;
namespace marks
{
class Program
{
public static int getMark()
{
int mark;
Console.Write("Enter mark: ");
mark = Convert.ToInt32(Console.ReadLine());
while ((mark < 0) || (mark > 100))
{
Console.WriteLine("Input is invalid. Please enter mark (between 0 and 100)");
Console.Write("Enter mark: ");
mark = Convert.ToInt32(Console.ReadLine());
}
return mark;
}
public static bool isPass(int mark)
{
if (mark >= 50)
return true;
else
return false;
}
public static void Main(string[] args)
{
int mark, cnt_isPass, sum;
double avg;
sum = 0;
cnt_isPass = 0;
for(int i=0; i<5; i++)
{
Console.Write("{0}. ", i+1);
mark = getMark();
sum = sum + mark;
if (isPass(mark))
cnt_isPass++;
}
avg = Convert.ToDouble(sum) / 5;
Console.WriteLine("\nSum: {0}", sum);
Console.WriteLine("Average: {0}", avg);
Console.WriteLine("Number of passes: {0}", cnt_isPass);
Console.Write("\nPress any key to continue . . . ");
Console.ReadKey(true);
}
}
}
Comments
Leave a comment