codeparrot/xlcost-text-to-code huggingface dataset:
Add two numbers represented by Stacks | Java program to implement the above approach ; Function to return the stack that contains the sum of two numbers ; Calculate the sum of the top elements of both the stacks ; Push the sum into the stack ; Store the carry ; Pop the top elements ; If N1 is not empty ; If N2 is not empty ; If carry remains ; Reverse the stack . so that most significant digit is at the bottom of the stack ; Function to display the resultamt stack ; Driver Code
import java . util . Stack ; class GFG { static Stack < Integer > addStack ( Stack < Integer > N1 , Stack < Integer > N2 ) { Stack < Integer > res = new Stack < Integer > ( ) ; int sum = 0 , rem = 0 ; while ( ! N1 . isEmpty ( ) && ! N2 . isEmpty ( ) ) { sum = ( rem + N1 . peek ( ) + N2 . peek ( ) ) ; res . add ( sum % 10 ) ; rem = sum / 10 ; N1 . pop ( ) ; N2 . pop ( ) ; } while ( ! N1 . isEmpty ( ) ) { sum = ( rem + N1 . peek ( ) ) ; res . add ( sum % 10 ) ; rem = sum / 10 ; N1 . pop ( ) ; } while ( ! N2 . isEmpty ( ) ) { sum = ( rem + N2 . peek ( ) ) ; res . add ( sum % 10 ) ; rem = sum / 10 ; N2 . pop ( ) ; } while ( rem > 0 ) { res . add ( rem ) ; rem /= 10 ; } while ( ! res . isEmpty ( ) ) { N1 . add ( res . peek ( ) ) ; res . pop ( ) ; } res = N1 ; return res ; } static void display ( Stack < Integer > res ) { int N = res . size ( ) ; String s = " " ; while ( ! res . isEmpty ( ) ) { s = String . valueOf ( res . peek ( ) ) + s ; res . pop ( ) ; } System . out . print ( s + "NEW_LINE"); } public static void main ( String [ ] args ) { Stack < Integer > N1 = new Stack < Integer > ( ) ; N1 . add ( 5 ) ; N1 . add ( 8 ) ; N1 . add ( 7 ) ; N1 . add ( 4 ) ; Stack < Integer > N2 = new Stack < Integer > ( ) ; N2 . add ( 2 ) ; N2 . add ( 1 ) ; N2 . add ( 3 ) ; Stack < Integer > res = addStack ( N1 , N2 ) ; display ( res ) ; } }
Check if a number has an odd count of odd divisors and even count of even divisors | Java implementation of the above approach ; Function to check if the number is a perfect square ; Find floating point value of square root of x . ; If square root is an integer ; Function to check if count of even divisors is even and count of odd divisors is odd ; Driver code
class GFG { static boolean isPerfectSquare ( double x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } static void checkFactors ( int x ) { if ( isPerfectSquare ( x ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int N = 36 ; checkFactors ( N ) ; } }
Queries to find the XOR of an Array after replacing all occurrences of X by Y | Java Program to implement the above approach ; Stores the bitwise XOR of array elements ; Function to find the total xor ; Loop to find the xor of all the elements ; Function to find the XOR after replacing all occurrences of X by Y for Q queries ; Remove contribution of X from total_xor ; Adding contribution of Y to total_xor ; Print total_xor ; Driver Code
import java . util . * ; class GFG { static int total_xor ; static void initialize_xor ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { total_xor = total_xor ^ arr [ i ] ; } } static void find_xor ( int X , int Y ) { total_xor = total_xor ^ X ; total_xor = total_xor ^ Y ; System . out . print ( total_xor + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 7 , 8 , 9 } ; int n = arr . length ; initialize_xor ( arr , n ) ; int [ ] [ ] Q = { { 5 , 6 } , { 8 , 1 } } ; for ( int i = 0 ; i < Q . length ; i ++ ) { find_xor ( Q [ i ] [ 0 ] , Q [ i ] [ 1 ] ) ; } } }
Check whether a given number is an ugly number or not | Java implementation to check if a number is ugly number ; Function to check the ugly number ; Base Cases ; Condition to check if a number is divide by 2 , 3 , or 5 ; Driver Code
class GFG { static int isUgly ( int n ) { if ( n == 1 ) return 1 ; if ( n <= 0 ) return 0 ; if ( n % 2 == 0 ) { return ( isUgly ( n / 2 ) ) ; } if ( n % 3 == 0 ) { return ( isUgly ( n / 3 ) ) ; } if ( n % 5 == 0 ) { return ( isUgly ( n / 5 ) ) ; } return 0 ; } public static void main ( String args [ ] ) { int no = isUgly ( 14 ) ; if ( no == 1 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Maximum possible GCD for a pair of integers with sum N | Java program to find the maximum possible GCD of any pair with sum N ; Function to find the required GCD value ; If i is a factor of N ; Return the largest factor possible ; If N is a prime number ; Driver Code
class GFG { static int maxGCD ( int N ) { for ( int i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { return N / i ; } } return 1 ; } public static void main ( String [ ] args ) { int N = 33 ; System . out . println ( " Maximum ▁ Possible ▁ GCD ▁ " + " value ▁ is ▁ : ▁ " + maxGCD ( N ) ) ; } }
Minimize K whose XOR with given array elements leaves array unchanged | Java program for the above approach ; Function to find the minimum value of K in given range ; Declare a set ; Initialize count variable ; Iterate in range [ 1 , 1024 ] ; counter set as 0 ; Iterating through the Set ; Check if the XOR calculated is present in the Set ; If the value of Bitwise XOR inside the given set then increment count ; Check if the value of count is equal to the size of set ; Return minimum value of K ; If no such K is found ; Driver Code ; Given array ; Function Call
import java . util . * ; class GFG { static int min_value ( int arr [ ] , int N ) { int x , X , K ; HashSet < Integer > S = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { S . add ( arr [ i ] ) ; } int count = 0 ; for ( int i = 1 ; i <= 1024 ; i ++ ) { count = 0 ; for ( int it : S ) { X = ( ( i it ) - ( i & it ) ) ; if ( S . contains ( X ) ) { count ++ ; } } if ( count == S . size ( ) ) { K = i ; return K ; } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 3 , 3 , 0 , 2 } ; int N = arr . length ; System . out . print ( min_value ( arr , N ) ) ; } }
Junction Numbers | Java program for above approach ; Function to find the sum Of digits of N ; To store sum of N and sumOfdigitsof ( N ) ; extracting digit ; Function to check Junction numbers ; To store count of ways n can be represented as i + SOD ( i ) ; Driver Code ; Function Call
class GFG { static int sum ( int n ) { int sum = 0 ; while ( n != 0 ) { int r = n % 10 ; sum = sum + r ; n = n / 10 ; } return sum ; } static boolean isJunction ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i + sum ( i ) == n ) count ++ ; } return count >= 2 ; } public static void main ( String [ ] args ) { int N = 111 ; if ( isJunction ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Pointer | Java program for above approach ; Function to find the product of digits of a number N ; Function that returns true if n is prime else returns false ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return the smallest prime number greater than N ; Base case ; Loop continuously until isPrime returns true for a number greater than n ; Function to check Pointer - Prime numbers ; Driver Code ; Given Number N ; Function Call
class GFG { static int digProduct ( int n ) { int product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = n / 10 ; } return product ; } static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int nextPrime ( int N ) { if ( N <= 1 ) return 2 ; int prime = N ; boolean found = false ; while ( ! found ) { prime ++ ; if ( isPrime ( prime ) ) found = true ; } return prime ; } static boolean isPointerPrime ( int n ) { if ( isPrime ( n ) && ( n + digProduct ( n ) == nextPrime ( n ) ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int N = 23 ; if ( isPointerPrime ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Perfect Digital Invariants number | Java program for the above approach ; Function to calculate x raised to the power y ; Function to check whether the given number is Perfect Digital Invariant number or not ; For each digit in temp ; If satisfies Perfect Digital Invariant condition ; If sum exceeds n , then not possible ; Driver Code ; Given Number N ; Function Call
import java . util . * ; class GFG { static int power ( int x , int y ) { if ( y == 0 ) { return 1 ; } if ( y % 2 == 0 ) { return ( power ( x , y / 2 ) * power ( x , y / 2 ) ) ; } return ( x * power ( x , y / 2 ) * power ( x , y / 2 ) ) ; } static boolean isPerfectDigitalInvariant ( int x ) { for ( int fixed_power = 1 ; ; fixed_power ++ ) { int temp = x , sum = 0 ; while ( temp > 0 ) { int r = temp % 10 ; sum += power ( r , fixed_power ) ; temp = temp / 10 ; } if ( sum == x ) { return true ; } if ( sum > x ) { return false ; } } } public static void main ( String [ ] args ) { int N = 4150 ; if ( isPerfectDigitalInvariant ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Brilliant Numbers | Java implementation for the above approach ; Function to generate all prime numbers less than n ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to return the number of digits in a number ; Function to check if N is a Brilliant number ; Generating primes using Sieve ; Traversing all numbers to find first pair ; Driver Code ; Given Number ; Function Call
import java . util . * ; class GFG { static void SieveOfEratosthenes ( int n , boolean isPrime [ ] ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) isPrime [ i ] = false ; } } } static int countDigit ( int n ) { int count = 0 ; while ( n != 0 ) { n = n / 10 ; ++ count ; } return count ; } static boolean isBrilliant ( int n ) { int flag = 0 ; boolean isPrime [ ] = new boolean [ n + 1 ] ; SieveOfEratosthenes ( n , isPrime ) ; for ( int i = 2 ; i < n ; i ++ ) { int x = n / i ; if ( isPrime [ i ] && isPrime [ x ] && ( x * i ) == n ) { if ( countDigit ( i ) == countDigit ( x ) ) return true ; } } return false ; } public static void main ( String [ ] args ) { int n = 1711 ; if ( isBrilliant ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Icosikaienneagonal Number | Java implementation for above approach ; Function to Find the Nth icosikaienneagonal Number ; Driver Code
class GFG { static int icosikaienneagonalNum ( int n ) { return ( 27 * n * n - 25 * n ) / 2 ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . print ( icosikaienneagonalNum ( n ) ) ; } }
Find the largest contiguous pair sum in given Array | Java program to find the a contiguous pair from the which has the largest sum ; Function to find and return the largest sum contiguous pair ; Stores the contiguous pair ; Initialize maximum sum ; Compare sum of pair with max_sum ; Insert the pair ; Driver Code
import java . util . * ; class GFG { public static Vector < Integer > largestSumpair ( int [ ] arr , int n ) { Vector < Integer > pair = new Vector < Integer > ( ) ; int max_sum = Integer . MIN_VALUE , i ; for ( i = 1 ; i < n ; i ++ ) { if ( max_sum < ( arr [ i ] + arr [ i - 1 ] ) ) { max_sum = arr [ i ] + arr [ i - 1 ] ; if ( pair . isEmpty ( ) ) { pair . add ( arr [ i - 1 ] ) ; pair . add ( arr [ i ] ) ; } else { pair . set ( 0 , arr [ i - 1 ] ) ; pair . set ( 1 , arr [ i ] ) ; } } } return pair ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , - 5 , 9 , - 3 , 2 } ; int N = arr . length ; Vector < Integer > pair = new Vector < Integer > ( ) ; pair = largestSumpair ( arr , N ) ; for ( Integer it : pair ) { System . out . print ( it + " ▁ " ) ; } } }
Sum of minimum value of x and y satisfying the equation ax + by = c | Java program for the above approach ; x and y store solution of equation ax + by = g ; Euclidean Algorithm ; store_gcd returns the gcd of a and b ; Function to find any possible solution ; Condition if solution does not exists ; Adjusting the sign of x0 and y0 ; Function to shift solution ; Shifting to obtain another solution ; Function to find minimum value of x and y ; g is the gcd of a and b ; Store sign of a and b ; If x is less than 0 , then shift solution ; If y is less than 0 , then shift solution ; Find intersection such that both x and y are positive ; miny is value of y corresponding to minx ; Returns minimum value of x + y ; Driver Code ; Given a , b , and c ; Function call
import java . lang . * ; class GFG { public static int x = 0 , y = 0 , x1 = 0 , y1 = 0 ; public static int x0 = 0 , y0 = 0 , g = 0 ; public static int gcd ( int a , int b ) { if ( b == 0 ) { x = 1 ; y = 0 ; return a ; } int store_gcd = gcd ( b , a % b ) ; x = y1 ; y = x1 - y1 * ( a / b ) ; return store_gcd ; } public static int possible_solution ( int a , int b , int c ) { g = gcd ( Math . abs ( a ) , Math . abs ( b ) ) ; if ( c % g != 0 ) { return 0 ; } x0 *= c / g ; y0 *= c / g ; if ( a < 0 ) x0 *= - 1 ; if ( b < 0 ) y0 *= - 1 ; return 1 ; } public static void shift_solution ( int a , int b , int shift_var ) { x += shift_var * b ; y -= shift_var * a ; } public static int find_min_sum ( int a , int b , int c ) { int x = 0 , y = 0 , g = 0 ; if ( possible_solution ( a , b , c ) == 0 ) return - 1 ; if ( g != 0 ) { a /= g ; b /= g ; } int sign_a = a > 0 ? + 1 : - 1 ; int sign_b = b > 0 ? + 1 : - 1 ; shift_solution ( a , b , - x / b ) ; if ( x < 0 ) shift_solution ( a , b , sign_b ) ; int minx1 = x ; shift_solution ( a , b , y / a ) ; if ( y < 0 ) shift_solution ( a , b , - sign_a ) ; int minx2 = x ; if ( minx2 > x ) { int temp = minx2 ; minx2 = x ; x = temp ; } int minx = Math . max ( minx1 , minx2 ) ; if ( minx > x ) return - 1 ; int miny = ( c - a * x ) / b ; return ( miny + minx ) ; } public static void main ( String [ ] args ) { int a = 2 , b = 2 , c = 0 ; System . out . println ( find_min_sum ( a , b , c ) ) ; } }
Super | Java implementation to check if N is a super - d number ; Function to check if N is a super - d number ; Utility function ; Driver Code
class GFG { static boolean isSuperdNum ( int n ) { for ( int d = 2 ; d < 10 ; d ++ ) { String subString = newString ( d ) ; if ( String . valueOf ( ( d * Math . pow ( n , d ) ) ) . contains ( subString ) ) return true ; } return false ; } private static String newString ( int d ) { String ans = " " ; for ( int i = 0 ; i < d ; i ++ ) { ans += String . valueOf ( d ) ; } return ans ; } public static void main ( String [ ] args ) { int n = 261 ; if ( isSuperdNum ( n ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Second hexagonal numbers | Java implementation to find N - th term in the series ; Function to find N - th term in the series ; Driver code
class GFG { static void findNthTerm ( int n ) { System . out . print ( n * ( 2 * n + 1 ) ) ; } public static void main ( String [ ] args ) { int N = 4 ; findNthTerm ( N ) ; } }
Apocalyptic Number | Java program for the above approach ; Function to check if a number N is Apocalyptic ; Driver Code ; Given Number N ; Function Call
class GFG { static boolean isApocalyptic ( int n ) { if ( String . valueOf ( ( Math . pow ( 2 , n ) ) ) . contains ( "666" ) ) return true ; return false ; } public static void main ( String [ ] args ) { int N = 157 ; if ( isApocalyptic ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Duffinian Numbers | Java program for the above approach ; Recursive function to return gcd of a and b ; Function to calculate the sum of all divisors of a given number ; Sum of divisors ; Find all divisors of num ; if ' i ' is divisor of ' n ' ; If both divisors are same then add it once ; Add 1 and n to result as above loop considers proper divisors greater than 1. ; Function to check if n is an Duffinian number ; Calculate the sum of divisors ; If number is prime return false ; Find the gcd of n and sum of divisors of n ; Returns true if N and sumDivisors are relatively prime ; Driver code ; Given Number ; Function Call
class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int divSum ( int n ) { int result = 0 ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == ( n / i ) ) result += i ; else result += ( i + n / i ) ; } } return ( result + n + 1 ) ; } static boolean isDuffinian ( int n ) { int sumDivisors = divSum ( n ) ; if ( sumDivisors == n + 1 ) return false ; int hcf = gcd ( n , sumDivisors ) ; return hcf == 1 ; } public static void main ( String [ ] args ) { int n = 36 ; if ( isDuffinian ( n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Kummer Numbers | Java program to find Kummer number upto n terms ; vector to store all prime less than and equal to 10 ^ 6 ; Function for the Sieve of Sundaram . This function stores all prime numbers less than MAX in primes ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half This array is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Function to calculate nth Kummer number ; Multiply first n primes ; return nth Kummer number ; Driver code
import java . util . * ; class GFG { static int MAX = 1000000 ; static Vector < Integer > primes = new Vector < Integer > ( ) ; static void sieveSundaram ( ) { boolean marked [ ] = new boolean [ MAX / 2 + 1 ] ; for ( int i = 1 ; i <= ( Math . sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j += 2 * i + 1 ) marked [ j ] = true ; primes . add ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . add ( 2 * i + 1 ) ; } static int calculateKummer ( int n ) { int result = 1 ; for ( int i = 0 ; i < n ; i ++ ) result = result * primes . get ( i ) ; return - 1 + result ; } public static void main ( String [ ] args ) { int n = 5 ; sieveSundaram ( ) ; for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( calculateKummer ( i ) + " , ▁ " ) ; } }
Check if given number contains a digit which is the average of all other digits | Java program to check whether a given number contains a digit which is the average of all other digits ; Function which checks if a digits exists in n which is the average of all other digits ; Calculate sum of digits in n ; Store the digits ; Increase the count of digits in n ; If average of all digits is an integer ; Otherwise ; Driver Code
import java . util . * ; class GFG { static void check ( int n ) { HashSet < Integer > digits = new HashSet < Integer > ( ) ; int temp = n ; int sum = 0 ; int count = 0 ; while ( temp > 0 ) { sum += temp % 10 ; digits . add ( temp % 10 ) ; count ++ ; temp = temp / 10 ; } if ( sum % count == 0 && digits . contains ( sum / count ) ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } public static void main ( String [ ] args ) { int n = 42644 ; check ( n ) ; } }
Count of lexicographically smaller characters on right |  ; function to count the smaller characters at the right of index i ; store the length of String ; for each index initialize count as zero ; increment the count if characters are smaller than at ith index ; print the count of characters smaller than the index i ; Driver code ; input String
class GFG { static void countSmaller ( String str ) { int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( str . charAt ( j ) < str . charAt ( i ) ) { cnt += 1 ; } } System . out . print ( cnt + " ▁ " ) ; } } public static void main ( String [ ] args ) { String str = " edcba " ; countSmaller ( str ) ; } }
Find the smallest contiguous sum pair in an Array | Java program to find the smallest sum contiguous pair ; Function to find the smallest sum contiguous pair ; Stores the contiguous pair ; Initialize minimum sum ; Checking for minimum value ; Add to pair ; Updating pair ; Driver Code
import java . util . * ; class GFG { public static Vector < Integer > smallestSumpair ( int [ ] arr , int n ) { Vector < Integer > pair = new Vector < Integer > ( ) ; int min_sum = Integer . MAX_VALUE , i ; for ( i = 1 ; i < n ; i ++ ) { if ( min_sum > ( arr [ i ] + arr [ i - 1 ] ) ) { min_sum = arr [ i ] + arr [ i - 1 ] ; if ( pair . isEmpty ( ) ) { pair . add ( arr [ i - 1 ] ) ; pair . add ( arr [ i ] ) ; } else { pair . set ( 0 , arr [ i - 1 ] ) ; pair . set ( 1 , arr [ i ] ) ; } } } return pair ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 9 , - 3 , 2 , 0 } ; int N = arr . length ; Vector < Integer > pair = new Vector < Integer > ( ) ; pair = smallestSumpair ( arr , N ) ; System . out . println ( pair . get ( 0 ) + " ▁ " + pair . get ( 1 ) ) ; } }
Add two integers of different base and represent sum in smaller base of the two | Java Program to find the sum of two integers of different bases . ; Initialize power of base ; Initialize result ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 2 ] * base + str [ len - 3 ] * ( base ^ 2 ) + ... ; Maximum base - 36 String base_num = " " ; ; print ans in reverse order ; Driver Code ; Integer in base 10 ; Sum of integers ; Minimum Base ; Sum of integers in Min Base
import java . util . * ; class GFG { static int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } static int convert ( String num , int bases ) { int len = ( num . length ( ) ) ; int power = 1 ; int res = 0 ; int i ; for ( i = len - 1 ; i >= 0 ; i -- ) { res += val ( num . charAt ( i ) ) * power ; power = power * bases ; } return res ; } static void dec_to_base ( int num , int bases ) { StringBuilder base_num = new StringBuilder ( ) ; while ( num > 0 ) { int dig = ( num % bases ) ; if ( dig < 10 ) base_num . append ( String . valueOf ( dig ) ) ; else base_num . append ( String . valueOf ( ( ' A ' + dig - 10 ) ) ) ; num /= bases ; } for ( int i = base_num . length ( ) - 1 ; i >= 0 ; i -- ) System . out . print ( base_num . charAt ( i ) ) ; } public static void main ( String [ ] args ) { String a = "123" ; String b = "234" ; int base_a = 6 ; int base_b = 8 ; int a10 = convert ( a , base_a ) ; int b10 = convert ( b , base_b ) ; int summ = a10 + b10 ; int min_base = Math . min ( base_a , base_b ) ; dec_to_base ( summ , min_base ) ; } }
Sum of the products of same placed digits of two numbers | Java program to calculate the sum of same placed digits of two numbers ; Function to find the sum of the products of their corresponding digits ; Loop until one of the numbers have no digits remaining ; Driver Code
class GFG { static int sumOfProductOfDigits ( int n1 , int n2 ) { int sum = 0 ; while ( n1 > 0 && n2 > 0 ) { sum += ( ( n1 % 10 ) * ( n2 % 10 ) ) ; n1 /= 10 ; n2 /= 10 ; } return sum ; } public static void main ( String args [ ] ) { int n1 = 25 ; int n2 = 1548 ; System . out . println ( sumOfProductOfDigits ( n1 , n2 ) ) ; } }