“Mencetak bilangan prima di java” Kode Jawaban

Mencetak bilangan prima di java

import java.util.Scanner;
public class Main
{
   public static void main (String[] args)
   {		
      Scanner scanner = new Scanner(System.in);
      int i =0;
      int num =0;
      //Empty String
      String  primeNumbers = "";
      System.out.println("Enter the value of n:");
      int n = scanner.nextInt();
      scanner.close();
      for (i = 1; i <= n; i++)  	   
      { 		 		  
         int counter=0; 		  
         for(num =i; num>=1; num--)
         {
	    if(i%num==0)
	    {
		counter = counter + 1;
	    }
	 }
	 if (counter ==2)
	 {
	    //Appended the Prime number to the String
	    primeNumbers = primeNumbers + i + " ";
	 }	
      }	
      System.out.println("Prime numbers from 1 to n are :");
      System.out.println(primeNumbers);
   }
}
Glamorous Gerenuk

Program bilangan prima di java

public class PrimeExample{    
  public static void main(String args[]){    
    int i,m=0,flag=0;      
    int n=3;//it is the number to be checked    
    m=n/2;      
    if(n==0||n==1) System.out.println(n+" is not prime number");      
    else{  
      for(i=2;i<=m;i++)	if(n%i==0) {      
        System.out.println(n+" is not prime number");      
        flag=1;      
        break;      
        }       
      if(flag==0) System.out.println(n+" is prime number");
    }//end of else  
  }    
}  
Bredo

Jawaban yang mirip dengan “Mencetak bilangan prima di java”

Pertanyaan yang mirip dengan “Mencetak bilangan prima di java”

Lebih banyak jawaban terkait untuk “Mencetak bilangan prima di java” di Java

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya