2. Prime numbers

 

1. Program to print no. of prime numbers in a given range using while loop. 




// program to check the prime numbers in a given range

  import java.util.*; 
  import java.util.Scanner;

  public class Primeno_inrange {

      public static void main (String[] args){
          int a,b,i,flag;  
          Scanner s = new Scanner(System.in);

          // taking two inputs for range

          System.out.println("enter the prime no range");
         a = s.nextInt();
         b = s.nextInt();
         // loop to check the prime numbers in a given range
          while(a < b){ 
              flag=0;                //this converts flag to 0 on every loop
              for(i=2; i<=a;i++){  // goes from 2 to the half of the given no. to check prime or not.
                  if(a%i==0){
                      flag=1;
                      break;
                  }
              }
              if(flag==0){
                  System.out.println("the prime numbers are:"+a+" ");
              }
              a++;
          }
      }
  }



2. Program to print no. of prime numbers in a given range using for loop.


  
    //prime number in a range using for loop
  import java.util.*;
  import java.util.Scanner;

  public class Prime_no_for
  {
      public static void main (String[] args)
      {
          Scanner sc = new Scanner(System.in);
          int a = sc.nextInt();
          int b = sc.nextInt();
          int flag;
          for(int k=a;k < = b;k++)
          {
              flag=0;
              for(int i=2;i < k;i++)
              {
                  int y=k%i;
                  if(y==0)
                  {
                      flag=1;
                      break;
                  }
              }
              if(flag==0)
              System.out.println(k);
          }
      }
  }


  

3. Program to check wether a number is prime or not



//Program to check wether given number is prime or not
  import java.util.*;
  import java.util.Scanner;

  public class Prime_number
  {
      public static void main(String[] args)
      {
          Scanner sc = new Scanner(System.in);
          int a = sc.nextInt();
          int flag=0;
          for(int i=2;i < = a;i++)
          {
              if(a%i==0)
              {
                  flag=1;
              }
          }
          if(flag==0)
          System.out.println(a+"\t it is a prime no.");
          else
          System.out.println(a+"\t it is not a prime no.");

      }
  }
 

4. find all prime numbers smaller than the N.



//find all prime numbers smaller than the N.
  import java.util.*;
  import java.util.Scanner;
 
  public class Prime_no_for
  {
      public static void main (String[] args)
      {
          Scanner sc = new Scanner(System.in);
          int a = sc.nextInt();
          int flag;
          for(int i=2;i<=a;i++)
          {
              flag=0;
              for(int j=2;j < i;j++)
              {
                  int y=i%j;
                  if(y==0)
                  {
                      flag=1;
                      break;
                  }
              }
              if(flag==0)
              System.out.println(i);
          }
      }
  }



5. Find two distinct prime numbers with given product.



//Find two distinct prime numbers with given product 
  import java.util.*;
  import java.util.Scanner;
  public class Prime_no4
  {
      static int isPrime(int i)
      {
          int flag=0;
              for(int j=2;j < i;j++)
              {
                  if(i%j==0)
                  {
                      flag=1;
                      break;
                  }
              }
              if(flag==0)
              {
                 return 0; 
              }
              else{
              return 1;
              }
          }
        static void distinctPrime(int array[],int k,int a)
        {
            for(int i=0;i < k-1;i++)
          {
              for(int j=i+1;j < k-1;j++)
              {
                  if(array[i]*array[j]==a)
                  {
                      System.out.println(array[i]+","+array[j]);
                  }
              }
              }
        }
      public static void main (String[] args)
      {
          Scanner sc = new Scanner(System.in);
          int a = sc.nextInt();
          int[] array = new int[100]; 
          int t,k=0;
          for(int i=2;i < = a;i++)
          {
              t=isPrime(i);
              if(t==0)
              {
                  array[k++]=i;
              }
          }
          distinctPrime(array,k,a);
          } 
      }
      

Comments

Popular posts from this blog

Cognizant Tekstac Solutions

BASIC PROGRAMS