"Products of ranges in an array | Java program to find Product in range Queries in O ( 1 ) ; Returns modulo inverse of a with respect to m using extended Euclid Algorithm Assumption : a and m are coprimes , i . e . , gcd ( a , m ) = 1 ; q is quotient ; m is remainder now , process same as Euclid 's algo ; Make x1 positive ; calculating pre_product array ; Cacluating inverse_product array . ; Function to calculate Product in the given range . ; As our array is 0 based as and L and R are given as 1 based index . ; Driver code ; Array ; Prime P ; Calculating PreProduct and InverseProduct ; Range [ L , R ] in 1 base index"
How can the above be solved in Java?

class GFG { static int MAX = 100 ; int pre_product [ ] = new int [ MAX ] ; int inverse_product [ ] = new int [ MAX ] ; int modInverse ( int a , int m ) { int m0 = m , t , q ; int x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } void calculate_Pre_Product ( int A [ ] , int N , int P ) { pre_product [ 0 ] = A [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { pre_product [ i ] = pre_product [ i - 1 ] * A [ i ] ; pre_product [ i ] = pre_product [ i ] % P ; } } void calculate_inverse_product ( int A [ ] , int N , int P ) { inverse_product [ 0 ] = modInverse ( pre_product [ 0 ] , P ) ; for ( int i = 1 ; i < N ; i ++ ) inverse_product [ i ] = modInverse ( pre_product [ i ] , P ) ; } int calculateProduct ( int A [ ] , int L , int R , int P ) { L = L - 1 ; R = R - 1 ; int ans ; if ( L == 0 ) ans = pre_product [ R ] ; else ans = pre_product [ R ] * inverse_product [ L - 1 ] ; return ans ; } public static void main ( String [ ] s ) { GFG d = new GFG ( ) ; int A [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int P = 113 ; d . calculate_Pre_Product ( A , A . length , P ) ; d . calculate_inverse_product ( A , A . length , P ) ; int L = 2 , R = 5 ; System . out . println ( d . calculateProduct ( A , L , R , P ) ) ; L = 1 ; R = 3 ; System . out . println ( d . calculateProduct ( A , L , R , P ) ) ; } }


"Products of ranges in an array | Product in range Queries in O ( N ) ; Function to calculate Product in the given range . ; As our array is 0 based as and L and R are given as 1 based index . ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int calculateProduct ( int [ ] A , int L , int R , int P ) { L = L - 1 ; R = R - 1 ; int ans = 1 ; for ( int i = L ; i <= R ; i ++ ) { ans = ans * A [ i ] ; ans = ans % P ; } return ans ; } static public void main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 , 6 } ; int P = 229 ; int L = 2 , R = 5 ; System . out . println ( calculateProduct ( A , L , R , P ) ) ; L = 1 ; R = 3 ; System . out . println ( calculateProduct ( A , L , R , P ) ) ; } }


"Program To Check whether a Triangle is Equilateral , Isosceles or Scalene | Java program for the above approach ; Function to check if the triangle is equilateral or isosceles or scalene ; Check for equilateral triangle ; Check for isosceles triangle ; Otherwise scalene triangle ; Driver Code ; Given sides of triangle ; Function call"
How can the above be solved in Java?

class GFG { static void checkTriangle ( int x , int y , int z ) { if ( x == y && y == z ) System . out . println ( " Equilateral ▁ Triangle " ) ; else if ( x == y y == z z == x ) System . out . println ( " Isosceles ▁ Triangle " ) ; else System . out . println ( " Scalene ▁ Triangle " ) ; } public static void main ( String [ ] args ) { int x = 8 , y = 7 , z = 9 ; checkTriangle ( x , y , z ) ; } }


"Program for Area And Perimeter Of Rectangle | Java program to find area and perimeter of rectangle ; Utility function ; Driver Function"
How can the above be solved in Java?

import java . io . * ; class Geometry { static int areaRectangle ( int a , int b ) { int area = a * b ; return area ; } static int perimeterRectangle ( int a , int b ) { int perimeter = 2 * ( a + b ) ; return perimeter ; } public static void main ( String [ ] args ) { int a = 5 ; int b = 6 ; System . out . println ( " Area ▁ = ▁ " + areaRectangle ( a , b ) ) ; System . out . println ( " Perimeter ▁ = ▁ " + perimeterRectangle ( a , b ) ) ; } }


"Program for Area Of Square after N | Java program to find the area of the square ; Function to calculate area of square after given number of folds ; Driver Code"
How can the above be solved in Java?

class GFG { static double areaSquare ( double side , double fold ) { double area = side * side ; return area * 1.0 / Math . pow ( 2 , fold ) ; } public static void main ( String [ ] args ) { double side = 4 , fold = 2 ; System . out . println ( areaSquare ( side , fold ) ) ; } }


"Program for Area Of Square | Java program to find the area of the square ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int areaSquare ( int side ) { int area = side * side ; return area ; } public static void main ( String [ ] args ) { int side = 5 ; System . out . println ( areaSquare ( 4 ) ) ; } }


"Program for Armstrong Numbers |"
How can the above be solved in Java?

public class armstrongNumber { public void isArmstrong ( String n ) { char [ ] s = n . toCharArray ( ) ; int size = s . length ; int sum = 0 ; for ( char num : s ) { int temp = 1 ; int i = Integer . parseInt ( Character . toString ( num ) ) ; for ( int j = 0 ; j <= size - 1 ; j ++ ) { temp *= i ; } sum += temp ; } if ( sum == Integer . parseInt ( n ) ) { System . out . println ( " True " ) ; } else { System . out . println ( " False " ) ; } } public static void main ( String [ ] args ) { armstrongNumber am = new armstrongNumber ( ) ; am . isArmstrong ( "2" ) ; am . isArmstrong ( "153" ) ; am . isArmstrong ( "1634" ) ; am . isArmstrong ( "231" ) ; } }


"Program for Best Fit algorithm in Memory Management | Java implementation of Best - Fit algorithm ; Method to allocate memory to blocks as per Best fit algorithm ; Stores block id of the block allocated to a process ; Initially no block is assigned to any process ; pick each process and find suitable blocks according to its size ad assign to it ; Find the best fit block for current process ; If we could find a block for current process ; allocate block j to p [ i ] process ; Reduce available memory in this block . ; Driver Method"
How can the above be solved in Java?

public class GFG { static void bestFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ ] = new int [ n ] ; for ( int i = 0 ; i < allocation . length ; i ++ ) allocation [ i ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int bestIdx = - 1 ; for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { if ( bestIdx == - 1 ) bestIdx = j ; else if ( blockSize [ bestIdx ] > blockSize [ j ] ) bestIdx = j ; } } if ( bestIdx != - 1 ) { allocation [ i ] = bestIdx ; blockSize [ bestIdx ] -= processSize [ i ] ; } } System . out . println ( " Process No . Process Size Block no . "); for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( " ▁ " + ( i + 1 ) + " 	 	 " + processSize [ i ] + " 	 	 " ) ; if ( allocation [ i ] != - 1 ) System . out . print ( allocation [ i ] + 1 ) ; else System . out . print ( " Not ▁ Allocated " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int blockSize [ ] = { 100 , 500 , 200 , 300 , 600 } ; int processSize [ ] = { 212 , 417 , 112 , 426 } ; int m = blockSize . length ; int n = processSize . length ; bestFit ( blockSize , m , processSize , n ) ; } }


"Program for Binary To Decimal Conversion | Function to convert binary to decimal ; Initializing base value to 1 , i . e 2 ^ 0 ; Driver Code"
How can the above be solved in Java?

class GFG { static int binaryToDecimal ( int n ) { int num = n ; int dec_value = 0 ; int base = 1 ; int temp = num ; while ( temp > 0 ) { int last_digit = temp % 10 ; temp = temp / 10 ; dec_value += last_digit * base ; base = base * 2 ; } return dec_value ; } public static void main ( String [ ] args ) { int num = 10101001 ; System . out . println ( binaryToDecimal ( num ) ) ; } }


"Program for Binomial Coefficients table | Java program for binomial coefficients ; Function to print binomial table ; B ( m , x ) is 1 if either m or x is is 0. ; Otherwise using recursive formula B ( m , x ) = B ( m , x - 1 ) * ( m - x + 1 ) / x ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void printbinomial ( int max ) { for ( int m = 0 ; m <= max ; m ++ ) { System . out . print ( m + " ▁ " ) ; int binom = 1 ; for ( int x = 0 ; x <= m ; x ++ ) { if ( m != 0 && x != 0 ) binom = binom * ( m - x + 1 ) / x ; System . out . print ( binom + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int max = 10 ; printbinomial ( max ) ; } }


"Program for Celsius To Fahrenheit conversion | Java program to convert Celsius scale to Fahrenheit scale ; function to convert Celsius scale to Fahrenheit scale ; Driver code"
How can the above be solved in Java?

class GFG { static float Cel_To_Fah ( float n ) { return ( ( n * 9.0f / 5.0f ) + 32.0f ) ; } public static void main ( String [ ] args ) { float n = 20.0f ; System . out . println ( Cel_To_Fah ( n ) ) ; } }


"Program for Centered Icosahedral Number | Java Program to find nth Centered icosahedral number Java Program to find nth Centered icosahedral number ; Function to find Centered icosahedral number ; Formula to calculate nth Centered icosahedral number and return it into main function . ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int centeredIcosahedralNum ( int n ) { return ( 2 * n + 1 ) * ( 5 * n * n + 5 * n + 3 ) / 3 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( centeredIcosahedralNum ( n ) ) ; n = 12 ; System . out . println ( centeredIcosahedralNum ( n ) ) ; } }


"Program for Chocolate and Wrapper Puzzle | Efficient Java program to find maximum number of chocolates ; Returns maximum number of chocolates we can eat with given money , price of chocolate and number of wrapprices required to get a chocolate . ; Corner case ; First find number of chocolates that can be purchased with the given amount ; Now just add number of chocolates with the chocolates gained by wrapprices ; Driver code ; total money ; cost of each candy ; no of wrappers needs to be ; exchanged for one chocolate ."
How can the above be solved in Java?

import java . io . * ; class GFG { static int countMaxChoco ( int money , int price , int wrap ) { if ( money < price ) return 0 ; int choc = money / price ; choc = choc + ( choc - 1 ) / ( wrap - 1 ) ; return choc ; } public static void main ( String [ ] args ) { int money = 15 ; int price = 1 ; int wrap = 3 ; System . out . println ( countMaxChoco ( money , price , wrap ) ) ; } }


"Program for Decimal to Binary Conversion | Java implementation of the approach ; Function to return the binary equivalent of decimal value N ; To store the binary number ; Count used to store exponent value ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int decimalToBinary ( int N ) { int B_Number = 0 ; int cnt = 0 ; while ( N != 0 ) { int rem = N % 2 ; double c = Math . pow ( 10 , cnt ) ; B_Number += rem * c ; N /= 2 ; cnt ++ ; } return B_Number ; } public static void main ( String [ ] args ) { int N = 17 ; System . out . println ( decimalToBinary ( N ) ) ; } }


"Program for Decimal to Binary Conversion | Java program to convert a decimal number to binary number ; function to convert decimal to binary ; array to store binary number ; counter for binary array ; storing remainder in binary array ; printing binary array in reverse order ; driver program"
How can the above be solved in Java?

import java . io . * ; class GFG { static void decToBinary ( int n ) { int [ ] binaryNum = new int [ 32 ] ; int i = 0 ; while ( n > 0 ) { binaryNum [ i ] = n % 2 ; n = n / 2 ; i ++ ; } for ( int j = i - 1 ; j >= 0 ; j -- ) System . out . print ( binaryNum [ j ] ) ; } public static void main ( String [ ] args ) { int n = 17 ; decToBinary ( n ) ; } }


"Program for First Fit algorithm in Memory Management | Java implementation of First - Fit algorithm ; Method to allocate memory to blocks as per First fit algorithm ; Stores block id of the block allocated to a process ; Initially no block is assigned to any process ; pick each process and find suitable blocks according to its size ad assign to it ; allocate block j to p [ i ] process ; Reduce available memory in this block . ; Driver Code"
How can the above be solved in Java?

class GFG { static void firstFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ ] = new int [ n ] ; for ( int i = 0 ; i < allocation . length ; i ++ ) allocation [ i ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { allocation [ i ] = j ; blockSize [ j ] -= processSize [ i ] ; break ; } } } System . out . println ( " Process No . Process Size Block no . "); for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( " ▁ " + ( i + 1 ) + " 	 	 " + processSize [ i ] + " 	 	 " ) ; if ( allocation [ i ] != - 1 ) System . out . print ( allocation [ i ] + 1 ) ; else System . out . print ( " Not ▁ Allocated " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int blockSize [ ] = { 100 , 500 , 200 , 300 , 600 } ; int processSize [ ] = { 212 , 417 , 112 , 426 } ; int m = blockSize . length ; int n = processSize . length ; firstFit ( blockSize , m , processSize , n ) ; } }


"Program for Gauss | Java Implementation for Gauss - Jordan Elimination Method ; Function to print the matrix ; function to reduce matrix to reduced row echelon form . ; Performing elementary operations ; Excluding all i == j ; Converting Matrix to reduced row echelon form ( diagonal matrix ) ; Function to print the desired result if unique solutions exists , otherwise prints no solution or infinite solutions depending upon the input given . ; Printing the solution by dividing constants by their respective diagonal elements ; To check whether infinite solutions exists or no solution exists ; flag == 2 for infinite solution flag == 3 for No solution ; Driver code ; Order of Matrix ( n ) ; Performing Matrix transformation ; Printing Final Matrix ; Printing Solutions ( if exist )"
How can the above be solved in Java?

class GFG { static int M = 10 ; static void PrintMatrix ( float a [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) System . out . print ( a [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } static int PerformOperation ( float a [ ] [ ] , int n ) { int i , j , k = 0 , c , flag = 0 , m = 0 ; float pro = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] [ i ] == 0 ) { c = 1 ; while ( ( i + c ) < n && a [ i + c ] [ i ] == 0 ) c ++ ; if ( ( i + c ) == n ) { flag = 1 ; break ; } for ( j = i , k = 0 ; k <= n ; k ++ ) { float temp = a [ j ] [ k ] ; a [ j ] [ k ] = a [ j + c ] [ k ] ; a [ j + c ] [ k ] = temp ; } } for ( j = 0 ; j < n ; j ++ ) { if ( i != j ) { float p = a [ j ] [ i ] / a [ i ] [ i ] ; for ( k = 0 ; k <= n ; k ++ ) a [ j ] [ k ] = a [ j ] [ k ] - ( a [ i ] [ k ] ) * p ; } } } return flag ; } static void PrintResult ( float a [ ] [ ] , int n , int flag ) { System . out . print ( " Result ▁ is ▁ : ▁ " ) ; if ( flag == 2 ) System . out . println ( " Infinite ▁ Solutions ▁ Exists " ) ; else if ( flag == 3 ) System . out . println ( " No ▁ Solution ▁ Exists " ) ; else { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] [ n ] / a [ i ] [ i ] + " ▁ " ) ; } } static int CheckConsistency ( float a [ ] [ ] , int n , int flag ) { int i , j ; float sum ; flag = 3 ; for ( i = 0 ; i < n ; i ++ ) { sum = 0 ; for ( j = 0 ; j < n ; j ++ ) sum = sum + a [ i ] [ j ] ; if ( sum == a [ i ] [ j ] ) flag = 2 ; } return flag ; } public static void main ( String [ ] args ) { float a [ ] [ ] = { { 0 , 2 , 1 , 4 } , { 1 , 1 , 2 , 6 } , { 2 , 1 , 1 , 7 } } ; int n = 3 , flag = 0 ; flag = PerformOperation ( a , n ) ; if ( flag == 1 ) flag = CheckConsistency ( a , n , flag ) ; System . out . println ( " Final ▁ Augumented ▁ Matrix ▁ is ▁ : ▁ " ) ; PrintMatrix ( a , n ) ; System . out . println ( " " ) ; PrintResult ( a , n , flag ) ; } }


"Program for Identity Matrix | Java program to check if a given matrix is identity ; Driver Code"
How can the above be solved in Java?

class GFG { int MAX = 100 ; static boolean isIdentity ( int mat [ ] [ ] , int N ) { for ( int row = 0 ; row < N ; row ++ ) { for ( int col = 0 ; col < N ; col ++ ) { if ( row == col && mat [ row ] [ col ] != 1 ) return false ; else if ( row != col && mat [ row ] [ col ] != 0 ) return false ; } } return true ; } public static void main ( String args [ ] ) { int N = 4 ; int mat [ ] [ ] = { { 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 0 } , { 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 1 } } ; if ( isIdentity ( mat , N ) ) System . out . println ( " Yes ▁ " ) ; else System . out . println ( " No ▁ " ) ; } }


"Program for Identity Matrix | Java program to print Identity Matrix ; Checking if row is equal to column ; Driver Code"
How can the above be solved in Java?

class GFG { static int identity ( int num ) { int row , col ; for ( row = 0 ; row < num ; row ++ ) { for ( col = 0 ; col < num ; col ++ ) { if ( row == col ) System . out . print ( 1 + " ▁ " ) ; else System . out . print ( 0 + " ▁ " ) ; } System . out . println ( ) ; } return 0 ; } public static void main ( String args [ ] ) { int size = 5 ; identity ( size ) ; } }


"Program for Least Recently Used ( LRU ) Page Replacement algorithm | Java implementation of above algorithm ; Method to find page faults using indexes ; To represent set of current pages . We use an unordered_set so that we quickly check if a page is present in set or not ; To store least recently used indexes of pages . ; Start from initial page ; Check if the set can hold more pages ; Insert it into set if not present already which represents page fault ; increment page fault ; Store the recently used index of each page ; If the set is full then need to perform lru i . e . remove the least recently used page and insert the current page ; Check if current page is not already present in the set ; Find the least recently used pages that is present in the set ; Remove the indexes page ; remove lru from hashmap ; insert the current page ; Increment page faults ; Update the current page index ; Driver method"
How can the above be solved in Java?

import java . util . HashMap ; import java . util . HashSet ; import java . util . Iterator ; class Test { static int pageFaults ( int pages [ ] , int n , int capacity ) { HashSet < Integer > s = new HashSet < > ( capacity ) ; HashMap < Integer , Integer > indexes = new HashMap < > ( ) ; int page_faults = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . size ( ) < capacity ) { if ( ! s . contains ( pages [ i ] ) ) { s . add ( pages [ i ] ) ; page_faults ++ ; } indexes . put ( pages [ i ] , i ) ; } else { if ( ! s . contains ( pages [ i ] ) ) { int lru = Integer . MAX_VALUE , val = Integer . MIN_VALUE ; Iterator < Integer > itr = s . iterator ( ) ; while ( itr . hasNext ( ) ) { int temp = itr . next ( ) ; if ( indexes . get ( temp ) < lru ) { lru = indexes . get ( temp ) ; val = temp ; } } s . remove ( val ) ; indexes . remove ( val ) ; s . add ( pages [ i ] ) ; page_faults ++ ; } indexes . put ( pages [ i ] , i ) ; } } return page_faults ; } public static void main ( String args [ ] ) { int pages [ ] = { 7 , 0 , 1 , 2 , 0 , 3 , 0 , 4 , 2 , 3 , 0 , 3 , 2 } ; int capacity = 4 ; System . out . println ( pageFaults ( pages , pages . length , capacity ) ) ; } }


"Program for Least Recently Used ( LRU ) Page Replacement algorithm | Java program for page replacement algorithms ; Driver method ; To represent set of current pages . We use an Arraylist ; Insert it into set if not present already which represents page fault ; Check if the set can hold equal pages ; Increment page faults ; Remove the indexes page ; insert the current page"
How can the above be solved in Java?

import java . util . ArrayList ; public class LRU { public static void main ( String [ ] args ) { int capacity = 4 ; int arr [ ] = { 7 , 0 , 1 , 2 , 0 , 3 , 0 , 4 , 2 , 3 , 0 , 3 , 2 } ; ArrayList < Integer > s = new ArrayList < > ( capacity ) ; int count = 0 ; int page_faults = 0 ; for ( int i : arr ) { if ( ! s . contains ( i ) ) { if ( s . size ( ) == capacity ) { s . remove ( 0 ) ; s . add ( capacity - 1 , i ) ; } else s . add ( count , i ) ; page_faults ++ ; ++ count ; } else { s . remove ( ( Object ) i ) ; s . add ( s . size ( ) , i ) ; } } System . out . println ( page_faults ) ; } }


"Program for Markov matrix | Java code to check Markov Matrix ; outer loop to access rows and inner to access columns ; Find sum of current row ; Driver Code ; Matrix to check ; calls the function check ( )"
How can the above be solved in Java?

import java . io . * ; public class markov { static boolean checkMarkov ( double m [ ] [ ] ) { for ( int i = 0 ; i < m . length ; i ++ ) { double sum = 0 ; for ( int j = 0 ; j < m [ i ] . length ; j ++ ) sum = sum + m [ i ] [ j ] ; if ( sum != 1 ) return false ; } return true ; } public static void main ( String args [ ] ) { double m [ ] [ ] = { { 0 , 0 , 1 } , { 0.5 , 0 , 0.5 } , { 1 , 0 , 0 } } ; if ( checkMarkov ( m ) ) System . out . println ( " ▁ yes ▁ " ) ; else System . out . println ( " ▁ no ▁ " ) ; } }


"Program for Mean Absolute Deviation | java Program to find mean absolute deviation of given array ; Function to find mean of the array elements . ; Calculate sum of all elements . ; Function to find mean absolute deviation of given elements . ; Calculate the sum of absolute deviation about mean . ; Return mean absolute deviation about mean . ; Driver function ."
How can the above be solved in Java?

import java . io . * ; class GFG { static float Mean ( float arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; return sum / n ; } static float meanAbsDevtion ( float arr [ ] , int n ) { float absSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) absSum = absSum + Math . abs ( arr [ i ] - Mean ( arr , n ) ) ; return absSum / n ; } public static void main ( String [ ] args ) { float arr [ ] = { 10 , 15 , 15 , 17 , 18 , 21 } ; int n = arr . length ; System . out . println ( meanAbsDevtion ( arr , n ) ) ; } }


"Program for Mean and median of an unsorted array | Java program to find mean and median of an array ; Function for calculating mean ; Function for calculating median ; First we sort the array ; check for even case ; Driver code ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { public static double findMean ( int a [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; return ( double ) sum / ( double ) n ; } public static double findMedian ( int a [ ] , int n ) { Arrays . sort ( a ) ; if ( n % 2 != 0 ) return ( double ) a [ n / 2 ] ; return ( double ) ( a [ ( n - 1 ) / 2 ] + a [ n / 2 ] ) / 2.0 ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 } ; int n = a . length ; System . out . println ( " Mean ▁ = ▁ " + findMean ( a , n ) ) ; System . out . println ( " Median ▁ = ▁ " + findMedian ( a , n ) ) ; } }


"Program for Method Of False Position | java program for implementation of Bisection Method for solving equations ; An example function whose solution is determined using Bisection Method . The function is x ^ 3 - x ^ 2 + 2 ; Prints root of func ( x ) in interval [ a , b ] ; Initialize result ; Find the point that touches x axis ; Check if the above found point is root ; Decide the side to repeat the steps ; Driver program ; Initial values assumed"
How can the above be solved in Java?

import java . io . * ; class GFG { static int MAX_ITER = 1000000 ; static double func ( double x ) { return ( x * x * x - x * x + 2 ) ; } static void regulaFalsi ( double a , double b ) { if ( func ( a ) * func ( b ) >= 0 ) { System . out . println ( " You ▁ have ▁ not ▁ assumed ▁ right ▁ a ▁ and ▁ b " ) ; } double c = a ; for ( int i = 0 ; i < MAX_ITER ; i ++ ) { c = ( a * func ( b ) - b * func ( a ) ) / ( func ( b ) - func ( a ) ) ; if ( func ( c ) == 0 ) break ; else if ( func ( c ) * func ( a ) < 0 ) b = c ; else a = c ; } System . out . println ( " The ▁ value ▁ of ▁ root ▁ is ▁ : ▁ " + ( int ) c ) ; } public static void main ( String [ ] args ) { double a = - 200 , b = 300 ; regulaFalsi ( a , b ) ; } }


"Program for Mobius Function | Java program to print all prime factors ; Returns value of mobius ( ) ; Handling 2 separately ; If 2 ^ 2 also divides N ; Check for all other prime factors ; If i divides n ; If i ^ 2 also divides N ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int mobius ( int n ) { int p = 0 ; if ( n % 2 == 0 ) { n = n / 2 ; p ++ ; if ( n % 2 == 0 ) return 0 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { n = n / i ; p ++ ; if ( n % i == 0 ) return 0 ; } } return ( p % 2 == 0 ) ? - 1 : 1 ; } public static void main ( String [ ] args ) { int N = 17 ; System . out . println ( " Mobius ▁ Functions " + " ▁ M ( N ) ▁ at ▁ N ▁ = ▁ " + N + " ▁ is : ▁ " + mobius ( N ) ) ; System . out . println ( " Mobius ▁ Functions " + " M ( N ) ▁ at ▁ N ▁ = ▁ " + 25 + " ▁ is : ▁ " + mobius ( 25 ) ) ; System . out . println ( " Mobius ▁ Functions " + " M ( N ) ▁ at ▁ N ▁ = ▁ " + 6 + " ▁ is : ▁ " + mobius ( 6 ) ) ; } }


"Program for Mobius Function | Set 2 | Java implementation of the approach ; Function to calculate least prime factor of each number ; If it is a prime number ; For all multiples which are not visited yet . ; Function to find the value of Mobius function for all the numbers from 1 to n ; To store the values of Mobius function ; If number is one ; If number has a squared prime factor ; Multiply - 1 with the previous number ; Driver code ; Function to find least prime factor ; Function to find mobius function"
How can the above be solved in Java?

import java . util . * ; class GFG { static int N = 100005 ; static int [ ] lpf = new int [ N ] ; static void least_prime_factor ( ) { for ( int i = 2 ; i < N ; i ++ ) if ( lpf [ i ] % 2 != 1 ) for ( int j = i ; j < N ; j += i ) if ( lpf [ j ] % 2 != 0 ) lpf [ j ] = i ; } static void Mobius ( int n ) { int [ ] mobius = new int [ N ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( i == 1 ) mobius [ i ] = 1 ; else { if ( lpf [ i / lpf [ i ] ] == lpf [ i ] ) mobius [ i ] = 0 ; else mobius [ i ] = - 1 * mobius [ i / lpf [ i ] ] ; } } for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( mobius [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 5 ; Arrays . fill ( lpf , - 1 ) ; least_prime_factor ( ) ; Mobius ( n ) ; } }


"Program for Muller Method | Java Program to find root of a function , f ( x ) ; function to calculate f ( x ) ; Taking f ( x ) = x ^ 3 + 2 x ^ 2 + 10 x - 20 ; Calculating various constants required to calculate x3 ; Taking the root which is closer to x2 ; checking for resemblance of x3 with x2 till two decimal places ; Driver main function"
How can the above be solved in Java?

import java . io . * ; import static java . lang . Math . * ; class Muller { static final int MAX_ITERATIONS = 10000 ; static double f ( double x ) { return 1 * pow ( x , 3 ) + 2 * x * x + 10 * x - 20 ; } static void Muller ( double a , double b , double c ) { int i ; double res ; for ( i = 0 ; ; ++ i ) { double f1 = f ( a ) ; double f2 = f ( b ) ; double f3 = f ( c ) ; double d1 = f1 - f3 ; double d2 = f2 - f3 ; double h1 = a - c ; double h2 = b - c ; double a0 = f3 ; double a1 = ( ( ( d2 * pow ( h1 , 2 ) ) - ( d1 * pow ( h2 , 2 ) ) ) / ( ( h1 * h2 ) * ( h1 - h2 ) ) ) ; double a2 = ( ( ( d1 * h2 ) - ( d2 * h1 ) ) / ( ( h1 * h2 ) * ( h1 - h2 ) ) ) ; double x = ( ( - 2 * a0 ) / ( a1 + abs ( sqrt ( a1 * a1 - 4 * a0 * a2 ) ) ) ) ; double y = ( ( - 2 * a0 ) / ( a1 - abs ( sqrt ( a1 * a1 - 4 * a0 * a2 ) ) ) ) ; if ( x >= y ) res = x + c ; else res = y + c ; double m = res * 100 ; double n = c * 100 ; m = floor ( m ) ; n = floor ( n ) ; if ( m == n ) break ; a = b ; b = c ; c = res ; if ( i > MAX_ITERATIONS ) { System . out . println ( " Root ▁ cannot ▁ be ▁ found ▁ using " + " ▁ Muller ' s ▁ method " ) ; break ; } } if ( i <= MAX_ITERATIONS ) System . out . println ( " The ▁ value ▁ of ▁ the ▁ root ▁ is ▁ " + res ) ; } public static void main ( String args [ ] ) { double a = 0 , b = 1 , c = 2 ; Muller ( a , b , c ) ; } }


"Program for N | Java program to find nth term of Arithmetic progression ; using formula to find the Nth term t ( n ) = a ( 1 ) + ( n - 1 ) * d ; Driver code ; starting number ; Common difference ; N th term to be find ; Display the output"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; class GFG { public static int Nth_of_AP ( int a , int d , int N ) { return ( a + ( N - 1 ) * d ) ; } public static void main ( String [ ] args ) { int a = 2 ; int d = 1 ; int N = 5 ; System . out . print ( " The ▁ " + N + " th ▁ term ▁ of ▁ the ▁ series ▁ is ▁ : ▁ " + Nth_of_AP ( a , d , N ) ) ; } }


"Program for Newton Raphson Method | Java program for implementation of Newton Raphson Method for solving equations ; An example function whose solution is determined using Bisection Method . The function is x ^ 3 - x ^ 2 + 2 ; Derivative of the above function which is 3 * x ^ x - 2 * x ; Function to find the root ; x ( i + 1 ) = x ( i ) - f ( x ) / f '(x) ; Driver code ; Initial values assumed"
How can the above be solved in Java?

class GFG { static final double EPSILON = 0.001 ; static double func ( double x ) { return x * x * x - x * x + 2 ; } static double derivFunc ( double x ) { return 3 * x * x - 2 * x ; } static void newtonRaphson ( double x ) { double h = func ( x ) / derivFunc ( x ) ; while ( Math . abs ( h ) >= EPSILON ) { h = func ( x ) / derivFunc ( x ) ; x = x - h ; } System . out . print ( " The ▁ value ▁ of ▁ the " + " ▁ root ▁ is ▁ : ▁ " + Math . round ( x * 100.0 ) / 100.0 ) ; } public static void main ( String [ ] args ) { double x0 = - 20 ; newtonRaphson ( x0 ) ; } }


"Program for Next Fit algorithm in Memory Management | Java program for next fit memory management algorithm ; Function to allocate memory to blocks as per Next fit algorithm ; Stores block id of the block allocated to a process ; Initially no block is assigned to any process ; pick each process and find suitable blocks according to its size ad assign to it ; Do not start from beginning ; allocate block j to p [ i ] process ; Reduce available memory in this block . ; mod m will help in traversing the blocks from starting block after we reach the end . ; Driver program"
How can the above be solved in Java?

import java . util . Arrays ; public class GFG { static void NextFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ ] = new int [ n ] , j = 0 ; Arrays . fill ( allocation , - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; while ( j < m ) { count ++ ; if ( blockSize [ j ] >= processSize [ i ] ) { allocation [ i ] = j ; blockSize [ j ] -= processSize [ i ] ; break ; } j = ( j + 1 ) % m ; } } System . out . print ( " Process No . Process Size Block no . "); for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( i + 1 + " 	 	 " + processSize [ i ] + " 	 	 " ) ; if ( allocation [ i ] != - 1 ) { System . out . print ( allocation [ i ] + 1 ) ; } else { System . out . print ( " Not ▁ Allocated " ) ; } System . out . println ( " " ) ; } } static public void main ( String [ ] args ) { int blockSize [ ] = { 5 , 10 , 20 } ; int processSize [ ] = { 10 , 20 , 5 } ; int m = blockSize . length ; int n = processSize . length ; NextFit ( blockSize , m , processSize , n ) ; } }


"Program for Perrin numbers | Optimized Java program for n 'th perrin number ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int per ( int n ) { int a = 3 , b = 0 , c = 2 , i ; int m = 0 ; if ( n == 0 ) return a ; if ( n == 1 ) return b ; if ( n == 2 ) return c ; while ( n > 2 ) { m = a + b ; a = b ; b = c ; c = m ; n -- ; } return m ; } public static void main ( String [ ] args ) { int n = 9 ; System . out . println ( per ( n ) ) ; } }


"Program for Point of Intersection of Two Lines | Class used to used to store the X and Y coordinates of a point respectively ; Method used to display X and Y coordinates of a point ; Line AB represented as a1x + b1y = c1 ; Line CD represented as a2x + b2y = c2 ; The lines are parallel . This is simplified by returning a pair of FLT_MAX ; Driver method ; NOTE : Further check can be applied in case of line segments . Here , we have considered AB and CD as lines"
How can the above be solved in Java?

class Point { double x , y ; public Point ( double x , double y ) { this . x = x ; this . y = y ; } static void displayPoint ( Point p ) { System . out . println ( " ( " + p . x + " , ▁ " + p . y + " ) " ) ; } } class Test { static Point lineLineIntersection ( Point A , Point B , Point C , Point D ) { double a1 = B . y - A . y ; double b1 = A . x - B . x ; double c1 = a1 * ( A . x ) + b1 * ( A . y ) ; double a2 = D . y - C . y ; double b2 = C . x - D . x ; double c2 = a2 * ( C . x ) + b2 * ( C . y ) ; double determinant = a1 * b2 - a2 * b1 ; if ( determinant == 0 ) { return new Point ( Double . MAX_VALUE , Double . MAX_VALUE ) ; } else { double x = ( b2 * c1 - b1 * c2 ) / determinant ; double y = ( a1 * c2 - a2 * c1 ) / determinant ; return new Point ( x , y ) ; } } public static void main ( String args [ ] ) { Point A = new Point ( 1 , 1 ) ; Point B = new Point ( 4 , 4 ) ; Point C = new Point ( 1 , 8 ) ; Point D = new Point ( 2 , 4 ) ; Point intersection = lineLineIntersection ( A , B , C , D ) ; if ( intersection . x == Double . MAX_VALUE && intersection . y == Double . MAX_VALUE ) { System . out . println ( " The ▁ given ▁ lines ▁ AB ▁ and ▁ CD ▁ are ▁ parallel . " ) ; } else { System . out . print ( " The ▁ intersection ▁ of ▁ the ▁ given ▁ lines ▁ AB ▁ " + " and ▁ CD ▁ is : ▁ " ) ; Point . displayPoint ( intersection ) ; } } }


"Program for Rank of Matrix | Java program to find rank of a matrix ; function for exchanging two rows of a matrix ; function for finding rank of matrix ; Before we visit current row ' row ' , we make sure that mat [ row ] [ 0 ] , ... . mat [ row ] [ row - 1 ] are 0. Diagonal element is not zero ; This makes all entries of current column as 0 except entry ' mat [ row ] [ row ] ' ; Diagonal element is already zero . Two cases arise : 1 ) If there is a row below it with non - zero entry , then swap this row with that row and process that row 2 ) If all elements in current column below mat [ r ] [ row ] are 0 , then remvoe this column by swapping it with last column and reducing number of columns by 1. ; Find the non - zero element in current column ; Swap the row with non - zero element with this row . ; If we did not find any row with non - zero element in current columnm , then all values in this column are 0. ; Reduce number of columns ; Copy the last column here ; Process this row again ; Uncomment these lines to see intermediate results display ( mat , R , C ) ; printf ( " \n " ) ; ; Function to display a matrix ; Driver code"
How can the above be solved in Java?

 class GFG { static final int R = 3 ; static final int C = 3 ; static void swap ( int mat [ ] [ ] , int row1 , int row2 , int col ) { for ( int i = 0 ; i < col ; i ++ ) { int temp = mat [ row1 ] [ i ] ; mat [ row1 ] [ i ] = mat [ row2 ] [ i ] ; mat [ row2 ] [ i ] = temp ; } } static int rankOfMatrix ( int mat [ ] [ ] ) { int rank = C ; for ( int row = 0 ; row < rank ; row ++ ) { if ( mat [ row ] [ row ] != 0 ) { for ( int col = 0 ; col < R ; col ++ ) { if ( col != row ) { double mult = ( double ) mat [ col ] [ row ] / mat [ row ] [ row ] ; for ( int i = 0 ; i < rank ; i ++ ) mat [ col ] [ i ] -= mult * mat [ row ] [ i ] ; } } } else { boolean reduce = true ; for ( int i = row + 1 ; i < R ; i ++ ) { if ( mat [ i ] [ row ] != 0 ) { swap ( mat , row , i , rank ) ; reduce = false ; break ; } } if ( reduce ) { rank -- ; for ( int i = 0 ; i < R ; i ++ ) mat [ i ] [ row ] = mat [ i ] [ rank ] ; } row -- ; } } return rank ; } static void display ( int mat [ ] [ ] , int row , int col ) { for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) System . out . print ( " ▁ " + mat [ i ] [ j ] ) ; System . out . print ( "
"); } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 10 , 20 , 10 } , { - 20 , - 30 , 10 } , { 30 , 50 , 0 } } ; System . out . print ( " Rank ▁ of ▁ the ▁ matrix ▁ is ▁ : ▁ " + rankOfMatrix ( mat ) ) ; } }


"Program for Shortest Job First ( SJF ) scheduling | Set 2 ( Preemptive ) | Java program to implement Shortest Remaining Time First Shortest Remaining Time First ( SRTF ) ; int pid ; Process ID int bt ; Burst Time int art ; Arrival Time ; Method to find the waiting time for all processes ; Copy the burst time into rt [ ] ; Process until all processes gets completed ; Find process with minimum remaining time among the processes that arrives till the current time ` ; Reduce remaining time by one ; Update minimum ; If a process gets completely executed ; Increment complete ; Find finish time of current process ; Calculate waiting time ; Increment time ; Method to calculate turn around time ; calculating turnaround time by adding bt [ i ] + wt [ i ] ; Method to calculate average time ; Function to find waiting time of all processes ; Function to find turn around time for all processes ; Display processes along with all details ; Calculate total waiting time and total turnaround time ; Driver Method"
How can the above be solved in Java?

class Process { public Process ( int pid , int bt , int art ) { this . pid = pid ; this . bt = bt ; this . art = art ; } } public class GFG { static void findWaitingTime ( Process proc [ ] , int n , int wt [ ] ) { int rt [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) rt [ i ] = proc [ i ] . bt ; int complete = 0 , t = 0 , minm = Integer . MAX_VALUE ; int shortest = 0 , finish_time ; boolean check = false ; while ( complete != n ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( proc [ j ] . art <= t ) && ( rt [ j ] < minm ) && rt [ j ] > 0 ) { minm = rt [ j ] ; shortest = j ; check = true ; } } if ( check == false ) { t ++ ; continue ; } rt [ shortest ] -- ; minm = rt [ shortest ] ; if ( minm == 0 ) minm = Integer . MAX_VALUE ; if ( rt [ shortest ] == 0 ) { complete ++ ; check = false ; finish_time = t + 1 ; wt [ shortest ] = finish_time - proc [ shortest ] . bt - proc [ shortest ] . art ; if ( wt [ shortest ] < 0 ) wt [ shortest ] = 0 ; } t ++ ; } } static void findTurnAroundTime ( Process proc [ ] , int n , int wt [ ] , int tat [ ] ) { for ( int i = 0 ; i < n ; i ++ ) tat [ i ] = proc [ i ] . bt + wt [ i ] ; } static void findavgTime ( Process proc [ ] , int n ) { int wt [ ] = new int [ n ] , tat [ ] = new int [ n ] ; int total_wt = 0 , total_tat = 0 ; findWaitingTime ( proc , n , wt ) ; findTurnAroundTime ( proc , n , wt , tat ) ; System . out . println ( " Processes ▁ " + " ▁ Burst ▁ time ▁ " + " ▁ Waiting ▁ time ▁ " + " ▁ Turn ▁ around ▁ time " ) ; for ( int i = 0 ; i < n ; i ++ ) { total_wt = total_wt + wt [ i ] ; total_tat = total_tat + tat [ i ] ; System . out . println ( " ▁ " + proc [ i ] . pid + " 	 	 " + proc [ i ] . bt + " 	 	 ▁ " + wt [ i ] + " 	 	 " + tat [ i ] ) ; } System . out . println ( " Average ▁ waiting ▁ time ▁ = ▁ " + ( float ) total_wt / ( float ) n ) ; System . out . println ( " Average ▁ turn ▁ around ▁ time ▁ = ▁ " + ( float ) total_tat / ( float ) n ) ; } public static void main ( String [ ] args ) { Process proc [ ] = { new Process ( 1 , 6 , 1 ) , new Process ( 2 , 8 , 1 ) ,...


"Program for Surface Area of Octahedron | Java Program to calculate surface area of Octahedron . ; utility Function ; Driver Function"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static double surface_area_octahedron ( double side ) { return ( 2 * ( Math . sqrt ( 3 ) ) * ( side * side ) ) ; } public static void main ( String [ ] args ) { double side = 7 ; System . out . println ( " Surface ▁ area ▁ of ▁ octahedron ▁ = " + surface_area_octahedron ( side ) ) ; } }


"Program for Tower of Hanoi | JAVA recursive function to solve tower of hanoi puzzle ; Driver code ; Number of disks ; A , B and C are names of rods"
How can the above be solved in Java?

import java . util . * ; import java . io . * ; import java . math . * ; class GFG { static void towerOfHanoi ( int n , char from_rod , char to_rod , char aux_rod ) { if ( n == 1 ) { System . out . println ( " Move ▁ disk ▁ 1 ▁ from ▁ rod ▁ " + from_rod + " ▁ to ▁ rod ▁ " + to_rod ) ; return ; } towerOfHanoi ( n - 1 , from_rod , aux_rod , to_rod ) ; System . out . println ( " Move ▁ disk ▁ " + n + " ▁ from ▁ rod ▁ " + from_rod + " ▁ to ▁ rod ▁ " + to_rod ) ; towerOfHanoi ( n - 1 , aux_rod , to_rod , from_rod ) ; } public static void main ( String args [ ] ) { int n = 4 ; towerOfHanoi ( n , ' A ' , ' C ' , ' B ' ) ; } }


"Program for Volume and Surface Area of Cube | Java program to find area and total surface area of cube ; utility function ; Driver code"
How can the above be solved in Java?

class GFG { static double areaCube ( double a ) { return ( a * a * a ) ; } static double surfaceCube ( double a ) { return ( 6 * a * a ) ; } public static void main ( String [ ] args ) { double a = 5 ; System . out . println ( " Area ▁ = ▁ " + areaCube ( a ) ) ; System . out . println ( " Total ▁ surface ▁ area ▁ = ▁ " + surfaceCube ( a ) ) ; } }


"Program for Volume and Surface Area of Cuboid | Java program to find volume and total surface area of cuboid ; utility function ; Driver code"
How can the above be solved in Java?

class GFG { static double areaCuboid ( double l , double h , double w ) { return ( l * h * w ) ; } static double surfaceAreaCuboid ( double l , double h , double w ) { return ( 2 * l * w + 2 * w * h + 2 * l * h ) ; } public static void main ( String [ ] args ) { double l = 1 ; double h = 5 ; double w = 7 ; System . out . println ( " Area ▁ = ▁ " + areaCuboid ( l , h , w ) ) ; System . out . println ( " Total ▁ Surface ▁ Area ▁ = ▁ " + surfaceAreaCuboid ( l , h , w ) ) ; } }


"Program for Volume and Surface area of Frustum of Cone | Java program to calculate Volume and Surface area of frustum of cone ; Function to calculate Volume of frustum of cone ; Function to calculate Curved Surface area of frustum of cone ; Function to calculate Total Surface area of frustum of cone ; Driver function ; Printing value of volume and surface area"
How can the above be solved in Java?

public class demo { static float pi = 3.14159f ; public static float volume ( float r , float R , float h ) { return ( float ) 1 / 3 * pi * h * ( r * r + R * R + r * R ) ; } public static float curved_surface_area ( float r , float R , float l ) { return pi * l * ( R + r ) ; } public static float total_surface_area ( float r , float R , float l , float h ) { return pi * l * ( R + r ) + pi * ( r * r + R * R ) ; } public static void main ( String args [ ] ) { float small_radius = 3 ; float big_radius = 8 ; float slant_height = 13 ; float height = 12 ; System . out . print ( " Volume ▁ Of ▁ Frustum ▁ of ▁ Cone ▁ : ▁ " ) ; System . out . println ( volume ( small_radius , big_radius , height ) ) ; System . out . print ( " Curved ▁ Surface ▁ Area ▁ Of " + " ▁ Frustum ▁ of ▁ Cone ▁ : ▁ " ) ; System . out . println ( curved_surface_area ( small_radius , big_radius , slant_height ) ) ; System . out . print ( " Total ▁ Surface ▁ Area ▁ Of " + " ▁ Frustum ▁ of ▁ Cone ▁ : ▁ " ) ; System . out . println ( total_surface_area ( small_radius , big_radius , slant_height , height ) ) ; } }


"Program for Worst Fit algorithm in Memory Management | Java implementation of worst - Fit algorithm ; Method to allocate memory to blocks as per worst fit algorithm ; Stores block id of the block allocated to a process ; Initially no block is assigned to any process ; pick each process and find suitable blocks according to its size ad assign to it ; Find the best fit block for current process ; If we could find a block for current process ; allocate block j to p [ i ] process ; Reduce available memory in this block . ; Driver Method"
How can the above be solved in Java?

public class GFG { static void worstFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ ] = new int [ n ] ; for ( int i = 0 ; i < allocation . length ; i ++ ) allocation [ i ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int wstIdx = - 1 ; for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { if ( wstIdx == - 1 ) wstIdx = j ; else if ( blockSize [ wstIdx ] < blockSize [ j ] ) wstIdx = j ; } } if ( wstIdx != - 1 ) { allocation [ i ] = wstIdx ; blockSize [ wstIdx ] -= processSize [ i ] ; } } System . out . println ( " Process No . Process Size Block no . "); for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( " ▁ " + ( i + 1 ) + " 	 	 " + processSize [ i ] + " 	 	 " ) ; if ( allocation [ i ] != - 1 ) System . out . print ( allocation [ i ] + 1 ) ; else System . out . print ( " Not ▁ Allocated " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int blockSize [ ] = { 100 , 500 , 200 , 300 , 600 } ; int processSize [ ] = { 212 , 417 , 112 , 426 } ; int m = blockSize . length ; int n = processSize . length ; worstFit ( blockSize , m , processSize , n ) ; } }


"Program for average of an array without running into overflow | Java program for the above approach ; Function to calculate average of an array using standard method ; Stores the sum of array ; Find the sum of the array ; Return the average ; Function to calculate average of an array using efficient method ; Store the average of the array ; Traverse the array arr [ ] ; Update avg ; Return avg ; Driver Code ; Input ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static Double average ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; return Double . valueOf ( sum / N ) ; } static Double mean ( int arr [ ] , int N ) { Double avg = 0.0 ; for ( int i = 0 ; i < N ; i ++ ) { avg += Double . valueOf ( ( arr [ i ] - avg ) / ( i + 1 ) ) ; } return avg ; } public static void main ( String args [ ] ) { int arr [ ] = { Integer . MAX_VALUE , 1 , 2 } ; int N = arr . length ; System . out . println ( " Average ▁ by ▁ Standard ▁ method : ▁ " + String . format ( " % .10f " , average ( arr , N ) ) ) ; System . out . println ( " Average ▁ by ▁ Efficient ▁ method : ▁ " + String . format ( " % .10f " , mean ( arr , N ) ) ) ; } }


"Program for centered nonagonal number | Java Program to find nth centered nonagonal number . ; Function to find nth centered nonagonal number . ; Formula to find nth centered nonagonal number . ; Driver function ."
How can the above be solved in Java?

import java . io . * ; class GFG { static int centeredNonagonal ( int n ) { return ( 3 * n - 2 ) * ( 3 * n - 1 ) / 2 ; } public static void main ( String args [ ] ) { int n = 10 ; System . out . println ( centeredNonagonal ( n ) ) ; } }


"Program for credit card number validation | Java program to check if a given credit card is valid or not . ; Main Method ; Return true if the card number is valid ; Get the result from Step 2 ; Return this number if it is a single digit , otherwise , return the sum of the two digits ; Return sum of odd - place digits in number ; Return true if the digit d is a prefix for number ; Return the number of digits in d ; Return the first k number of digits from number . If the number of digits in number is less than k , return number ."
How can the above be solved in Java?

import java . util . Scanner ; public class CreditCard { public static void main ( String [ ] args ) { long number = 5196081888500645L ; System . out . println ( number + " ▁ is ▁ " + ( isValid ( number ) ? " valid " : " invalid " ) ) ; } public static boolean isValid ( long number ) { return ( getSize ( number ) >= 13 && getSize ( number ) <= 16 ) && ( prefixMatched ( number , 4 ) || prefixMatched ( number , 5 ) || prefixMatched ( number , 37 ) || prefixMatched ( number , 6 ) ) && ( ( sumOfDoubleEvenPlace ( number ) + sumOfOddPlace ( number ) ) % 10 == 0 ) ; } public static int sumOfDoubleEvenPlace ( long number ) { int sum = 0 ; String num = number + " " ; for ( int i = getSize ( number ) - 2 ; i >= 0 ; i -= 2 ) sum += getDigit ( Integer . parseInt ( num . charAt ( i ) + " " ) * 2 ) ; return sum ; } public static int getDigit ( int number ) { if ( number < 9 ) return number ; return number / 10 + number % 10 ; } public static int sumOfOddPlace ( long number ) { int sum = 0 ; String num = number + " " ; for ( int i = getSize ( number ) - 1 ; i >= 0 ; i -= 2 ) sum += Integer . parseInt ( num . charAt ( i ) + " " ) ; return sum ; } public static boolean prefixMatched ( long number , int d ) { return getPrefix ( number , getSize ( d ) ) == d ; } public static int getSize ( long d ) { String num = d + " " ; return num . length ( ) ; } public static long getPrefix ( long number , int k ) { if ( getSize ( number ) > k ) { String num = number + " " ; return Long . parseLong ( num . substring ( 0 , k ) ) ; } return number ; } }


"Program for cube sum of first n natural numbers | Efficient Java program to find sum of cubes of first n natural numbers that avoids overflow if result is going to be withing limits . ; Returns the sum of series ; Driver Function"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; class GFG { public static int sumOfSeries ( int n ) { int x ; if ( n % 2 == 0 ) x = ( n / 2 ) * ( n + 1 ) ; else x = ( ( n + 1 ) / 2 ) * n ; return x * x ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( sumOfSeries ( n ) ) ; } }


"Program for decimal to hexadecimal conversion | Java program to convert a decimal number to hexadecimal number ; function to convert decimal to hexadecimal ; char array to store hexadecimal number ; counter for hexadecimal number array ; temporary variable to store remainder ; storing remainder in temp variable . ; check if temp < 10 ; printing hexadecimal number array in reverse order ; driver program"
How can the above be solved in Java?

import java . io . * ; class GFG { static void decToHexa ( int n ) { char [ ] hexaDeciNum = new char [ 100 ] ; int i = 0 ; while ( n != 0 ) { int temp = 0 ; temp = n % 16 ; if ( temp < 10 ) { hexaDeciNum [ i ] = ( char ) ( temp + 48 ) ; i ++ ; } else { hexaDeciNum [ i ] = ( char ) ( temp + 55 ) ; i ++ ; } n = n / 16 ; } for ( int j = i - 1 ; j >= 0 ; j -- ) System . out . print ( hexaDeciNum [ j ] ) ; } public static void main ( String [ ] args ) { int n = 2545 ; decToHexa ( n ) ; } }


"Program for dot product and cross product of two vectors | java implementation for dot product and cross product of two vector . ; Function that return dot product of two vector array . ; Loop for calculate cot product ; Function to find cross product of two vector array . ; Driver code ; dotProduct function call ; crossProduct function call ; Loop that print cross product of two vector array ."
How can the above be solved in Java?

import java . io . * ; class GFG { static int n = 3 ; static int dotProduct ( int vect_A [ ] , int vect_B [ ] ) { int product = 0 ; for ( int i = 0 ; i < n ; i ++ ) product = product + vect_A [ i ] * vect_B [ i ] ; return product ; } static void crossProduct ( int vect_A [ ] , int vect_B [ ] , int cross_P [ ] ) { cross_P [ 0 ] = vect_A [ 1 ] * vect_B [ 2 ] - vect_A [ 2 ] * vect_B [ 1 ] ; cross_P [ 1 ] = vect_A [ 2 ] * vect_B [ 0 ] - vect_A [ 0 ] * vect_B [ 2 ] ; cross_P [ 2 ] = vect_A [ 0 ] * vect_B [ 1 ] - vect_A [ 1 ] * vect_B [ 0 ] ; } public static void main ( String [ ] args ) { int vect_A [ ] = { 3 , - 5 , 4 } ; int vect_B [ ] = { 2 , 6 , 5 } ; int cross_P [ ] = new int [ n ] ; System . out . print ( " Dot ▁ product : " ) ; System . out . println ( dotProduct ( vect_A , vect_B ) ) ; System . out . print ( " Cross ▁ product : " ) ; crossProduct ( vect_A , vect_B , cross_P ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( cross_P [ i ] + " ▁ " ) ; } }


"Program for finding the Integral of a given function using Boole 's Rule | Java program to implement Boole 's Rule on the given function ; Function to return the value of f ( x ) for the given value of x ; Function to computes the integrand of y at the given intervals of x with step size h and the initial limit a and final limit b ; Number of intervals ; Computing the step size ; Substituing a = 0 , b = 4 and h = 1 ; Driver code"
How can the above be solved in Java?

class GFG { static float y ( float x ) { return ( 1 / ( 1 + x ) ) ; } static float BooleRule ( float a , float b ) { int n = 4 ; int h ; h = ( int ) ( ( b - a ) / n ) ; float sum = 0 ; float bl = ( 7 * y ( a ) + 32 * y ( a + h ) + 12 * y ( a + 2 * h ) + 32 * y ( a + 3 * h ) + 7 * y ( a + 4 * h ) ) * 2 * h / 45 ; sum = sum + bl ; return sum ; } public static void main ( String [ ] args ) { System . out . printf ( ( " f ( x ) ▁ = ▁ % .4f " ) , BooleRule ( 0 , 4 ) ) ; } }


"Program for focal length of a lens | Java program to determine the focal length of a lens ; Function to determine the focal length of a lens ; Driver code ; variable to store the distance between the lens and the image ; variable to store the distance between the lens and the object"
How can the above be solved in Java?

import java . io . * ; class GFG { static float focal_length ( float image_distance , float object_distance ) { return 1 / ( ( 1 / image_distance ) + ( 1 / object_distance ) ) ; } public static void main ( String [ ] args ) { float image_distance = 2 ; float object_distance = 50 ; System . out . println ( " Focal ▁ length ▁ of ▁ a ▁ lens ▁ is ▁ " + focal_length ( image_distance , object_distance ) + " ▁ units . " ) ; } }


"Program for harmonic mean of numbers | JAVA program to find harmonic mean . ; Function that returns harmonic mean . ; Driver code"
How can the above be solved in Java?

class GFG { static float harmonicMean ( int arr [ ] , int freq [ ] , int n ) { float sum = 0 , frequency_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ( float ) freq [ i ] / arr [ i ] ; frequency_sum = frequency_sum + freq [ i ] ; } return ( frequency_sum / sum ) ; } public static void main ( String args [ ] ) { int num [ ] = { 13 , 14 , 15 , 16 , 17 } ; int freq [ ] = { 2 , 5 , 13 , 7 , 3 } ; int n = num . length ; System . out . println ( harmonicMean ( num , freq , n ) ) ; } }


"Program for harmonic mean of numbers | JAVA program to find harmonic mean of numbers . ; Function that returns harmonic mean . ; Declare sum variables and initialize with zero ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static float harmonicMean ( float arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( float ) 1 / arr [ i ] ; return ( float ) n / sum ; } public static void main ( String args [ ] ) { float arr [ ] = { 13.5f , 14.5f , 14.8f , 15.2f , 16.1f } ; int n = arr . length ; System . out . println ( harmonicMean ( arr , n ) ) ; } }


"Program for length of a string using recursion | java program to calculate length of a string using recursion ; Function to calculate length ; if we reach at the end of the string ; Driver program to test above function"
How can the above be solved in Java?

import java . util . * ; public class GFG { private static int recLen ( String str ) { if ( str . equals ( " " ) ) return 0 ; else return recLen ( str . substring ( 1 ) ) + 1 ; } public static void main ( String [ ] args ) { String str = " GeeksforGeeks " ; System . out . println ( recLen ( str ) ) ; } }


"Program for length of the longest word in a sentence | Java program to find the number of charters in the longest word in the sentence . ; Driver code"
How can the above be solved in Java?

class GFG { static int LongestWordLength ( String str ) { String [ ] words = str . split ( " ▁ " ) ; int length = 0 ; for ( String word : words ) { if ( length < word . length ( ) ) { length = word . length ( ) ; } } return length ; } public static void main ( String args [ ] ) { String str = " I ▁ am ▁ an ▁ intern ▁ at ▁ geeksforgeeks " ; System . out . println ( LongestWordLength ( str ) ) ; } }


"Program for n 'th node from the end of a Linked List |"
How can the above be solved in Java?

static void printNthFromLast ( Node head , int n ) { static int i = 0 ; if ( head == null ) return ; printNthFromLast ( head . next , n ) ; if ( ++ i == n ) System . out . print ( head . data ) ; }


"Program for n | JAVA program to find the nth EVEN number ; Function to find the nth odd number ; Driver code"
How can the above be solved in Java?

class GFG { static int nthEven ( int n ) { return ( 2 * n ) ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( nthEven ( n ) ) ; } }


"Program for n | JAVA program to find the nth odd number ; Function to find the nth odd number ; Driver code"
How can the above be solved in Java?

class GFG { static int nthOdd ( int n ) { return ( 2 * n - 1 ) ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( nthOdd ( n ) ) ; } }


"Program for nth Catalan Number |  ; A recursive function to find nth catalan number ; Base case ; catalan ( n ) is sum of catalan ( i ) * catalan ( n - i - 1 ) ; Driver Code"
How can the above be solved in Java?

class CatalnNumber { int catalan ( int n ) { if ( n <= 1 ) { return 1 ; } int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { res += catalan ( i ) * catalan ( n - i - 1 ) ; } return res ; } public static void main ( String [ ] args ) { CatalnNumber cn = new CatalnNumber ( ) ; for ( int i = 0 ; i < 10 ; i ++ ) { System . out . print ( cn . catalan ( i ) + " ▁ " ) ; } } }


"Program for nth Catalan Number |  ; Function to print the number ; For the first number C ( 0 ) ; Iterate till N ; Calculate the number and print it ; Driver code ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void catalan ( int n ) { int cat_ = 1 ; System . out . print ( cat_ + " ▁ " ) ; for ( int i = 1 ; i < n ; i ++ ) { cat_ *= ( 4 * i - 2 ) ; cat_ /= ( i + 1 ) ; System . out . print ( cat_ + " ▁ " ) ; } } public static void main ( String args [ ] ) { int n = 5 ; catalan ( n ) ; } }


"Program for nth Catalan Number | A dynamic programming based function to find nth Catalan number ; Table to store results of subproblems ; Initialize first two values in table ; Fill entries in catalan [ ] using recursive formula ; Return last entry ; Driver code"
How can the above be solved in Java?

class GFG { static int catalanDP ( int n ) { int catalan [ ] = new int [ n + 2 ] ; catalan [ 0 ] = 1 ; catalan [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { catalan [ i ] = 0 ; for ( int j = 0 ; j < i ; j ++ ) { catalan [ i ] += catalan [ j ] * catalan [ i - j - 1 ] ; } } return catalan [ n ] ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 10 ; i ++ ) { System . out . print ( catalanDP ( i ) + " ▁ " ) ; } } }


"Program for nth Catalan Number | Java program for nth Catalan Number ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver code"
How can the above be solved in Java?

class GFG { static long binomialCoeff ( int n , int k ) { long res = 1 ; if ( k > n - k ) { k = n - k ; } for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static long catalan ( int n ) { long c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 10 ; i ++ ) { System . out . print ( catalan ( i ) + " ▁ " ) ; } } }


"Program for nth Fuss – Catalan Number | Java program for nth Fuss - Catalan Number ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth Fuss - Catalan number in O ( n ) time ; Calculate value of 3 nCn ; return 3 nCn / ( 2 n + 1 ) ; Driver code"
How can the above be solved in Java?

class GFG { static int binomialCoeff ( int n , int k ) { int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static int Fuss_catalan ( int n ) { int c = binomialCoeff ( 3 * n , n ) ; return c / ( 2 * n + 1 ) ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 10 ; i ++ ) System . out . print ( Fuss_catalan ( i ) + " ▁ " ) ; } }


"Program for quotient and remainder of big number | Java program to find quotient and remainder when a number is divided by large number represented as String . ; Function to calculate the modulus ; Store the modulus of big number ; Do step by step division ; Update modulo by concatenating current digit . ; Update quotient ; Update mod for next iteration . ; Flag used to remove starting zeros ; Driver Code"
How can the above be solved in Java?

import java . util . Vector ; class GFG { static void modBigNumber ( String num , long m ) { Vector < Integer > vec = new Vector < > ( ) ; long mod = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) { int digit = num . charAt ( i ) - '0' ; mod = mod * 10 + digit ; int quo = ( int ) ( mod / m ) ; vec . add ( vec . size ( ) , quo ) ; mod = mod % m ; } System . out . print ( " Remainder : " ▁ + ▁ mod ▁ + ▁ " "); System . out . print ( " Quotient ▁ : ▁ " ) ; boolean zeroflag = false ; for ( int i = 0 ; i < vec . size ( ) ; i ++ ) { if ( vec . get ( i ) == 0 && zeroflag == false ) { continue ; } zeroflag = true ; System . out . print ( vec . get ( i ) ) ; } return ; } public static void main ( String [ ] args ) { String num = "14598499948265358486" ; long m = 487 ; modBigNumber ( num , m ) ; } }


"Program for sum of arithmetic series | JAVA Program to find the sum of arithmetic series . ; Function to find sum of series . ; Driver function"
How can the above be solved in Java?

class GFG { static float sumOfAP ( float a , float d , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + a ; a = a + d ; } return sum ; } public static void main ( String args [ ] ) { int n = 20 ; float a = 2.5f , d = 1.5f ; System . out . println ( sumOfAP ( a , d , n ) ) ; } }


"Program for sum of arithmetic series | Java Efficient solution to find sum of arithmetic series . ; Driver code"
How can the above be solved in Java?

class GFG { static float sumOfAP ( float a , float d , float n ) { float sum = ( n / 2 ) * ( 2 * a + ( n - 1 ) * d ) ; return sum ; } public static void main ( String [ ] args ) { float n = 20 ; float a = 2.5f , d = 1.5f ; System . out . print ( sumOfAP ( a , d , n ) ) ; } }


"Program for sum of cos ( x ) series | Java program to find the sum of cos ( x ) series ; here x is in degree . we have to convert it to radian for using it with series formula , as in series expansion angle is in radian ; Driver Code"
How can the above be solved in Java?

import java . lang . Math . * ; class GFG { static final double PI = 3.142 ; static double cosXSertiesSum ( double x , int n ) { x = x * ( PI / 180.0 ) ; double res = 1 ; double sign = 1 , fact = 1 , pow = 1 ; for ( int i = 1 ; i < 5 ; i ++ ) { sign = sign * - 1 ; fact = fact * ( 2 * i - 1 ) * ( 2 * i ) ; pow = pow * x * x ; res = res + sign * pow / fact ; } return res ; } public static void main ( String [ ] args ) { float x = 50 ; int n = 5 ; System . out . println ( ( float ) ( cosXSertiesSum ( x , 5 ) * 1000000 ) / 1000000.00 ) ; } }


"Program for sum of cosh ( x ) series upto Nth term | Java program for the sum of cosh ( x ) series ; function to return the factorial of a number ; function to return the sum of the series ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int fact ( int n ) { int i = 1 , fac = 1 ; for ( i = 1 ; i <= n ; i ++ ) fac = fac * i ; return fac ; } static double log_Expansion ( double x , int n ) { double sum = 0 ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum = sum + Math . pow ( x , 2 * i ) / fact ( 2 * i ) ; } return sum ; } public static void main ( String [ ] args ) { double x = 1 ; int n = 10 ; System . out . println ( log_Expansion ( x , n ) ) ; } }


"Program for weighted mean of natural numbers . | JAVA Program to find weighted mean of natural numbers . ; Function to calculate weighted mean . ; Driver program to test the function . ; Take num array and corresponding weight array and initialize it . ; Calculate the size of array . ; Check the size of both array is equal or not ."
How can the above be solved in Java?

class GFG { static float weightedMean ( int X [ ] , int W [ ] , int n ) { int sum = 0 , numWeight = 0 ; for ( int i = 0 ; i < n ; i ++ ) { numWeight = numWeight + X [ i ] * W [ i ] ; sum = sum + W [ i ] ; } return ( float ) ( numWeight ) / sum ; } public static void main ( String args [ ] ) { int X [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int W [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int n = X . length ; int m = W . length ; if ( n == m ) System . out . println ( weightedMean ( X , W , n ) ) ; else System . out . println ( " - 1" ) ; } }


"Program for weighted mean of natural numbers . | Program to find weighted mean of first n natural numbers using formula . ; Returns weighted mean assuming for numbers { 1 , 2 , . . n } and weights { 1 , 2 , . . n } ; Driver program to test the function ."
How can the above be solved in Java?

import java . io . * ; public class GFG { static int weightedMean ( int n ) { return ( 2 * n + 1 ) / 3 ; } static public void main ( String [ ] args ) { int n = 10 ; System . out . println ( weightedMean ( n ) ) ; } }


"Program to Calculate e ^ x by Recursion ( using Taylor Series ) | Java implementation of the approach ; Recursive Function with static variables p and f ; Termination condition ; Recursive call ; Update the power of x ; Factorial ; Driver code"
How can the above be solved in Java?

import java . text . * ; class GFG { static double p = 1 , f = 1 ; static double e ( int x , int n ) { double r ; if ( n == 0 ) return 1 ; r = e ( x , n - 1 ) ; p = p * x ; f = f * n ; return ( r + p / f ) ; } public static void main ( String [ ] args ) { int x = 4 , n = 15 ; DecimalFormat df = new DecimalFormat ( "0 . # # # # # # " ) ; System . out . println ( df . format ( e ( x , n ) ) ) ; } }


"Program to Calculate the Perimeter of a Decagon | package whatever do not write package name here Java program to Calculate the Perimeter of a Decagon ; Function for finding the perimeter ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void CalPeri ( ) { int S = 5 , Perimeter ; Perimeter = 10 * S ; System . out . println ( " The ▁ Perimeter ▁ of ▁ Decagon ▁ is ▁ : ▁ " + Perimeter ) ; } public static void main ( String [ ] args ) { CalPeri ( ) ; } }


"Program to Change RGB color model to HSV color model | Java program change RGB Color Model to HSV Color Model ; R , G , B values are divided by 255 to change the range from 0. .255 to 0. .1 ; h , s , v = hue , saturation , value double cmax = Math . max ( r , Math . max ( g , b ) ) ; maximum of r , g , b double cmin = Math . min ( r , Math . min ( g , b ) ) ; minimum of r , g , b double diff = cmax - cmin ; diff of cmax and cmin . ; if cmax and cmax are equal then h = 0 ; if cmax equal r then compute h ; if cmax equal g then compute h ; if cmax equal b then compute h ; if cmax equal zero ; compute v ; Driver Code ; rgb_to_hsv ( 45 , 215 , 0 ) ; rgb_to_hsv ( 31 , 52 , 29 ) ;"
How can the above be solved in Java?

class GFG { static void rgb_to_hsv ( double r , double g , double b ) { r = r / 255.0 ; g = g / 255.0 ; b = b / 255.0 ; double h = - 1 , s = - 1 ; if ( cmax == cmin ) h = 0 ; else if ( cmax == r ) h = ( 60 * ( ( g - b ) / diff ) + 360 ) % 360 ; else if ( cmax == g ) h = ( 60 * ( ( b - r ) / diff ) + 120 ) % 360 ; else if ( cmax == b ) h = ( 60 * ( ( r - g ) / diff ) + 240 ) % 360 ; if ( cmax == 0 ) s = 0 ; else s = ( diff / cmax ) * 100 ; double v = cmax * 100 ; System . out . println ( " ( " + h + " ▁ " + s + " ▁ " + v + " ) " ) ; } public static void main ( String [ ] args ) { rgb_to_hsv ( 129 , 88 , 47 ) ; } }


"Program to Convert BCD number into Decimal number | Java code to convert BCD to its decimal number ( base 10 ) . Including Header Files ; Function to convert BCD to Decimal ; Iterating through the bits backwards ; Forming the equivalent digit ( 0 to 9 ) from the group of 4. ; Reinitialize all variables and compute the number . ; Update the answer ; Reverse the number formed . ; Driver code ; Function Call"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { public static int bcdToDecimal ( String s ) { int len = s . length ( ) ; int check = 0 , check0 = 0 ; int num = 0 , sum = 0 ; int mul = 1 , rev = 0 ; for ( int i = len - 1 ; i >= 0 ; i -- ) { sum += ( s . charAt ( i ) - '0' ) * mul ; mul *= 2 ; check ++ ; if ( check == 4 i == 0 ) { if ( sum == 0 && check0 == 0 ) { num = 1 ; check0 = 1 ; } else { num = num * 10 + sum ; } check = 0 ; sum = 0 ; mul = 1 ; } } while ( num > 0 ) { rev = rev * 10 + ( num % 10 ) ; num /= 10 ; } if ( check0 == 1 ) return rev - 1 ; return rev ; } public static void main ( String [ ] args ) { String s = "100000101000" ; System . out . println ( bcdToDecimal ( s ) ) ; } }


"Program to Convert Hexadecimal Number to Binary | Java program to convert Hexadecimal number to Binary ; function to convert Hexadecimal to Binary Number ; Driver code ; Get the Hexadecimal number ; Convert HexaDecimal to Binary"
How can the above be solved in Java?

public class Improve { static void HexToBin ( char hexdec [ ] ) { int i = 0 ; while ( hexdec [ i ] != ' \u0000' ) { switch ( hexdec [ i ] ) { case '0' : System . out . print ( "0000" ) ; break ; case '1' : System . out . print ( "0001" ) ; break ; case '2' : System . out . print ( "0010" ) ; break ; case '3' : System . out . print ( "0011" ) ; break ; case '4' : System . out . print ( "0100" ) ; break ; case '5' : System . out . print ( "0101" ) ; break ; case '6' : System . out . print ( "0110" ) ; break ; case '7' : System . out . print ( "0111" ) ; break ; case '8' : System . out . print ( "1000" ) ; break ; case '9' : System . out . print ( "1001" ) ; break ; case ' A ' : case ' a ' : System . out . print ( "1010" ) ; break ; case ' B ' : case ' b ' : System . out . print ( "1011" ) ; break ; case ' C ' : case ' c ' : System . out . print ( "1100" ) ; break ; case ' D ' : case ' d ' : System . out . print ( "1101" ) ; break ; case ' E ' : case ' e ' : System . out . print ( "1110" ) ; break ; case ' F ' : case ' f ' : System . out . print ( "1111" ) ; break ; default : System . out . print ( " Invalid hexadecimal digit " + hexdec[i]); } i ++ ; } } public static void main ( String args [ ] ) { String s = "1AC5" ; char hexdec [ ] = new char [ 100 ] ; hexdec = s . toCharArray ( ) ; System . out . print ( " Equivalent Binary value is : "); try { HexToBin ( hexdec ) ; } catch ( ArrayIndexOutOfBoundsException e ) { System . out . print ( " " ) ; } } }


"Program to Convert Km / hr to miles / hr and vice versa | Java program for the conversion kmph to mph and vice versa ; Function to convert kmph to mph ; Function to convert mph to kmph ; Driver code to check the above function"
How can the above be solved in Java?

import java . io . * ; class GFG { static double kmphTOmph ( double kmph ) { return 0.6214 * kmph ; } static double mphTOkmph ( double mph ) { return mph * 1.60934 ; } public static void main ( String [ ] args ) { double kmph = 150 ; double mph = 100 ; System . out . println ( " speed ▁ in ▁ miles / hr ▁ is ▁ " + kmphTOmph ( kmph ) ) ; System . out . println ( " speed ▁ in ▁ km / hr ▁ is ▁ " + mphTOkmph ( mph ) ) ; } }


"Program to Encrypt a String using ! and @ | Java program to Encrypt the String using ! and @ ; Function to encrypt the string ; evenPos is for storing encrypting char at evenPosition oddPos is for storing encrypting char at oddPosition ; Get the number of times the character is to be repeated ; if i is odd , print ' ! ' else print ' @ ' ; Driver code ; Encrypt the String"
How can the above be solved in Java?

class GFG { static void encrypt ( char input [ ] ) { char evenPos = ' @ ' , oddPos = ' ! ' ; int repeat , ascii ; for ( int i = 0 ; i < input . length ; i ++ ) { ascii = input [ i ] ; repeat = ascii >= 97 ? ascii - 96 : ascii - 64 ; for ( int j = 0 ; j < repeat ; j ++ ) { if ( i % 2 == 0 ) System . out . printf ( " % c " , oddPos ) ; else System . out . printf ( " % c " , evenPos ) ; } } } public static void main ( String [ ] args ) { char input [ ] = { ' A ' , ' b ' , ' C ' , ' d ' } ; encrypt ( input ) ; } }


"Program to Find the value of cos ( nÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬¦½ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬¹ ÃƒÆ ’ Ã ¢ â ‚¬¦ Ãƒ ¢ Ã ¢ â € š ¬ Ã … â € œ ) | Java program to find the value of cos ( n - theta ) ; Function to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of cos ( n - theta ) ; find sinTheta from cosTheta ; to store required answer ; use to toggle sign in sequence . ; Driver code"
How can the above be solved in Java?

class GFG { static int MAX = 16 ; static int [ ] [ ] nCr = new int [ MAX ] [ MAX ] ; static void binomial ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i ] [ j ] = 1 ; else nCr [ i ] [ j ] = nCr [ i - 1 ] [ j ] + nCr [ i - 1 ] [ j - 1 ] ; } } } static double findCosnTheta ( double cosTheta , int n ) { double sinTheta = Math . sqrt ( 1 - cosTheta * cosTheta ) ; double ans = 0 ; int toggle = 1 ; for ( int i = 0 ; i <= n ; i += 2 ) { ans = ans + nCr [ n ] [ i ] * Math . pow ( cosTheta , n - i ) * Math . pow ( sinTheta , i ) * toggle ; toggle = toggle * - 1 ; } return ans ; } public static void main ( String [ ] args ) { binomial ( ) ; double cosTheta = 0.5 ; int n = 10 ; System . out . println ( String . format ( " % .5f " , findCosnTheta ( cosTheta , n ) ) ) ; } }


"Program to Interchange Diagonals of Matrix | Java program to interchange the diagonals of matrix ; Function to interchange diagonals ; swap elements of diagonal ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { public static int N = 3 ; static void interchangeDiagonals ( int array [ ] [ ] ) { for ( int i = 0 ; i < N ; ++ i ) if ( i != N / 2 ) { int temp = array [ i ] [ i ] ; array [ i ] [ i ] = array [ i ] [ N - i - 1 ] ; array [ i ] [ N - i - 1 ] = temp ; } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) System . out . print ( array [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int array [ ] [ ] = { { 4 , 5 , 6 } , { 1 , 2 , 3 } , { 7 , 8 , 9 } } ; interchangeDiagonals ( array ) ; } }


"Program to Print Matrix in Z form | Java program to print a square matrix in Z form ; Driver code"
How can the above be solved in Java?

import java . lang . * ; import java . io . * ; class GFG { public static void diag ( int arr [ ] [ ] , int n ) { int i = 0 , j , k ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( i == 0 ) { System . out . print ( arr [ i ] [ j ] + " ▁ " ) ; } else if ( i == j ) { System . out . print ( arr [ i ] [ j ] + " ▁ " ) ; } else if ( i == n - 1 ) { System . out . print ( arr [ i ] [ j ] + " ▁ " ) ; } } } } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 4 , 5 , 6 , 8 } , { 1 , 2 , 3 , 1 } , { 7 , 8 , 9 , 4 } , { 1 , 8 , 7 , 5 } } ; diag ( a , 4 ) ; } }


"Program to accept String starting with Capital letter | Java program to accept String starting with Capital letter ; Function to check if first character is Capital ; Function to check ; Driver function"
How can the above be solved in Java?

class GFG { static int checkIfStartsWithCapital ( String str ) { if ( str . charAt ( 0 ) >= ' A ' && str . charAt ( 0 ) <= ' Z ' ) return 1 ; else return 0 ; } static void check ( String str ) { if ( checkIfStartsWithCapital ( str ) == 1 ) System . out . println ( " Accepted " ) ; else System . out . println ( " Not ▁ Accepted " ) ; } public static void main ( String [ ] args ) { String str = " GeeksforGeeks " ; check ( str ) ; str = " geeksforgeeks " ; check ( str ) ; } }


"Program to accept Strings starting with a Vowel | Java program to accept String starting with Vowel ; Function to check if first character is vowel ; Function to check ; Driver code"
How can the above be solved in Java?

 class GFG { static int checkIfStartsWithVowels ( char [ ] str ) { if ( ! ( str [ 0 ] == ' A ' str [ 0 ] == ' a ' str [ 0 ] == ' E ' str [ 0 ] == ' e ' str [ 0 ] == ' I ' str [ 0 ] == ' i ' str [ 0 ] == ' O ' str [ 0 ] == ' o ' str [ 0 ] == ' U ' str [ 0 ] == ' u ' ) ) return 1 ; else return 0 ; } static void check ( String str ) { if ( checkIfStartsWithVowels ( str . toCharArray ( ) ) == 1 ) System . out . print ( "Not Accepted
"); else System . out . print ( "Accepted
"); } public static void main ( String [ ] args ) { String str = " animal " ; check ( str ) ; str = " zebra " ; check ( str ) ; } }


"Program to accept a Strings which contains all the Vowels | Java implementation to check that a String contains all vowels ; Function to to check that a String contains all vowels ; Hash Array of size 5 such that the index 0 , 1 , 2 , 3 and 4 represent the vowels a , e , i , o and u ; Loop the String to mark the vowels which are present ; Loop to check if there is any vowel which is not present in the String ; Function to to check that a String contains all vowels ; Driver Code"
How can the above be solved in Java?

 class GFG { static int checkIfAllVowels ( String str ) { int [ ] hash = new int [ 5 ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' A ' || str . charAt ( i ) == ' a ' ) hash [ 0 ] = 1 ; else if ( str . charAt ( i ) == ' E ' || str . charAt ( i ) == ' e ' ) hash [ 1 ] = 1 ; else if ( str . charAt ( i ) == ' I ' || str . charAt ( i ) == ' i ' ) hash [ 2 ] = 1 ; else if ( str . charAt ( i ) == ' O ' || str . charAt ( i ) == ' o ' ) hash [ 3 ] = 1 ; else if ( str . charAt ( i ) == ' U ' || str . charAt ( i ) == ' u ' ) hash [ 4 ] = 1 ; } for ( int i = 0 ; i < 5 ; i ++ ) { if ( hash [ i ] == 0 ) { return 1 ; } } return 0 ; } static void checkIfAllVowelsArePresent ( String str ) { if ( checkIfAllVowels ( str ) == 1 ) System . out . print ( "Not Accepted
"); else System . out . print ( "Accepted
"); } public static void main ( String [ ] args ) { String str = " aeioubc " ; checkIfAllVowelsArePresent ( str ) ; } }


"Program to add two fractions | Java program to add 2 fractions ; Function to return gcd of a and b ; Function to convert the obtained fraction into it 's simplest form ; Finding gcd of both terms ; Converting both terms into simpler terms by dividing them by common factor ; Function to add two fractions ; Finding gcd of den1 and den2 ; Denominator of final fraction obtained finding LCM of den1 and den2 LCM * GCD = a * b ; Changing the fractions to have same denominator Numerator of the final fraction obtained ; Calling function to convert final fraction into it 's simplest form ; Driver program"
How can the above be solved in Java?

class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void lowest ( int den3 , int num3 ) { int common_factor = gcd ( num3 , den3 ) ; den3 = den3 / common_factor ; num3 = num3 / common_factor ; System . out . println ( num3 + " / " + den3 ) ; } static void addFraction ( int num1 , int den1 , int num2 , int den2 ) { int den3 = gcd ( den1 , den2 ) ; den3 = ( den1 * den2 ) / den3 ; int num3 = ( num1 ) * ( den3 / den1 ) + ( num2 ) * ( den3 / den2 ) ; lowest ( den3 , num3 ) ; } public static void main ( String [ ] args ) { int num1 = 1 , den1 = 500 , num2 = 2 , den2 = 1500 ; System . out . print ( num1 + " / " + den1 + " ▁ + ▁ " + num2 + " / " + den2 + " ▁ is ▁ equal ▁ to ▁ " ) ; addFraction ( num1 , den1 , num2 , den2 ) ; } }


"Program to add two integers of given base | Java implementation to find the sum of two integers of base B ; Function to find the sum of two integers of base B ; Padding 0 in front of the number to make both numbers equal ; Condition to check if the strings have lengths mis - match ; Loop to find the find the sum of two integers of base B ; Current Place value for the resultant sum ; Update carry ; Find current digit ; Update sum result ; Driver Code ; Function Call"
How can the above be solved in Java?

class GFG { static String sumBaseB ( String a , String b , int base ) { int len_a , len_b ; len_a = a . length ( ) ; len_b = b . length ( ) ; String sum , s ; s = " " ; sum = " " ; int diff ; diff = Math . abs ( len_a - len_b ) ; for ( int i = 1 ; i <= diff ; i ++ ) s += "0" ; if ( len_a < len_b ) a = s + a ; else b = s + b ; int curr , carry = 0 ; for ( int i = Math . max ( len_a , len_b ) - 1 ; i > - 1 ; i -- ) { curr = carry + ( a . charAt ( i ) - '0' ) + ( b . charAt ( i ) - '0' ) ; carry = curr / base ; curr = curr % base ; sum = ( char ) ( curr + '0' ) + sum ; } if ( carry > 0 ) sum = ( char ) ( carry + '0' ) + sum ; return sum ; } public static void main ( String [ ] args ) { String a , b , sum ; int base ; a = "123" ; b = "234" ; base = 6 ; sum = sumBaseB ( a , b , base ) ; System . out . println ( sum ) ; } }


"Program to balance the given Chemical Equation | Java program to balance the given Chemical Equation ; Function to calculate GCD ; Function to calculate b1 , b2 and b3 ; Variable declaration ; temp variable to store gcd ; Computing GCD ; Driver code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void balance ( int x , int y , int p , int q ) { int b1 , b2 , b3 ; if ( p % x == 0 && q % y == 0 ) { b1 = p / x ; b2 = q / y ; b3 = 1 ; } else { p = p * y ; q = q * x ; b3 = x * y ; int temp = gcd ( p , gcd ( q , b3 ) ) ; b1 = p / temp ; b2 = q / temp ; b3 = b3 / temp ; } System . out . print ( b1 + " ▁ " + b2 + " ▁ " + b3 + "
"); } public static void main ( String [ ] args ) { int x = 2 , y = 3 , p = 4 , q = 5 ; balance ( x , y , p , q ) ; } }


"Program to build a DFA that checks if a string ends with "01" or "10" | Java Program to DFA that accepts string ending with 01 or 10. ; End position is checked using the string length value . q0 is the starting state . q1 and q2 are intermediate states . q3 and q4 are final states . ; state transitions 0 takes to q1 , 1 takes to q3 ; state transitions 0 takes to q4 , 1 takes to q2 ; state transitions 0 takes to q4 , 1 takes to q2 ; state transitions 0 takes to q1 , 1 takes to q3 ; state transitions 0 takes to q1 , 1 takes to q2 ; Driver Code ; all state transitions are printed . if string is accpetable , YES is printed . else NO is printed"
How can the above be solved in Java?

class GFG { static void q1 ( String s , int i ) { System . out . print ( " q1 - > " ) ; if ( i == s . length ( ) ) { System . out . println ( " NO " ) ; return ; } if ( s . charAt ( i ) == '0' ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } static void q2 ( String s , int i ) { System . out . print ( " q2 - > " ) ; if ( i == s . length ( ) ) { System . out . println ( " NO ▁ " ) ; return ; } if ( s . charAt ( i ) == '0' ) q4 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } static void q3 ( String s , int i ) { System . out . print ( " q3 - > " ) ; if ( i == s . length ( ) ) { System . out . println ( " YES " ) ; return ; } if ( s . charAt ( i ) == '0' ) q4 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } static void q4 ( String s , int i ) { System . out . print ( " q4 - > " ) ; if ( i == s . length ( ) ) { System . out . println ( " YES " ) ; return ; } if ( s . charAt ( i ) == '0' ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } static void q0 ( String s , int i ) { System . out . print ( " q0 - > " ) ; if ( i == s . length ( ) ) { System . out . println ( " NO " ) ; return ; } if ( s . charAt ( i ) == '0' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } public static void main ( String [ ] args ) { String s = "010101" ; System . out . print ( " State ▁ transitions ▁ are ▁ " ) ; q0 ( s , 0 ) ; } }


"Program to build a DFA to accept strings that start and end with same character | Java Program for DFA that accepts string if it starts and ends with same character ; Function for the state Q1 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q2 ; Function for the state Q2 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q2 ; Function for the state Q3 ; Condition to check end of string ; State transitions ' a ' takes to q4 , and ' b ' takes to q3 ; Function for the state Q4 ; Condition to check end of string ; State transitions ' a ' takes to q4 , and ' b ' takes to q3 ; Function for the state Q0 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q3 ; Driver Code ; Since q0 is the starting state Send the string to q0"
How can the above be solved in Java?

class GFG { static void q1 ( String s , int i ) { if ( i == s . length ( ) ) { System . out . println ( " Yes " ) ; return ; } if ( s . charAt ( i ) == ' a ' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } static void q2 ( String s , int i ) { if ( i == s . length ( ) ) { System . out . println ( " No " ) ; return ; } if ( s . charAt ( i ) == ' a ' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } static void q3 ( String s , int i ) { if ( i == s . length ( ) ) { System . out . println ( " Yes " ) ; return ; } if ( s . charAt ( i ) == ' a ' ) q4 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } static void q4 ( String s , int i ) { if ( i == s . length ( ) ) { System . out . println ( " No " ) ; return ; } if ( s . charAt ( i ) == ' a ' ) q4 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } static void q0 ( String s , int i ) { if ( i == s . length ( ) ) { System . out . println ( " No " ) ; return ; } if ( s . charAt ( i ) == ' a ' ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } public static void main ( String [ ] args ) { String s = " abbaabb " ; q0 ( s , 0 ) ; } }


"Program to calculate Area Of Octagon | Java Program to find area of Octagon . ; utility function ; driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static double areaOctagon ( double side ) { return ( float ) ( 2 * ( 1 + Math . sqrt ( 2 ) ) * side * side ) ; } public static void main ( String arg [ ] ) { double side = 4 ; System . out . print ( " Area ▁ of ▁ Regular ▁ Octagon ▁ = ▁ " + areaOctagon ( side ) ) ; } }


"Program to calculate Electricity Bill | Java implementation to calculate the electricity bill ; Function to calculate the electricity bill ; Condition to find the charges bar in which the units consumed is fall ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class ComputeElectricityBill { public static int calculateBill ( int units ) { if ( units <= 100 ) { return units * 10 ; } else if ( units <= 200 ) { return ( 100 * 10 ) + ( units - 100 ) * 15 ; } else if ( units <= 300 ) { return ( 100 * 10 ) + ( 100 * 15 ) + ( units - 200 ) * 20 ; } else if ( units > 300 ) { return ( 100 * 10 ) + ( 100 * 15 ) + ( 100 * 20 ) + ( units - 300 ) * 25 ; } return 0 ; } public static void main ( String args [ ] ) { int units = 250 ; System . out . println ( calculateBill ( units ) ) ; } }


"Program to calculate GST from original and net prices | Java Program to compute GST from original and net prices . ; return value after calculate GST % ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static float Calculate_GST ( float org_cost , float N_price ) { return ( ( ( N_price - org_cost ) * 100 ) / org_cost ) ; } public static void main ( String [ ] args ) { float org_cost = 100 ; float N_price = 120 ; System . out . print ( " ▁ GST ▁ = ▁ " + Calculate_GST ( org_cost , N_price ) + " % " ) ; } }


"Program to calculate Kinetic Energy and Potential Energy | Java program to implement the above approach ; Function to calculate Kinetic Energy ; Stores the Kinetic Energy ; Function to calculate Potential Energy ; Stores the Potential Energy ; Driver Code"
How can the above be solved in Java?

class GFG { static double kineticEnergy ( double M , double V ) { double KineticEnergy ; KineticEnergy = 0.5 * M * V * V ; return KineticEnergy ; } static double potentialEnergy ( double M , double H ) { double PotentialEnergy ; PotentialEnergy = M * 9.8 * H ; return PotentialEnergy ; } public static void main ( String [ ] args ) { double M = 5.5 , H = 23.5 , V = 10.5 ; System . out . println ( " Kinetic ▁ Energy ▁ = ▁ " + kineticEnergy ( M , V ) ) ; System . out . println ( " Potential ▁ Energy ▁ = ▁ " + potentialEnergy ( M , H ) ) ; } }


"Program to calculate Percentile of a student based on rank | Java program to calculate Percentile of a student based on rank ; Program to calculate the percentile ; flat variable to store the result ; calculate and return the percentile ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static float getPercentile ( int rank , int students ) { float result = ( float ) ( students - rank ) / students * 100 ; return result ; } public static void main ( String [ ] args ) { int your_rank = 805 ; int total_students = 97481 ; System . out . print ( getPercentile ( your_rank , total_students ) ) ; } }


"Program to calculate Profit Or Loss | Java code to demonstrate Profit and Loss ; Function to calculate Profit . ; Function to calculate Loss . ; Driver Code ."
How can the above be solved in Java?

class GFG { static int Profit ( int costPrice , int sellingPrice ) { int profit = ( sellingPrice - costPrice ) ; return profit ; } static int Loss ( int costPrice , int sellingPrice ) { int Loss = ( costPrice - sellingPrice ) ; return Loss ; } public static void main ( String [ ] args ) { int costPrice = 1500 , sellingPrice = 2000 ; if ( sellingPrice == costPrice ) System . out . println ( " No ▁ profit ▁ nor ▁ Loss " ) ; else if ( sellingPrice > costPrice ) System . out . println ( Profit ( costPrice , sellingPrice ) + " ▁ Profit ▁ " ) ; else System . out . println ( Loss ( costPrice , sellingPrice ) + " ▁ Loss ▁ " ) ; } }


"Program to calculate Root Mean Square | Java program to calculate Root Mean Square ; Function that Calculate Root Mean Square ; Calculate square . ; Calculate Mean . ; Calculate Root . ; Driver Code"
How can the above be solved in Java?

class GFG { static float rmsValue ( int arr [ ] , int n ) { int square = 0 ; float mean = 0 ; float root = 0 ; for ( int i = 0 ; i < n ; i ++ ) { square += Math . pow ( arr [ i ] , 2 ) ; } mean = ( square / ( float ) ( n ) ) ; root = ( float ) Math . sqrt ( mean ) ; return root ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 4 , 6 , 8 } ; int n = arr . length ; System . out . println ( rmsValue ( arr , n ) ) ; } }


"Program to calculate Surface Area of Ellipsoid | Java program of the above approach ; Function to find the surface area of the given Ellipsoid ; Formula to find surface area of an Ellipsoid ; Print the area ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void findArea ( double a , double b , double c ) { double area = 4 * 3.141592653 * Math . pow ( ( Math . pow ( a * b , 1.6 ) + Math . pow ( a * c , 1.6 ) + Math . pow ( b * c , 1.6 ) ) / 3 , 1 / 1.6 ) ; System . out . print ( String . format ( " % .2f " , area ) ) ; } public static void main ( String [ ] args ) { double A = 11 , B = 12 , C = 13 ; findArea ( A , B , C ) ; } }


"Program to calculate Variance of first N Natural Numbers | Java program to implement the above approach ; Function to calculate Variance of first N natural numbers ; Driver Code"
How can the above be solved in Java?

class GFG { static double find_Variance ( int n ) { long numerator = n * n - 1 ; double ans = ( numerator * 1.0 ) / 12 ; return ans ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( find_Variance ( N ) ) ; } }


"Program to calculate angle between two N | Java program for the above approach ; Function to find the magnitude of the given vector ; Stores the final magnitude ; Traverse the array ; Return square root of magnitude ; Function to find the dot product of two vectors ; Stores dot product ; Traverse the array ; Return the product ; Stores dot product of two vectors ; Stores magnitude of vector A ; Stores magnitude of vector B ; Stores angle between given vectors ; Print the angle ; Driver Code ; Given magnitude arrays ; Size of the array ; Function call to find the angle between two vectors"
How can the above be solved in Java?

class GFG { static double magnitude ( double arr [ ] , int N ) { double magnitude = 0 ; for ( int i = 0 ; i < N ; i ++ ) magnitude += arr [ i ] * arr [ i ] ; return Math . sqrt ( magnitude ) ; } static double dotProduct ( double [ ] arr , double [ ] brr , int N ) { double product = 0 ; for ( int i = 0 ; i < N ; i ++ ) product = product + arr [ i ] * brr [ i ] ; return product ; } static void angleBetweenVectors ( double [ ] arr , double [ ] brr , int N ) { double dotProductOfVectors = dotProduct ( arr , brr , N ) ; double magnitudeOfA = magnitude ( arr , N ) ; double magnitudeOfB = magnitude ( brr , N ) ; double angle = dotProductOfVectors / ( magnitudeOfA * magnitudeOfB ) ; System . out . println ( angle ) ; } public static void main ( String [ ] args ) { double [ ] arr = { - 0.5 , - 2 , 1 } ; double [ ] brr = { - 1 , - 1 , 0.3 } ; int N = arr . length ; angleBetweenVectors ( arr , brr , N ) ; } }


"Program to calculate angle on circumference subtended by the chord when the central angle subtended by the chord is given | Java Program to calculate angle on the circumference subtended by the chord when the central angle subtended by the chord is given ; Driver code ; Angle on center"
How can the above be solved in Java?

class GFG { static float angleOncirCumference ( float z ) { return ( z / 2 ) ; } public static void main ( String [ ] args ) { float angle = 65 ; float z = angleOncirCumference ( angle ) ; System . out . println ( " The ▁ angle ▁ is ▁ " + z + " ▁ degrees " ) ; } }


"Program to calculate area and perimeter of a rhombus whose diagonals are given | Java program to calculate area and perimeter of a rhombus using diagonals ; calculate area and perimeter of a rhombus ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int rhombusAreaPeri ( int d1 , int d2 ) { int area , perimeter ; area = ( d1 * d2 ) / 2 ; perimeter = ( int ) ( 2 * Math . sqrt ( Math . pow ( d1 , 2 ) + Math . pow ( d2 , 2 ) ) ) ; System . out . println ( " The ▁ area ▁ of ▁ rhombus ▁ with ▁ diagonals ▁ " + d1 + " ▁ and ▁ " + d2 + " ▁ is ▁ " + area + " . " ) ; System . out . println ( " The ▁ perimeter ▁ of ▁ rhombus ▁ with ▁ diagonals ▁ " + d1 + " ▁ and ▁ " + d2 + " ▁ is ▁ " + perimeter + " . " ) ; return 0 ; } public static void main ( String [ ] args ) { int d1 = 2 , d2 = 4 ; rhombusAreaPeri ( d1 , d2 ) ; } }


"Program to calculate area and perimeter of equilateral triangle | Java Program to find area and perimeter of equilateral triangle ; Function to calculate Area of equilateral triangle ; Function to calculate Perimeter of equilateral triangle ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static float area_equi_triangle ( float side ) { return ( float ) ( ( ( Math . sqrt ( 3 ) ) / 4 ) * side * side ) ; } static float peri_equi_triangle ( float side ) { return 3 * side ; } public static void main ( String arg [ ] ) { float side = 4 ; System . out . print ( " Area ▁ of ▁ Equilateral ▁ Triangle : " ) ; System . out . println ( area_equi_triangle ( side ) ) ; System . out . print ( " Perimeter ▁ of ▁ Equilateral ▁ Triangle : " ) ; System . out . println ( peri_equi_triangle ( side ) ) ; } }


"Program to calculate area and volume of a Tetrahedron | Java Program to Calculate area of tetrahedron ; Utility Function ; Driver code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; class GFG { public static double area_of_tetrahedron ( int side ) { return ( Math . sqrt ( 3 ) * ( side * side ) ) ; } public static void main ( String [ ] args ) { int side = 3 ; System . out . println ( " Area ▁ of ▁ Tetrahedron ▁ = " + area_of_tetrahedron ( side ) ) ; } }


"Program to calculate area and volume of a Tetrahedron | Java code to find the volume of a tetrahedron ; Function to calculate volume ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class Tetrahedron { static double vol_tetra ( int side ) { double volume = ( Math . pow ( side , 3 ) / ( 6 * Math . sqrt ( 2 ) ) ) ; return volume ; } public static void main ( String [ ] args ) { int side = 3 ; double vol = vol_tetra ( side ) ; vol = ( double ) Math . round ( vol * 100 ) / 100 ; System . out . println ( vol ) ; } }


"Program to calculate area of a parallelogram | Java program for above approach ; Function to return the area of parallelogram using sides and angle at the intersection of diagonal ; Calculate area of parallelogram ; Return the answer ; Function to return the area of parallelogram using sides and angle at the intersection of sides ; Calculate area of parallelogram ; Return the answer ; Function to return the area of parallelogram using diagonals and angle at the intersection of diagonals ; Calculate area of parallelogram ; Return the answer ; Driver code ; Given diagonal and angle ; Function call ; Print the area"
How can the above be solved in Java?

import java . io . * ; class GFG { static double Area_Parallelogram1 ( int a , int b , int theta ) { double area = ( Math . abs ( Math . tan ( Math . toRadians ( theta ) ) ) / 2 ) * Math . abs ( a * a - b * b ) ; return area ; } static double Area_Parallelogram2 ( int a , int b , int gamma ) { double area = ( Math . abs ( Math . sin ( Math . toRadians ( gamma ) ) ) ) * Math . abs ( a * b ) ; return area ; } static double Area_Parallelogram3 ( int d1 , int d2 , int theta ) { double area = ( Math . abs ( Math . sin ( Math . toRadians ( theta ) ) ) / 2 ) * Math . abs ( d1 * d2 ) ; return area ; } public static void main ( String [ ] args ) { int d1 = 3 ; int d2 = 5 ; int theta = 90 ; double area = Area_Parallelogram3 ( d1 , d2 , theta ) ; System . out . format ( " % .2f " , area ) ; } }


"Program to calculate area of a rhombus whose one side and diagonal are given | Java program to calculate the area of a rhombus whose one side and one diagonal is given ; function to calculate the area of the rhombus ; Second diagonal ; area of rhombus ; return the area ; Driver code"
How can the above be solved in Java?

class GFG { static double area ( double d1 , double a ) { double d2 = Math . sqrt ( 4 * ( a * a ) - d1 * d1 ) ; double area = 0.5 * d1 * d2 ; return area ; } public static void main ( String [ ] args ) { double d = 7.07 ; double a = 5 ; System . out . println ( area ( d , a ) ) ; } }