FIBONACCI CODE :
PALINDROME CODE :
ARMSTRONG CODE :
PRIME NUMBER:
CONVERT Number to Words:
PRINT LIST with Nodes:
public class Fibonacci { public static void main(String args[]) { int number = 4; for (int i = 1; i <= number; i++) { System.out.println(fibonacciLoop(i)+""); } } // Java program for Fibonacci number using recursion. public static int fibonacciRecusion(int number) { if (number == 1 || number == 2) { return 1; } return fibonacciRecusion(number - 1) + fibonacciRecusion(number - 2); // tail recursion } }
PALINDROME CODE :
public class Palindrome { static boolean isPalin(String s) { int len = s.length(); for (int i = 0; i < len / 2; i++) { if (s.charAt(i) != s.charAt(len - i - 1)) { return false; } } return true; } public static void main(String[] args) { System.out.println(isPalin("123321")); } }
ARMSTRONG CODE :
public class Armstrong { public static void main(String args[]) { int n=407, sum = 0, temp, r; temp = n; while (temp != 0) { r = temp % 10; sum = sum + r * r * r; temp = temp / 10; } if (n == sum) System.out.println("Entered number is an armstrong number."); else System.out.println("Entered number is not an armstrong number."); } }
PRIME NUMBER:
private static boolean isPrimeNumber(int number){ for(int i=2; i<=number/2; i++){ if(number % i == 0){ return false; } } return true; }
CONVERT Number to Words:
public class NumberToWordsConverter { final private static String[] units = { "Zero", "One", "Two", "Three","Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven","Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen","Seventeen", "Eighteen", "Nineteen" }; final private static String[] tens = { "", "", "Twenty", "Thirty", "Forty","Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; public static String convert(int i) { if (i < 20) return units[i]; if (i < 100) return tens[i / 10] + ((i % 10 > 0) ? " " + convert(i % 10) : ""); if (i < 1000) return units[i / 100] + " Hundred"+ ((i % 100 > 0) ? " and " + convert(i % 100) : ""); if (i < 1000000) return convert(i / 1000) + " Thousand "+ ((i % 1000 > 0) ? " " + convert(i % 1000) : ""); return convert(i / 1000000) + " Million "+ ((i % 1000000 > 0) ? " " + convert(i % 1000000) : ""); } public static void main(String[] args) { //Number to word String xx = "1234"; System.out.println("XXXXX : " + convert(Integer.parseInt(xx))); } }
PRINT LIST with Nodes:
import java.util.ArrayList; import java.util.List; public class RecursionList { public static void main(String args[]) { List list = new ArrayList(); list.add("A"); list.add("B"); List list1 = new ArrayList(); list1.add("C"); list1.add("D"); list1.add("E"); list.add(list1); list.add("F"); printList(list, "list"); } private static void printList(List genericList, String previousPrintStatement) { int index = 0; String previousString = ""; for (Object object : genericList) { previousString = previousPrintStatement + "." + index++; if (object instanceof List) { printList((List) object, previousString); } else { System.out.println("" + previousString + " " + " = " + object); } } } }
No comments:
Post a Comment