You are on page 1of 8

# Armstrong Number:

## import import import public java.io.*; java.util.*; java.util.Scanner; class ArmstrongTest{

public static void main(String args[]) { //input number to check if its Armstrong number System.out.println("Please enter a 3 digit number to find if its an Armstrong number:"); int number = new Scanner(System.in).nextInt(); //printing result if(isArmStrong(number)){ System.out.println("Number : " + number + " is an Armstrong number"); }else{ System.out.println("Number : " + number + " is not an Armstrong number"); }

} /* * @return true if number is Armstrong number or return false */ private static boolean isArmStrong(int number) { int result = 0; int orig = number; while(number != 0){ int remainder = number%10; result = result + remainder*remainder*remainder; number = number/10; } //number is Armstrong return true if(orig == result){ return true; } return false; } }

Fibonacci:
import java.util.Scanner; public class FibonacciCalculator { public static void main(String args[]) { //input to print Fibonacci series upto how many numbers System.out.println("Enter number upto which Fibonacci series to print: "); int number = new Scanner(System.in).nextInt(); System.out.println("Fibonacci series upto " + number +" numbers : "); //printing Fibonacci series upto number for(int i=1; i<=number; i++){ System.out.print(fibonacci2(i) +" "); }

} /* * Java program for Fibonacci number using recursion. * This program uses tail recursion to calculate Fibonacci number for a given number * @return Fibonacci number */ public static int fibonacci(int number){ if(number == 1 || number == 2){ return 1; } return fibonacci(number-1) + fibonacci(number -2); //tail recursion } /* * Java program to calculate Fibonacci number using loop or Iteration. * @return Fibonacci number */ public static int fibonacci2(int number){ if(number == 1 || number == 2){ return 1; } int fibo1=1, fibo2=1, fibonacci=1; for(int i= 3; i<= number; i++){ fibonacci = fibo1 + fibo2; //Fibonacci number is sum of previous two Fibonacci number fibo1 = fibo2; fibo2 = fibonacci; } return fibonacci; //Fibonacci number }}

Palindrome:
import java.io.*; import java.util.*; import java.util.Scanner; class Palindrome{ public static void main(String args[]){ System.out.println("Please Enter a number : "); int palindrome = new Scanner(System.in).nextInt(); if(isPalindrome(palindrome)){ System.out.println("Number : " + palindrome + " is a palindrome"); }else{ System.out.println("Number : " + palindrome + " is not a palindrome"); } } public static boolean isPalindrome(int number) { int palindrome = number; // copied number into variable int reverse = 0; while (palindrome != 0) { int remainder = palindrome % 10; reverse = reverse * 10 + remainder; palindrome = palindrome / 10; } // if original and reverse of number is equal means // number is palindrome in Java if (number == reverse) { return true; } return false; } }

Prime Factor:
import java.io.*; import java.util.*; import java.util.Scanner; class PrimeFactor { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter any Number to find Prime Factors "); int number=sc.nextInt(); System.out.println("Given Number is : " + number); System.out.print("Prime Factors are : " );

//

for (int i = 2; i <= number / i; i++) { while (number % i == 0) { System.out.print(i + " "); number = number / i; } } if (number > 1) System.out.println(number); } }

Print Prime:
import import import public java.io.*; java.util.*; java.util.Scanner; class PrimeNumberExample {

public static void main(String args[]) { //get input till which prime number to be printed System.out.println("Enter the number till which prime number to be printed: "); int limit = new Scanner(System.in).nextInt(); //printing primer numbers till the limit ( 1 to 100) System.out.println("Printing prime number from 1 to " + limit); for(int number = 2; number<=limit; number++){ //print prime numbers only if(isPrime(number)){ System.out.println(number); } } } /* * Prime number is not divisible by any number other than 1 and itself * @return true if number is prime */ public static boolean isPrime(int number){ for(int i=2; i<number; i++){ if(number%i == 0){ return false; //number is divisible so its not prime } } return true; //number is prime now } }

Reverse Number:
import java.util.Scanner; public class ReverseNumberExample { public static void main(String args[]) { //input number to reverse System.out.println("Please enter number to be reversed using Java program: "); int number = new Scanner(System.in).nextInt(); int reverse = reverse(number); System.out.println("Reverse of number: " + number + " is " + reverse(number)); } /* * reverse a number in Java using iteration * @return reverse of number */ public static int reverse(int number){ int reverse = 0; int remainder = 0; do{ remainder = number%10; reverse = reverse*10 + remainder; number = number/10; }while(number > 0); return reverse; } }

String Reverse:
import import import public java.util.Scanner; java.io.FileNotFoundException; java.io.IOException; class StringReverseExample {

public static void main(String args[]) throws FileNotFoundException, IOException { //original string String str = "Sony is going to introduce Internet TV soon"; System.out.println("Original String: " + str); //reversed string using Stringbuffer String reverseStr = new StringBuffer(str).reverse().toString();

System.out.println("Reverse String in Java using StringBuffer: " + reverseStr); //iterative method to reverse String in Java reverseStr = reverse(str); System.out.println("Reverse String in Java using Iteration: " + reverseStr); //recursive method to reverse String in Java reverseStr = reverseRecursively(str); System.out.println("Reverse String in Java using Recursion: " + reverseStr); } public static String reverse(String str) { StringBuilder strBuilder = new StringBuilder(); char[] strChars = str.toCharArray(); for (int i = strChars.length - 1; i >= 0; i--) { strBuilder.append(strChars[i]); } return strBuilder.toString(); } public static String reverseRecursively(String str) { //base case to handle one char string and empty string if (str.length() < 2) { return str; } return reverseRecursively(str.substring(1)) + str.charAt(0); } }

Factorial:
import java.util.Scanner; class Factorial { public static void main(String args[]) { int n, c, fact = 1; System.out.println("Enter an integer to calculate it's factorial"); Scanner in = new Scanner(System.in); n = in.nextInt(); if ( n < 0 ) System.out.println("Number should be non-negative.");

## Unique Characters in a String:

public class StringRep { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String str="aabbqbccccdadeeeeffffggx"; String[] ary = str.split(""); for(int i=0;i<str.length();i++){ if (counter(str,str.charAt(i))==1) System.out.println(str.charAt(i)); } } public static int counter(String str,char strC) { int count=0; for(int i=0;i<str.length();i++) if(str.charAt(i)==strC) count++; return count; } }

Knight Moves:
import java.util.Vector; import java.util.regex.Matcher; import java.util.regex.Pattern;

public class KnightMoves { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub

int boardRows[]={1,2,3,4,5,6,7,8}; char[] boardCols={'a','b','c','d','e','f','g','h'}; String Curr="c3"; Vector Board=new Vector(); for(int i:boardRows) for(char j:boardCols) Board.add(j+""+i); char Col=Curr.charAt(0); char Row=Curr.charAt(1); String Moves[]={(char)(Col-1)+""+(char)(Row-2), (char)(Col+1)+""+(char)(Row-2), (char)(Col-1)+""+(char)(Row+2), (char)(Col+1)+""+(char)(Row+2), (char)(Col+2)+""+(char)(Row+1), (char)(Col-2)+""+(char)(Row+1), (char)(Col-2)+""+(char)(Row-1), (char)(Col+2)+""+(char)(Row-1)}; for(String i:Moves) if(Board.contains(i)) System.out.println(i); } }