Computer Science, asked by Sreyashpedia, 1 year ago

write a program in java to print all the non prime numbers between 1 to 100.
Pls it's urgent! (Correct answer will be marked BRAINLIEST!)

Answers

Answered by gururandhawa62
0
import java.util.Scanner; class PrimeNumbers2 { 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); } }

Output:

Enter the value of n: 150 Prime numbers from 1 to n are : 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 101 103 107 109 113 127 131 137 139 149

Answered by GAssitant
3

class PrimeNumbers

{

  public static void main (String[] args)

  {  

      int i =0;

      int num =0;

      //Empty String

      String  primeNumbers = "";

      for (i = 1; i <= 100; 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 100 are :");

      System.out.println(primeNumbers);

  }

}

Output:

Prime numbers from 1 to 100 are :

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

Similar questions