codeparrot/xlcost-text-to-code huggingface dataset:
Find Excel column name from a given column number |  ; Step 1 : Converting to number assuming 0 in number system ; Step 2 : Getting rid of 0 , as 0 is not part of number system ; Driver code
using System ; class GFG { static void printString ( int n ) { int [ ] arr = new int [ 10000 ] ; int i = 0 ; while ( n > 0 ) { arr [ i ] = n % 26 ; n = n / 26 ; i ++ ; } for ( int j = 0 ; j < i - 1 ; j ++ ) { if ( arr [ j ] <= 0 ) { arr [ j ] += 26 ; arr [ j + 1 ] = arr [ j + 1 ] - 1 ; } } for ( int j = i ; j >= 0 ; j -- ) { if ( arr [ j ] > 0 ) Console . Write ( ( char ) ( ' A ' + arr [ j ] - 1 ) ) ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { printString ( 26 ) ; printString ( 51 ) ; printString ( 52 ) ; printString ( 80 ) ; printString ( 676 ) ; printString ( 702 ) ; printString ( 705 ) ; } }
Calculate the angle between hour hand and minute hand | C # program to find angle between hour and minute hands ; Function to calculate the angle ; validate the input ; Calculate the angles moved by hour and minute hands with reference to 12 : 00 ; Find the difference between two angles ; smaller angle of two possible angles ; Driver code
using System ; class GFG { static int calcAngle ( double h , double m ) { if ( h < 0 m < 0 h > 12 m > 60 ) Console . Write ( " Wrong ▁ input " ) ; if ( h == 12 ) h = 0 ; if ( m == 60 ) { m = 0 ; h += 1 ; if ( h > 12 ) h = h - 12 ; } int hour_angle = ( int ) ( 0.5 * ( h * 60 + m ) ) ; int minute_angle = ( int ) ( 6 * m ) ; int angle = Math . Abs ( hour_angle - minute_angle ) ; angle = Math . Min ( 360 - angle , angle ) ; return angle ; } public static void Main ( ) { Console . WriteLine ( calcAngle ( 9 , 60 ) ) ; Console . Write ( calcAngle ( 3 , 30 ) ) ; } }
How to check if an instance of 8 puzzle is solvable ? | C # program to check if a given instance of 8 puzzle is solvable or not ; A utility function to count inversions in given array ' arr [ ] ' ; Value 0 is used for empty space ; This function returns true if given 8 puzzle is solvable . ; Count inversions in given 8 puzzle ; return true if inversion count is even . ; Driver code
using System ; class GFG { static int getInvCount ( int [ , ] arr ) { int inv_count = 0 ; for ( int i = 0 ; i < 3 - 1 ; i ++ ) for ( int j = i + 1 ; j < 3 ; j ++ ) if ( arr [ j , i ] > 0 && arr [ j , i ] > arr [ i , j ] ) inv_count ++ ; return inv_count ; } static bool isSolvable ( int [ , ] puzzle ) { int invCount = getInvCount ( puzzle ) ; return ( invCount % 2 == 0 ) ; } static void Main ( ) { int [ , ] puzzle = new int [ 3 , 3 ] { { 1 , 8 , 2 } , { 0 , 4 , 3 } , { 7 , 6 , 5 } } ; if ( isSolvable ( puzzle ) ) Console . WriteLine ( " Solvable " ) ; else Console . WriteLine ( " Not ▁ Solvable " ) ; } }
Birthday Paradox | C # program to approximate number of people in Birthday Paradox problem . ; Returns approximate number of people for a given probability ; Driver code
using System ; class GFG { static double find ( double p ) { return Math . Ceiling ( Math . Sqrt ( 2 * 365 * Math . Log ( 1 / ( 1 - p ) ) ) ) ; } public static void Main ( ) { Console . Write ( find ( 0.70 ) ) ; } }
Count Distinct Non | C # code to Count Distinct Non - Negative Integer Pairs ( x , y ) that Satisfy the inequality x * x + y * y < n ; This function counts number of pairs ( x , y ) that satisfy the inequality x * x + y * y < n . ; Driver program
using System ; class GFG { static int countSolutions ( int n ) { int res = 0 ; for ( int x = 0 ; x * x < n ; x ++ ) for ( int y = 0 ; x * x + y * y < n ; y ++ ) res ++ ; return res ; } public static void Main ( ) { Console . WriteLine ( " Total ▁ Number ▁ of ▁ " + " distinct ▁ Non - Negative ▁ pairs ▁ is ▁ " + countSolutions ( 6 ) ) ; } }
Count Distinct Non | An efficient C # program to find different ( x , y ) pairs that satisfy x * x + y * y < n . ; This function counts number of pairs ( x , y ) that satisfy the inequality x * x + y * y < n . ; Find the count of different y values for x = 0. ; One by one increase value of x , and find yCount forcurrent x . If yCount becomes 0 , then we have reached maximum possible value of x . ; Add yCount ( count of different possible values of y for current x ) to result ; Increment x ; Update yCount for current x . Keep reducing yCount while the inequality is not satisfied . ; Driver program
using System ; class GFG { static int countSolutions ( int n ) { int x = 0 , yCount , res = 0 ; for ( yCount = 0 ; yCount * yCount < n ; yCount ++ ) ; while ( yCount != 0 ) { res += yCount ; x ++ ; while ( yCount != 0 && ( x * x + ( yCount - 1 ) * ( yCount - 1 ) >= n ) ) yCount -- ; } return res ; } public static void Main ( ) { Console . WriteLine ( " Total ▁ Number ▁ of ▁ " + " distinct ▁ Non - Negative ▁ pairs ▁ is ▁ " + countSolutions ( 6 ) ) ; } }
Program for Method Of False Position | C # program for implementation of Bisection Method for solving equations ; An example function whose solution is determined using Bisection Method . The function is x ^ 3 - x ^ 2 + 2 ; Prints root of func ( x ) in interval [ a , b ] ; Initialize result ; Find the point that touches x axis ; Check if the above found point is root ; Decide the side to repeat the steps ; Driver program ; Initial values assumed
using System ; class GFG { static int MAX_ITER = 1000000 ; static double func ( double x ) { return ( x * x * x - x * x + 2 ) ; } static void regulaFalsi ( double a , double b ) { if ( func ( a ) * func ( b ) >= 0 ) { Console . WriteLine ( " You ▁ have ▁ not ▁ assumed ▁ right ▁ a ▁ and ▁ b " ) ; } double c = a ; for ( int i = 0 ; i < MAX_ITER ; i ++ ) { c = ( a * func ( b ) - b * func ( a ) ) / ( func ( b ) - func ( a ) ) ; if ( func ( c ) == 0 ) break ; else if ( func ( c ) * func ( a ) < 0 ) b = c ; else a = c ; } Console . WriteLine ( " The ▁ value ▁ of ▁ root ▁ is ▁ : ▁ " + ( int ) c ) ; } public static void Main ( String [ ] args ) { double a = - 200 , b = 300 ; regulaFalsi ( a , b ) ; } }
Program for Newton Raphson Method | C # program for implementation of Newton Raphson Method for solving equations ; An example function whose solution is determined using Bisection Method . The function is x ^ 3 - x ^ 2 + 2 ; Derivative of the above function which is 3 * x ^ x - 2 * x ; Function to find the root ; x ( i + 1 ) = x ( i ) - f ( x ) / f '(x) ; Driver code ; Initial values assumed
using System ; class GFG { static double EPSILON = 0.001 ; static double func ( double x ) { return x * x * x - x * x + 2 ; } static double derivFunc ( double x ) { return 3 * x * x - 2 * x ; } static void newtonRaphson ( double x ) { double h = func ( x ) / derivFunc ( x ) ; while ( Math . Abs ( h ) >= EPSILON ) { h = func ( x ) / derivFunc ( x ) ; x = x - h ; } Console . Write ( " The ▁ value ▁ of ▁ the " + " ▁ root ▁ is ▁ : ▁ " + Math . Round ( x * 100.0 ) / 100.0 ) ; } public static void Main ( ) { double x0 = - 20 ; newtonRaphson ( x0 ) ; } }
Find the element that appears once | C # code to find the element that occur only once ; Method to find the element that occur only once ; " one ▁ & ▁ arr [ i ] " gives the bits that are there in both ' ones ' and new element from arr [ ] . We add these bits to ' twos ' using bitwise OR ; " one ▁ & ▁ arr [ i ] " gives the bits that are there in both ' ones ' and new element from arr [ ] . We add these bits to ' twos ' using bitwise OR ; The common bits are those bits which appear third time So these bits should not be there in both ' ones ' and ' twos ' . common_bit_mask contains all these bits as 0 , so that the bits can be removed from ' ones ' and ' twos ' ; Remove common bits ( the bits that appear third time ) from ' ones ' ; Remove common bits ( the bits that appear third time ) from ' twos ' ; Driver code
using System ; class GFG { static int getSingle ( int [ ] arr , int n ) { int ones = 0 , twos = 0 ; int common_bit_mask ; for ( int i = 0 ; i < n ; i ++ ) { twos = twos | ( ones & arr [ i ] ) ; ones = ones ^ arr [ i ] ; common_bit_mask = ~ ( ones & twos ) ; ones &= common_bit_mask ; twos &= common_bit_mask ; } return ones ; } public static void Main ( ) { int [ ] arr = { 3 , 3 , 2 , 3 } ; int n = arr . Length ; Console . WriteLine ( " The ▁ element ▁ with ▁ single " + " occurrence ▁ is ▁ " + getSingle ( arr , n ) ) ; } }
Find the element that appears once | C # code to find the element that occur only once ; Initialize result ; Iterate through every bit ; Find sum of set bits at ith position in all array elements ; The bits with sum not multiple of 3 , are the bits of element with single occurrence . ; Driver Code
using System ; class GFG { static int INT_SIZE = 32 ; static int getSingle ( int [ ] arr , int n ) { int result = 0 ; int x , sum ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) { sum = 0 ; x = ( 1 << i ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & x ) == 0 ) sum ++ ; } if ( ( sum % 3 ) != 0 ) result |= x ; } return result ; } public static void Main ( ) { int [ ] arr = { 12 , 1 , 12 , 3 , 12 , 1 , 1 , 2 , 3 , 2 , 2 , 3 , 7 } ; int n = arr . Length ; Console . WriteLine ( " The ▁ element ▁ with ▁ single ▁ " + " occurrence ▁ is ▁ " + getSingle ( arr , n ) ) ; } }
Detect if two integers have opposite signs | C # Program to Detect if two integers have opposite signs . ; Function to detect signs ; Driver Code
using System ; class GFG { static bool oppositeSigns ( int x , int y ) { return ( ( x ^ y ) < 0 ) ; } public static void Main ( ) { int x = 100 , y = - 100 ; if ( oppositeSigns ( x , y ) == true ) Console . Write ( " Signs ▁ are ▁ opposite " ) ; else Console . Write ( " Signs ▁ are ▁ not ▁ opposite " ) ; } }
Count total set bits in all numbers from 1 to n | A simple C # program to count set bits in all numbers from 1 to n . ; Returns count of set bits present in all numbers from 1 to n ; initialize the result ; A utility function to count set bits in a number x ; Driver program
using System ; class GFG { static int countSetBits ( int n ) { int bitCount = 0 ; for ( int i = 1 ; i <= n ; i ++ ) bitCount += countSetBitsUtil ( i ) ; return bitCount ; } static int countSetBitsUtil ( int x ) { if ( x <= 0 ) return 0 ; return ( x % 2 == 0 ? 0 : 1 ) + countSetBitsUtil ( x / 2 ) ; } public static void Main ( ) { int n = 4 ; Console . Write ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " ) ; Console . Write ( countSetBits ( n ) ) ; } }
Count total set bits in all numbers from 1 to n |  ; ans store sum of set bits from 0 to n ; while n greater than equal to 2 ^ i ; This k will get flipped after 2 ^ i iterations ; change is iterator from 2 ^ i to 1 ; This will loop from 0 to n for every bit position ; When change = 1 flip the bit ; again set change to 2 ^ i ; increment the position ; Driver program
using System ; class GFG { static int countSetBits ( int n ) { int i = 0 ; int ans = 0 ; while ( ( 1 << i ) <= n ) { bool k = false ; int change = 1 << i ; for ( int j = 0 ; j <= n ; j ++ ) { if ( k == true ) ans += 1 ; else ans += 0 ; if ( change == 1 ) { k = ! k ; change = 1 << i ; } else { change -- ; } } i ++ ; } return ans ; } static void Main ( ) { int n = 17 ; Console . Write ( countSetBits ( n ) ) ; } }
Count total set bits in all numbers from 1 to n | C # A O ( Logn ) complexity program to count set bits in all numbers from 1 to n ; Returns position of leftmost set bit . The rightmost position is considered as 0 ; Given the position of previous leftmost set bit in n ( or an upper bound onleftmost position ) returns the new position of leftmost set bit in n ; The main recursive function used by countSetBits ( ) Returns count of set bits present in all numbers from 1 to n ; Get the position of leftmost set bit in n . This will be used as an upper bound for next set bit function ; Use the position ; Base Case : if n is 0 , then set bit count is 0 ; get position of next leftmost set bit ; If n is of the form 2 ^ x - 1 , i . e . , if n is like 1 , 3 , 7 , 15 , 31 , . . etc , then we are done . Since positions are considered starting from 0 , 1 is added to m ; update n for next recursive call ; Driver code
using System ; class GFG { static int getLeftmostBit ( int n ) { int m = 0 ; while ( n > 1 ) { n = n >> 1 ; m ++ ; } return m ; } static int getNextLeftmostBit ( int n , int m ) { int temp = 1 << m ; while ( n < temp ) { temp = temp >> 1 ; m -- ; } return m ; } static int countSetBits ( int n ) { int m = getLeftmostBit ( n ) ; return countSetBits ( n , m ) ; } static int countSetBits ( int n , int m ) { if ( n == 0 ) return 0 ; m = getNextLeftmostBit ( n , m ) ; if ( n == ( ( int ) 1 << ( m + 1 ) ) - 1 ) return ( int ) ( m + 1 ) * ( 1 << m ) ; n = n - ( 1 << m ) ; return ( n + 1 ) + countSetBits ( n ) + m * ( 1 << ( m - 1 ) ) ; } static public void Main ( ) { int n = 17 ; Console . Write ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " + countSetBits ( n ) ) ; } }
Count total set bits in all numbers from 1 to n |
static int getSetBitsFromOneToN ( int N ) { int two = 2 , ans = 0 ; int n = N ; while ( n != 0 ) { ans += ( N / two ) * ( two >> 1 ) ; if ( ( N & ( two - 1 ) ) > ( two >> 1 ) - 1 ) ans += ( N & ( two - 1 ) ) - ( two >> 1 ) + 1 ; two <<= 1 ; n >>= 1 ; } return ans ; }
Swap bits in a given number | C # Program to swap bits in a given number ; Move all bits of first set to rightmost side ; Move all bits of second set set to rightmost side ; XOR the two sets ; Put the xor bits back to their original positions ; XOR the ' xor ' with the original number so that the two sets are swapped ; Driver program
using System ; class GFG { static int swapBits ( int x , int p1 , int p2 , int n ) { int set1 = ( x >> p1 ) & ( ( 1 << n ) - 1 ) ; int set2 = ( x >> p2 ) & ( ( 1 << n ) - 1 ) ; int xor = ( set1 ^ set2 ) ; xor = ( xor << p1 ) | ( xor << p2 ) ; int result = x ^ xor ; return result ; } public static void Main ( ) { int res = swapBits ( 28 , 0 , 3 , 2 ) ; Console . WriteLine ( " Result ▁ = ▁ " + res ) ; } }
Swap bits in a given number |  ; Setting bit at p1 position to 1 ; Setting bit at p2 position to 1 ; value1 and value2 will have 0 if num at the respective positions - p1 and p2 is 0. ; check if value1 and value2 are different i . e . at one position bit is set and other it is not ; if bit at p1 position is set ; unset bit at p1 position ; set bit at p2 position ; if bit at p2 position is set ; set bit at p2 position ; unset bit at p2 position ; return final result ; Driver code
using System ; class GFG { static int swapBits ( int num , int p1 , int p2 , int n ) { int shift1 , shift2 , value1 , value2 ; while ( n -- > 0 ) { shift1 = 1 << p1 ; shift2 = 1 << p2 ; value1 = ( ( num & shift1 ) ) ; value2 = ( ( num & shift2 ) ) ; if ( ( value1 == 0 && value2 != 0 ) || ( value2 == 0 && value1 != 0 ) ) { if ( value1 != 0 ) { num = num & ( ~ shift1 ) ; num = num | shift2 ; } else { num = num & ( ~ shift2 ) ; num = num | shift1 ; } } p1 ++ ; p2 ++ ; } return num ; } static void Main ( ) { int res = swapBits ( 28 , 0 , 3 , 2 ) ; Console . WriteLine ( " Result ▁ = ▁ " + res ) ; } }
Smallest of three integers without comparison operators | C # program to find Smallest of three integers without comparison operators ; Driver Code
using System ; class GFG { static int smallest ( int x , int y , int z ) { int c = 0 ; while ( x != 0 && y != 0 && z != 0 ) { x -- ; y -- ; z -- ; c ++ ; } return c ; } public static void Main ( ) { int x = 12 , y = 15 , z = 5 ; Console . Write ( " Minimum ▁ of ▁ 3" + " ▁ numbers ▁ is ▁ " + smallest ( x , y , z ) ) ; } }
Smallest of three integers without comparison operators | C # implementation of above approach ; Function to find minimum of x and y ; Function to find minimum of 3 numbers x , y and z ; Driver code
using System ; class GFG { static int CHAR_BIT = 8 ; static int min ( int x , int y ) { return y + ( ( x - y ) & ( ( x - y ) >> ( sizeof ( int ) * CHAR_BIT - 1 ) ) ) ; } static int smallest ( int x , int y , int z ) { return Math . Min ( x , Math . Min ( y , z ) ) ; } static void Main ( ) { int x = 12 , y = 15 , z = 5 ; Console . WriteLine ( " Minimum ▁ of ▁ 3 ▁ numbers ▁ is ▁ " + smallest ( x , y , z ) ) ; } }
Smallest of three integers without comparison operators | C # program of above approach ; Using division operator to find minimum of three numbers ; Same as " if ▁ ( y ▁ < ▁ x ) " ; Driver code
using System ; public class GfG { static int smallest ( int x , int y , int z ) { if ( ( y / x ) != 1 ) return ( ( y / z ) != 1 ) ? y : z ; return ( ( x / z ) != 1 ) ? x : z ; } public static void Main ( ) { int x = 78 , y = 88 , z = 68 ; Console . Write ( " Minimum ▁ of ▁ 3 ▁ numbers " + " ▁ is ▁ { 0 } " , smallest ( x , y , z ) ) ; } }
Next higher number with same number of set bits | C # Implementation on above approach ; this function returns next higher number with same number of set bits as x . ; right most set bit ; reset the pattern and set next higher bit left part of x will be here ; nextHigherOneBit is now part [ D ] of the above explanation . isolate the pattern ; right adjust pattern ; correction factor ; rightOnesPattern is now part [ A ] of the above explanation . integrate new pattern ( Add [ D ] and [ A ] ) ; Driver code
using System ; class GFG { static int snoob ( int x ) { int rightOne , nextHigherOneBit , rightOnesPattern , next = 0 ; if ( x > 0 ) { rightOne = x & - x ; nextHigherOneBit = x + rightOne ; rightOnesPattern = x ^ nextHigherOneBit ; rightOnesPattern = ( rightOnesPattern ) / rightOne ; rightOnesPattern >>= 2 ; next = nextHigherOneBit | rightOnesPattern ; } return next ; } static void Main ( ) { int x = 156 ; Console . WriteLine ( " Next ▁ higher ▁ number ▁ with ▁ same " + " number ▁ of ▁ set ▁ bits ▁ is ▁ " + snoob ( x ) ) ; } }
Add 1 to a given number | C # code to add one to a given number ; Flip all the set bits until we find a 0 ; flip the rightmost 0 bit ; Driver code
using System ; class GFG { static int addOne ( int x ) { int m = 1 ; while ( ( int ) ( x & m ) == 1 ) { x = x ^ m ; m <<= 1 ; } x = x ^ m ; return x ; } public static void Main ( ) { Console . WriteLine ( addOne ( 13 ) ) ; } }
Add 1 to a given number | C # code to Add 1 to a given number ; Driver program
using System ; class GFG { static int addOne ( int x ) { return ( - ( ~ x ) ) ; } public static void Main ( ) { Console . WriteLine ( addOne ( 13 ) ) ; } }
Multiply a given Integer with 3.5 | C # Program to multiply a number with 3.5 ; Driver program to test above functions
using System ; class GFG { static int multiplyWith3Point5 ( int x ) { return ( x << 1 ) + x + ( x >> 1 ) ; } public static void Main ( ) { int x = 2 ; Console . Write ( multiplyWith3Point5 ( x ) ) ; } }
Multiply a given Integer with 3.5 | C # program for above approach ; Function to multiple number with 3.5 ; The 3.5 is 7 / 2 , so multiply by 7 ( x * 7 ) then divide the result by 2 ( result / 2 ) x * 7 -> 7 is 0111 so by doing mutiply by 7 it means we do 2 shifting for the number but since we doing multiply we need to take care of carry one . ; Then divide by 2 r / 2 ; Driver Code
using System ; class GFG { static int multiplyWith3Point5 ( int x ) { int r = 0 ; int x1Shift = x << 1 ; int x2Shifts = x << 2 ; r = ( x ^ x1Shift ) ^ x2Shifts ; int c = ( x & x1Shift ) | ( x & x2Shifts ) | ( x1Shift & x2Shifts ) ; while ( c > 0 ) { c <<= 1 ; int t = r ; r ^= c ; c &= t ; } r = r >> 1 ; return r ; } public static void Main ( string [ ] args ) { Console . WriteLine ( multiplyWith3Point5 ( 5 ) ) ; } }
Turn off the rightmost set bit | C # program to unset the rightmost set bit of an integer . ; unsets the rightmost set bit of n and returns the result ; Driver code
using System ; class GFG { static int fun ( int n ) { return n & ( n - 1 ) ; } public static void Main ( ) { int n = 7 ; Console . Write ( " The ▁ number ▁ after ▁ unsetting ▁ " + " the ▁ rightmost ▁ set ▁ bit ▁ " + fun ( n ) ) ; } }
Find whether a given number is a power of 4 or not | C # code to check if given number is power of 4 or not ; Function to check if x is power of 4 ; Driver code
using System ; class GFG { static int isPowerOfFour ( int n ) { if ( n == 0 ) return 0 ; while ( n != 1 ) { if ( n % 4 != 0 ) return 0 ; n = n / 4 ; } return 1 ; } public static void Main ( ) { int test_no = 64 ; if ( isPowerOfFour ( test_no ) == 1 ) Console . Write ( test_no + " ▁ is ▁ a ▁ power ▁ of ▁ 4" ) ; else Console . Write ( test_no + " ▁ is ▁ not ▁ a ▁ power ▁ of ▁ 4" ) ; } }
Find whether a given number is a power of 4 or not | C # program to check if given number is power of 4 or not ; Function to check if x is power of 4 ; Check if there is only one bit set in n ; count 0 bits before set bit ; If count is even then return true else false ; If there are more than 1 bit set then n is not a power of 4 ; Driver program to test above function
using System ; class GFG { static int isPowerOfFour ( int n ) { int count = 0 ; int x = n & ( n - 1 ) ; if ( n > 0 && x == 0 ) { while ( n > 1 ) { n >>= 1 ; count += 1 ; } return ( count % 2 == 0 ) ? 1 : 0 ; } return 0 ; } static void Main ( ) { int test_no = 64 ; if ( isPowerOfFour ( test_no ) > 0 ) Console . WriteLine ( " { 0 } ▁ is ▁ a ▁ power ▁ of ▁ 4" , test_no ) ; else Console . WriteLine ( " { 0 } ▁ is ▁ not ▁ a ▁ power ▁ of ▁ 4" , test_no ) ; } }
Find whether a given number is a power of 4 or not | C # program to check if given number is power of 4 or not ; Driver Code
using System ; class GFG { static bool isPowerOfFour ( int n ) { return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) && ( n & 0xAAAAAAAA ) == 0 ; } static void Main ( ) { int test_no = 64 ; if ( isPowerOfFour ( test_no ) ) Console . WriteLine ( " { 0 } ▁ is ▁ a ▁ power ▁ of ▁ 4" , test_no ) ; else Console . WriteLine ( " { 0 } ▁ is ▁ not ▁ a ▁ power ▁ of ▁ 4" , test_no ) ; } }
Find whether a given number is a power of 4 or not | C # program to check if given number is power of 4 or not ; 0 is not considered as a power of 4 ; Driver code
using System ; class GFG { static double logn ( int n , int r ) { return Math . Log ( n ) / Math . Log ( r ) ; } static bool isPowerOfFour ( int n ) { if ( n == 0 ) return false ; return Math . Floor ( logn ( n , 4 ) ) == Math . Ceiling ( logn ( n , 4 ) ) ; } public static void Main ( String [ ] args ) { int test_no = 64 ; if ( isPowerOfFour ( test_no ) ) Console . Write ( test_no + " ▁ is ▁ a ▁ power ▁ of ▁ 4" ) ; else Console . Write ( test_no + " ▁ is ▁ not ▁ a ▁ power ▁ of ▁ 4" ) ; } }
Compute the minimum or maximum of two integers without branching | C # program to Compute the minimum or maximum of two integers without branching ; Function to find minimum of x and y ; Function to find maximum of x and y ; Driver program to test above functions
using System ; public class AWS { public static int min ( int x , int y ) { return y ^ ( ( x ^ y ) & - ( x << y ) ) ; } public static int max ( int x , int y ) { return x ^ ( ( x ^ y ) & - ( x << y ) ) ; } public static void Main ( string [ ] args ) { int x = 15 ; int y = 6 ; Console . Write ( " Minimum ▁ of ▁ " + x + " ▁ and ▁ " + y + " ▁ is ▁ " ) ; Console . WriteLine ( min ( x , y ) ) ; Console . Write ( " Maximum ▁ of ▁ " + x + " ▁ and ▁ " + y + " ▁ is ▁ " ) ; Console . WriteLine ( max ( x , y ) ) ; } }
Compute the minimum or maximum of two integers without branching | C # implementation of above approach ; Function to find minimum of x and y ; Function to find maximum of x and y ; Driver code
using System ; class GFG { static int CHAR_BIT = 8 ; static int min ( int x , int y ) { return y + ( ( x - y ) & ( ( x - y ) >> ( sizeof ( int ) * CHAR_BIT - 1 ) ) ) ; } static int max ( int x , int y ) { return x - ( ( x - y ) & ( ( x - y ) >> ( sizeof ( int ) * CHAR_BIT - 1 ) ) ) ; } static void Main ( ) { int x = 15 ; int y = 6 ; Console . WriteLine ( " Minimum ▁ of ▁ " + x + " ▁ and ▁ " + y + " ▁ is ▁ " + min ( x , y ) ) ; Console . WriteLine ( " Maximum ▁ of ▁ " + x + " ▁ and ▁ " + y + " ▁ is ▁ " + max ( x , y ) ) ; } }