In: Computer Science
Prompt the user to input their lucky number. Upon getting the users input, validate that the input is a valid number using Int32.TryParse (more info can be found at https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number (Links to an external site.)). If the input is not a valid number, output a message to the user stating that you are sorry but you are unable to run the FizzBuzz process due to the input not being a valid number. If the input is valid, you are clear to start the FizzBuzz process. If the input is divisible by 3, then output {number} Fizz. If the input is divisible by 5, then output {number} Buzz. If the input is divisible by 3 AND 5, output {number} FizzBuzz. If it is not divisible by either 3 or 5, simply just output the number.
/* Please read the comments and see the output*/
using System;
using System.IO;
class FizBuzz
{
public static void Main() {
string input; // For reading input
int num; // To store the int value if input is valid
bool isValidInput; // Boolean variable to check input is valid is or not
do {
Console.WriteLine("Enter Lucky Number: ");
input = Console.ReadLine(); // Read user input
isValidInput = int.TryParse(input,out num); // Check input is integer or not
if(!isValidInput) // If input is not valid, show error message
Console.WriteLine("you are sorry but you are unable to run the FizzBuzz process due to the input not being a valid number.");
}while(!isValidInput); // Run this until, user give correct input
string result =""; // Variable to keep the result for the valid input integer
if(num % 15 == 0) result = num + " FizzBuzz"; // DECIDE parameter for BUZ FIZZ GAME
else if(num % 3 == 0 ) result = num + " Fizz";
else if(num % 5 == 0) result = num + " Buzz";
else result = ""+ num;
Console.WriteLine(result); // Show the result
Console.WriteLine("PRESS enter for exit"); // Exit of game
Console.ReadLine();
}
}
/* OUTPUT*/