codeparrot/xlcost-text-to-code huggingface dataset:
Check if a large number is divisible by 13 or not | Java program to check whether a number is divisible by 13 or not . ; Returns true if number is divisible by 13 else returns false ; Append required 0 s . at the beginning . ; Same as strcat ( num , "00" ) ; in c . ; Same as strcat ( num , "0" ) ; in c . ; Alternatively add / subtract digits in group of three to result . ; Store group of three numbers in group variable . ; Generate alternate series of plus and minus ; Driver code
class GFG { static boolean checkDivisibility ( String num ) { int length = num . length ( ) ; if ( length == 1 && num . charAt ( 0 ) == '0' ) return true ; if ( length % 3 == 1 ) { num += "00" ; length += 2 ; } else if ( length % 3 == 2 ) { num += "0" ; length += 1 ; } int sum = 0 , p = 1 ; for ( int i = length - 1 ; i >= 0 ; i -- ) { int group = 0 ; group += num . charAt ( i -- ) - '0' ; group += ( num . charAt ( i -- ) - '0' ) * 10 ; group += ( num . charAt ( i ) - '0' ) * 100 ; sum = sum + group * p ; p *= ( - 1 ) ; } sum = Math . abs ( sum ) ; return ( sum % 13 == 0 ) ; } public static void main ( String [ ] args ) { String number = "83959092724" ; if ( checkDivisibility ( number ) ) System . out . println ( number + " ▁ is ▁ divisible ▁ by ▁ 13 . " ) ; else System . out . println ( number + " ▁ is ▁ not ▁ divisible ▁ by ▁ 13 . " ) ; } }
Given two numbers a and b find all x such that a % x = b | Java program to find x such that a % x is equal to b . ; if a is less than b then no solution ; if a is equal to b then every number greater than a will be the solution so its infinity ; count variable store the number of values possible ; checking for both divisor and quotient whether they divide ( a - b ) completely and greater than b . ; Here y is added twice in the last iteration so 1 y should be decremented to get correct solution ; Driver code
import java . io . * ; class GFG { static void modularEquation ( int a , int b ) { if ( a < b ) { System . out . println ( " No ▁ solution ▁ possible ▁ " ) ; return ; } if ( a == b ) { System . out . println ( " Infinite ▁ Solution ▁ possible ▁ " ) ; return ; } int count = 0 ; int n = a - b ; int y = ( int ) Math . sqrt ( a - b ) ; for ( int i = 1 ; i <= y ; ++ i ) { if ( n % i == 0 ) { if ( n / i > b ) count ++ ; if ( i > b ) count ++ ; } } if ( y * y == n && y > b ) count -- ; System . out . println ( count ) ; } public static void main ( String [ ] args ) { int a = 21 , b = 5 ; modularEquation ( a , b ) ; } }
Count different numbers that can be generated such that there digits sum is equal to ' n ' | Java program to count ways to write ' n ' as sum of digits ; Function to count ' num ' as sum of digits ( 1 , 2 , 3 , 4 ) ; Initialize dp [ ] array ; Base case ; Initialize the current dp [ ] array as '0' ; if i == j then there is only one way to write with element itself ' i ' ; If j == 1 , then there exist two ways , one from '1' and other from '4' ; if i - j is positive then pick the element from ' i - j ' element of dp [ ] array ; Check for modulas ; return the final answer ; Driver code
import java . io . * ; public class GFG { static int countWays ( int num ) { int [ ] dp = new int [ num + 1 ] ; int MOD = ( int ) 1E9 + 7 ; dp [ 1 ] = 2 ; for ( int i = 2 ; i <= num ; ++ i ) { dp [ i ] = 0 ; for ( int j = 1 ; j <= 3 ; ++ j ) { if ( i - j == 0 ) dp [ i ] += 1 ; else if ( j == 1 ) dp [ i ] += dp [ i - j ] * 2 ; else if ( i - j > 0 ) dp [ i ] += dp [ i - j ] ; if ( dp [ i ] >= MOD ) dp [ i ] %= MOD ; } } return dp [ num ] ; } static public void main ( String [ ] args ) { int n = 3 ; System . out . println ( countWays ( n ) ) ; } }
Check whether a number can be represented by sum of two squares | An efficient approach based implementation to find if a number can be written as sum of two squares . ; function to check if there exist two numbers sum of whose squares is n . ; store square value in hashmap ; Driver Code
import java . util . * ; class GFG { static boolean sumSquare ( int n ) { HashMap < Integer , Integer > s = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i * i <= n ; ++ i ) { s . put ( i * i , 1 ) ; if ( s . containsKey ( n - i * i ) ) { System . out . println ( ( int ) Math . sqrt ( n - i * i ) + " ^ 2 ▁ + ▁ " + i + " ^ 2" ) ; return true ; } } return false ; } public static void main ( String [ ] args ) { int n = 169 ; System . out . print ( sumSquare ( n ) ? "YES " ▁ : ▁ " NO "); } }
Check whether a number can be represented by sum of two squares | Java program to Check whether a number can be represented by sum of two squares using Fermat Theorem . ; Count all the prime factors . ; Ifany prime factor of the form ( 4 k + 3 ) ( 4 k + 3 ) occurs an odd number of times . ; If n itself is a prime number and can be expressed in the form of 4 k + 3 we return false . ; Driver Code
import java . util . * ; import java . lang . * ; class GFG { public static boolean judgeSquareSum ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { int count = 0 ; if ( n % i == 0 ) { while ( n % i == 0 ) { count ++ ; n /= i ; } if ( i % 4 == 3 && count % 2 != 0 ) return false ; } } return n % 4 != 3 ; } public static void main ( String argc [ ] ) { int n = 17 ; if ( judgeSquareSum ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Total no of 1 's in numbers | / Java code to count the frequency of 1 in numbers less than or equal to the given number . ; function to count the frequency of 1. ; Driver Code
import java . io . * ; class GFG { static int countDigitOne ( int n ) { int countr = 0 ; for ( int i = 1 ; i <= n ; i *= 10 ) { int divider = i * 10 ; countr += ( n / divider ) * i + Math . min ( Math . max ( n % divider - i + 1 , 0 ) , i ) ; } return countr ; } public static void main ( String [ ] args ) { int n = 13 ; System . out . println ( countDigitOne ( n ) ) ; n = 113 ; System . out . println ( countDigitOne ( n ) ) ; n = 205 ; System . out . println ( countDigitOne ( n ) ) ; } }
Largest number with prime digits | Java program to find largest number smaller than equal to n with all prime digits . ; check if character is prime ; replace with previous prime character ; if 2 erase s [ i ] and replace next with 7 ; find first non prime char ; find first char greater than 2 ; like 20 ; like 7721 ; replace remaining with 7 ; Driver code
class GFG { public static boolean isPrime ( char c ) { return ( c == '2' c == '3' c == '5' c == '7' ) ; } public static void decrease ( StringBuilder s , int i ) { if ( s . charAt ( i ) <= '2' ) { s . deleteCharAt ( i ) ; s . setCharAt ( i , '7' ) ; } else if ( s . charAt ( i ) == '3' ) s . setCharAt ( i , '2' ) ; else if ( s . charAt ( i ) <= '5' ) s . setCharAt ( i , '3' ) ; else if ( s . charAt ( i ) <= '7' ) s . setCharAt ( i , '5' ) ; else s . setCharAt ( i , '7' ) ; return ; } public static String primeDigits ( StringBuilder s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ! isPrime ( s . charAt ( i ) ) ) { while ( i >= 0 && s . charAt ( i ) <= '2' ) i -- ; if ( i < 0 ) { i = 0 ; decrease ( s , i ) ; } else decrease ( s , i ) ; for ( int j = i + 1 ; j < s . length ( ) ; j ++ ) s . setCharAt ( j , '7' ) ; break ; } } return s . toString ( ) ; } public static void main ( String [ ] args ) { StringBuilder s = new StringBuilder ( "45" ) ; System . out . println ( primeDigits ( s ) ) ; s = new StringBuilder ( "1000" ) ; System . out . println ( primeDigits ( s ) ) ; s = new StringBuilder ( "7721" ) ; System . out . println ( primeDigits ( s ) ) ; s = new StringBuilder ( "7221" ) ; System . out . println ( primeDigits ( s ) ) ; s = new StringBuilder ( "74545678912345689748593275897894708927680" ) ; System . out . println ( primeDigits ( s ) ) ; } }
Divisors of n | Java program to count number of divisors of n ^ 2 which are not divisible by divisor of n ; Function to count divisors of n ^ 2 having no factors of ' n ' ; Increment count of i - th prime divisor ; Find next prime divisor ; Increment count if divisor still remains ; Initialize variable for counting the factors of n ^ 2 and n as ans1 and ans2 respectively ; Range based for - loop ; Use formula as discussed in above ; return the difference of answers ; Driver code
import java . util . * ; class GFG { static int factors ( int n ) { HashMap < Integer , Integer > prime = new HashMap < Integer , Integer > ( ) ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; ++ i ) { while ( n % i == 0 ) { if ( prime . containsKey ( i ) ) { prime . put ( i , prime . get ( i ) + 1 ) ; } else { prime . put ( i , 1 ) ; } n = n / i ; } } if ( n > 2 ) { if ( prime . containsKey ( n ) ) { prime . put ( n , prime . get ( n ) + 1 ) ; } else { prime . put ( n , 1 ) ; } } int ans1 = 1 , ans2 = 1 ; for ( Map . Entry < Integer , Integer > it : prime . entrySet ( ) ) { ans1 *= 2 * it . getValue ( ) + 1 ; ans2 *= it . getValue ( ) + 1 ; } return ans1 - ans2 ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( factors ( n ) ) ; n = 8 ; System . out . println ( factors ( n ) ) ; } }
Print digit 's position to be removed to make a number divisible by 6 | Java program to print digit 's position to be removed to make number divisible by 6 ; function to print the number divisible by 6 after exactly removing a digit ; stores the sum of all elements ; traverses the string and converts string to number array and sums up ; if ( a [ n - 1 ] % 2 != 0 ) ODD CHECK ; if second last is odd or sum of n - 1 elements are not divisible by 3. ; second last is even and print n - 1 elements removing last digit ; last digit removed ; counter to check if any element after removing , its sum % 3 == 0 ; traverse till second last element ; to check if any element after removing , its sum % 3 == 0 ; the leftmost element ; break at the leftmost element ; stores the right most element ; if no element has been found as a [ i + 1 ] > a [ i ] ; if second last is even , then remove last if ( sum - last ) % 3 == 0 ; if no element which on removing gives sum % 3 == 0 ; driver program to test the above function
import java . util . * ; class solution { static void greatest ( String s ) { int n = s . length ( ) ; int [ ] a = new int [ n ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = s . charAt ( i ) - '0' ; sum += a [ i ] ; } { if ( a [ n - 2 ] % 2 != 0 || ( sum - a [ n - 1 ] ) % 3 != 0 ) { System . out . println ( " - 1" ) ; } else { System . out . println ( n ) ; } } else { int re = sum % 3 ; int del = - 1 ; int flag = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( ( a [ i ] ) % 3 == re ) { if ( a [ i + 1 ] > a [ i ] ) { del = i ; flag = 1 ; break ; } else { del = i ; } } } if ( flag == 0 ) { if ( a [ n - 2 ] % 2 == 0 && re == a [ n - 1 ] % 3 ) del = n - 1 ; } if ( del == - 1 ) System . out . println ( - 1 ) ; else { System . out . println ( del + 1 ) ; } } } public static void main ( String args [ ] ) { String s = "7510222" ; greatest ( s ) ; } }
Representation of a number in powers of other | Java program to check if m can be represented as powers of w . ; break ; None of 3 worked . ; If m is not zero means , it can 't be  represented in terms of powers of w. ; Driver function
class GFG { static boolean asPowerSum ( int w , int m ) { while ( m > 0 ) { if ( ( m - 1 ) % w == 0 ) m = ( m - 1 ) / w ; else if ( ( m + 1 ) % w == 0 ) m = ( m + 1 ) / w ; else if ( m % w == 0 ) m = m / w ; else } return ( m == 0 ) ; } public static void main ( String [ ] args ) { int w = 3 , m = 7 ; if ( asPowerSum ( w , m ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Number of digits to be removed to make a number divisible by 3 | Java program to find the minimum number of digits to be removed to make a large number divisible by 3. ; function to count the no of removal of digits to make a very large number divisible by 3 ; add up all the digits of num ; if num is already is divisible by 3 then no digits are to be removed ; if there is single digit , then it is not possible to remove one digit . ; traverse through the number and find out if any number on removal makes the sum divisible by 3 ; if there are two numbers then it is not possible to remove two digits . ; Otherwise we can always make a number multiple of 2 by removing 2 digits . ; Driver Code
import java . io . * ; class GFG { static int divisible ( String num ) { int n = num . length ( ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( int ) ( num . charAt ( i ) ) ; if ( sum % 3 == 0 ) return 0 ; if ( n == 1 ) return - 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( sum % 3 == ( num . charAt ( i ) - '0' ) % 3 ) return 1 ; if ( n == 2 ) return - 1 ; return 2 ; } public static void main ( String [ ] args ) { String num = "1234" ; System . out . println ( divisible ( num ) ) ; } }
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 .
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 ] + " ▁ " ) ; } }
Number of n digit numbers that do not contain 9 | Java program to find number of n digit numbers that do not contain 9 as it 's digit ; function to find number of n digit numbers possible ; Driver Code
import java . io . * ; public class GFG { static int totalNumber ( int n ) { return 8 * ( int ) Math . pow ( 9 , n - 1 ) ; } static public void main ( String [ ] args ) { int n = 3 ; System . out . println ( totalNumber ( n ) ) ; } }
Count ways to express even number â €˜ nâ €™ as sum of even integers | JAVA program to count ways to write number as sum of even integers ; Initialize mod variable as constant ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Return number of ways to write ' n ' as sum of even integers ; Driver code
class GFG { static int MOD = 1000000007 ; static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( 1 * res * x ) % p ; x = ( 1 * x * x ) % p ; } return res ; } static int countEvenWays ( int n ) { return power ( 2 , n / 2 - 1 , MOD ) ; } public static void main ( String args [ ] ) { int n = 6 ; System . out . println ( countEvenWays ( n ) ) ; n = 8 ; System . out . println ( countEvenWays ( n ) ) ; } }
Number of steps to convert to prime factors | Java program to count number of steps required to convert an integer array to array of factors . ; array to store prime factors ; function to generate all prime factors of numbers from 1 to 10 ^ 6 ; Initializes all the positions with their value . ; Initializes all multiples of 2 with 2 ; A modified version of Sieve of Eratosthenes to store the smallest prime factor that divides every number . ; check if it has no prime factor . ; Initializes of j starting from i * i ; if it has no prime factor before , then stores the smallest prime divisor ; function to calculate the number of representations ; keep an count of prime factors ; traverse for every element ; count the no of factors ; subtract 1 if Ai is not 1 as the last step wont be taken into count ; Driver code ; call sieve to calculate the factors
class GFG { static final int MAX = 1000001 ; static int factor [ ] = new int [ MAX ] ; static void cal_factor ( ) { factor [ 1 ] = 1 ; for ( int i = 2 ; i < MAX ; i ++ ) factor [ i ] = i ; for ( int i = 4 ; i < MAX ; i += 2 ) factor [ i ] = 2 ; for ( int i = 3 ; i * i < MAX ; i ++ ) { if ( factor [ i ] == i ) { for ( int j = i * i ; j < MAX ; j += i ) { if ( factor [ j ] == j ) factor [ j ] = i ; } } } } static int no_of_representations ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int temp = a [ i ] ; int flag = 0 ; while ( factor [ temp ] != 1 ) { flag = - 1 ; count ++ ; temp = temp / factor [ temp ] ; } count += flag ; } return count ; } public static void main ( String [ ] args ) { cal_factor ( ) ; int a [ ] = { 4 , 4 , 4 } ; int n = a . length ; System . out . print ( no_of_representations ( a , n ) ) ; } }
Subsequences of size three in an array whose sum is divisible by m | Java program to find count of subsequences of size three divisible by M . ; Three nested loop to find all the sub sequences of length three in the given array A [ ] . ; checking if the sum of the chosen three number is divisible by m . ; Driver code
import java . io . * ; class GFG { static int coutSubSeq ( int A [ ] , int N , int M ) { int sum = 0 ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { for ( int k = j + 1 ; k < N ; k ++ ) { sum = A [ i ] + A [ j ] + A [ k ] ; if ( sum % M == 0 ) ans ++ ; } } } return ans ; } public static void main ( String args [ ] ) { int M = 3 ; int A [ ] = { 1 , 2 , 4 , 3 } ; int N = A . length ; System . out . println ( coutSubSeq ( A , N , M ) ) ; } }
Subsequences of size three in an array whose sum is divisible by m | Java program to find count of subsequences of size three divisible by M . ; Storing frequencies of all remainders when divided by M . ; including i and j in the sum rem calculate the remainder required to make the sum divisible by M ; if the required number is less than j , we skip as we have already calculated for that value before . As j here starts with i and rem is less than j . ; if satisfies the first case . ; if satisfies the second case ; if satisfies the third case ; Driver code
import java . io . * ; import java . util . Arrays ; class GFG { static int countSubSeq ( int A [ ] , int N , int M ) { int ans = 0 ; int h [ ] = new int [ M ] ; Arrays . fill ( h , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) { A [ i ] = A [ i ] % M ; h [ A [ i ] ] ++ ; } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = i ; j < M ; j ++ ) { int rem = ( M - ( i + j ) % M ) % M ; if ( rem < j ) continue ; if ( i == j && rem == j ) ans += h [ i ] * ( h [ i ] - 1 ) * ( h [ i ] - 2 ) / 6 ; else if ( i == j ) ans += h [ i ] * ( h [ i ] - 1 ) * h [ rem ] / 2 ; else if ( i == rem ) ans += h [ i ] * ( h [ i ] - 1 ) * h [ j ] / 2 ; else if ( rem == j ) ans += h [ j ] * ( h [ j ] - 1 ) * h [ i ] / 2 ; else ans = ans + h [ i ] * h [ j ] * h [ rem ] ; } } return ans ; } public static void main ( String args [ ] ) { int M = 3 ; int A [ ] = { 1 , 2 , 4 , 3 } ; int N = A . length ; System . out . println ( countSubSeq ( A , N , M ) ) ; } }
Find n | Java program to find nth term ; utility function ; since first element of the series is 7 , we initialise a variable with 7 ; Using iteration to find nth term ; Driver code
import java . lang . * ; class GFG { static int findTerm ( int n ) { if ( n == 1 ) return n ; else { int term = 7 ; for ( int i = 2 ; i <= n ; i ++ ) term = term * 2 + ( i - 1 ) ; return term ; } } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( findTerm ( n ) ) ; } }
Find n | Java program to find the value at n - th place in the given sequence ; Returns n - th number in sequence 1 , 1 , 2 , 1 , 2 , 3 , 1 , 2 , 4 , ... ; One by one subtract counts elements in different blocks ; Driver Code
import java . io . * ; class GFG { static int findNumber ( int n ) { n -- ; int i = 1 ; while ( n >= 0 ) { n -= i ; ++ i ; } return ( n + i ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( findNumber ( n ) ) ; } }
Program to find correlation coefficient | JAVA Program to find correlation coefficient ; function that returns correlation coefficient . ; sum of elements of array X . ; sum of elements of array Y . ; sum of X [ i ] * Y [ i ] . ; sum of square of array elements . ; use formula for calculating correlation coefficient . ; Driver function ; Find the size of array . ; Function call to correlationCoefficient .
import java . math . * ; class GFG { static float correlationCoefficient ( int X [ ] , int Y [ ] , int n ) { int sum_X = 0 , sum_Y = 0 , sum_XY = 0 ; int squareSum_X = 0 , squareSum_Y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum_X = sum_X + X [ i ] ; sum_Y = sum_Y + Y [ i ] ; sum_XY = sum_XY + X [ i ] * Y [ i ] ; squareSum_X = squareSum_X + X [ i ] * X [ i ] ; squareSum_Y = squareSum_Y + Y [ i ] * Y [ i ] ; } float corr = ( float ) ( n * sum_XY - sum_X * sum_Y ) / ( float ) ( Math . sqrt ( ( n * squareSum_X - sum_X * sum_X ) * ( n * squareSum_Y - sum_Y * sum_Y ) ) ) ; return corr ; } public static void main ( String args [ ] ) { int X [ ] = { 15 , 18 , 21 , 24 , 27 } ; int Y [ ] = { 25 , 25 , 27 , 31 , 32 } ; int n = X . length ; System . out . printf ( " % 6f " , correlationCoefficient ( X , Y , n ) ) ; } }
Find the number of spectators standing in the stadium at time t | Java program to find number of spectators standing at a time ; If the time is less than k then we can print directly t time . ; If the time is n then k spectators are standing . ; Otherwise we calculate the spectators standing . ; Driver code ; Stores the value of n , k and t t is time n & k is the number of specators
class GFG { static void result ( long n , long k , long t ) { if ( t <= k ) System . out . println ( t ) ; else if ( t <= n ) System . out . println ( k ) ; else { long temp = t - n ; temp = k - temp ; System . out . println ( temp ) ; } } public static void main ( String args [ ] ) { long n , k , t ; n = 10 ; k = 5 ; t = 12 ; result ( n , k , t ) ; } }
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 .
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 to find GCD of floating point numbers | JAVA code for finding the GCD of two floating numbers . ; Recursive function to return gcd of a and b ; base case ; Driver Function .
import java . io . * ; class GFG { static double gcd ( double a , double b ) { if ( a < b ) return gcd ( b , a ) ; if ( Math . abs ( b ) < 0.001 ) return a ; else return ( gcd ( b , a - Math . floor ( a / b ) * b ) ) ; } public static void main ( String args [ ] ) { double a = 1.20 , b = 22.5 ; System . out . printf ( " % .1f " , gcd ( a , b ) ) ; } }
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
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 ) ) ; } }