codeparrot/xlcost-text-to-code huggingface dataset:
Find Cube Pairs | Set 2 ( A n ^ ( 1 / 3 ) Solution ) | C # program to find pairs that can represent the given number as sum of two cubes ; Function to find pairs that can represent the given number as sum of two cubes ; find cube root of n ; create a array of size of size ' cubeRoot ' ; for index i , cube [ i ] will contain i ^ 3 ; Find all pairs in above sorted array cube [ ] whose sum is equal to n ; Driver Code
using System ; class GFG { static void findPairs ( int n ) { int cubeRoot = ( int ) Math . Pow ( n , 1.0 / 3.0 ) ; int [ ] cube = new int [ cubeRoot + 1 ] ; for ( int i = 1 ; i <= cubeRoot ; i ++ ) cube [ i ] = i * i * i ; int l = 1 ; int r = cubeRoot ; while ( l < r ) { if ( cube [ l ] + cube [ r ] < n ) l ++ ; else if ( cube [ l ] + cube [ r ] > n ) r -- ; else { Console . WriteLine ( " ( " + l + " , ▁ " + r + " ) " ) ; l ++ ; r -- ; } } } public static void Main ( ) { int n = 20683 ; findPairs ( n ) ; } }
Find Cube Pairs | Set 1 ( A n ^ ( 2 / 3 ) Solution ) | C # program to find pairs that can represent the given number as sum of two cubes ; Function to find pairs that can represent the given number as sum of two cubes ; find cube root of n ; create an empty map ; Consider all pairs such with values less than cuberoot ; find sum of current pair ( x , y ) ; do nothing if sum is not equal to given number ; if sum is seen before , we found two pairs ; if sum is seen for the first time ; Driver code
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 void findPairs ( int n ) { int cubeRoot = ( int ) Math . Pow ( n , 1.0 / 3.0 ) ; Dictionary < int , pair > s = new Dictionary < int , pair > ( ) ; for ( int x = 1 ; x < cubeRoot ; x ++ ) { for ( int y = x + 1 ; y <= cubeRoot ; y ++ ) { int sum = x * x * x + y * y * y ; if ( sum != n ) continue ; if ( s . ContainsKey ( sum ) ) { Console . Write ( " ( " + s [ sum ] . first + " , ▁ " + s [ sum ] . second + " ) ▁ and ▁ ( " + x + " , ▁ " + y + " ) " + " STRNEWLINE " ) ; } else s . Add ( sum , new pair ( x , y ) ) ; } } } public static void Main ( String [ ] args ) { int n = 13832 ; findPairs ( n ) ; } }
Find the minimum difference between Shifted tables of two numbers | C # program to find the minimum difference between any two terms of two tables ; Utility function to find GCD of a and b ; Returns minimum difference between any two terms of shifted tables of ' a ' and ' b ' . ' x ' is shift in table of ' a ' and ' y ' is shift in table of ' b ' . ; Calculate gcd of a nd b ; Calculate difference between x and y ; Driver Code
using System ; class GFG { static int gcd ( int a , int b ) { while ( b != 0 ) { int t = b ; b = a % b ; a = t ; } return a ; } static int findMinDiff ( int a , int b , int x , int y ) { int g = gcd ( a , b ) ; int diff = Math . Abs ( x - y ) % g ; return Math . Min ( diff , g - diff ) ; } static void Main ( ) { int a = 20 , b = 52 , x = 5 , y = 7 ; Console . WriteLine ( findMinDiff ( a , b , x , y ) ) ; } }
Find all divisors of a natural number | Set 2 | A O ( sqrt ( n ) ) C # program that prints all divisors in sorted order ; method to print the divisors ; Vector to store half of the divisors ; check if divisors are equal ; push the second divisor in the vector ; The vector will be printed in reverse ; Driver Code
using System ; class GFG { static void printDivisors ( int n ) { int [ ] v = new int [ n ] ; int t = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) Console . Write ( i + " ▁ " ) ; else { Console . Write ( i + " ▁ " ) ; v [ t ++ ] = n / i ; } } } for ( int i = t - 1 ; i >= 0 ; i -- ) Console . Write ( v [ i ] + " ▁ " ) ; } public static void Main ( ) { Console . Write ( " The ▁ divisors ▁ of ▁ 100 ▁ are : ▁ STRNEWLINE " ) ; printDivisors ( 100 ) ; } }
Find all divisors of a natural number | Set 2 | A O ( sqrt ( n ) ) program that prints all divisors in sorted order ; Function to print the divisors ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void printDivisors ( int n ) { for ( int i = 1 ; i * i < n ; i ++ ) { if ( n % i == 0 ) Console . Write ( i + " ▁ " ) ; } for ( int i = ( int ) Math . Sqrt ( n ) ; i >= 1 ; i -- ) { if ( n % i == 0 ) Console . Write ( n / i + " ▁ " ) ; } } public static void Main ( string [ ] arg ) { Console . Write ( " The ▁ divisors ▁ of ▁ 100 ▁ are : ▁ STRNEWLINE " ) ; printDivisors ( 100 ) ; } }
Find all factors of a natural number | Set 1 | C # implementation of Naive method to print all divisors ; method to print the divisors ; Driver method
using System ; class GFG { static void printDivisors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) if ( n % i == 0 ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { Console . Write ( " The ▁ divisors ▁ of " , " ▁ 100 ▁ are : ▁ " ) ; printDivisors ( 100 ) ; ; } }
Find all factors of a natural number | Set 1 | A Better ( than Naive ) Solution to find all divisors ; method to print the divisors ; Note that this loop runs till square root ; If divisors are equal , print only one ; Otherwise print both ; Driver method
using System ; class GFG { static void printDivisors ( int n ) { for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) Console . Write ( i + " ▁ " ) ; else Console . Write ( i + " ▁ " + n / i + " ▁ " ) ; } } } public static void Main ( ) { Console . Write ( " The ▁ divisors ▁ of ▁ " + "100 ▁ are : ▁ STRNEWLINE " ) ; printDivisors ( 100 ) ; } }
Sieve of Atkin | C # program for implementation of Sieve of Atkin ; 2 and 3 are known to be prime ; Initialise the sieve array with false values ; Mark sieve [ n ] is true if one of the following is true : a ) n = ( 4 * x * x ) + ( y * y ) has odd number of solutions , i . e . , there exist odd number of distinct pairs ( x , y ) that satisfy the equation and n % 12 = 1 or n % 12 = 5. b ) n = ( 3 * x * x ) + ( y * y ) has odd number of solutions and n % 12 = 7 c ) n = ( 3 * x * x ) - ( y * y ) has odd number of solutions , x > y and n % 12 = 11 ; Main part of Sieve of Atkin ; Mark all multiples of squares as non - prime ; Print primes using sieve [ ] ; Driver code
using System ; class GFG { static int SieveOfAtkin ( int limit ) { if ( limit > 2 ) Console . Write ( 2 + " ▁ " ) ; if ( limit > 3 ) Console . Write ( 3 + " ▁ " ) ; bool [ ] sieve = new bool [ limit ] ; for ( int i = 0 ; i < limit ; i ++ ) sieve [ i ] = false ; for ( int x = 1 ; x * x < limit ; x ++ ) { for ( int y = 1 ; y * y < limit ; y ++ ) { int n = ( 4 * x * x ) + ( y * y ) ; if ( n <= limit && ( n % 12 == 1 n % 12 == 5 ) ) sieve [ n ] ^= true ; n = ( 3 * x * x ) + ( y * y ) ; if ( n <= limit && n % 12 == 7 ) sieve [ n ] ^= true ; n = ( 3 * x * x ) - ( y * y ) ; if ( x > y && n <= limit && n % 12 == 11 ) sieve [ n ] ^= true ; } } for ( int r = 5 ; r * r < limit ; r ++ ) { if ( sieve [ r ] ) { for ( int i = r * r ; i < limit ; i += r * r ) sieve [ i ] = false ; } } for ( int a = 5 ; a < limit ; a ++ ) if ( sieve [ a ] ) Console . Write ( a + " ▁ " ) ; return 0 ; } public static void Main ( ) { int limit = 20 ; SieveOfAtkin ( limit ) ; } }
Find if a point lies inside a Circle | C # program to check if a point lies inside a circle or not ; Compare radius of circle with distance of its center from given point ; Driver Program to test above function
using System ; class GFG { static bool isInside ( int circle_x , int circle_y , int rad , int x , int y ) { if ( ( x - circle_x ) * ( x - circle_x ) + ( y - circle_y ) * ( y - circle_y ) <= rad * rad ) return true ; else return false ; } public static void Main ( ) { int x = 1 , y = 1 ; int circle_x = 0 , circle_y = 1 , rad = 2 ; if ( isInside ( circle_x , circle_y , rad , x , y ) ) Console . Write ( " Inside " ) ; else Console . Write ( " Outside " ) ; } }
Find all possible outcomes of a given expression | C # program to evaluate all possible values of a expression ; Utility function to evaluate a simple expression with one operator only . ; This function evaluates all possible values and returns a list of evaluated values . ; To store result ( all possible evaluations of given expression ' expr ' ) ; If there is only one character , it must be a digit ( or operand ) , return it . ; If there are only three characters , middle one must be operator and corner ones must be operand ; every i refers to an operator ; l refers to all the possible values in the left of operator ' expr [ i ] ' ; r refers to all the possible values in the right of operator ' expr [ i ] ' ; Take above evaluated all possible values in left side of ' i ' ; Take above evaluated all possible values in right side of ' i ' ; Calculate value for every pair and add the value to result . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int eval ( int a , char op , int b ) { if ( op == ' + ' ) { return a + b ; } if ( op == ' - ' ) { return a - b ; } if ( op == ' * ' ) { return a * b ; } return int . MaxValue ; } static List < int > evaluateAll ( String expr , int low , int high ) { List < int > res = new List < int > ( ) ; if ( low == high ) { res . Add ( expr [ low ] - '0' ) ; return res ; } if ( low == ( high - 2 ) ) { int num = eval ( expr [ low ] - '0' , expr [ low + 1 ] , expr [ low + 2 ] - '0' ) ; res . Add ( num ) ; return res ; } for ( int i = low + 1 ; i <= high ; i += 2 ) { List < int > l = evaluateAll ( expr , low , i - 1 ) ; List < int > r = evaluateAll ( expr , i + 1 , high ) ; for ( int s1 = 0 ; s1 < l . Count ; s1 ++ ) { for ( int s2 = 0 ; s2 < r . Count ; s2 ++ ) { int val = eval ( l [ s1 ] , expr [ i ] , r [ s2 ] ) ; res . Add ( val ) ; } } } return res ; } public static void Main ( ) { String expr = "1*2 + 3*4" ; int len = expr . Length ; List < int > ans = evaluateAll ( expr , 0 , len - 1 ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) { Console . WriteLine ( ans [ i ] ) ; } } }
Program to check if a given number is Lucky ( all digits are different ) | C # program to check if a given number is lucky ; This function returns true if n is lucky ; Create an array of size 10 and initialize all elements as false . This array is used to check if a digit is already seen or not . ; Traverse through all digits of given number ; Find the last digit ; If digit is already seen , return false ; Mark this digit as seen ; Remove the last digit from number ; Driver code
using System ; class GFG { static bool isLucky ( int n ) { bool [ ] arr = new bool [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) arr [ i ] = false ; while ( n > 0 ) { int digit = n % 10 ; if ( arr [ digit ] ) return false ; arr [ digit ] = true ; n = n / 10 ; } return true ; } public static void Main ( ) { int [ ] arr = { 1291 , 897 , 4566 , 1232 , 80 , 700 } ; int n = arr . Length ; for ( int i = 0 ; i < n ; i ++ ) if ( isLucky ( arr [ i ] ) ) Console . Write ( arr [ i ] + " ▁ is ▁ Lucky ▁ STRNEWLINE " ) ; else Console . Write ( arr [ i ] + " ▁ is ▁ not ▁ Lucky ▁ STRNEWLINE " ) ; } }
Print squares of first n natural numbers without using * , / and | C # program to print squares of first ' n ' natural numbers without using * , / and - ; Initialize ' square ' and first odd number ; Calculate and print squares ; Print square ; Update ' square ' and ' odd ' ; Driver Code
using System ; class GFG { static void printSquares ( int n ) { int square = 0 , odd = 1 ; for ( int x = 0 ; x < n ; x ++ ) { Console . Write ( square + " ▁ " ) ; square = square + odd ; odd = odd + 2 ; } } public static void Main ( ) { int n = 5 ; printSquares ( n ) ; } }
Write a program to reverse digits of a number | Recursive function to reverse digits of num ; Driver Code
using System ; class GFG { static int rev_num = 0 ; static int base_pos = 1 ; static int reversDigits ( int num ) { if ( num > 0 ) { reversDigits ( num / 10 ) ; rev_num += ( num % 10 ) * base_pos ; base_pos *= 10 ; } return rev_num ; } public static void Main ( ) { int num = 4562 ; Console . WriteLine ( reversDigits ( num ) ) ; } }
Find a number such that maximum in array is minimum possible after XOR | C # program that find the minimum possible maximum ; Recursive function that find the minimum value after exclusive - OR ; Condition if ref size is zero or bit is negative then return 0 ; Condition if current bit is off then push current value in curr_off vector ; Condition if current bit is on then push current value in curr_on vector ; Condition if curr_off is empty then call recursive function on curr_on vector ; Condition if curr_on is empty then call recursive function on curr_off vector ; Return the minimum of curr_off and curr_on and add power of 2 of current bit ; Function that print the minimum value after exclusive - OR ; Pushing values in vector ; Printing answer ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int RecursiveFunction ( List < int > re , int bit ) { if ( re . Count == 0 bit < 0 ) return 0 ; List < int > curr_on = new List < int > ( ) ; List < int > curr_off = new List < int > ( ) ; for ( int i = 0 ; i < re . Count ; i ++ ) { if ( ( ( re [ i ] >> bit ) & 1 ) == 0 ) curr_off . Add ( re [ i ] ) ; else curr_on . Add ( re [ i ] ) ; } if ( curr_off . Count == 0 ) return RecursiveFunction ( curr_on , bit - 1 ) ; if ( curr_on . Count == 0 ) return RecursiveFunction ( curr_off , bit - 1 ) ; return Math . Min ( RecursiveFunction ( curr_off , bit - 1 ) , RecursiveFunction ( curr_on , bit - 1 ) ) + ( 1 << bit ) ; } static void PrintMinimum ( int [ ] a , int n ) { List < int > v = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) v . Add ( a [ i ] ) ; Console . WriteLine ( RecursiveFunction ( v , 30 ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 } ; int size = arr . Length ; PrintMinimum ( arr , size ) ; } }
Count of elements which are equal to the XOR of the next two elements | C # implementation of the approach ; Function to return the count of elements which are equal to the XOR of the next two elements ; To store the required count ; For every element of the array such that it has at least two elements appearing after it in the array ; If current element is equal to the XOR of the next two elements in the array ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int cntElements ( int [ ] arr , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( arr [ i ] == ( arr [ i + 1 ] ^ arr [ i + 2 ] ) ) { cnt ++ ; } } return cnt ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 2 , 1 , 3 , 7 , 8 } ; int n = arr . Length ; Console . WriteLine ( cntElements ( arr , n ) ) ; } }
Number of triplets in array having subarray xor equal | C # program to find Number of triplets in array having subarray xor equal ; Function to return the count ; Initialise result ; Pick 1 st element of the triplet ; Pick 2 nd element of the triplet ; Pick 3 rd element of the triplet ; Taking xor in the first subarray ; Taking xor in the second subarray ; If both xor is equal ; Driver Code ; Function Calling
using System ; class GFG { static int xor_triplet ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j ; k < n ; k ++ ) { int xor1 = 0 , xor2 = 0 ; for ( int x = i ; x < j ; x ++ ) { xor1 ^= arr [ x ] ; } for ( int x = j ; x <= k ; x ++ ) { xor2 ^= arr [ x ] ; } if ( xor1 == xor2 ) { ans ++ ; } } } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; Console . WriteLine ( xor_triplet ( arr , n ) ) ; } }
Find the number of pair of Ideal nodes in a given tree | C # implementation of the above approach ; Adjacency list ; bit : bit array i and j are starting and ending index INCLUSIVE ; bit : bit array n : size of bit array i is the index to be updated diff is ( new_val - old_val ) ; DFS function to find ideal pairs ; Function for initialisation ; Function to add an edge ; Function to find number of ideal pairs ; Find root of the tree ; Driver code ; Add edges ; Function call
using System ; using System . Collections . Generic ; class GFG { static readonly int N = 100005 ; static int n , k ; static List < int > [ ] al = new List < int > [ N ] ; static long Ideal_pair ; static long [ ] bit = new long [ N ] ; static bool [ ] root_node = new bool [ N ] ; static long bit_q ( int i , int j ) { long sum = 0 ; while ( j > 0 ) { sum += bit [ j ] ; j -= ( j & ( j * - 1 ) ) ; } i -- ; while ( i > 0 ) { sum -= bit [ i ] ; i -= ( i & ( i * - 1 ) ) ; } return sum ; } static void bit_up ( int i , long diff ) { while ( i <= n ) { bit [ i ] += diff ; i += i & - i ; } } static void dfs ( int node ) { Ideal_pair += bit_q ( Math . Max ( 1 , node - k ) , Math . Min ( n , node + k ) ) ; bit_up ( node , 1 ) ; for ( int i = 0 ; i < al [ node ] . Count ; i ++ ) dfs ( al [ node ] [ i ] ) ; bit_up ( node , - 1 ) ; } static void initialise ( ) { Ideal_pair = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { root_node [ i ] = true ; bit [ i ] = 0 ; } } static void Add_Edge ( int x , int y ) { al [ x ] . Add ( y ) ; root_node [ y ] = false ; } static long Idealpairs ( ) { int r = - 1 ; for ( int i = 1 ; i <= n ; i ++ ) if ( root_node [ i ] ) { r = i ; break ; } dfs ( r ) ; return Ideal_pair ; } public static void Main ( String [ ] args ) { n = 6 ; k = 3 ; for ( int i = 0 ; i < al . Length ; i ++ ) al [ i ] = new List < int > ( ) ; initialise ( ) ; Add_Edge ( 1 , 2 ) ; Add_Edge ( 1 , 3 ) ; Add_Edge ( 3 , 4 ) ; Add_Edge ( 3 , 5 ) ; Add_Edge ( 3 , 6 ) ; Console . Write ( Idealpairs ( ) ) ; } }
Print bitwise AND set of a number N | C # program to print all bitwise subsets of N ( Efficient approach ) ; function to find bitwise subsets Efficient approach ; Driver Code
using System ; public class GFG { static void printSubsets ( int n ) { for ( int i = n ; i > 0 ; i = ( i - 1 ) & n ) Console . Write ( i + " ▁ " ) ; Console . WriteLine ( "0" ) ; } static public void Main ( ) { int n = 9 ; printSubsets ( n ) ; } }
Check if a number is divisible by 17 using bitwise operators | C # program to check if a number is divisible by 17 or not using bitwise operator . ; function to check recursively if the number is divisible by 17 or not ; if n = 0 or n = 17 then yes ; if n is less then 17 , not divisible by 17 ; reducing the number by floor ( n / 16 ) - n % 16 ; Driver function
using System ; class GFG { static bool isDivisibleby17 ( int n ) { if ( n == 0 n == 17 ) return true ; if ( n < 17 ) return false ; return isDivisibleby17 ( ( int ) ( n >> 4 ) - ( int ) ( n & 15 ) ) ; } public static void Main ( ) { int n = 35 ; if ( isDivisibleby17 ( n ) == true ) Console . WriteLine ( n + " is ▁ divisible ▁ by ▁ 17" ) ; else Console . WriteLine ( n + " ▁ is ▁ not ▁ divisible ▁ by ▁ 17" ) ; } }
Largest number with binary representation is m 1 's and m | C # program to find largest number smaller than equal to n with m set bits then m - 1 0 bits . ; Returns largest number with m set bits then m - 1 0 bits . ; Start with 2 bits . ; initial answer is 1 which meets the given condition ; check for all numbers ; compute the number ; if less then N ; increment m to get the next number ; Driver Code
using System ; class GFG { static long answer ( long n ) { long m = 2 ; long ans = 1 ; long r = 1 ; while ( r < n ) { r = ( ( long ) Math . Pow ( 2 , m ) - 1 ) * ( ( long ) Math . Pow ( 2 , m - 1 ) ) ; if ( r < n ) ans = r ; m ++ ; } return ans ; } static public void Main ( ) { long n = 7 ; Console . WriteLine ( answer ( n ) ) ; } }
Find most significant set bit of a number | Simple C # program to find MSB number for given n . ; Driver code
using System ; class GFG { static int setBitNumber ( int n ) { if ( n == 0 ) return 0 ; int msb = 0 ; n = n / 2 ; while ( n != 0 ) { n = n / 2 ; msb ++ ; } return ( 1 << msb ) ; } static public void Main ( ) { int n = 0 ; Console . WriteLine ( setBitNumber ( n ) ) ; } }
Find most significant set bit of a number | C # program to find MSB number for given n . ; Suppose n is 273 ( binary is 100010001 ) . It does following 100010001 | 010001000 = 110011001 ; This makes sure 4 bits ( From MSB and including MSB ) are set . It does following 110011001 | 001100110 = 111111111 ; Increment n by 1 so that there is only one set bit which is just before original MSB . n now becomes 1000000000 ; Return original MSB after shifting . n now becomes 100000000 ; Driver code
using System ; class GFG { static int setBitNumber ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; n = n + 1 ; return ( n >> 1 ) ; } public static void Main ( ) { int n = 273 ; Console . WriteLine ( setBitNumber ( n ) ) ; } }
Count trailing zero bits using lookup table | Simple C # code for counting trailing zeros in binary representation of a number ; Driver Code
using System ; class GFG { public static int countTrailingZero ( int x ) { int count = 0 ; while ( ( x & 1 ) == 0 ) { x = x >> 1 ; count ++ ; } return count ; } static public void Main ( ) { Console . WriteLine ( countTrailingZero ( 11 ) ) ; } }
Count trailing zero bits using lookup table | C # code for counting trailing zeros in binary representation of a number ; Map a bit value mod 37 to its position ; Only difference between ( x and - x ) is the value of signed magnitude ( leftmostbit ) negative numbers signed bit is 1 ; Driver Code
using System ; class GFG { static int countTrailingZero ( int x ) { int [ ] lookup = { 32 , 0 , 1 , 26 , 2 , 23 , 27 , 0 , 3 , 16 , 24 , 30 , 28 , 11 , 0 , 13 , 4 , 7 , 17 , 0 , 25 , 22 , 31 , 15 , 29 , 10 , 12 , 6 , 0 , 21 , 14 , 9 , 5 , 20 , 8 , 19 , 18 } ; return lookup [ ( - x & x ) % 37 ] ; } static public void Main ( ) { Console . WriteLine ( countTrailingZero ( 48 ) ) ; } }
Calculate 7 n / 8 without using division and multiplication operators | C # program to evaluate ceil ( 7 n / 8 ) without using * and ; Note the inner bracket here . This is needed because precedence of ' - ' operator is higher than ' < < ' ; Driver code
using System ; public class GFG { static int multiplyBySevenByEight ( int n ) { return ( n - ( n >> 3 ) ) ; } public static void Main ( ) { int n = 9 ; Console . WriteLine ( multiplyBySevenByEight ( n ) ) ; } }
Calculate 7 n / 8 without using division and multiplication operators | C # program to evaluate 7 n / 8 without using * and / ; Step 1 ) First multiply number by 7 i . e . 7 n = ( n << 3 ) - n * Step 2 ) Divide result by 8 ; Driver program
using System ; public class GFG { static int multiplyBySevenByEight ( int n ) { return ( ( n << 3 ) - n ) >> 3 ; } public static void Main ( ) { int n = 15 ; Console . WriteLine ( multiplyBySevenByEight ( n ) ) ; } }