-3

I'm a newbie to C# Programming and we're still starting on the loops. For our exercise today, we were tasked to create program using (while loop).

the question is : Read 5 marks from the user, print the sum and "Passed" if the marks greater or equal to 50 or if among the marks there is only one mark less than 50. if the user enters 2 marks less than 50 then the program should print STOP and end the program.

this is my try unfortunately not complete and I can't do it

May you help me, please ?

int sum=0 , counter = 0, number=0 ;

        while (counter < 5 || number < 50)
        {

            number = Convert.ToInt16(Console.ReadLine());
            sum = sum + number;
            counter++;
        }

        Console.WriteLine(sum + "\nPassed");
  • 1
    I would suggest you splitting the task into the smaller ones and not trying to optimise it ahead. First - just read the input from the user, fill in the array of 5 numbers. Then calculate the sum of those and print it. And finally, if there are 1 or less marks of 50 - print "Passed". – JleruOHeP Dec 10 '18 at 23:42
  • @JleruOHeP Hello sir thank you for your effort and your time Sir how can I compare the inputs if two inputs less than 50 to stop the program – Yazan Arafat Dec 11 '18 at 00:10

2 Answers2

0

In the while loop you need && (and) instead of || (or).

After reading a number you can check if it's less than 50 or not and you can also count these.

So after the loop you need to check the lessCounter to decide to print Passed or STOP.

int sum = 0, counter = 0, number = 0, lessCounter = 0;

while (counter < 5 && lessCounter <= 1)
{
    number = Convert.ToInt16(Console.ReadLine());
    sum += number;
    if (number < 50)
        lessCounter++;
    counter++;
}
if (lessCounter <= 1)
    Console.WriteLine(sum + "\nPassed");
else
    Console.WriteLine(sum + "\nSTOP");
szkup
  • 82
  • 10
-1

Thank you all for all your effort and a time, special thanks to Mr.Szkup.

I was able to do it this way:

int number = 0, sum = 0, count = 0;
while (number < 5)
{
    Console.Write("Input Number {0} : " , (number + 1));
    int mark = Convert.ToInt16(Console.ReadLine());
    if (mark < 50)
    {
        count++;
    }
    if (count == 2)
    {
        Console.WriteLine("\nStop\n");
        break;
    }
    sum += mark;
    number++;
}
if (count <= 1)
{
    Console.WriteLine("\nsum = {0}\nPassed\n",sum);
}
Pavel Kovalev
  • 7,521
  • 5
  • 45
  • 67