» » Program to print prime numbers in java


  1. package com.instaceofjava;
  2. public class primenumbers {
  3.  
  4. public static void main(String[] args) {
  5.  
  6. int num=50;
  7. int count=0;
  8.  
  9. for(int i=2;i<=num;i++){
  10.  
  11. count=0;
  12.  
  13. for(int j=2;j<=i/2;j++){
  14.  
  15. if(i%j==0){
  16. count++;
  17. break;
  18. }
  19.  
  20. }
  21.  
  22. if(count==0){
  23.  
  24. System.out.println(i);
  25.  
  26. }
  27.  
  28. }
  29.  
  30. }
  31.  
  32. }




Output:
  1. 2
  2. 3
  3. 5
  4. 7
  5. 11
  6. 13
  7. 17
  8. 19
  9. 23
  10. 29
  11. 31
  12. 37
  13. 41
  14. 43
  15. 47

Java programming interview questions
  1. Print prime numbers? 
  2. What happens if we place return statement in try catch blocks 
  3. Write a java program to convert binary to decimal 
  4. Java Program to convert Decimal to Binary
  5. Java program to restrict a class from creating not more than three objects
  6. Java basic interview programs on this keyword 
  7. Interfaces allows constructors? 
  8. Can we create static constructor in java 
  9. Super keyword interview questions java 
  10. Java interview questions on final keyword
  11. Can we create private constructor in java
  12. Java Program Find Second highest number in an integer array 
  13. Java interview programming questions on interfaces 
  14. Top 15 abstract class interview questions  
  15. Java interview Questions on main() method  
  16. Top 20 collection framework interview Questions
  17. Java Interview Program to find smallest and second smallest number in an array 
  18. Java Coding Interview programming Questions : Java Test on HashMap  
  19. Explain java data types with example programs 
  20. Constructor chaining in java with example programs 
  21. Swap two numbers without using third variable in java 
  22. Find sum of digits in java 
  23. How to create immutable class in java 
  24. AtomicInteger in java 
  25. Check Even or Odd without using modulus and division  
  26. String Reverse Without using String API 
  27. Find Biggest substring in between specified character
  28. Check string is palindrome or not?
  29. Reverse a number in java?
  30. Fibonacci series with Recursive?
  31. Fibonacci series without using Recursive?
  32. Sort the String using string API?
  33. Sort the String without using String API?
  34. what is the difference between method overloading and method overriding?
  35. How to find largest element in an array with index and value ?
  36. Sort integer array using bubble sort in java?
  37. Object Cloning in java example?
  38. Method Overriding in java?
  39. Program for create Singleton class?
  40. Print numbers in pyramid shape?


  41. Check armstrong number or not?
  42. Producer Consumer Problem?
  43. Remove duplicate elements from an array
  44. Convert Byte Array to String
  45. Print 1 to 10 without using loops
  46. Add 2 Matrices
  47. Multiply 2 Matrices
  48. How to Add elements to hash map and Display
  49. Sort ArrayList in descending order
  50. Sort Object Using Comparator
  51. Count Number of Occurrences of character in a String
  52. Can we Overload static methods in java
  53. Can we Override static methods in java 
  54. Can we call super class static methods from sub class 
  55. Explain return type in java 
  56. Can we call Sub class methods using super class object? 
  57. Can we Override private methods ? 
  58. Basic Programming Questions to Practice : Test your Skill
  59. Java programming interview questions on collections

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

7 comments for Program to print prime numbers in java

  1. how this count functions in this program?

    ReplyDelete
  2. public class Print_prime {

    public void findPrime (int start_number, int last_number){
    System.out.println("prime number between "+start_number+" and "+last_number
    +" are");
    for (int i = start_number; i < last_number+1; i++) {
    if(i%2 == 0){

    System.out.println(i);
    }
    }
    }

    public static void main(String[] args) {
    Print_prime pp = new Print_prime();
    pp.findPrime(10, 101);
    }

    }

    ReplyDelete
    Replies
    1. your program is to print even numbers, not prime

      Delete
    2. Yes, the above program is not correct. To print prime numbers, the logic mentioned here is not correct. Please see correct logic to print all prime numbers till the input upperbase number

      import java.util.Scanner;


      public class PrimeNumbers {

      /**
      * @param args
      */
      public static void main(String[] args) {
      // TODO Auto-generated method stub

      PrimeNumbers object = new PrimeNumbers();
      object.printPrimeNumbers();

      }


      public void printPrimeNumbers(){

      int upperBase;
      int num;
      System.out.println("Enter the upper base number:");
      Scanner input = new Scanner(System.in);
      upperBase = input.nextInt();
      input.close();

      System.out.println("\nPrime numbers between 0 and "+upperBase+" are:\t");

      for(int i=2; i<=upperBase;i++){

      int count=0;

      for(num=i;num>1;num--){

      if(i%num==0){
      count++;

      }
      }

      if(count==1){
      System.out.println(i);
      }

      }

      }
      }

      Delete
  3. i want to print above prime numbers in desending order.but not the way replaceing values of for loop value.values will be same.

    ReplyDelete
  4. Good work sir, Thanks for the proper explanation about JAVA . I found one of the good resource related JAVA and OOPS concepts. It is providing in-depth knowledge on JAVA and OOPS. which I am sharing a link with you where you can get more clear on JAVA and OOPS. To know more Just have a look at this link

    Java Tutorial
    Class and object
    Inheritance
    Polymorphism
    Abstraction
    Encapsulation
    ,,

    ReplyDelete

Select Menu