Problem:
Solution:
package com.basics;
import java.util.Scanner;
public class EvenOperations {
static int number;
public static void main(String[] args) {
EvenOperations evenOperations = new EvenOperations();
evenOperations.getTheNumber();
if(evenOperations.isEvenNumber(number)){
System.out.println("the given number is even number");
}
else {
System.out.println("the given number is not even number");
}
}
public void getTheNumber(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter the number");
number = scanner.nextInt();
scanner.close();
}
public boolean isEvenNumber(int number){
boolean isEvenNumber = false;
while(true){
number = number - 2;
if(number == 0){
isEvenNumber = true;
break;
}
else if(number == -1){
break;
}
}
return isEvenNumber;
}
}
Output:
enter the number
1222223
the given number is not even number
Write a short Java method, isEven, that takes an int i and returns true if and only
if i is even. Your method cannot use the multiplication, modulus, or division
operators, however.
Solution:
package com.basics;
import java.util.Scanner;
public class EvenOperations {
static int number;
public static void main(String[] args) {
EvenOperations evenOperations = new EvenOperations();
evenOperations.getTheNumber();
if(evenOperations.isEvenNumber(number)){
System.out.println("the given number is even number");
}
else {
System.out.println("the given number is not even number");
}
}
public void getTheNumber(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter the number");
number = scanner.nextInt();
scanner.close();
}
public boolean isEvenNumber(int number){
boolean isEvenNumber = false;
while(true){
number = number - 2;
if(number == 0){
isEvenNumber = true;
break;
}
else if(number == -1){
break;
}
}
return isEvenNumber;
}
}
Output:
enter the number
1222223
the given number is not even number
enter the number
234
the given number is even number
No comments:
Post a Comment