codeparrot/xlcost-text-to-code huggingface dataset:
Find unit digit of x raised to power y | Efficient Java program to find unit digit of x ^ y . ; Returns unit digit of x raised to power y ; Initialize result as 1 to handle case when y is 0. ; One by one multiply with x mod 10 to avoid overflow . ; Driver program
import java . io . * ; class GFG { static int unitDigitXRaisedY ( int x , int y ) { int res = 1 ; for ( int i = 0 ; i < y ; i ++ ) res = ( res * x ) % 10 ; return res ; } public static void main ( String args [ ] ) throws IOException { System . out . println ( unitDigitXRaisedY ( 4 , 2 ) ) ; } }
Evaluation of Risk in Investments | Java code for above approach ; Vector stores the observation in pairs of format ( xi , fi ) , where xi = value of observation ; Function to calculate the summation of fi * xi ; Function to calculate summation fi ; Function to calculate the mean of the set of observations v ; Function to calculate the std deviation of set of observations v ; Get sum of frequencies ; Get the mean of the set of observations ; Driver Code
import java . util . * ; class GFG { static class pair { float first , second ; public pair ( float first , float second ) { this . first = first ; this . second = second ; } } static Vector < pair > Vector ; static float sigma_fx ( pair [ ] a ) { float sum = 0 ; for ( pair i : a ) { sum += i . first * i . second ; } return sum ; } static float sigma_f ( pair [ ] a ) { float sum = 0.0f ; for ( pair i : a ) { sum += i . second ; } return sum ; } static float calculate_mean ( pair [ ] a ) { return sigma_fx ( a ) / sigma_f ( a ) ; } static float calculate_std ( pair [ ] a ) { float f = sigma_f ( a ) ; float mean = sigma_fx ( a ) / f ; float sum = 0 ; for ( pair i : a ) { sum += ( i . first - mean ) * ( i . first - mean ) * i . second ; } return ( float ) Math . sqrt ( sum / f ) ; } public static void main ( String [ ] args ) { pair [ ] A = { new pair ( 0f , 0.1f ) , new pair ( 100f , 0.1f ) , new pair ( 200f , 0.2f ) , new pair ( 333f , 0.3f ) , new pair ( 400f , 0.3f ) } ; pair [ ] B = { new pair ( 100f , 0.1f ) , new pair ( 200f , 0.5f ) , new pair ( 700f , 0.4f ) } ; float avg_A = calculate_mean ( A ) ; float avg_B = calculate_mean ( B ) ; float std_A = calculate_std ( A ) ; float std_B = calculate_std ( B ) ; System . out . print ( " For ▁ Investment ▁ A " + "NEW_LINE"); System . out . print ( " Average : ▁ " + avg_A + "NEW_LINE"); System . out . print ( " Standard ▁ Deviation : ▁ " + std_A + "NEW_LINE"); System . out . print ( " Normalised ▁ Std : ▁ " + std_A / avg_A + "NEW_LINE"); System . out . print ( " For ▁ Investment ▁ B " + "NEW_LINE"); System . out . print ( " Average : ▁ " + avg_B + "NEW_LINE"); System . out . print ( " Standard ▁ Deviation : ▁ " + std_B + "NEW_LINE"); System . out . print ( " Normalised ▁ Std : ▁ " + std_B / avg_B + "NEW_LINE"); if ( ( std_B / avg_B ) < ( std_A / avg_A ) ) System . out . print ( "Investment B is less riskyNEW_LINE"); else System . out . print ( "Investment A is less riskyNEW_LINE"); } }
Max occurring divisor in an interval | Efficient Java program to find maximum occurring factor in an interval ; function to find max occurring divisor interval [ x , y ] ; if there is only one number in the in the interval , return that number ; otherwise , 2 is the max occurring divisor ; Driver program
import java . io . * ; class GFG { static int findDivisor ( int x , int y ) { if ( x == y ) return y ; return 2 ; } public static void main ( String [ ] args ) { int x = 3 , y = 16 ; System . out . println ( findDivisor ( x , y ) ) ; } }
Average of Squares of Natural Numbers | Java program to calculate 1 ^ 2 + 2 ^ 2 + 3 ^ 2 + ... average of square number ; Function to calculate average of square number ; Driver code
import java . io . * ; public class GFG { static float AvgofSquareN ( int n ) { float sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( i * i ) ; return sum / n ; } static public void main ( String [ ] args ) { int n = 2 ; System . out . println ( AvgofSquareN ( n ) ) ; } }
Find sum of even factors of a number | Formula based Java program to find sum of all divisors of n . ; Returns sum of all factors of n . ; If n is odd , then there are no even factors . ; Traversing through all prime factors . ; While i divides n , print i and divide n ; here we remove the 2 ^ 0 that is 1. All other factors ; This condition is to handle the case when n is a prime number . ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { public static int sumofFactors ( int n ) { if ( n % 2 != 0 ) return 0 ; int res = 1 ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; if ( i == 2 && count == 1 ) curr_sum = 0 ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } public static void main ( String argc [ ] ) { int n = 18 ; System . out . println ( sumofFactors ( n ) ) ; } }
Find LCM of rational numbers | JAVA program to find LCM of given array ; get lcm of two numbers ; Finds LCM of numerators ; calculate the lcm of all numerators ; return all numerator lcm ; Get GCD of all the denominators ; calculate the gcd of all the denominators ; return all denominator gcd ; find lcm of all the rational number ; return the LCM of all numerator / GCD of all denominator ; Driver code ; give rational number 2 / 7 , 3 / 14 , 5 / 3 make pair as a numerator and denominator
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int LCM ( int a , int b ) { return ( a * b ) / ( __gcd ( a , b ) ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static int lcmOfNumerator ( Vector < pair > vect ) { int lcm = vect . get ( 0 ) . first ; for ( int i = 1 ; i < vect . size ( ) ; i ++ ) lcm = LCM ( vect . get ( i ) . first , lcm ) ; return lcm ; } static int gcdOfDemoninators ( Vector < pair > vect ) { int gcd = vect . get ( 0 ) . second ; for ( int i = 1 ; i < vect . size ( ) ; i ++ ) gcd = __gcd ( vect . get ( i ) . second , gcd ) ; return gcd ; } static void lcmOfRationals ( Vector < pair > vect ) { System . out . print ( lcmOfNumerator ( vect ) + " / " + gcdOfDemoninators ( vect ) ) ; } public static void main ( String [ ] args ) { Vector < pair > vect = new Vector < pair > ( ) ; vect . add ( new pair ( 2 , 7 ) ) ; vect . add ( new pair ( 3 , 14 ) ) ; vect . add ( new pair ( 5 , 3 ) ) ; lcmOfRationals ( vect ) ; } }
Program to determine focal length of a spherical mirror | Java program to determine the focal length of a of a spherical mirror ; Determines focal length of a spherical concave mirror ; Determines focal length of a spherical convex mirror ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { public static float focal_length_concave ( float R ) { return R / 2 ; } public static float focal_length_convex ( float R ) { return - ( R / 2 ) ; } public static void main ( String argc [ ] ) { float R = 30 ; System . out . print ( " Focal ▁ length ▁ of " + " spherical ▁ concave " + " mirror ▁ is ▁ : ▁ " + focal_length_concave ( R ) + " unitsNEW_LINE"); System . out . println ( " Focal ▁ length ▁ of " + " spherical ▁ convex " + " mirror ▁ is ▁ : ▁ " + focal_length_convex ( R ) + " ▁ units " ) ; } }
Find sum of odd factors of a number | Formula based Java program to find sum of all divisors of n . ; Returns sum of all factors of n . ; Traversing through all prime factors . ; ignore even factors by removing all powers of 2 ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number . ; Driver code
import java . io . * ; import java . math . * ; class GFG { static int sumofoddFactors ( int n ) { int res = 1 ; while ( n % 2 == 0 ) n = n / 2 ; for ( int i = 3 ; i <= Math . sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } public static void main ( String args [ ] ) throws IOException { int n = 30 ; System . out . println ( sumofoddFactors ( n ) ) ; } }
Number of non | Java program to find the numbers of non negative integral solutions ; return number of non negative integral solutions ; initialize total = 0 ; Base Case if n = 1 and val >= 0 then it should return 1 ; iterate the loop till equal the val ; total solution of equations and again call the recursive function Solutions ( variable , value ) ; return the total no possible solution ; Driver code
class GFG { static int countSolutions ( int n , int val ) { int total = 0 ; if ( n == 1 && val >= 0 ) return 1 ; for ( int i = 0 ; i <= val ; i ++ ) { total += countSolutions ( n - 1 , val - i ) ; } return total ; } public static void main ( String [ ] args ) { int n = 5 ; int val = 20 ; System . out . print ( countSolutions ( n , val ) ) ; } }
Fibonomial coefficient and Fibonomial triangle | Java Program to print Fibonomial Triangle of height n . ; Function to produce Fibonacci Series . ; 0 th and 1 st number of the series are 0 and 1 ; Add the previous 2 numbers in the series and store it ; Function to produce fibonomial coefficient ; Function to print Fibonomial Triangle . ; Finding the fibonacci series . ; to store triangle value . ; initialising the 0 th element of each row and diagonal element equal to 0. ; for each row . ; for each column . ; finding each element using recurrence relation . ; printing the Fibonomial Triangle . ; Driver code
class GFG { static final int N = 6 ; static void fib ( int f [ ] , int n ) { int i ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; for ( i = 2 ; i <= n ; i ++ ) f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; } static void fibcoef ( int fc [ ] [ ] , int f [ ] , int n ) { for ( int i = 0 ; i <= n ; i ++ ) fc [ i ] [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { int k = j ; while ( k > 0 ) { k -- ; fc [ i ] [ j ] *= f [ k ] ; } k = 1 ; while ( ( j + 1 ) != k ) fc [ i ] [ j ] /= f [ k ++ ] ; } } } static void printFibonomialTriangle ( int n ) { int f [ ] = new int [ N + 1 ] ; fib ( f , n ) ; int dp [ ] [ ] = new int [ N + 1 ] [ N + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = dp [ i ] [ i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j < i ; j ++ ) dp [ i ] [ j ] = f [ i - j + 1 ] * dp [ i - 1 ] [ j - 1 ] + f [ j - 1 ] * dp [ i - 1 ] [ j ] ; } for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) System . out . print ( dp [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 6 ; printFibonomialTriangle ( n ) ; } }
Sum of Arithmetic Geometric Sequence | Java Program to find the sum of first n terms . ; Return the sum of first n term of AGP ; finding the each term of AGP and adding it to sum . ; Driven Program
import java . io . * ; class GFG { static int sumofNterm ( int a , int d , int b , int r , int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( ( a + ( i - 1 ) * d ) * ( b * Math . pow ( r , i - 1 ) ) ) ; return sum ; } public static void main ( String args [ ] ) { int a = 1 , d = 1 , b = 2 , r = 2 , n = 3 ; System . out . println ( sumofNterm ( a , d , b , r , n ) ) ; } }
Sum of the series 2 + ( 2 + 4 ) + ( 2 + 4 + 6 ) + ( 2 + 4 + 6 + 8 ) + …… + ( 2 + 4 + 6 + 8 + … . + 2 n ) | Java implementation to find the sum of the given series ; function to find the sum of the given series ; first term of each i - th term ; next term ; required sum ; Driver program to test above
class GFG { static int sumOfTheSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int k = 2 ; for ( int j = 1 ; j <= i ; j ++ ) { sum += k ; k += 2 ; } } return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . printf ( " Sum ▁ = ▁ % d " , sumOfTheSeries ( n ) ) ; } }
Program to find sum of series 1 * 2 * 3 + 2 * 3 * 4 + 3 * 4 * 5 + . . . + n * ( n + 1 ) * ( n + 2 ) | Java Program to find the sum of series 1 * 2 * 3 + 2 * 3 * 4 + . . . + n * ( n + 1 ) * ( n + 1 ) ; Function to calculate sum of series . ; Driver Code
public class GfG { static int sumOfSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum = sum + i * ( i + 1 ) * ( i + 2 ) ; return sum ; } public static void main ( String s [ ] ) { int n = 10 ; System . out . println ( sumOfSeries ( n ) ) ; } }
Program to get the Sum of series : 1 | Java program to get the sum of the series ; Function to get the series ; Sum of n - 1 terms starting from 2 nd term ; Driver Code
import java . io . * ; class MathSeries { static double Series ( double x , int n ) { double sum = 1 , term = 1 , fct , j , y = 2 , m ; int i ; for ( i = 1 ; i < n ; i ++ ) { fct = 1 ; for ( j = 1 ; j <= y ; j ++ ) { fct = fct * j ; } term = term * ( - 1 ) ; m = Math . pow ( x , y ) / fct ; m = m * term ; sum = sum + m ; y += 2 ; } return sum ; } public static void main ( String [ ] args ) { double x = 3 ; int n = 4 ; System . out . println ( Math . round ( Series ( x , n ) * 10000.0 ) / 10000.0 ) ; } }
Program to get the Sum of series : 1 | Java program to get the sum of the series ; Function to get the series ; Computing sum of remaining n - 1 terms . ; Driver Code
import java . io . * ; class GFG { static double Series ( double x , int n ) { double sum = 1 , term = 1 , fct = 1 ; double p = 1 , multi = 1 ; for ( int i = 1 ; i < n ; i ++ ) { fct = fct * multi * ( multi + 1 ) ; p = p * x * x ; term = ( - 1 ) * term ; multi += 2 ; sum = sum + ( term * p ) / fct ; } return sum ; } public static void main ( String args [ ] ) { double x = 9 ; int n = 10 ; System . out . printf ( " % .4f " , Series ( x , n ) ) ; } }
Find the number of consecutive zero at the end after multiplying n numbers | Java program to find the number of consecutive zero at the end after multiplying n numbers ; Function to count two 's factor ; Count number of 2 s present in n ; Function to count five 's  factor ; Function to count number of zeros ; Count the two 's factor  of n number ; Count the five 's factor  of n number ; Return the minimum ; driver function
public class GfG { static int two_factor ( int n ) { int twocount = 0 ; while ( n % 2 == 0 ) { twocount ++ ; n = n / 2 ; } return twocount ; } static int five_factor ( int n ) { int fivecount = 0 ; while ( n % 5 == 0 ) { fivecount ++ ; n = n / 5 ; } return fivecount ; } static int find_con_zero ( int arr [ ] , int n ) { int twocount = 0 ; int fivecount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { twocount += two_factor ( arr [ i ] ) ; fivecount += five_factor ( arr [ i ] ) ; } if ( twocount < fivecount ) return twocount ; else return fivecount ; } public static void main ( String s [ ] ) { int arr [ ] = { 100 , 10 , 5 , 25 , 35 , 14 } ; int n = 6 ; System . out . println ( find_con_zero ( arr , n ) ) ; } }
First occurrence of a digit in a given fraction | Java program to find first occurrence of c in a / b ; Function to print the first digit ; Reduce the number to its mod ; Traverse for every decimal places ; Get every fraction places when ( a * 10 / b ) / c ; Check if it is equal to the required integer ; Mod the number ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { public static int first ( int a , int b , int c ) { a %= b ; for ( int i = 1 ; i <= b ; i ++ ) { a = a * 10 ; if ( a / b == c ) return i ; a %= b ; } return - 1 ; } public static void main ( String argc [ ] ) { int a = 1 , b = 4 , c = 5 ; System . out . println ( first ( a , b , c ) ) ; } }
Minimize the absolute difference of sum of two subsets | Java program for Minimize the absolute difference of sum of two subsets ; function to print difference ; summation of n elements ; if divisible by 4 ; if remainder 1 or 2. In case of remainder 2 , we divide elements from 3 to n in groups of size 4 and put 1 in one group and 2 in group . This also makes difference 1. ; We put elements from 4 to n in groups of size 4. Remaining elements 1 , 2 and 3 can be divided as ( 1 , 2 ) and ( 3 ) . ; Driver program to test above function
import java . util . * ; class GFG { static void subsetDifference ( int n ) { int s = n * ( n + 1 ) / 2 ; if ( n % 4 == 0 ) { System . out . println ( " First ▁ subset ▁ sum ▁ = ▁ " + s / 2 ) ; System . out . println ( " Second ▁ subset ▁ sum ▁ = ▁ " + s / 2 ) ; System . out . println ( " Difference ▁ = ▁ " + 0 ) ; } else { if ( n % 4 == 1 n % 4 == 2 ) { System . out . println ( " First ▁ subset ▁ sum ▁ = ▁ " + s / 2 ) ; System . out . println ( " Second ▁ subset ▁ sum ▁ = ▁ " + ( ( s / 2 ) + 1 ) ) ; System . out . println ( " Difference ▁ = ▁ " + 1 ) ; } else { System . out . println ( " First ▁ subset ▁ sum ▁ = ▁ " + s / 2 ) ; System . out . println ( " Second ▁ subset ▁ sum ▁ = ▁ " + s / 2 ) ; System . out . println ( " Difference ▁ = ▁ " + 0 ) ; } } } public static void main ( String [ ] args ) { int n = 6 ; subsetDifference ( n ) ; } }
Time required to meet in equilateral triangle | Java code to find time taken by animals to meet ; function to calculate time to meet ; Driver Code
import java . io . * ; public class GFG { static void timeToMeet ( double s , double v ) { double V = 3 * v / 2 ; double time = s / V ; System . out . println ( ( float ) time ) ; } static public void main ( String [ ] args ) { double s = 25 , v = 56 ; timeToMeet ( s , v ) ; } }
Sum of the series 1 + ( 1 + 3 ) + ( 1 + 3 + 5 ) + ( 1 + 3 + 5 + 7 ) + â €¦ â €¦ + ( 1 + 3 + 5 + 7 + â €¦ + ( 2 n | Java implementation to find the sum of the given series ; functionn to find the sum of the given series ; first term of each i - th term ; next term ; required sum ; Driver program
import java . util . * ; class GFG { static int sumOfTheSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int k = 1 ; for ( int j = 1 ; j <= i ; j ++ ) { sum += k ; k += 2 ; } } return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( " Sum ▁ = ▁ " + sumOfTheSeries ( n ) ) ; } }
Check if a number can be written as sum of three consecutive integers | JAVA Code to check if a number can be written as sum of three consecutive integers . ; function to check if a number can be written as sum of three consecutive integer . ; if n is 0 ; if n is positive , increment loop by 1. ; if n is negative , decrement loop by 1. ; Running loop from 0 to n - 2 ; check if sum of three consecutive integer is equal to n . ; Driver program to test above function
import java . util . * ; class GFG { static void checksum ( int n ) { if ( n == 0 ) { System . out . println ( " - 1 ▁ 0 ▁ 1" ) ; return ; } int inc ; if ( n > 0 ) inc = 1 ; else inc = - 1 ; for ( int i = 0 ; i <= n - 2 ; i += inc ) { if ( i + i + 1 + i + 2 == n ) { System . out . println ( i + " ▁ " + ( i + 1 ) + " ▁ " + ( i + 2 ) ) ; return ; } } System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 6 ; checksum ( n ) ; } }
Sum of all divisors from 1 to n | JAVA program to find sum of all divisor of number up to ' n ' ; Utility function to find sum of all divisor of number up to ' n ' ; Find all divisors of i and add them ; Driver code
import java . io . * ; class GFG { static int divisorSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = 1 ; j * j <= i ; ++ j ) { if ( i % j == 0 ) { if ( i / j == j ) sum += j ; else sum += j + i / j ; } } } return sum ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( divisorSum ( n ) ) ; n = 5 ; System . out . println ( divisorSum ( n ) ) ; } }
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
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 ) ; } }
Find largest prime factor of a number | Java Program to find largest prime factor of number ; function to find largest prime factor ; Initialize the maximum prime factor variable with the lowest one ; Print the number of 2 s that divide n ; equivalent to n /= 2 ; n must be odd at this point ; now we have to iterate only for integers who does not have prime factor 2 and 3 ; This condition is to handle the case when n is a prime number greater than 4 ; Driver code
import java . io . * ; import java . util . * ; class GFG { static long maxPrimeFactors ( long n ) { long maxPrime = - 1 ; while ( n % 2 == 0 ) { maxPrime = 2 ; n >>= 1 ; } while ( n % 3 == 0 ) { maxPrime = 3 ; n = n / 3 ; } for ( int i = 5 ; i <= Math . sqrt ( n ) ; i += 6 ) { while ( n % i == 0 ) { maxPrime = i ; n = n / i ; } while ( n % ( i + 2 ) == 0 ) { maxPrime = i + 2 ; n = n / ( i + 2 ) ; } } if ( n > 4 ) maxPrime = n ; return maxPrime ; } public static void main ( String [ ] args ) { Long n = 15l ; System . out . println ( maxPrimeFactors ( n ) ) ; n = 25698751364526l ; System . out . println ( maxPrimeFactors ( n ) ) ; } }
Count unset bits in a range | Java implementation to count unset bits in the given range ; Function to get no of set bits in the binary representation of ' n ' ; function to count unset bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; returns number of unset bits in the range ' l ' to ' r ' in ' n ' ; Driver code
class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int countUnsetBitsInGivenRange ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return ( r - l + 1 ) - countSetBits ( n & num ) ; } public static void main ( String [ ] args ) { int n = 80 ; int l = 1 , r = 4 ; System . out . print ( countUnsetBitsInGivenRange ( n , l , r ) ) ; } }