This blog is all about to learn Core Java Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Previously we have discussed the Program for
Write a Java Program for Calculator ?Here we learn the program that finds all the prime numbers from 1 to N ,where N is the user input.In this program we have taken N as 100 ,you can change accordingly.
public class PrimeNumbersFromOneToN {
public void allPrimeNumbers(int num){
System.out.println("Prime Numbers From 1 to "+num);
for(int i=7;i<num;i++){
boolean isPrime = true;
for(int j=2;j<i;j++){
if(i % j == 0){
isPrime = false;
break;
}
}
if(isPrime){
System.out.print(i+",");
}
}
}
public static void main(String[] args) {
PrimeNumbersFromOneToN p = new PrimeNumbersFromOneToN();
p.allPrimeNumbers(100);
}
}
Program Output :-
Prime Numbers From 1 to 100
7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,

No comments:
Post a Comment