Skip to main content

Posts

Showing posts from June, 2022

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.*...

Binary Search program in java

 Binary Search program in java Program: import java.util.*; import java.lang.*; import java.io.*; class Test{ public static void main(String[] args){ int[] arr = {-8,-3,-1,5,7,9}; System.out.println(bin(arr,5)); } public static int bin(int[] arr,int num){ int s = 0; int e = arr.length-1; while(s<=e){ int mid = (s+e)/2; if(num==arr[mid]){ return mid; }else if(num > arr[mid]){ s = mid+1; }else{ e = mid-1; } } return -1; } } Recursive  ======= Code: import java.util.*; import java.lang.*; import java.io.*; class Codechef{ public static void main(String[] args){ int[] arr = {-8,-3,-1,5,7,9}; System.out.println(rec(5,arr,0,arr.length-1)); } public static int rec(int val,int[] arr,int s,int e){ if(s<=e){ int mid = (s+e)/2; int cmp = arr[mid]; if(cmp == val){ return mid; }else if(cmp > val){ return rec(val,arr,s,mid-1); }else{ return rec(val,arr,mid+1,e); ...

java program for finding the nth fibonacci number using recursion

 java program for finding the nth fibonacci number using recursion Code:: import java.util.*; import java.lang.*; import java.io.*; class Test{ public static int fib(int n){ if(n<=1){ return n; } return fib(n-1) + fib(n-2); } public static void main(String[] args){ System.out.println(fib(7)); } }  o/p::  13 0 -> 0th fib num 1-> 1st fib num 1 -> 2nd fib num 2 -> 3rd fib num 3....>4th 5...->5th 8->6th 13-> 7th

binary to decimal conversion code in java

 binary to decimal conversion code in java Code: import java.util.*; import java.lang.*; import java.io.*; class Test{ public static void main(String[] args){ Scanner scn = new Scanner(System.in); System.out.println("Enter the Binary Number : "); int bin = scn.nextInt(); deci(bin); } public static void deci(int bin){ int res = 0; int i = 0; while(bin!=0){ int r = bin%10; res = res + (r*(int)Math.pow(2,i)); i++; bin = bin/10; } System.out.println(res); } } ex: input : 1010 o/p:  10 input : 1000 o/p: 8 i/p: 10101 o/p: 21

Decimal to Hexadecimal Conversion code in java

 Decimal to Hexadecimal Conversion Code:: import java.util.*; import java.lang.*; import java.io.*; class Test{ public static void main(String[] args){ Scanner scn = new Scanner(System.in); int dec = scn.nextInt(); hexa(dec); } public static void hexa(int dec){ String hexa = "0123456789ABCDEF"; String res = ""; while(dec!=0){ int r = dec%16; res = hexa.charAt(r) + res; dec = dec/16; } System.out.println(res); } } ex:: hexadecimal Number :: F Decimal Number :: 15 hexadecimal number : 1A decimal number : 26

Population Census hackerrank Solution

Population Census hackerrank Solution for explanation watch video: Code:: select sum(city.population) from city inner join country on CITY.CountryCode = COUNTRY.Code where country.CONTINENT = 'Asia';

Valid Parentheses Question Solution in java

  Valid Parentheses Question Solution in java Code:  import java.util.*; class Test { public static void main(String[] args){ Scanner scn = new Scanner(System.in); String s = scn.next(); System.out.println(isValid(s)); } public static boolean isValid(String s) {        Stack<Character> st = new Stack<>();         for(int i=0;i<s.length();i++){             char ch = s.charAt(i);             if(ch=='('||ch=='['||ch=='{'){                 st.push(ch);             }else if(st.isEmpty()){                 return false;             }else { char top = st.pop();                 if(ch==')'&&top!='('){                   ...

Kadane's Algorithm implementation in java | Largest Sum Contiguous Subarray

 Kadane's Algorithm implementation in java Code::  import java.util.*; class Test { public static void main(String[] args){ int[] arr =  {1,2,3,-2,5}; int maxSum = arr[0]; int currSum = 0; for(int i=0;i<arr.length;i++){ currSum += arr[i]; if(currSum > maxSum){ maxSum = currSum; } if(currSum<0){ currSum = 0; } } System.out.println(maxSum); } } o/p: 9 Largest Sum Contiguous Subarray Brute Force  Code:  import java.util.*; class Test { public static void main(String[] args){ int[] arr =  {1,2,3,-2,5}; int max = Integer.MIN_VALUE; for(int i=0;i<arr.length;i++){ int sum = arr[i]; max = Math.max(sum,max); for(int j=i+1;j<arr.length;j++){ sum = sum + arr[j]; max = Math.max(sum,max); } } System.out.println(max); } }

Sorting Algorithms Implementation In java

  Selection Sort Program in java Code: import java.util.*; import java.lang.*; import java.io.*; //selection Sort class Test { public static void main(String[] args){ int[] arr = {2,7,4,1,5,3}; for(int i=0;i<arr.length;i++){ int minVal = arr[i]; int ind = i; for(int j= i+1;j<arr.length;j++){ if(arr[j] < minVal){ ind = j; minVal = arr[j]; } } //swap with minimum  int temp = arr[i]; arr[i] = minVal; arr[ind] = temp; } for(int i : arr){ System.out.print(i+" "); } } }  Bubble Sort Program in java Code:: import java.util.*; import java.lang.*; import java.io.*; //Bubble Sort class Test { public static void main(String[] args){ int[] arr = {2,7,4,1,5,3}; for(int i=0;i<arr.length;i++){ //the last val is at its coorect Position for(int j=0;j<arr.length-i-1;j++){ if(arr[j]>arr[j+1]){ //swap int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp...

Quick Sort Program in java

 Quick Sort Program in java Code:: import java.util.*; import java.lang.*; import java.io.*; //Quick Sort class Test { public static void main(String[] args){ int[] arr = {8,7,3,4,11,13,14,2,17}; quickSort(arr,0,arr.length-1); for(int i : arr){ System.out.print(i+ " "); } } public static void quickSort(int[] arr,int s,int e){ if(s>=e){ return; } int pIndex = partition(arr,s,e); quickSort(arr,s,pIndex-1); quickSort(arr,pIndex+1,e); } public static int partition(int[] arr,int s,int e){ int pivot = arr[e]; int pIndex = s; for(int i=s;i<=e-1;i++){  if(arr[i] <= pivot){ int temp = arr[i]; arr[i] = arr[pIndex]; arr[pIndex] = temp; pIndex++; } } int temp = arr[e]; arr[e] = arr[pIndex]; arr[pIndex] = temp; return pIndex; } }

insertion Sort Program in java

 insertion Sort Program in java Code: import java.util.*; import java.lang.*; import java.io.*; //Insertion Sort class Test{ public static void main(String[] args){ int[] arr = {7,2,4,1,5,3}; insertion(arr); for(int i : arr){ System.out.print(i+" "); } } public static void insertion(int[] arr){ for(int i=1;i<arr.length;i++){ int hole = i; int val = arr[i]; while(hole>0 && arr[hole-1]>val){ arr[hole] = arr[hole-1]; hole--; } arr[hole] = val; } } }

Bubble Sort Program in java

 Bubble Sort Program in java Code:: import java.util.*; import java.lang.*; import java.io.*; //Bubble Sort class Test{ public static void main(String[] args){ int[] arr = {2,7,4,1,5,3}; bubble(arr); for(int i : arr){ System.out.print(i+" "); } } public static void bubble(int[] arr){ for(int i=0;i<arr.length;i++){ //the last val is at its correct Position for(int j=0;j<arr.length-i-1;j++){ if(arr[j]>arr[j+1]){ //swap int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } } Optimized code if the right part of the array is sorted code: import java.util.*; import java.lang.*; import java.io.*; //Bubble Sort class Test{ public static void main(String[] args){ int[] arr = {2,7,4,1,5,3}; bubble(arr); for(int i : arr){ System.out.print(i+" "); } } public static void bubble(int[] arr){ for(int i=0;i<arr.length;i++){ int swap = 0; for(int j=0;j<...

Selection Sort Program in java

 Selection Sort Program in java Code: import java.util.*; import java.lang.*; import java.io.*; class Test{ public static void main(String[] args){ int[] arr = {7,2,4,1,5,3}; selection(arr); for(int i : arr){ System.out.print(i+ " "); } } public static void selection(int[] arr){ for(int i=0;i<arr.length;i++){ int minVal = arr[i]; int ind = i; for(int j= i+1;j<arr.length;j++){ if(arr[j] < minVal){ ind = j; minVal = arr[j]; } } //swap with minimum  int temp = arr[i]; arr[i] = minVal; arr[ind] = temp; } } }

decimal to binary , decimal to octal , decimal to hexadeciaml conversions and vice versa programs in java

 binary to decimal conversion code: import java.util.*; import java.lang.*; import java.io.*; //binary to decimal class Test { public static void main(String[] args){ Scanner scn = new Scanner(System.in); //take the binary number from the user int bin = scn.nextInt(); //write the lgic to convert it into decimal int dec = 0; int i = 0;//digit place while(bin!=0){ //last digit int r = bin%10; //multiple remaider with 2 power digit place dec = dec +(r* (int)Math.pow(2,i)); //increment the position of i i++; //remove the last digit from the number bin = bin/10; } //print the decimal number System.out.println(dec); } } ex:: input : 1010 o/p: 10 input : 1000 o/p: 8 octal to decimal Conversion Code:: import java.util.*; import java.lang.*; import java.io.*; //binary to decimal class Test { public static void main(String[] args){ Scanner scn = new Scanner(System.in); //take the octal number from the user int oct =...

program for finding lcm and hcf of two numbers using modulo operator in euclidean algorithm in java

 hcf of the numbers Code:: import java.util.*; import java.lang.*; import java.io.*; //hcf of two numbers  //by efficient use of modulo operator in euclidean algorithm class Test { public static void main(String[] args){ int num1=36,num2=60,hcf; hcf = getHcf(num1,num2); System.out.println("the hcf : "+hcf); } public static int getHcf(int a,int b){ return b==0 ? a : getHcf(b,a%b); } } LCM of two numbers property  if we have 2 numbers num1,num2 and there lcm is l and hcf is h then num1 * num2 = l * h Code:: import java.util.*; import java.lang.*; import java.io.*; //lcm of two numbers  //by efficient use of modulo operator in euclidean algorithm class Test { public static void main(String[] args){ int num1=36,num2=60,hcf; hcf = getHcf(num1,num2); int lcm = (num1*num2)/hcf; System.out.println("the lcm: "+lcm); } public static int getHcf(int a,int b){ return b==0 ? a : getHcf(b,a%b); } }

Merge Sort Program in java

 Merge Sort Program in java Code :: import java.util.*; import java.lang.*; import java.io.*; class Test { public static void main(String[] args){ int[] arr = {7,2,4,1,5,3}; mergeSort(arr); for(int i : arr){ System.out.print(i+" "); } } public static void mergeSort(int[] arr){ int n = arr.length; if(n<2){ return; } int mid = n/2; int[] left = new int[mid]; int[] right = new int[n-mid]; for(int i=0;i<mid;i++){ left[i] = arr[i]; } for(int i=mid;i<n;i++){ right[i-mid] = arr[i]; } mergeSort(left); mergeSort(right); merge(left,right,arr); } public static void merge(int[] left,int[] right,int[] arr){ int i = 0; int j = 0; int k = 0; while(i<left.length && j<right.length){ if(left[i]<right[j]){ arr[k] = left[i]; i++; }else{ arr[k] = right[j]; j++; } k++; } while(i!=left.length){ arr[k] = left[i]; i++; k++; } wh...

Interacting With Multiple Databases in Hibernate

 Interacting With Multiple Databases in Hibernate For Explanation watch video :: Directory Structure: pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>OracleMySQLHB</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>OracleMySQLHB</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.9</maven.compiler.source> <maven.compiler.target>1.9</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>j...

Component Mapping in Hibernate Using Annotations

Component Mapping in Hibernate Using Annotations For explanation watch video:: Directory Structure pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>ComponentMapping</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>ComponentMapping</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.9</maven.compiler.source> <maven.compiler.target>1.9</maven.compiler.target> </properties> <dependencies> <dependency> <groupId...