java programs for printing from 1 to 100

*/ prime number is a number which is divisible by 1 and itself, example 2,3.
for printing prime number from 1 to 3 we need to devide 1 by 1 ,2 by 1 and 2 both and 3 by 1,2,and 3.

Remember 1 is not considered as prime number.

so we will start from 2 so 2 is divisible by 1 and 2 exactly two times so 2 is prime number.
in case of 3, 3 is divisible by 1 but not divisible by 2 and again 3 is divisible by 3 ,so here we can see that 3 is divisible by 1
and 3 exactly two divisible 2 times so 3 is prime.

*/


import java.io.*;
import java.util.*;


class Prime1To100{
public static void main(String args[]){
 
int i,j,num=100,flag;
for(i=1;i<=num;i++)
{
    //when i will be incremented from 1 to 2 flag will be reset to 0 that's why we are initializing flag here.
     flag=0;
 
for(j=1;j<=i;j++)
{
if(i%j==0)
flag++;
}
if(flag==2)
{
System.out.println(i+" ");
}
}
}
}


out-2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Comments

Popular posts from this blog

cpanel exam CPSP Answers

How to install zimbra collaboration suite 8.8.11 on CentOS 7

awstats installation