-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

About me

Hello

I'mITEngineer8995

Spatial Data Specialist

Do you want all of your codes in one place?? Then you are landed on the right blog... Here you will find varieties of codes of various languages starting from noob to advance with their samples. Do learn, execute and share your knowledge with others. Do visit my YouTube Channel for execution of these codes .

Experience

Video Clone Detector using Hadoop

2017-2018

A detector which differentiates copied videos from the original ones using Hadoop.

Portfolio

Java Program To Check Prime Number using While Loop

  

Java Program To Check Prime Number using While Loop

In this java program, we check whether the user input number is prime or not.

A prime number is a natural number greater than 1 that is not a product of two small natural numbers.

E.g. 2, 3, 5 etc are prime numbers.

Steps :

Step 1 : Import Scanner class

Step 2 : Create class PrimeChec

Step 3 : Create main method

Step 4 : Create variable temp

Step 5 : Create object of Scanner 

Step 6 : Ask the user to enter any number by using System.out.print method

Step 7 : Create variable num and store the user entered number in it.

Step 8 : Close the Scanner class

Step 9 : Initialize int i equals to 2

Step 10 : Apply while loop where i is smaller than equal to num divide by 2

Step 11 : Apply if where num mod i is equals equals to zero

Step 12 : Initialize isPrime to False

Step 13 : If isPrime is true then the number is prime else not

Step 14 : Print the number

Program :

import java.util.Scanner;

public class PrimeChecWhile 

    {

        public static void main(String[] args)

        {

            int temp;

            boolean isPrime = true;

            Scanner sc = new Scanner(System.in);

            System.out.println("Enter any number:");

            //capture the input in an integer

            int num = sc.nextInt();

            sc.close();

            int i = 2;

            while(i<=num/2)

            {

                if(num%i==0)

                {

                    isPrime = false;

                    break;

                }

                i++;

            }

            //If isPrime is true then the number is prime else not

        if(isPrime)

        System.out.println(num+"is a Prime Number");

        else

        System.out.println(num+"is not a Prime Number");

        }

        }

Output :

Enter any number:
19
19is a Prime Number

For execution of the same do visit my YouTube Channel :

https://youtu.be/jYxikmPpNb4
ITEngineer8995
India

SEND ME A MESSAGE

Search This Blog

Powered by Blogger.

Pages