Thursday, 19 February 2015

Java Program to check whether a Number is Amstrong or Not

Find Armstrong Number using Java

Java Code to Display the Given Number is Armstrong


                    An Armstrong number is N digit number, Which is equal to the sum of the Nth powers of its digits.The Java programme will accept a number keyboard and the whether it is Armstrong.

Program:


import java.util.Scanner;

public class Armstrong {

   public static boolean isArmstrong(int input) {
       String inputAsString = input + "";
       int numberOfDigits = inputAsString.length();
       int copyOfInput = input;
       int sum = 0;
       while (copyOfInput != 0) {
           int lastDigit = copyOfInput % 10;
           sum = sum + (int) Math.pow(lastDigit, numberOfDigits);
           copyOfInput = copyOfInput / 10;
       }
           if (sum == input) {
           return true;
       } else {
           return false;
       }
   }

   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.print("Enter a number: ");
       int inputNumber = scanner.nextInt();
       boolean result = isArmstrong(inputNumber);
       if (result) {
           System.out.println(inputNumber + " is an armstrong ");
       } else {
           System.out.println(inputNumber + " is not an armstrong ");
       }
   }
}


Output:


Enter a number: 1634
1634 is an armstrong 

Java Program To Find Fibonacci Sequence

Fibonacci Series Of a Number Using Java

A program to print Fibonacci series of a given number


               The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...The next number is found by adding up the two numbers before it. Below example shows how to create Fibonacci series.

Program


import java.util.Scanner;
public class FibonacciJavaProgram{
public static void main(String a[]){
     
         System.out.print("Enter number up to which Fibonacci series to print: ");
         int febCount = new Scanner(System.in).nextInt(); 
         int[] feb = new int[febCount];
         feb[0] = 0;
         feb[1] = 1;
         
         for(int i=2; i < febCount; i++){
                   feb[i] = feb[i-1] + feb[i-2];            
         }
 
         for(int i=0; i< febCount; i++){
                 System.out.print(feb[i] + " ");
         }
    }
}  

Output


Enter number up to which Fibonacci series to print: 10
0 1 1 2 3 5 8 13 21 34                                                                                                               

Decimal to Binary Conversion

Conversion Of Decimal to Binary Using Java

 Java Code to Convert Decimal to Binary
                    
                        This program helps to convert the given Decimal number to Binary

Program:


import java.util.Scanner;

public class DecimalToBinary {

    public void printBinaryFormat(int number){
       
        int binary[] = new int[number];
        int index = 0;
       
        while(number > 0){
            binary[index++] = number%2;
            number = number/2;
        }
       
        for(int i = index-1;i >= 0;i--){
            System.out.print(binary[i]);
        }
    }
   
    public static void main(String a[]){
       
        int n;
        System.out.println("Enter a number to print decimal to binary");
        Scanner in = new Scanner(System.in);
        n = in.nextInt();

        DecimalToBinary dtb = new DecimalToBinary();
        dtb.printBinaryFormat(n);
    }
}



Output:


Enter a number to print decimal to binary
5
101

Monday, 16 February 2015

Java Program to Demonstrate the Compound Assignment Operators

Compound Assignment Operator Example Program in Java

A Program to show the use of Compound Assignment Operator


               The program shows a few compound assignment operators and how it can be used in programs to write program intelligently. This helps to reduce a few lines while writing a program. It is also efficient than normal coding.

Program:


// Demonstrate compound assignment operators.

public class CompoundAssignemntOperators {
    
    public static void main(String[] args) {
        
        int x = 1;
        int y = 3;
        int z = 5;
        
        x += 2;                    //Equals to x=x+2; Now x=3;
        y *= 4;                    //Equals to y=y*4; Now y=12;
        z += x * y;              // z= z+(x*y); Now z=41 i.e z=5+(3*12)
        z %= 6;                  // z= z%6; Now z=5; i.e z=41%6
        
        System.out.println("x = " + x);
        System.out.println("y = " + y);
        System.out.println("z = " + z);
    }
    
}

Output:


x = 3
y = 12
z = 5

Compound Assignment Operators Example Program