simple program to print prime number in java & c++/c#.
This program print 1st 100 prime numbers
The Prime Number has no divisor other than 1 & itself .From the following code when count becomes 2 (i.e count=2 ) it is a prime number i.e 'if(num%i==0)' condition is satisfied two times, when the number is divided by 1 & itself.
2
3
5
7
11 .......
To run this code :
1. Save the code as 'primenum.java'
2. Compile the primenum.java using below command.
javac primenum.java
3. Run the code
java primenum
Above method is to run the code in the command prompt or terminal. You can also run it in IDE.
Simple java Program to print prime numbers
To find Prime Number or Not in javapublic class primenum {
public void pri() {
int num = 1 ;
int i;
int count=0;
while(num<100)
{
for(i=1;i<=num;i++) {
if(num%i==0) {
count++;
}}
if(count==2){
System.out.println("prime"+num);
count=0;
num++;
}
else {
count = 0;
num++;
}
}}
public static void main(){
primenum p1 = new primenum;
p1.pri();
}
}
This program print 1st 100 prime numbers
The Prime Number has no divisor other than 1 & itself .From the following code when count becomes 2 (i.e count=2 ) it is a prime number i.e 'if(num%i==0)' condition is satisfied two times, when the number is divided by 1 & itself.
if(num%i==0) {Output:
count++;
}
2
3
5
7
11 .......
To run this code :
1. Save the code as 'primenum.java'
2. Compile the primenum.java using below command.
javac primenum.java
3. Run the code
java primenum
Above method is to run the code in the command prompt or terminal. You can also run it in IDE.
0 comments:
Post a Comment