codeparrot/xlcost-text-to-code huggingface dataset:
Sum of fifth powers of the first n natural numbers | C # Program to find the sum of fifth power of first n natural numbers ; calculate the sum of fifth power of first n natural numbers ; Driven Program
using System ; class GFG { static long fifthPowerSum ( int n ) { return ( ( 2 * n * n * n * n * n * n ) + ( 6 * n * n * n * n * n ) + ( 5 * n * n * n * n ) - ( n * n ) ) / 12 ; } public static void Main ( ) { int n = 5 ; Console . Write ( fifthPowerSum ( n ) ) ; } }
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
using System ; 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 ( ) { Console . WriteLine ( unitDigitXRaisedY ( 4 , 2 ) ) ; } }
Evaluation of Risk in Investments | C # code for above approach ; List 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
using System ; using System . Collections . Generic ; class GFG { class pair { public float first , second ; public pair ( float first , float second ) { this . first = first ; this . second = second ; } } static List < pair > List ; static float sigma_fx ( pair [ ] a ) { float sum = 0 ; foreach ( pair i in a ) { sum += i . first * i . second ; } return sum ; } static float sigma_f ( pair [ ] a ) { float sum = 0.0f ; foreach ( pair i in 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 ; foreach ( pair i in 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 ) ; Console . Write ( " For ▁ Investment ▁ A " + " STRNEWLINE " ) ; Console . Write ( " Average : ▁ " + avg_A + " STRNEWLINE " ) ; Console . Write ( " Standard ▁ Deviation : ▁ " + std_A + " STRNEWLINE " ) ; Console . Write ( " Normalised ▁ Std : ▁ " + std_A / avg_A + " STRNEWLINE " ) ; Console . Write ( " For ▁ Investment ▁ B " + " STRNEWLINE " ) ; Console . Write ( " Average : ▁ " + avg_B + " STRNEWLINE " ) ; Console . Write ( " Standard ▁ Deviation : ▁ " + std_B + " STRNEWLINE " ) ; Console . Write ( " Normalised ▁ Std : ▁ " + std_B / avg_B + " STRNEWLINE " ) ; if ( ( std_B / avg_B ) < ( std_A / avg_A ) ) Console . Write ( " Investment ▁ B ▁ is ▁ less ▁ risky STRNEWLINE " ) ; else Console . Write ( " Investment ▁ A ▁ is ▁ less ▁ risky STRNEWLINE " ) ; } }
Max occurring divisor in an interval | Efficient C # 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 Code
using System ; class GFG { static int findDivisor ( int x , int y ) { if ( x == y ) return y ; return 2 ; } public static void Main ( ) { int x = 3 , y = 16 ; Console . Write ( findDivisor ( x , y ) ) ; } }
Average of Squares of Natural Numbers | C # program to calculate 1 ^ 2 + 2 ^ 2 + 3 ^ 2 + ... average of square number ; Function to calculate average of square number ; Driver code
using System ; 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 ; Console . WriteLine ( AvgofSquareN ( n ) ) ; } }
Find sum of even factors of a number | Formula based C # 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 Code
using System ; 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 ( ) { int n = 18 ; Console . WriteLine ( sumofFactors ( n ) ) ; } }
Find LCM of rational numbers | C # 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
using System ; using System . Collections . Generic ; class GFG { class pair { public 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 ( List < pair > vect ) { int lcm = vect [ 0 ] . first ; for ( int i = 1 ; i < vect . Count ; i ++ ) lcm = LCM ( vect [ i ] . first , lcm ) ; return lcm ; } static int gcdOfDemoninators ( List < pair > vect ) { int gcd = vect [ 0 ] . second ; for ( int i = 1 ; i < vect . Count ; i ++ ) gcd = __gcd ( vect [ i ] . second , gcd ) ; return gcd ; } static void lcmOfRationals ( List < pair > vect ) { Console . Write ( lcmOfNumerator ( vect ) + " / " + gcdOfDemoninators ( vect ) ) ; } public static void Main ( String [ ] args ) { List < pair > vect = new List < 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 | C # 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
using System ; 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 ; Console . Write ( " Focal ▁ length ▁ of " + " spherical ▁ concave " + " mirror ▁ is ▁ : ▁ " + focal_length_concave ( R ) + " ▁ units STRNEWLINE " ) ; Console . Write ( " Focal ▁ length ▁ of " + " spherical ▁ convex " + " mirror ▁ is ▁ : ▁ " + focal_length_convex ( R ) + " ▁ units " ) ; } }
Find sum of odd factors of a number | Formula based C # 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
using System ; 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 [ ] argc ) { int n = 30 ; Console . Write ( sumofoddFactors ( n ) ) ; } }
Number of non | C # 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
using System ; 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 ( ) { int n = 5 ; int val = 20 ; Console . WriteLine ( countSolutions ( n , val ) ) ; } }
Fibonomial coefficient and Fibonomial triangle | C # 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
using System ; class GFG { static 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 ++ ) Console . Write ( dp [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( ) { int n = 6 ; printFibonomialTriangle ( n ) ; } }
Sum of Arithmetic Geometric Sequence | C # 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 . ; Driver Code
using System ; 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 += ( int ) ( ( a + ( i - 1 ) * d ) * ( b * Math . Pow ( r , i - 1 ) ) ) ; return sum ; } public static void Main ( ) { int a = 1 , d = 1 , b = 2 , r = 2 , n = 3 ; Console . Write ( 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 ) | C # 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
using System ; 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 ( ) { int n = 5 ; Console . Write ( " Sum ▁ = ▁ " + sumOfTheSeries ( n ) ) ; } }
Program to find sum of series 1 * 2 * 3 + 2 * 3 * 4 + 3 * 4 * 5 + . . . + n * ( n + 1 ) * ( n + 2 ) | C # 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
using System ; 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 ( ) { int n = 10 ; Console . WriteLine ( sumOfSeries ( n ) ) ; } }
Program to get the Sum of series : 1 | C # 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
using System ; class GFG { 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 ( ) { double x = 9 ; int n = 10 ; Console . Write ( Series ( x , n ) * 10000.0 / 10000.0 ) ; } }
Program to get the Sum of series : 1 | C # program to get the sum of the series ; Function to get the series ; Computing sum of remaining n - 1 terms . ; Driver Code
using System ; class GFG { static float 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 ( float ) sum ; } public static void Main ( ) { double x = 9 ; int n = 10 ; Console . Write ( Series ( x , n ) ) ; } }
Find the number of consecutive zero at the end after multiplying n numbers | C # 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
using System ; 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 ( ) { int [ ] arr = { 100 , 10 , 5 , 25 , 35 , 14 } ; int n = 6 ; Console . WriteLine ( find_con_zero ( arr , n ) ) ; } }
First occurrence of a digit in a given fraction | C # 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
using System ; 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 ( ) { int a = 1 , b = 4 , c = 5 ; Console . WriteLine ( first ( a , b , c ) ) ; } }
Minimize the absolute difference of sum of two subsets | C # 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
using System ; class GFG { static void subsetDifference ( int n ) { int s = n * ( n + 1 ) / 2 ; if ( n % 4 == 0 ) { Console . WriteLine ( " First ▁ " + " subset ▁ sum ▁ = ▁ " + s / 2 ) ; Console . WriteLine ( " Second ▁ " + " subset ▁ sum ▁ = ▁ " + s / 2 ) ; Console . WriteLine ( " Difference " + " ▁ = ▁ " + 0 ) ; } else { if ( n % 4 == 1 n % 4 == 2 ) { Console . WriteLine ( " First ▁ " + " subset ▁ sum ▁ = ▁ " + s / 2 ) ; Console . WriteLine ( " Second ▁ " + " subset ▁ sum ▁ = ▁ " + ( ( s / 2 ) + 1 ) ) ; Console . WriteLine ( " Difference " + " ▁ = ▁ " + 1 ) ; } else { Console . WriteLine ( " First ▁ " + " subset ▁ sum ▁ = ▁ " + s / 2 ) ; Console . WriteLine ( " Second ▁ " + " subset ▁ sum ▁ = ▁ " + s / 2 ) ; Console . WriteLine ( " Difference " + " ▁ = ▁ " + 0 ) ; } } } public static void Main ( ) { int n = 6 ; subsetDifference ( n ) ; } }
Time required to meet in equilateral triangle | C # code to find time taken by animals to meet ; function to calculate time to meet ; Driver Code
using System ; public class GFG { static void timeToMeet ( double s , double v ) { double V = 3 * v / 2 ; double time = s / V ; Console . WriteLine ( ( float ) time ) ; } static public void Main ( ) { 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 | C # 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
using System ; 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 ( ) { int n = 5 ; Console . Write ( " Sum ▁ = ▁ " + sumOfTheSeries ( n ) ) ; } }
Check if a number can be written as sum of three consecutive integers | C # 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
using System ; class GFG { static void checksum ( int n ) { if ( n == 0 ) { Console . WriteLine ( " - 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 ) { Console . WriteLine ( i + " ▁ " + ( i + 1 ) + " ▁ " + ( i + 2 ) ) ; return ; } } Console . WriteLine ( " - 1" ) ; } public static void Main ( ) { int n = 6 ; checksum ( n ) ; } }
Sum of all divisors from 1 to n | C # 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
using System ; 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 ( ) { int n = 4 ; Console . WriteLine ( divisorSum ( n ) ) ; n = 5 ; Console . WriteLine ( divisorSum ( n ) ) ; } }
Program for Binomial Coefficients table | C # 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 Function
using System ; public class GFG { static void printbinomial ( int max ) { for ( int m = 0 ; m <= max ; m ++ ) { Console . Write ( m + " ▁ " ) ; int binom = 1 ; for ( int x = 0 ; x <= m ; x ++ ) { if ( m != 0 && x != 0 ) binom = binom * ( m - x + 1 ) / x ; Console . Write ( binom + " ▁ " ) ; } Console . WriteLine ( ) ; } } static public void Main ( ) { int max = 10 ; printbinomial ( max ) ; } }
Find largest prime factor of a number | C # 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
using System ; 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 ( ) { long n = 15L ; Console . WriteLine ( maxPrimeFactors ( n ) ) ; n = 25698751364526L ; Console . WriteLine ( maxPrimeFactors ( n ) ) ; } }
Count unset bits in a range | C # 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
using System ; 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 ( ) { int n = 80 ; int l = 1 , r = 4 ; Console . Write ( countUnsetBitsInGivenRange ( n , l , r ) ) ; } }