Skip to main content

Even Or Odd Number!!

Such programs are too simple and requires the fundamental logic for checking whether a number is odd or ever. Any number which is divisible by 2 is even else odd. In the below code snippet, you will find two operators % and /. 

x=4
y=2

x%y will computes to 0 which is the remainder!!
x/y will compute to 2 which is the quotient.

# Below is the code:

class EvenOrOdd {
public String compute(int x) {
if (x % 2 == 0) {
return x + " is even";
} else
return x + " is odd";
}
}

public class TestEvenOrOdd {

public static void main(String[] args) {
EvenOrOdd ref = new EvenOrOdd();
System.out.println(ref.compute(5));
System.out.println(ref.compute(10));

}

}

#Output:






Comments

Popular posts from this blog

Read different types of data from standard input, process them as shown in output format and print the answer to standard output.

/* Problem Statement: Read different types of data from standard input, process them as shown in output format and print the answer to standard output. Input format: First line contains integer N. Second line contains string S. Output format: First line should contain N x 2. Second line should contain the same string S. Constraints: 0≤N≤10 1≤|S|≤15 where |S|= length of string S  */ import java.util.Scanner; public class TestClass {      public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         int number = 0;         String string = null;         int firstInput = sc.nextInt();         String secondInput = sc.next();         // apply constraints         if (firstInput >= 0 && firstInput <= 1...

Basics of Git

Git is one among the popular open source version control software. This version control software helps you to track the changes in the source code or any text file. The changes can be in the terms of adding, modifying or deleting. All can be tracked through Git!. As per the official documentation " In Git, however, every developer is potentially both a node and a hub — that is, every developer can both contribute code to other repositories and maintain a public repository on which others can base their work and which they can contribute to ." which defines the distributed nature of it. Let's try to understand how it works. 1. Downloading and Installing Git: Based on the operating system, choose the installer file. Windows user can download it from the link  https://git-scm.com/ and Ubuntu users can type this command from terminal  "sudo apt-get install git" 2. Configuring Git: git config command is used to set Git configuration values on a global o...

Generate the Series up to N and sum it all together! N=13 [+1-2+3-4+5-6+7-8+9-1+0-1+1-1+2-1+3]

Let's say N=13, then the we have to generate the series till the number(including N) and evaluate the entire expression as  [+1-2+3-4+5-6+7-8+9-1+0-1+1-1+2-1+3]. It can be solved in various way, among all i am sharing one of the approach to solve it. public class ExpressionEvaluator { public static void main(String[] args) { int temp; int rem; int sum=0; int number=10; // get the sequence of numbers starting from 1 to 13 int i=1; while(i<=number) { if(i<=9) { rem=i%10; if(rem%2==0) { rem=-(rem); sum=sum+rem; } else if(rem==1) { sum=sum+rem; } else { sum=sum+rem; } } else { rem=i%10; //+0     // +1    //+2  //3  // 0  // 1  //2 // 3 temp=i/10; // 1   //1     //1    // 1   //2  ...