Quantcast
Channel: C++ – Programming
Viewing all articles
Browse latest Browse all 8

Prime Number

$
0
0

Prime Number:-

 

Prime Number :WAP to check prime numbers.

Prime Number

Code:-

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

 

package javaapplication40;

import java.util.Scanner;

 

/**

*

* @author Dell

*/

public class JavaApplication40 {

 

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

int i , n , res;

Scanner sc = new Scanner(System.in);

System.out.println(” Enter a no”);

n = sc.nextInt();

for(i= 2;i<=n/2;i++)

{

if (n % i == 0)

{

System.out.println(” The number is prime”);

}

else

{

System.out.println(” The number is not prime”);

}

}

 

}

 

}

 

Note:-

This program is written and compiled in Netbeans 8.0.

 

Explanation :-

This program is used to check whether the entered number is prime or no.

This program uses a single for loop.

This program divides the entered number from all its previous number upto 1.

If that number gets divided by other numbers except 1 and itself then it is not a prime number.

Otherwise that number falls in the category of prime numbers.

 

The post Prime Number appeared first on Programming.


Viewing all articles
Browse latest Browse all 8

Trending Articles