Even and Odd Number Program. Are you eager to learn coding and tackle basic coding questions? In this guide, we’ll walk you through a common coding challenge: determining whether a number is even or odd. We’ll provide solutions in various programming languages, including C, C++, Java, JavaScript, and Python.
By following our step-by-step approach, you’ll gain insights into problem-solving and be well-equipped to handle similar tasks.
Here’s the solution for checking if a number is even or odd in C, C++, JavaScript, and Python, following the 6-step strategy:
Even and Odd Number Program
Step 1: Understand the Problem
The task is to create a program that can identify whether a given number is even or odd.
Step 2: Design Test Cases
Let’s design a few test cases to verify the accuracy of our programs:
Input: 4
Expected Output: Even
Input: 7
Expected Output: Odd
Input: 0
Expected Output: Even
Step 3: Derive the Solution (Pseudocode)
Next, we will derive the solution using pseudocode to understand the logic without worrying about the specific syntax of the programming language
FUNCTION isEvenOrOdd(number)
IF number MOD 2 EQUALS 0
RETURN "Even"
ELSE
RETURN "Odd"
END FUNCTION
Step 4: Test the Solution
Before writing the actual code, let’s perform a dry run of the pseudocode with our test cases to ensure it works as expected.
number = 4
4 % 2 = 0,
so return "Even".
Even and Odd Number Program
Step 5: Write the Code
Now that we have a clear understanding of the logic, we can write the code in our preferred programming language.
Program to Find if Number is Even or Odd in C, C++, Java, JavaScript, Python
#include <stdio.h>
const char* isEvenOrOdd(int number) {
if (number % 2 == 0) {
return "Even";
} else {
return "Odd";
}
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
const char* result = isEvenOrOdd(number);
printf("The number is: %s\n", result);
return 0;
}
C#include <iostream>
using namespace std;
const char* isEvenOrOdd(int number) {
if (number % 2 == 0) {
return "Even";
} else {
return "Odd";
}
}
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
const char* result = isEvenOrOdd(number);
cout << "The number is: " << result << endl;
return 0;
}
C++import java.util.Scanner;
public class EvenOddChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
String result = isEvenOrOdd(number);
System.out.println("The number is: " + result);
scanner.close();
}
public static String isEvenOrOdd(int number) {
if (number % 2 == 0) {
return "Even";
} else {
return "Odd";
}
}
}
Javafunction isEvenOrOdd(number) {
return (number % 2 === 0) ? "Even" : "Odd";
}
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Enter a number: ', input => {
const number = parseInt(input);
const result = isEvenOrOdd(number);
console.log(`The number is: ${result}`);
readline.close();
});
JavaScriptdef is_even_or_odd(number):
return "Even" if number % 2 == 0 else "Odd"
number = int(input("Enter a number: "))
result = is_even_or_odd(number)
print("The number is:", result)
PythonStep 6: Test the Code
Compile and run each respective program for its language. Test them with the provided test cases to ensure they produce the correct output.
Conclusion
Congratulations! You’ve successfully mastered the art of creating programs to determine whether a number is even or odd in multiple programming languages. With this newfound knowledge, you’re well on your way to building a strong foundation in coding and problem-solving.