Tuesday 24 March 2015

PROGRAM 2 : WAP TO ENTER A CHOICE IN WHICH '1' CHECKS WHETHER  NUMBER IS PRIME OR NOT AND '2' FOR CHECKING WHETHER NUMBER IS BUZZ NUMBER OR NOT.

(BUZZ number is a number which ends with digit '7' or is divisible by 7.
Example: 14 , 27 , 47 , 21 are BUZZ numbers.
 34 is not BUZZ number as it does not end with 7 or is divisible by 7)

PROGRAM:

import java.util.Scanner;
public class assign5
{
   public static void main(String args[]){
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter your choice: 1 for finding whether a number is prime or not and 2 for finding whether a number is BUZZ number or not.");
       int ch = sc.nextInt();
       int num;
       switch(ch)
       {
           case 1: System.out.println("enter a number to find out whether it is prime or not:");
                   num = sc.nextInt();
                    if(isprime(num)){
                       System.out.println("The number entered is a prime number.");
                    }
                    else{
                        System.out.println("The number entered is a prime number.");
                    }
           break;
           case 2: System.out.println("Enter a number  to find out whether it is BUZZ number or not:");
                   num = sc.nextInt();
                   if(isbuzz(num)){
                       System.out.println("The number entered is a BUZZ number.");
                    }
                    else{
                        System.out.println("The number entered is not a BUZZ number.");
                    }
           break;
           default: System.out.println("Sorry.Wrong choice!");
        }
    }
    public static boolean isprime(int n){
        int lim = n/2;
        int count = 0;
        for(int i = 2 ; i<=lim ; i++){
            if(n%i==0){
                count = 1;
                break;
            }
            else{
                count = 0;
            }
        }
        if(count == 1){
            return(false);
        }
        else{
            return(true);
        }
    }
    public static boolean isbuzz(int a){
        if( a%10==7 || a%7==0){
            return(true);
        }
        else{
            return(false);
        }
    }
}
 

No comments:

Post a Comment