Skip to main content

Posts

Program for the power of the number using recursion in java

 Program for the power of the number using recursion in java Code: import java.util.Scanner; public class Test{ public static void main(String[] args) { Scanner scn = new Scanner(System.in); System.out.println(power(6,3)); } public static int power(int n,int pow){ if(pow==1){ return n; } return n * power(n,pow-1); } } or == import java.util.Scanner; public class Test{ public static void main(String[] args) { Scanner scn = new Scanner(System.in); System.out.println(power(2,10)); } public static int power(int n,int pow){ if(pow==1){ return n; } if(n==0){ return 0; } if(pow%2==0){ return power(n,pow/2) * power(n,pow/2); }else{ return power(n,pow/2) * power(n,pow/2) * n; } } }

program for printing the fibonacci series upto n using recursion in java

 program for printing the fibonacci series upto n using recursion in java Code:: import java.util.Scanner; public class Test{ static int a=0,b=1,c; public static void main(String[] args) { Scanner scn = new Scanner(System.in); System.out.println("Enter the number "); int n = scn.nextInt(); if(n==1){ System.out.println(0); }else if(n==2){ System.out.println(0+" "+1); }else{ System.out.print(0+" "+1+" "); fib(n-2); } } public static void fib(int n){ if(n==0){ System.out.println(); return; } c = a+b; a = b; b = c; System.out.print(c+" "); fib(n-1); } }

program for printing the 1 to n using recursion in java

 program for printing the 1 to n using recursion in java Code: import java.util.Scanner; public class Test{ public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); print(n); } public static void print(int n){ if(n==0){ return; } print(n-1); System.out.println(n); } } i/p: 5 o/p: 1 2 3 4 5

program for the sum of n numbers using recursion in java

 program for the sum of n numbers using recursion in java Code: import java.util.Scanner; public class Test{ public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); System.out.println(sum(n)); } public static int sum(int n){ if(n==0){ return 0; } return n + sum(n-1); } } i/p: 5 o/p: 15 i/p: 10 o/p: 55

find the nearest prime number in java

 find the nearest prime number in java The closest prime can be greater or smaller than the passed input integer. If there are equi-distant  prime-numbers, print both. Example: Input#1: 32 Output#1: 31 Input#2: 30 Output#2: 29 31 Code:: import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); find(n); } public static void find(int num) { // greater number int num1 = num + 1; while (true) { if (isPrime(num1)) { break; } num1++; } // smaller int num2 = num - 1; while (num2 > 1) { if (isPrime(num2)) { break; } num2--; } // System.out.println(num1+" "+num2); if (num2 == 1) { System.out.println(num1); } else if (num1 - num == num - num2) { System.out.println(num2 + " " + num1); } else if (num1 - num < num - num2) { System.out.println(num1); } else { System.out.println(num2)...

how to iterate hashmap in java

how to iterate hashmap in java  for explanation watch video:: 1) import java.util.*; public class Test{   public static void main(String args[])  {   HashMap<Integer,String> hm = new HashMap<>(); hm.put(1,"raja"); hm.put(2,"ram"); hm.put(3,"mohan"); hm.put(4,"roy"); for(Map.Entry<Integer,String> e : hm.entrySet()){ System.out.println(e.getKey() + " "+e.getValue()); } }    }  2) Code: import java.util.*; public class Test{   public static void main(String args[])  {   HashMap<Integer,String> hm = new HashMap<>(); hm.put(1,"raja"); hm.put(2,"ram"); hm.put(3,"mohan"); hm.put(4,"roy"); //keys for(int i : hm.keySet()){ System.out.print(i+" "); } System.out.println(); for(String s : hm.values()){ System.out.print(s+" "); } System.out.println(); }    }  3) import java.util.*...