19 February 2014

Program to display all prime number below a user entered number

Create a new Class with name Disp_primes:

import java.util.Scanner;

public class Disp_primes {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number : ");
        int num = sc.nextInt();
        System.out.println("prime numbers below "+num+" are :");
        int rem=0;
        if(num==2){
            System.out.print(num+" is even prime");
        }
        else{
            for(int i=num;i>2;i--){
                int count=2;
        while(count<i){
            rem = i%count;
            if(rem==0)
                break;
            else
                count++;
                    }
        if(rem!=0)
            System.out.print(i+",");
        }
            if(num>2){
                System.out.print("2");
            }
        }
    }

}

Output:(Click on image to enlarge):
Program-to-display-all-prime-number-below-a-user-entered-number-output
(Comment if you have any doubts)
>

Contact Form

Name

Email *

Message *

Smooth Graphics Java

// Arrange Bricks in Game //java brick breaker game code without flickering // added game levels 1,2,3 in the game //java paddle ball ga...