Problem Statement
IsEven? Write a function to find whether the given input number is Even. If the given number is even, the function should return 2 else it should return 1. Note: The number passed to the function can either be negative, positive or zero. Zero should be treated as Even.
Sample Input/Output
|
---|
Algorithm
- Start
- Read the input number
- Check if the number is even (number % 2 == 0)
- If even, return 2
- If odd, return 1
- End
C Programming:
Objective: This C function determines if a given number is even and returns 2 for even numbers and 1 for odd numbers. It handles negative, positive, and zero values.
#include <stdio.h>
// Function to determine if number is even
int isEven(int input1) {
if (input1 % 2 == 0)
return 2;
else
return 1;
}
int main() {
int input1;
printf("Enter a number: ");
scanf("%d", &input1);
printf("Result: %d\n", isEven(input1));
return 0;
}
Input/Output:
Enter a number: 4
Result: 2
#include <stdio.h>
// Function to determine if number is even
int isEven(int input1) {
if (input1 % 2 == 0)
return 2;
else
return 1;
}
int main() {
int input1;
printf("Enter a number: ");
scanf("%d", &input1);
printf("Result: %d\n", isEven(input1));
return 0;
}
C++ Programming:
Objective: This C++ function determines if a given number is even and returns 2 for even numbers and 1 for odd numbers. It handles negative, positive, and zero values.
#include <iostream>
// Function to determine if number is even
int isEven(int input1) {
if (input1 % 2 == 0)
return 2;
else
return 1;
}
int main() {
int input1;
std::cout << "Enter a number: ";
std::cin >> input1;
std::cout << "Result: " << isEven(input1) << std::endl;
return 0;
}
#include <iostream>
// Function to determine if number is even
int isEven(int input1) {
if (input1 % 2 == 0)
return 2;
else
return 1;
}
int main() {
int input1;
std::cout << "Enter a number: ";
std::cin >> input1;
std::cout << "Result: " << isEven(input1) << std::endl;
return 0;
}
Input/Output:
Enter a number: 7
Result: 1
Java Programming:
Objective: This Java function determines if a given number is even and returns 2 for even numbers and 1 for odd numbers. It handles negative, positive, and zero values.
import java.io.*;
import java.util.*;
class UserMainCode {
public int isEven(int input1) {
if (input1 % 2 == 0)
return 2;
else
return 1;
}
}
import java.io.*;
import java.util.*;
class UserMainCode {
public int isEven(int input1) {
if (input1 % 2 == 0)
return 2;
else
return 1;
}
}
Input/Output:
Enter a number: 6
Result: 2
Python Programming:
Objective: IThis Python function determines if a given number is even and returns 2 for even numbers and 1 for odd numbers. It handles negative, positive, and zero values.
# Function to determine if number is even
def is_even(input1):
if input1 % 2 == 0:
return 2
else:
return 1
if __name__ == "__main__":
input1 = int(input("Enter a number: "))
print("Result:", is_even(input1))
# Function to determine if number is even
def is_even(input1):
if input1 % 2 == 0:
return 2
else:
return 1
if __name__ == "__main__":
input1 = int(input("Enter a number: "))
print("Result:", is_even(input1))
Input/Output:
Enter a number: -3
Result: 1
FAQs
How does this function handle negative numbers?
- The function checks if a number is even by using the modulus operator. Negative numbers can also be even or odd, so the same logic applies.
What is the significance of returning 2 for even and 1 for odd numbers?
- This is a specific requirement for the function. It could easily be modified to return other values, but the essence is to distinguish even and odd numbers.
Is zero considered even?
- Yes, zero is considered an even number because it is divisible by 2.
Can this function be implemented in other programming languages?
- Absolutely, the logic is simple and can be translated into any programming language.
Why use the modulus operator to check for even numbers?
- The modulus operator (%) returns the remainder of a division. For even numbers, the remainder when divided by 2 is zero.
What happens if the input is not a number?
- The function assumes the input is a valid integer. In a practical implementation, input validation would be necessary to handle non-numeric inputs.
FAQs
How does this function handle negative numbers?
- The function checks if a number is even by using the modulus operator. Negative numbers can also be even or odd, so the same logic applies.
What is the significance of returning 2 for even and 1 for odd numbers?
- This is a specific requirement for the function. It could easily be modified to return other values, but the essence is to distinguish even and odd numbers.
Is zero considered even?
- Yes, zero is considered an even number because it is divisible by 2.
Can this function be implemented in other programming languages?
- Absolutely, the logic is simple and can be translated into any programming language.
Why use the modulus operator to check for even numbers?
- The modulus operator (%) returns the remainder of a division. For even numbers, the remainder when divided by 2 is zero.
What happens if the input is not a number?
- The function assumes the input is a valid integer. In a practical implementation, input validation would be necessary to handle non-numeric inputs.
0 Comments
Please do not Enter any spam link in the comment box