“Cetak semua prime no 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

Cetak semua prime no java

import java.util.Scanner;

public class SumOfPrimeNo {
    public static void main(String[] args) {

        int temp, no,sum=0,count=0,loop1=0,loop2=0;;
        System.out.println("--The sum of the All prime Number in Between 2 to ??--");

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a Number ");
        no = sc.nextInt();

       for (int j = 2; j <= no; j++)
       {
            loop1++;
              temp=0;

           for (int i = 2; i <= j/2; i++) {
                 loop2++;
               if (j % i == 0) {
                   temp=1;
                   break;
               }

           }
           if (temp==0) {
               System.out.print(j+"\t");
               count++;
               sum=sum+j;
           }


       }

        System.out.println("\nThe Total no of Prime Number between 1 to "+no+" is = "+count);
        System.out.println("The sum of all Prime Numbers in between 1 to "+no+" is = "+sum);

        System.out.println("How many times Loop ran = "+(loop1+loop2)+" times,"+" loop1 = "+loop1+" times, "+"loop2 = "+loop2+" times");


    }}
IGNOU GURU

Jawaban yang mirip dengan “Cetak semua prime no java”

Pertanyaan yang mirip dengan “Cetak semua prime no java”

Lebih banyak jawaban terkait untuk “Cetak semua prime no java” di Java

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya