codeparrot/xlcost-text-to-code huggingface dataset:
10 's Complement of a decimal number | C # program to find 10 's complement ; Function to find 10 's complement ; Calculating total digits in num ; restore num ; calculate 10 's complement ; Driver code
using System ; class GFG { static int complement ( int num ) { int len = 0 , temp , comp ; temp = num ; while ( true ) { len ++ ; num = num / 10 ; if ( Math . Abs ( num ) == 0 ) break ; } num = temp ; comp = ( int ) Math . Pow ( 10 , len ) - num ; return comp ; } public static void Main ( ) { Console . WriteLine ( complement ( 25 ) ) ; Console . WriteLine ( complement ( 456 ) ) ; } }
Program to find HCF ( Highest Common Factor ) of 2 Numbers | C # program to find GCD of two numbers ; Recursive function to return gcd of a and b ; Everything divides 0 ; base case ; a is greater ; Driver method
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 && b == 0 ) return 0 ; if ( a == 0 ) return b ; if ( b == 0 ) return a ; if ( a == b ) return a ; if ( a > b ) return gcd ( a - b , b ) ; return gcd ( a , b - a ) ; } public static void Main ( ) { int a = 98 , b = 56 ; Console . WriteLine ( " GCD ▁ of ▁ " + a + " ▁ and ▁ " + b + " ▁ is ▁ " + gcd ( a , b ) ) ; } }
Number of sub arrays with odd sum | C # program to find number of subarrays with odd sum ; Function to find number of subarrays with odd sum ; ' odd ' stores number of odd numbers upto ith index ' c _ odd ' stores number of odd sum subarrays starting at ith index ' Result ' stores the number of odd sum subarrays ; First find number of odd sum subarrays starting at 0 th index ; Find number of odd sum subarrays starting at ith index add to result ; Driver code
using System ; public class GFG { static int countOddSum ( int [ ] a , int n ) { int c_odd = 0 , result = 0 ; bool odd = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) { odd = ! odd ; } if ( odd ) { c_odd ++ ; } } for ( int i = 0 ; i < n ; i ++ ) { result += c_odd ; if ( a [ i ] % 2 == 1 ) { c_odd = ( n - i - c_odd ) ; } } return result ; } public static void Main ( String [ ] args ) { int [ ] ar = { 5 , 4 , 4 , 5 , 1 , 3 } ; int n = ar . Length ; Console . Write ( " The ▁ Number ▁ of ▁ Subarrays ▁ " + " with ▁ odd ▁ sum ▁ is ▁ " + countOddSum ( ar , n ) ) ; } }
Calculating n | C # Program to find n - th real root of x ; Initialize boundary values ; used for taking approximations of the answer ; Do binary search ; Driver code
using System ; public class GFG { static void findNthRoot ( double x , int n ) { double low , high ; if ( x >= 0 && x <= 1 ) { low = x ; high = 1 ; } else { low = 1 ; high = x ; } double epsilon = 0.00000001 ; double guess = ( low + high ) / 2 ; while ( Math . Abs ( ( Math . Pow ( guess , n ) ) - x ) >= epsilon ) { if ( Math . Pow ( guess , n ) > x ) { high = guess ; } else { low = guess ; } guess = ( low + high ) / 2 ; } Console . WriteLine ( guess ) ; } static public void Main ( ) { double x = 5 ; int n = 2 ; findNthRoot ( x , n ) ; } }
Sum of all elements up to Nth row in a Pascal triangle | C # program to find sum of all elements upto nth row in Pascal triangle . ; Function to find sum of all elements upto nth row . ; Initialize sum with 0 ; Loop to calculate power of 2 upto n and add them ; Driver Code
using System ; public class GFG { static long calculateSum ( int n ) { long sum = 0 ; for ( int row = 0 ; row < n ; row ++ ) { sum = sum + ( 1 << row ) ; } return sum ; } static public void Main ( ) { int n = 10 ; Console . WriteLine ( " Sum ▁ of ▁ all ▁ elements : " + calculateSum ( n ) ) ; } }
Number of sequences which has HEAD at alternate positions to the right of the first HEAD | C # program to find number of sequences ; function to calculate total sequences possible ; Value of N is even ; Value of N is odd ; Driver code
using System ; public class GFG { static int findAllSequence ( int N ) { if ( N % 2 == 0 ) { return ( int ) ( Math . Pow ( 2 , N / 2 + 1 ) + Math . Pow ( 2 , N / 2 ) - 2 ) ; } else { return ( int ) ( Math . Pow ( 2 , ( N + 1 ) / 2 ) + Math . Pow ( 2 , ( N + 1 ) / 2 ) - 2 ) ; } } public static void Main ( ) { int N = 2 ; Console . WriteLine ( findAllSequence ( N ) ) ; } }
Compute power of power k times % m | C # program for computing x ^ x ^ x ^ x . . % m ; Create an array to store phi or totient values ; Function to calculate Euler Totient values ; indicates not evaluated yet and initializes for product formula . ; Compute other Phi values ; If phi [ p ] is not computed already , then number p is prime ; Phi of a prime number p is always equal to p - 1. ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function to calculate ( x ^ x ^ x ^ x ... k times ) % m ; to store different mod values ; run loop in reverse to calculate result ; Driver Code ; compute euler totient function values ; Calling function to compute answer
using System ; class GFG { static int N = 1000000 ; static long [ ] phi = new long [ N + 5 ] ; static void computeTotient ( ) { for ( int i = 1 ; i <= N ; i ++ ) phi [ i ] = i ; for ( int p = 2 ; p <= N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( int i = 2 * p ; i <= N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } static long power ( long x , long y , long p ) { while ( y > 0 ) { if ( ( y & 1 ) > 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static long calculate ( long x , long k , long mod ) { long [ ] arr = new long [ N ] ; long count = 0 ; while ( mod > 1 ) { arr [ ( int ) count ++ ] = mod ; mod = phi [ ( int ) mod ] ; } long result = 1 ; long loop = count + 1 ; arr [ ( int ) count ] = 1 ; for ( int i = ( int ) Math . Min ( k , loop ) - 1 ; i >= 0 ; i -- ) result = power ( x , result , arr [ i ] ) ; return result ; } static public void Main ( ) { computeTotient ( ) ; long x = 3 , k = 2 , m = 3 ; Console . WriteLine ( calculate ( x , k , m ) ) ; } }
Number of ones in the smallest repunit | C # program to print the number of 1 s in smallest repunit multiple of the number . ; Function to find number of 1 s in smallest repunit multiple of the number ; to store number of 1 s in smallest repunit multiple of the number . ; initialize rem with 1 ; run loop until rem becomes zero ; rem * 10 + 1 here represents the repunit modulo n ; when remainder becomes 0 return count ; Driver Code ; Calling function
using System ; class GFG { static int countOnes ( int n ) { int count = 1 ; int rem = 1 ; while ( rem != 0 ) { rem = ( rem * 10 + 1 ) % n ; count ++ ; } return count ; } static public void Main ( ) { int n = 13 ; Console . WriteLine ( countOnes ( n ) ) ; } }
Largest of two distinct numbers without using any conditional statements or operators | C # program for above implementation ; Function to find the largest number ; Driver code
using System ; class GFG { static int largestNum ( int a , int b ) { return a * ( ( a / b ) > 0 ? 1 : 0 ) + b * ( ( b / a ) > 0 ? 1 : 0 ) ; } public static void Main ( String [ ] args ) { int a = 22 , b = 1231 ; Console . Write ( largestNum ( a , b ) ) ; } }
Number of Distinct Meeting Points on a Circular Road | C # Program to find number of distinct point of meet on a circular road ; Returns the GCD of two number . ; Returns the number of distinct meeting points . ; Find the relative speed . ; convert the negative value to positive . ; Driver Code
using System ; class GFG { static int gcd ( int a , int b ) { int c = a % b ; while ( c != 0 ) { a = b ; b = c ; c = a % b ; } return b ; } static int numberOfmeet ( int a , int b ) { int ans ; if ( a > b ) ans = a - b ; else ans = b - a ; if ( a < 0 ) a = a * ( - 1 ) ; if ( b < 0 ) b = b * ( - 1 ) ; return ans / gcd ( a , b ) ; } static public void Main ( ) { int a = 1 , b = - 1 ; Console . WriteLine ( numberOfmeet ( a , b ) ) ; } }
Minimum number of Square Free Divisors | C # Program to find the minimum number of square free divisors ; Initializing MAX with SQRT ( 10 ^ 6 ) ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers ; This function returns the minimum number of Square Free divisors ; Precomputing Prime Factors ; holds max of max power of all prime factors ; holds the max power of current prime factor ; If number itself is prime , it will be included as answer and thus minimum required answer is 1 ; Driver Code to test above functions
using System ; using System . Collections . Generic ; public class GFG { static int MAX = 1005 ; static void SieveOfEratosthenes ( List < int > primes ) { bool [ ] prime = new bool [ MAX ] ; for ( int i = 0 ; i < prime . Length ; i ++ ) { prime [ i ] = true ; } for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i < MAX ; i += p ) { prime [ i ] = false ; } } } for ( int p = 2 ; p < MAX ; p ++ ) { if ( prime [ p ] ) { primes . Add ( p ) ; } } } static int minimumSquareFreeDivisors ( int N ) { List < int > primes = new List < int > ( ) ; SieveOfEratosthenes ( primes ) ; int max_count = 0 ; for ( int i = 0 ; i < primes . Count && primes [ i ] * primes [ i ] <= N ; i ++ ) { if ( N % primes [ i ] == 0 ) { int tmp = 0 ; while ( N % primes [ i ] == 0 ) { tmp ++ ; N /= primes [ i ] ; } max_count = Math . Max ( max_count , tmp ) ; } } if ( max_count == 0 ) { max_count = 1 ; } return max_count ; } public static void Main ( ) { int N = 24 ; Console . WriteLine ( " Minimum ▁ Number ▁ of ▁ Square ▁ Free ▁ Divisors ▁ is ▁ " + minimumSquareFreeDivisors ( N ) ) ; N = 6 ; Console . WriteLine ( " Minimum ▁ Number ▁ of ▁ Square ▁ Free ▁ Divisors ▁ is ▁ " + minimumSquareFreeDivisors ( N ) ) ; } }
Subsequence of size k with maximum possible GCD | C # program to find subsequence of size k with maximum possible GCD ; function to find GCD of sub sequence of size k with max GCD in the array ; Computing highest element ; Array to store the count of divisors i . e . Potential GCDs ; Iterating over every element ; Calculating all the divisors ; Divisor found ; Incrementing count for divisor ; Element / divisor is also a divisor Checking if both divisors are not same ; Checking the highest potential GCD ; If this divisor can divide at least k numbers , it is a GCD of at least one sub sequence of size k ; Driver code ; Array in which sub sequence with size k with max GCD is to be found
using System ; using System . Linq ; public class GFG { static int findMaxGCD ( int [ ] arr , int n , int k ) { int high = arr . Max ( ) ; int [ ] divisors = new int [ high + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j <= Math . Sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % j == 0 ) { divisors [ j ] ++ ; if ( j != arr [ i ] / j ) divisors [ arr [ i ] / j ] ++ ; } } } for ( int i = high ; i >= 1 ; i -- ) if ( divisors [ i ] >= k ) return i ; return 0 ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 8 , 8 , 12 } ; int k = 3 ; int n = arr . Length ; Console . WriteLine ( findMaxGCD ( arr , n , k ) ) ; } }
System of Linear Equations in three variables using Cramer 's Rule | C # program to calculate solutions of linear equations using cramer 's rule ; This functions finds the determinant of Matrix ; This function finds the solution of system of linear equations using cramer 's rule ; Matrix d using coeff as given in cramer 's rule ; Matrix d1 using coeff as given in cramer 's rule ; Matrix d2 using coeff as given in cramer 's rule ; Matrix d3 using coeff as given in cramer 's rule ; Calculating Determinant of Matrices d , d1 , d2 , d3 ; Case 1 ; Coeff have a unique solution . Apply Cramer 's Rule ; double z = D3 / D ; calculating z using cramer 's rule ; Case 2 ; Driver Code ; storing coefficients of linear equations in coeff matrix
using System ; class GFG { static double determinantOfMatrix ( double [ , ] mat ) { double ans ; ans = mat [ 0 , 0 ] * ( mat [ 1 , 1 ] * mat [ 2 , 2 ] - mat [ 2 , 1 ] * mat [ 1 , 2 ] ) - mat [ 0 , 1 ] * ( mat [ 1 , 0 ] * mat [ 2 , 2 ] - mat [ 1 , 2 ] * mat [ 2 , 0 ] ) + mat [ 0 , 2 ] * ( mat [ 1 , 0 ] * mat [ 2 , 1 ] - mat [ 1 , 1 ] * mat [ 2 , 0 ] ) ; return ans ; } static void findSolution ( double [ , ] coeff ) { double [ , ] d = { { coeff [ 0 , 0 ] , coeff [ 0 , 1 ] , coeff [ 0 , 2 ] } , { coeff [ 1 , 0 ] , coeff [ 1 , 1 ] , coeff [ 1 , 2 ] } , { coeff [ 2 , 0 ] , coeff [ 2 , 1 ] , coeff [ 2 , 2 ] } , } ; double [ , ] d1 = { { coeff [ 0 , 3 ] , coeff [ 0 , 1 ] , coeff [ 0 , 2 ] } , { coeff [ 1 , 3 ] , coeff [ 1 , 1 ] , coeff [ 1 , 2 ] } , { coeff [ 2 , 3 ] , coeff [ 2 , 1 ] , coeff [ 2 , 2 ] } , } ; double [ , ] d2 = { { coeff [ 0 , 0 ] , coeff [ 0 , 3 ] , coeff [ 0 , 2 ] } , { coeff [ 1 , 0 ] , coeff [ 1 , 3 ] , coeff [ 1 , 2 ] } , { coeff [ 2 , 0 ] , coeff [ 2 , 3 ] , coeff [ 2 , 2 ] } , } ; double [ , ] d3 = { { coeff [ 0 , 0 ] , coeff [ 0 , 1 ] , coeff [ 0 , 3 ] } , { coeff [ 1 , 0 ] , coeff [ 1 , 1 ] , coeff [ 1 , 3 ] } , { coeff [ 2 , 0 ] , coeff [ 2 , 1 ] , coeff [ 2 , 3 ] } , } ; double D = determinantOfMatrix ( d ) ; double D1 = determinantOfMatrix ( d1 ) ; double D2 = determinantOfMatrix ( d2 ) ; double D3 = determinantOfMatrix ( d3 ) ; Console . Write ( " D ▁ is ▁ : ▁ { 0 : F6 } ▁ STRNEWLINE " , D ) ; Console . Write ( " D1 ▁ is ▁ : ▁ { 0 : F6 } ▁ STRNEWLINE " , D1 ) ; Console . Write ( " D2 ▁ is ▁ : ▁ { 0 : F6 } ▁ STRNEWLINE " , D2 ) ; Console . Write ( " D3 ▁ is ▁ : ▁ { 0 : F6 } ▁ STRNEWLINE " , D3 ) ; if ( D != 0 ) { double x = D1 / D ; double y = D2 / D ; Console . Write ( " Value ▁ of ▁ x ▁ is ▁ : ▁ { 0 : F6 } STRNEWLINE " , x ) ; Console . Write ( " Value ▁ of ▁ y ▁ is ▁ : ▁ { 0 : F6 } STRNEWLINE " , y ) ; Console . Write ( " Value ▁ of ▁ z ▁ is ▁ : ▁ { 0 : F6 } STRNEWLINE " , z ) ; } else { if ( D1 == 0 && D2 == 0 && D3 == 0 ) Console . Write ( " Infinite ▁ solutions STRNEWLINE " ) ; else if ( D1 != 0 D2 != 0 D3 != 0 ) Console . Write ( " No ▁ solutions STRNEWLINE " ) ; } } public static void Main ( ) { double [ , ] coeff = { { 2 , - 1 , 3 , 9 } , { 1 , 1 , 1 , 6 } , { 1 , - 1 , 1 , 2 } } ; findSolution ( coeff ) ; } }
Find larger of x ^ y and y ^ x | C # program to print greater of x ^ y and y ^ x ; Driver Code
using System ; class GFG { static void printGreater ( int x , int y ) { double X = y * Math . Log ( x ) ; double Y = x * Math . Log ( y ) ; if ( Math . Abs ( X - Y ) < 1e-9 ) { Console . WriteLine ( " Equal " ) ; } else if ( X > Y ) { Console . WriteLine ( x + " ^ " + y ) ; } else { Console . WriteLine ( y + " ^ " + x ) ; } } public static void Main ( ) { int x = 5 , y = 8 ; printGreater ( x , y ) ; } }
n | C # program to find the n - th term in series ; Function to find nth term ; Driver Code
using System ; class Series { static int sumOfSeries ( int n ) { return n * ( n + 1 ) * ( 6 * n * n * n + 9 * n * n + n - 1 ) / 30 ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( sumOfSeries ( n ) ) ; } }
Product of first N factorials | C # Program to find the product of first N factorials ; To compute ( a * b ) % MOD ; long res = 0 ; Initialize result ; If b is odd , add ' a ' to result ; Multiply ' a ' with 2 ; Divide b by 2 ; Return result ; This function computes factorials and product by using above function i . e . modular multiplication ; Initialize product and fact with 1 ; ith factorial ; product of first i factorials ; If at any iteration , product becomes divisible by MOD , simply return 0 ; ; Driver Code to Test above functions
using System ; public class GFG { static double mulmod ( long a , long b , long mod ) { a = a % mod ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( res + a ) % mod ; a = ( a * 2 ) % mod ; b /= 2 ; } return res % mod ; } static long findProduct ( long N ) { long product = 1 , fact = 1 ; long MOD = ( long ) ( 1e9 + 7 ) ; for ( int i = 1 ; i <= N ; i ++ ) { fact = ( long ) mulmod ( fact , i , MOD ) ; product = ( long ) mulmod ( product , fact , MOD ) ; if ( product == 0 ) return 0 ; } return product ; } static public void Main ( ) { long N = 3 ; Console . WriteLine ( findProduct ( N ) ) ; N = 5 ; Console . WriteLine ( findProduct ( N ) ) ; } }
Check if sum of divisors of two numbers are same | C # program to find if two numbers are equivalent or not ; Function to calculate sum of all proper divisors num -- > given natural number ; To store sum of divisors ; Find all divisors and add them ; Function to check if both numbers are equivalent or not ; Driver code
using System ; class GFG { static int divSum ( int n ) { int sum = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) sum = sum + i + n / i ; return sum ; } static bool areEquivalent ( int num1 , int num2 ) { return divSum ( num1 ) == divSum ( num2 ) ; } static public void Main ( ) { int num1 = 559 ; int num2 = 703 ; if ( areEquivalent ( num1 , num2 ) ) Console . WriteLine ( " Equivalent " ) ; else Console . WriteLine ( " Not ▁ Equivalent " ) ; } }
Dodecahedral number | C # program to find nth dodecahedral number ; Function to find dodecahedral number ; Formula to calculate nth dodecahedral number and return it into main function . ; Driver Code ; print result
using System ; class GFG { static int dodecahedral_num ( int n ) { return n * ( 3 * n - 1 ) * ( 3 * n - 2 ) / 2 ; } public static void Main ( ) { int n = 5 ; Console . Write ( n + " the ▁ Dodecahedral " + " ▁ number : " ) ; Console . WriteLine ( dodecahedral_num ( n ) ) ; } }
Count of divisors having more set bits than quotient on dividing N | C # Program to find number of Divisors which on integer division produce quotient having less set bit than divisor ; Return the count of set bit . ; check if q and d have same number of set bit . ; Binary Search to find the point at which number of set in q is less than or equal to d . ; while left index is less than right index ; finding the middle . ; check if q and d have same number of set it or not . ; Driver Code
using System ; class GFG { static int bit ( int x ) { int ans = 0 ; while ( x > 0 ) { x /= 2 ; ans ++ ; } return ans ; } static bool check ( int d , int x ) { if ( bit ( x / d ) <= bit ( d ) ) return true ; return false ; } static int bs ( int n ) { int l = 1 , r = ( int ) Math . Sqrt ( n ) ; while ( l < r ) { int m = ( l + r ) / 2 ; if ( check ( m , n ) ) r = m ; else l = m + 1 ; } if ( ! check ( l , n ) ) return l + 1 ; else return l ; } static int countDivisor ( int n ) { return n - bs ( n ) + 1 ; } static public void Main ( ) { int n = 5 ; Console . WriteLine ( countDivisor ( n ) ) ; } }
Check if two people starting from different points ever meet | C # program to find if two people starting from different positions ever meet or not . ; If speed of a person at a position before other person is smaller , then return false . ; Making sure that x1 is greater ; Until one person crosses other ; first person taking one jump in each iteration ; second person taking one jump in each iteration ; Driver code
using System ; class GFG { static void swap ( ref int a , ref int b ) { int t = a ; a = b ; b = t ; } static bool everMeet ( int x1 , int x2 , int v1 , int v2 ) { if ( x1 < x2 && v1 <= v2 ) return false ; if ( x1 > x2 && v1 >= v2 ) return false ; if ( x1 < x2 ) { swap ( ref x1 , ref x2 ) ; swap ( ref v1 , ref v2 ) ; } while ( x1 >= x2 ) { if ( x1 == x2 ) return true ; x1 = x1 + v1 ; x2 = x2 + v2 ; } return false ; } static void Main ( ) { int x1 = 5 , v1 = 8 , x2 = 4 , v2 = 7 ; if ( everMeet ( x1 , x2 , v1 , v2 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Divisibility by 3 where each digit is the sum of all prefix digits modulo 10 | C # code to check divisibility by 3 ; Function to check the divisibility ; Cycle ; no of residual terms ; sum of residual terms ; if no of residue term = 0 ; if no of residue term = 1 ; if no of residue term = 2 ; if no of residue term = 3 ; sum of all digits ; divisibility check ; Driver Code
using System ; class GFG { static String check ( int k , int d0 , int d1 ) { int s = ( 2 * ( d0 + d1 ) ) % 10 + ( 4 * ( d0 + d1 ) ) % 10 + ( 8 * ( d0 + d1 ) ) % 10 + ( 6 * ( d0 + d1 ) ) % 10 ; int a = ( k - 3 ) % 4 ; int x = 0 ; switch ( a ) { case 0 : x = 0 ; break ; case 1 : x = ( 2 * ( d0 + d1 ) ) % 10 ; break ; case 2 : x = ( 2 * ( d0 + d1 ) ) % 10 + ( 4 * ( d0 + d1 ) ) % 10 ; break ; case 3 : x = ( 2 * ( d0 + d1 ) ) % 10 + ( 4 * ( d0 + d1 ) ) % 10 + ( 8 * ( d0 + d1 ) ) % 10 ; break ; } int sum = d0 + d1 + ( ( ( k - 3 ) / 4 ) * s + x ) ; if ( sum % 3 == 0 ) return " YES " ; return " NO " ; } static public void Main ( ) { int k , d0 , d1 ; k = 13 ; d0 = 8 ; d1 = 1 ; Console . WriteLine ( check ( k , d0 , d1 ) ) ; k = 5 ; d0 = 3 ; d1 = 4 ; Console . WriteLine ( check ( k , d0 , d1 ) ) ; } }
Find ceil of a / b without using ceil ( ) function | C # program to find ceil ( a / b ) without using ceil ( ) function ; Driver Code ; taking input 1 ; example of perfect division taking input 2
using System ; class GFG { static void Main ( ) { int a = 4 ; int b = 3 , val = 0 ; if ( ( a % b ) != 0 ) val = ( a / b ) + ( a % b ) ; else val = ( a / b ) ; Console . WriteLine ( " The ▁ ceiling ▁ " + " value ▁ of ▁ 4/3 ▁ is ▁ " + val ) ; a = 6 ; b = 3 ; if ( ( a % b ) != 0 ) val = ( a / b ) + ( a % b ) ; else val = ( a / b ) ; Console . WriteLine ( " The ▁ ceiling ▁ " + " value ▁ of ▁ 6/3 ▁ is ▁ " + val ) ; } }
Program to print Collatz Sequence | C # program to print Collatz sequence ; We simply follow steps while we do not reach 1 ; If n is odd ; If even ; Print 1 at the end ; Driver code
using System ; class GFG { static void printCollatz ( int n ) { while ( n != 1 ) { Console . Write ( n + " ▁ " ) ; if ( ( n & 1 ) == 1 ) n = 3 * n + 1 ; else n = n / 2 ; } Console . Write ( n ) ; } static void Main ( ) { printCollatz ( 6 ) ; } }
Powers of 2 to required sum | C # program to find the blocks for given number . ; Convert decimal number to its binary equivalent ; Displaying the output when the bit is '1' in binary equivalent of number . ; Driver Code here
using System ; using System . Collections . Generic ; class GFG { static void block ( long x ) { List < int > v = new List < int > ( ) ; Console . Write ( " Blocks ▁ for ▁ " + x + " ▁ : ▁ " ) ; while ( x > 0 ) { v . Add ( ( int ) x % 2 ) ; x = x / 2 ; } for ( int i = 0 ; i < v . Count ; i ++ ) { if ( v [ i ] == 1 ) { Console . Write ( i ) ; if ( i != v . Count - 1 ) Console . Write ( " , ▁ " ) ; } } Console . WriteLine ( ) ; } public static void Main ( ) { block ( 71307 ) ; block ( 1213 ) ; block ( 29 ) ; block ( 100 ) ; } }
Given a number N in decimal base , find number of its digits in any base ( base b ) | C # program to Find Number of digits in base b . ; function to print number of digits ; Calculating log using base changing property and then taking it floor and then adding 1. ; printing output ; Driver method ; taking inputs ; calling the method
using System ; class GFG { static void findNumberOfDigits ( long n , int b ) { int dig = ( int ) ( Math . Floor ( Math . Log ( n ) / Math . Log ( b ) ) + 1 ) ; Console . Write ( " The ▁ Number ▁ of ▁ digits " + " ▁ of ▁ Number ▁ " + n + " ▁ in ▁ base ▁ " + b + " ▁ is ▁ " + dig ) ; } public static void Main ( ) { long n = 1446 ; int b = 7 ; findNumberOfDigits ( n , b ) ; } }
Nesbitt 's Inequality | C # code to verify Nesbitt 's Inequality ; 3 parts of the inequality sum ; Driver code
using System ; class GFG { static bool isValidNesbitt ( double a , double b , double c ) { double A = a / ( b + c ) ; double B = b / ( a + c ) ; double C = c / ( a + b ) ; double inequality = A + B + C ; return ( inequality >= 1.5 ) ; } static public void Main ( ) { double a = 1.0 , b = 2.0 , c = 3.0 ; if ( isValidNesbitt ( a , b , c ) == true ) { Console . Write ( " Nesbitt ' s ▁ inequality " + " ▁ satisfied ▁ " ) ; Console . WriteLine ( " for ▁ real ▁ numbers ▁ " + a + " , ▁ " + b + " , ▁ " + c ) ; } else Console . WriteLine ( " Nesbitts ▁ inequality " + " ▁ not ▁ satisfied " ) ; } }