Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

Even no or Prime no?????

Posted By: Falak Khan     Category: Java     Views: 39160

Code for Even no or Prime no????? in Java

 */
package filespaths;
import java.nio.file.*;
//import java.nio.file.Paths;
import java.io.*;


import java.util.*;
/** * * @author Hira */publicclass Filespaths {

    /**     * @param args the command line arguments     */publicstaticvoid main(String[] args) {
  
    
     System.out.println("Enter a no");
     Scanner in=new Scanner(System.in);
     int num=in.nextInt();
     myclass obj=new myclass();
     System.out.println("Enter 1 to check even/odd");
     System.out.println("Enter 2 to check Prime/not Prime");
     System.out.println("Enter 3 to find Factorial. ");
     System.out.println("Enter your choice?");
     int n=in.nextInt();
     switch(n){
         case 1:
            boolean e=obj.isEven(num);
            if(e)
              System.out.println(num+" is Even ");
            else
              System.out.println(num+" is Odd ");  
            break;
         case 2:
             boolean r=obj.isPrime(num);
             if(r)
              System.out.println(num+" is Prime ");
            else
              System.out.println(num+" is No Prime ");
             break;
         case 3:
            int y= myclass.fact(num);
            System.out.println(y);
             break;
         default:
             System.exit(0);
     }
     
    }   
    
}
class myclass{
    public  boolean isEven(int num){
        boolean iseven;
        if(num%2==0)
            iseven=true;
        else
            iseven=false;
        return iseven;
    }
    public  boolean isPrime(int num){
        boolean p=true;
        if(num==2||num==3||num==1)
           p=true; 
        elsefor(int i=2;i<num;i++)
              {
                if(num%i==0){
                    p=false;
                    break;
                }
              }
       return p; 
    }
    publicstaticint fact(int num){
        int facto=1;
        for(int i=num;i>=1;i--){
            facto=facto*i;
        }
        return facto;
    }
}
  
Share: 


Didn't find what you were looking for? Find more on Even no or Prime no????? Or get search suggestion and latest updates.

Falak Khan
Falak Khan author of Even no or Prime no????? is from Pakistan.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!