settingsAccountsettings
Menusettings

Q: Program in C# that applies bonus scores to numbers from 1 to 9

+3 votes

I need to write a program in C# that applies bonus score to given score in the range [1…9] by the following rules:

  • If the score is between 1 and 3, the program multiplies it by 10.
  • If the score is between 4 and 6, the program multiplies it by 100.
  • If the score is between 7 and 9, the program multiplies it by 1000.
  • If the score is 0 or more than 9, the program prints “invalid score”

Examples:

score

result

2

20

4

400

9

9000

-1

invalid score

10

invalid score

 

asked in C# category by user Jolie Ann

2 Answers

+2 votes

Here is my solution with switch case:

using System;

class BonusNumber
{
    static void Main()
    {
        Console.WriteLine("Please write a number between 1 and 9: ");
        int a = int.Parse(Console.ReadLine());

        switch (a)
        {
            case 1:
            case 2:
            case 3:
                Console.WriteLine("The BONUS score is: " + (a * 10));
                break;
            case 4:
            case 5:
            case 6:
                Console.WriteLine("The BONUS score is: " + (a * 100));
                break;
            case 7:
            case 8:
            case 9:
                Console.WriteLine("The BONUS score is: " + (a*1000));
                break;
            default:
                Console.WriteLine("Invalid Score!");
                break;

        }
    }
}
answered by user mitko
edited by user golearnweb
Or this default:

default: if ((bonusScore == 0) || (bonusScore > 9))
                 {
                     Console.WriteLine("invalid score"); ;
                 }
                 break;
            }
+1 vote

I used if - else and logical conditional statements:

using System;

class BonusScore
{
    static void Main()
    {
        Console.WriteLine("Please enter integer between 1 and 9: ");
        int a = int.Parse(Console.ReadLine());

        if (a == 1 || a == 2 || a == 3)
        {
            Console.WriteLine("The BONUS score is: {0}", (a * 10));
        }
        else if (a == 4 || a == 5 || a == 6)
        {
            Console.WriteLine("The BONUS score is: {0}", (a * 100));
        }
        else if (a == 7 || a == 8 || a == 9)
        {
            Console.WriteLine("The BONUS score is: {0}", (a * 1000));
        }
        else
        {
            Console.WriteLine("Invalid score!");
        }
    }
}
answered by user paulcabalit
edited by user golearnweb
...