Program to check for a Gold Batch Number (Java)


Welcome friends to Blogger Codemaster. This is our another post. So in this post, I would like to give you a Java program to check for a Gold Batch Number.

So let's get started with our today's topic - PROGRAM TO CHECK FOR A GOLD BATCH NUMBER.

 

QUESTION

Write a Java Program to check whether is a Gold Batch Number or not.


FULL CODE

import java.util.*;

public class GoldBatch{

    static boolean checkPrime(int n)

    {

        for(int i=2;i<n;i++){

            if(n%i==0)

            return false;

        }

        return true;

    }

    static int checkGB(int n)

    {

        if(n%2!=0)

        return 0;

        int r=0;

        for(int i=3;i<n;i+=2){

            if(checkPrime(i))

            {

                if(checkPrime(n-i)){

                    r=i;

                    break;

                }

            }

        }

        return r;

    }

    public static void main(){

        System.out.print("Enter a number: ");

        Scanner in = new Scanner(System.in);

        int tno = in.nextInt();

        int check  = checkGB(tno);

        if(check > 0)

        System.out.print("A GoldBatch Number, it is the sum of "+check+" and "+(tno-check));

        else

        System.out.print("Not A GoldBatch Number.");

    }

}




 

VARIABLE DESCRIPTION

Variable Data Type Description
n int Argument Variable for functions.
tno int Input variable.
r int Stores the output for checkGB function.
check int Store whether `tno` is a GoldBatch Num or not.
i int Loop Variable.


 

OUTPUT



Here, we come to the end of our another post. We will discuss various other interesting topics in our upcoming posts. 

So uptil then I bid you a goodbye😇. 

Please follow our 👉blog and comment💬 below for any queries.



Post a Comment

Previous Post Next Post