Prime Number Sequence Program Source Code in Java
Hello world ! In the past we have posted prime number sequence program source code in C. This time we'd like to share the same program but in Java. Before that, we have to know what is prime number. In mathematics, prime numbers are native numbers greater than 1, whose divisor is 1 and the number itself. 2 and 3 are primes. 4 is not a prime number because 4 can be divided by 2.
The result of listing program above is like this :
package Prime; import java.util.Scanner; public class Prime { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.print(" Prime number limit : "); int num = in.nextInt(), ce, i, j, tot = 0, tamp[] = new int[4444]; for(i=2; i<=num; i++){ ce = 0; for(j=2; j<=i; j++) if(i%j == 0) ce++; if(ce == 1){ tamp[tot] = i; tot++;} } for(i=0; i<tot; i++) System.out.print(" " + tamp[i]); } }
The result of listing program above is like this :
If you have any question regarding this post, please leave a comment below. Please don't forget to explore our blog. Thank you :D.
Comments
Post a Comment