Skip to main content

NEAREST PRIME

Program 10:
Nearest Prime Of A Number
For Example User inputted 100 then 101 is nearest prime
user inputs 102 then 103 and 101 are nearest primes.
user inputs 97 then 97 itself is a prime.

import java.io.*;
class np //nearstprime
  { 
      int posprime,negprime;
        public void checkip(int n)
             { int i,c=0;
               for(i=2;i<n;i++)
                {
                  if(n%i==0)
                    {
                       c++;
                     }
                  }
            if(c==0)
            {
                System.out.println("The number itself is a prime");
            }
        
    }
    public static void checkpos(int n)
    { 
       int cpos=0;
        int m=n,c=0;
        m=m+1;
        for(int i=2;i<m;i++)
       {
           if(m%i==0)
           c++;
        }
       if(c==0)
        posprime=m;
        else checkpos(m);//recursion
           
    }
    public static void checkneg(int n)
    { int cneg=0;
        int m=n,c=0;
        m=m-1;
       for(int i=2;i<m;i++)
       {
           if(m%i==0)
           c++;
        }
        if(c==0)
        negprime=m;
           else{
               checkneg(m);//recursion
        }}
    
    public void printCalc(int n)
    { 
        if((posprime-n)<(n-negprime))
        {
            System.out.println(posprime+" is nearest prime");
        }
        else if((n-negprime)<(posprime-n))
        {
            System.out.println(negprime+" is nearest prime");
        }
        else
        System.out.println(negprime+" & "+ posprime+" both are nearest");
    }
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a number to check its nearest prime");
        int num=Integer.parseInt(br.readLine());
        np obj=new np();
        obj.checkip(num);
        obj.checkpos(num);
        obj.checkneg(num);
        obj.printCalc(num);
    }
}

        
 posprime  and negprime are global variables to store nearest prime greater then input and lesser than input 


Comments

Post a Comment

Popular posts from this blog

SMITH NUMBER

PROGRAM: SMITH NUMBER A   Smith number  is a composite number, the sum of whose digits is the sum of the digits of its prime factors obtained as a result of prime factorization (excluding 1). The first few such numbers are 4, 22, 58, 85, 94, 121 ……………….. For Example 58=> 5+8=13 2+(2+9) of 29 =13 Since sum of digits=sum of digits of prime factors 58 is a SMITH NUMBER SYNTAX: import java.io.*; class smith_no { static int sum,sum1;     public static void main(String args[])throws IOException     {         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));         int n;int c=0;         System.out.println("Enter a Number");         n=Integer.parseInt(br.readLine());         int n1=n;         while(n1!=0){         int d=n1%10;         sum1+=d; ...

LUCKY NUMBER

PROGRAM : LUCKY NUMBER QUESTION: Consider the sequence of natural numbers. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 …………………………………. Removing every second number produces the sequences 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23 …………………………. Removing every third number produces the sequences 1, 3, 7, 9, 13, 15, 19, 21, 25 …………………………. This process continues indefinitely by removing the fourth, fifth…and so on, till after a fixed number of steps, certain natural numbers remain indefinitely. These are known as  Lucky Numbers . Write a program to generate and print lucky numbers less than a given number N. SAMPLE INPUT :   N = 10 OUTPUT :   The Lucky numbers less than 10 are:     1     3     7 SAMPLE INPUT :   N = 25 OUTPUT :   The Lucky numbers less than 25 are:     1     3  ...

Arranging according to potential.

PROGRAM: Requested by Sir  Ankur Acharya   ARRANGING WORDS IN A STRING ACCORDING TO THEIR POTENTIAL The encryption of alphabets are to be done as follows: A = 1 B = 2 C = 3 . . . Z = 26 The potential of a word is found by adding the encrypted value of the alphabets. Example: KITE Potential = 11 + 9 + 20 + 5 = 45 Accept a sentence which is terminated by either “ . ” , “ ? ” or “ ! ”. Each word of sentence is separated by single space. Decode the words according to their potential and arrange them in ascending order. Output the result in format given below: Example 1 INPUT : THE SKY IS THE LIMIT. POTENTIAL : THE = 33 SKY = 55 IS = 28 THE = 33 LIMIT = 63 OUTPUT : IS THE THE SKY LIMIT Example 2 INPUT : LOOK BEFORE YOU LEAP. POTENTIAL : LOOK = 53 BEFORE = 51 YOU = 61 LEAP = 34 OUTPUT : LEAP BEFORE LOOK YOU SYNTAX import java.io.*; class Potential {     public static void main(String args[])throws IOException     {       ...