3
07/11/2024 2:23 pm
Emne starter
I need to create a program which checks if a number (random one) is Even or Odd in Visual Studio. And prints the result
4 Svar
2
07/11/2024 2:23 pm
Check this code out:
using System; class RandomNumberCheck { static void Main() { Random rnd = new Random(); int a = rnd.Next(0, 100); Console.WriteLine(a); if (a % 2==0) { Console.WriteLine("Even"); } else { Console.WriteLine("Odd"); } } }
1
07/11/2024 2:24 pm
Another solution:
using System; class OddOrEvenIntegers { static void Main() { Console.WriteLine("Please write your number, so the expression can check whether it is odd or even:"); int n = int.Parse(Console.ReadLine()); bool result = n % 2 == 0; Console.WriteLine("Your number {0} is even? Answer: {1}", n, result); } }
1
07/11/2024 2:25 pm
using System; class EvenOrOdd { static void Main() { Console.WriteLine("Enter an integer number: "); int number = int.Parse(Console.ReadLine()); bool check = number % 2 != 0; Console.WriteLine("Does the number is odd?"); Console.WriteLine(check); } }
1
07/11/2024 2:25 pm
Here is my solution with bool:
using System; class Program { static void Main() { int n = int.Parse(Console.ReadLine()); bool result = (n % 2 != 0 && n > 0); Console.WriteLine("Odd? {0}", result); } }