Pages

Sunday, February 24, 2019

Java Example to check if Number is Odd or Even

Java Example to check if the entered number is Odd or Even. This example is for the beginners who just started learning programming.



import java.util.Scanner;

public class OddOrEven {

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

        System.out.print("Enter the number: ");
        int num = reader.nextInt();

        if(num % 2 == 0)
            System.out.println(num + " is even.");
        else
            System.out.println(num + " is odd.");
    }
}
Watch video here
https://youtu.be/sX0XAROhuiQ

Java Example to check if entered Number is Prime or not

This Java Example if for beginners to check if entered number is prime i.e. number which is divisible by only two numbers first 1 and second itself. If any number is divisible by 3 or more numbers, it is not a prime number.

Play Free Online Games and Win Big Prize Money 

import java.util.Scanner;
public class Prime {

    public static void main(String[] args) {
        
        
        Scanner keyboard = new Scanner(System.in);

        int number = keyboard.nextInt();
        boolean flag = false;
        for(int i = 2; i <= number/2; ++i)
        {
            // condition for nonprime number
            if(number % i == 0)
            {
                flag = true;
                break;
            }
        }

        if (!flag)
            System.out.println(number + " is a prime number.");
        else
            System.out.println(number + " is not a prime number.");
    }
}

Watch video here
https://youtu.be/sX0XAROhuiQ