Sunday, 20 September 2015

To Check Prime Number

package com.tutorial4u;

import java.util.Scanner;

public class PrimeNumber {
       public static void main(String[] args) {
             int temp ;
             boolean isPrimeNumber = true;
             System.out.print("Please enter a number to check prime : ");
             Scanner sc = new Scanner(System.in);
             int num = sc.nextInt();
             for(int i=2;i<num/2;i++){
                    temp = num%i;
                    if(temp==0){
                           isPrimeNumber = false;
                           break;
                    }
             }
             if(isPrimeNumber){
                    System.out.println(num +" is Prime Number");
             }else{
                    System.out.println(num +" is not Prime Number");
             }
       }

}

Output:


Please enter a number to check prime : 23
23 is Prime Number

Palindrome Number



package com.tutorial4u;

import java.util.Scanner;

public class PalindromeNumber {
       public static void main(String[] args) {
             int num, reverse=0,temp;
             System.out.print("Please enter any number to check palindrome : ");
             Scanner sc = new Scanner(System.in);
             num = sc.nextInt();
            
             temp = num;
             while(num!=0){
                    reverse = reverse*10;
                    reverse=reverse+num%10;
                    num= num/10;
             }
             if(temp == reverse){
                    System.out.println("Given number is palindrome");
             }else{
                    System.out.println("Given number is not palindrome");
             }
       }

}

Output:

Please enter any number to check palindrome : 121

Given number is palindrome

Even Or Odd Number Without (%,/) Operator

package com.tutorial4u;

import java.util.Scanner;

public class EvenOrOdd {
       public static void main(String[] args) {
             System.out.print("Enter a num to check even or odd : ");
             Scanner sc = new Scanner(System.in);
             int num = sc.nextInt();
             if((num&1) == 0){
                    System.out.println("Given number is Even ");
             }else{
                    System.out.println("Given number is Odd");
             }
       }

}

Output: 

Enter a num to check even or odd : 34
Given number is Even 


No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...