Question

In: Computer Science

3. (Even or Odd) Write method IsEven that uses the remainder operator (%) to determine whether...

3. (Even or Odd) Write method IsEven that uses the remainder operator (%) to determine whether an integer is even. The method should take an integer argument and return true if the integer is even and false otherwise. Incorporate this method into an application that inputs a sequence of integers (one at time) and determines whether each is even or odd. (C# please)

Solutions

Expert Solution

Create a new C# console application with the name EvenOddApp add the below code in Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EvenOddApp
{
class Program
{
static void Main(string[] args)
{
bool repeat = true;
string n;
int num;
bool isEven;
do
{
//prompt and read a number or q to quit
Console.Write("\nEnter a integer [or q to quit]: ");
n = Console.ReadLine();

//if n is an integer store it in num
bool isInteger = Int32.TryParse(n, out num);

//if the user entered a number
if (isInteger)
{
//call IsEven method and check wether it is even or odd
isEven = IsEven(num);
//print wether the number is even or not
if (isEven)
Console.WriteLine("\n" + num + " is an even number");
else
Console.WriteLine("\n" + num + " is an odd number");
}
//if user didnot enter integer
else
{
//check if user entered q
if(n.ToLower()=="q")
repeat = false; //if yes exit the loop and quit the program
else
Console.WriteLine("\nInvalid input. Try again..."); //else print invalid input
}

} while (repeat);

Console.WriteLine("\nPress any key to exit...");
Console.ReadLine();
}

//method IsEven and checks whether a number is even or odd
public static bool IsEven(int n)
{
//check if n leves a remainder 0 when divided by 2
if (n % 2 == 0)
return true; //if yes its eve, return true
else
return false; //else return false
}
}
}

Output:


Related Solutions

determine whether the given function is even, odd, or neither. Please write a code in MatLab...
determine whether the given function is even, odd, or neither. Please write a code in MatLab to solve this problem below: 1.f(x) = sin 3x please only use Matlab to solve this problem
Write c code to determine if a binary number is even or odd. If it is...
Write c code to determine if a binary number is even or odd. If it is odd, it outputs 1, and if it is even, it outputs 0. It has to be less than 12 operations. The operations have to be logical, bitwise, and arithmetic.
Write a program to print random numbers and whether they were even or odd. Ask the...
Write a program to print random numbers and whether they were even or odd. Ask the user to enter a positive number to indicate how many random numbers to pull (verify number is positive). Zero is positive. Declare two variables and initialized to zero. int evens = 0, odds = 0; Use them to count how many evens and how many odds were pulled. Enter how many random numbers to pull : 3 41           odd 18467    odd 6334    even 1...
Write program numsODD that uses the method to create arrays of odd nums starting 1 of...
Write program numsODD that uses the method to create arrays of odd nums starting 1 of the given length: public static int[] getnumsOdd(int length) {} int[] numbers = getnumsOdd(5); System.out.println(Arrays.toString(numbers)); output: [1, 3, 5, 7, 9]
Create an application that checks whether an integer is an odd or even number. Welcome to...
Create an application that checks whether an integer is an odd or even number. Welcome to the Odd/Even Checker! Enter an integer: ten Error! Invalid integer. Try again. Enter an integer: 10.3 Error! Invalid integer. Try again. Enter an integer: 10 The number 10 is even. Continue? (y/n): Error! This entry is required. Try again. Continue? (y/n): y Enter an integer: 9 The number 9 is odd. Continue? (y/n): n Specifications: Create a version of the Console class presented in...
Using for loop . Input an integer and identify whether it's EVEN OR ODD ( without...
Using for loop . Input an integer and identify whether it's EVEN OR ODD ( without using modulo operator ) using only <iostream> . C++ programming
3. To begin a proof by contradiction for “If n is even then n+1 is odd,”...
3. To begin a proof by contradiction for “If n is even then n+1 is odd,” what would you “assume true? 4. Prove that the following is not true by finding a counterexample. “The sum of any 3 consecutive integers is even" 5. Show a Proof by exhaustion for the following: For n = 2, 4, 6, n²-1 is odd 6.  Show an informal Direct Proof for “The sum of 2 even integers is even.” Recursive Definitions 7.  The Fibonacci Sequence is...
Write a complete program called EvenNums that uses a method to create arrays of even numbers...
Write a complete program called EvenNums that uses a method to create arrays of even numbers starting 1 of the given length: public static int[] getEvenNums(int length) {} EX: int[] nums = getEvenNumbers(6); System.out.println(Arrays.toString(nums)); expected output: [2, 4, 6, 8, 10, 12]
Question1. (lesson 2) In the following code we determine a number if its even or odd....
Question1. (lesson 2) In the following code we determine a number if its even or odd. Explain what logic is used for that?    section .text    global _start            ;must be declared for using gcc        _start:                     ;tell linker entry point    mov   ax,   8h           ;getting 8 in the ax    and   ax, 1              ;and ax with 1    jz    evnn    mov   eax, 4             ;system call number (sys_write)    mov   ebx, 1             ;file descriptor (stdout)    mov   ecx, odd_msg       ;message...
Write a function name as 'checkEvenOrOdd' that checks the input value is odd or even number....
Write a function name as 'checkEvenOrOdd' that checks the input value is odd or even number. The main function is given: int main(){     int number;     cout << "Check number input: ";     cin >> number;     cout << "The input number " << number << " is " << checkEvenOrOdd(number) << endl;     return 0; } simple c++ code please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT