codeparrot/xlcost-text-to-code huggingface dataset:
Binomial Random Variables | C # program to compute Binomial Probability . ; function to calculate nCr i . e . , number of ways to choose r out of n objects ; Since nCr is same as nC ( n - r ) To decrease number of iterations ; function to calculate binomial r . v . probability ; Driver code
using System ; class GFG { static int nCr ( int n , int r ) { if ( r > n / 2 ) r = n - r ; int answer = 1 ; for ( int i = 1 ; i <= r ; i ++ ) { answer *= ( n - r + i ) ; answer /= i ; } return answer ; } static float binomialProbability ( int n , int k , float p ) { return nCr ( n , k ) * ( float ) Math . Pow ( p , k ) * ( float ) Math . Pow ( 1 - p , n - k ) ; } public static void Main ( ) { int n = 10 ; int k = 5 ; float p = ( float ) 1.0 / 3 ; float probability = binomialProbability ( n , k , p ) ; Console . Write ( " Probability ▁ of ▁ " + k ) ; Console . Write ( " ▁ heads ▁ when ▁ a ▁ coin ▁ " + " is ▁ tossed ▁ " + n ) ; Console . Write ( " ▁ times ▁ where ▁ " + " probability ▁ of ▁ each ▁ head ▁ is ▁ " + p ) ; Console . Write ( " ▁ is ▁ = ▁ " + probability ) ; } }
Find pair with maximum GCD in an array | C # Code for Find pair with maximum GCD in an array ; Function to find GCD of pair 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 2 numbers , it is a GCD of at least 1 pair ; Driver Code ; Array in which pair with max GCD is to be found ; Size of array
using System ; class GFG { public static int findMaxGCD ( int [ ] arr , int n ) { int high = 0 ; for ( int i = 0 ; i < n ; i ++ ) high = Math . Max ( high , arr [ i ] ) ; 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 ] > 1 ) return i ; return 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 8 , 8 , 12 } ; int n = arr . Length ; Console . WriteLine ( findMaxGCD ( arr , n ) ) ; } }
Find pair with maximum GCD in an array | C # Code to find pair with maximum GCD in an array ; function to find GCD of pair with max max GCD in the array ; Calculating Max in array ; Maintaining count array ; Variable to store the multiples of a number ; Iterating from MAX to 1 GCD is always between MAX and 1 The first GCD found will be the highest as we are decrementing the potential GCD ; Iterating from current potential GCD till it is less than MAX ; A multiple found ; Incrementing potential GCD by itself To check i , 2 i , 3 i ... . ; 2 multiples found , max GCD found ; Driver Code ; Array in which pair with max GCD is to be found ; Size of array
using System ; class GFG { public static int findMaxGCD ( int [ ] arr , int n ) { int high = 0 ; for ( int i = 0 ; i < n ; i ++ ) high = Math . Max ( high , arr [ i ] ) ; int [ ] count = new int [ high + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; int counter = 0 ; for ( int i = high ; i >= 1 ; i -- ) { int j = i ; while ( j <= high ) { if ( count [ j ] > 0 ) counter += count [ j ] ; j += i ; if ( counter == 2 ) return i ; } counter = 0 ; } return 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 8 , 8 , 12 } ; int n = arr . Length ; Console . WriteLine ( findMaxGCD ( arr , n ) ) ; } }
Evil Number | C # program to check if a number is Evil number or Odious Number ; Returns number of 1 s from the binary number ; counting 1 s ; Check if number is evil or not ; converting n to binary form ; calculating remainder ; storing the remainders in binary form as a number ; Calling the count_one function to count and return number of 1 s in bin ; Driver Code
using System ; class GFG { static int count_one ( int n ) { int c_one = 0 ; while ( n != 0 ) { int rem = n % 10 ; if ( rem == 1 ) c_one = c_one + 1 ; n = n / 10 ; } return c_one ; } static int checkEvil ( int n ) { int i = 0 , bin = 0 , n_one = 0 ; while ( n != 0 ) { int r = n % 2 ; bin = bin + r * ( int ) ( Math . Pow ( 10 , i ) ) ; n = n / 2 ; } n_one = count_one ( bin ) ; if ( n_one % 2 == 0 ) return 1 ; else return 0 ; } public static void Main ( String [ ] args ) { int check , num ; num = 32 ; check = checkEvil ( num ) ; if ( check == 1 ) Console . WriteLine ( num + " ▁ is ▁ Evil ▁ Number " ) ; else Console . WriteLine ( num + " ▁ is ▁ Odious ▁ Number " ) ; } }
Count number of pairs ( A <= N , B <= N ) such that gcd ( A , B ) is B | C # implementation of counting pairs such that gcd ( a , b ) = b ; returns number of valid pairs ; initialize k ; loop till imin <= n ; Initialize result ; max i with given k floor ( n / k ) ; adding k * ( number of i with floor ( n / i ) = k to ans ; set imin = imax + 1 and k = n / imin ; Driver code
using System ; class GFG { static int CountPairs ( int n ) { int k = n ; int imin = 1 ; int ans = 0 ; while ( imin <= n ) { int imax = n / k ; ans += k * ( imax - imin + 1 ) ; imin = imax + 1 ; k = n / imin ; } return ans ; } public static void Main ( String [ ] args ) { Console . WriteLine ( CountPairs ( 1 ) ) ; Console . WriteLine ( CountPairs ( 2 ) ) ; Console . WriteLine ( CountPairs ( 3 ) ) ; } }
Find the last digit of given series | C # program to calculate to find last digit of above expression ; 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 ; Returns modulo inverse of a with respect to m using extended Euclid Algorithm ; q is quotient ; m is remainder now , process same as Euclid 's algo  ; Make x1 positive ; Function to calculate the above expression ; Initialize the result ; Compute first part of expression ; Compute second part of expression i . e . , ( ( 4 ^ ( n + 1 ) - 1 ) / 3 ) mod 10 Since division of 3 in modulo can ' t ▁ ▁ ▁ be ▁ performed ▁ directly ▁ therefore ▁ we ▁ ▁ ▁ need ▁ to ▁ find ▁ it ' s modulo Inverse ; Driver code
class GFG { static long powermod ( 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 modInverse ( long a , long m ) { long m0 = m , t , q ; long x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } static long evaluteExpression ( long n ) { long firstsum = 0 , mod = 10 ; for ( int i = 2 , j = 0 ; ( 1 << j ) <= n ; i *= i , ++ j ) firstsum = ( firstsum + i ) % mod ; long secondsum = ( powermod ( 4L , n + 1 , mod ) - 1 ) * modInverse ( 3L , mod ) ; return ( firstsum * secondsum ) % mod ; } public static void Main ( ) { long n = 3 ; System . Console . WriteLine ( evaluteExpression ( n ) ) ; n = 10 ; System . Console . WriteLine ( evaluteExpression ( n ) ) ; } }
Sum of all proper divisors of natural numbers in an array | C # program to find sum of proper divisors for every element in an array . ; To store prime factors and their powers ; Fills factors such that factors [ i ] is a vector of pairs containing prime factors ( of i ) and their powers . Also sets values in isPrime [ ] ; To check if a number is prime ; If i is prime , then update its powers in all multiples of it . ; Returns sum of proper divisors of num using factors [ ] ; Applying above discussed formula for every array element ; Driver code
using System ; using System . Collections . Generic ; class GFG { static readonly int MAX = 100001 ; class pair { public int F , S ; public pair ( int f , int s ) { F = f ; S = s ; } } static List < pair > [ ] factors = new List < pair > [ MAX ] ; static void sieveOfEratothenese ( ) { bool [ ] isPrime = new bool [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) isPrime [ i ] = true ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i < MAX ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i ; j < MAX ; j += i ) { int k , l ; isPrime [ j ] = false ; for ( k = j , l = 0 ; k % i == 0 ; l ++ , k /= i ) ; factors [ j ] . Add ( new pair ( i , l ) ) ; } } } } static int sumOfProperDivisors ( int num ) { int mul = 1 ; for ( int i = 0 ; i < factors [ num ] . Count ; i ++ ) mul *= ( int ) ( ( Math . Pow ( factors [ num ] [ i ] . F , factors [ num ] [ i ] . S + 1 ) - 1 ) / ( factors [ num ] [ i ] . F - 1 ) ) ; return mul - num ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < MAX ; i ++ ) factors [ i ] = new List < pair > ( ) ; sieveOfEratothenese ( ) ; int [ ] arr = { 8 , 13 , 24 , 36 , 59 , 75 , 91 } ; for ( int i = 0 ; i < arr . Length ; i ++ ) Console . Write ( sumOfProperDivisors ( arr [ i ] ) + " ▁ " ) ; Console . WriteLine ( ) ; } }
Finding power of prime number p in n ! | C # implementation of finding power of p in n ! ; Method to calculate power of prime number p in n ! ; initializing answer ; initializing ; loop until temp <= n ; add number of numbers divisible by n ; each time multiply temp by p ; Driver Code
using System ; public class GFG { static int PowerOFPINnfactorial ( int n , int p ) { int ans = 0 ; int temp = p ; while ( temp <= n ) { ans += n / temp ; temp = temp * p ; } return ans ; } public static void Main ( String [ ] args ) { Console . WriteLine ( PowerOFPINnfactorial ( 4 , 2 ) ) ; } }
Program for Decimal to Binary Conversion | C # implementation of the approach ; Function to return the binary equivalent of decimal value N ; To store the binary number ; Count used to store exponent value ; Driver code
using System ; class GFG { static int decimalToBinary ( int N ) { int B_Number = 0 ; int cnt = 0 ; while ( N != 0 ) { int rem = N % 2 ; int c = ( int ) Math . Pow ( 10 , cnt ) ; B_Number += rem * c ; N /= 2 ; cnt ++ ; } return B_Number ; } static public void Main ( ) { int N = 17 ; Console . Write ( decimalToBinary ( N ) ) ; } }
Program for Binary To Decimal Conversion | Function to convert binary to decimal ; Initializing base1 value to 1 , i . e 2 ^ 0 ; Driver Code
class GFG { public static int binaryToDecimal ( int n ) { int num = n ; int dec_value = 0 ; int base1 = 1 ; int temp = num ; while ( temp > 0 ) { int last_digit = temp % 10 ; temp = temp / 10 ; dec_value += last_digit * base1 ; base1 = base1 * 2 ; } return dec_value ; } public static void Main ( ) { int num = 10101001 ; System . Console . Write ( binaryToDecimal ( num ) ) ; } }
Calculating Factorials using Stirling Approximation | C # program for calculating factorial of a number using Stirling Approximation ; function for calculating factorial ; evaluating factorial using stirling approximation ; Driver Code
class GFG { public static int stirlingFactorial ( double n ) { if ( n == 1 ) return 1 ; double z ; z = System . Math . Sqrt ( 2 * 3.14 * n ) * System . Math . Pow ( ( n / e ) , n ) ; return ( int ) ( z ) ; } public static void Main ( ) { System . Console . WriteLine ( stirlingFactorial ( 1 ) ) ; System . Console . WriteLine ( stirlingFactorial ( 2 ) ) ; System . Console . WriteLine ( stirlingFactorial ( 3 ) ) ; System . Console . WriteLine ( stirlingFactorial ( 4 ) ) ; System . Console . WriteLine ( stirlingFactorial ( 5 ) ) ; System . Console . WriteLine ( stirlingFactorial ( 6 ) ) ; System . Console . WriteLine ( stirlingFactorial ( 7 ) ) ; } }
Count pairs with Odd XOR | C # program to count pairs in array whose XOR is odd ; A function will return number of pair whose XOR is odd ; To store count of odd and even numbers ; Increase even if number is even otherwise increase odd ; Return number of pairs ; Driver program to test countXorPair ( )
using System ; public class CountXor { static int countXorPair ( int [ ] arr , int n ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } return odd * even ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; Console . WriteLine ( countXorPair ( arr , arr . Length ) ) ; } }
Lychrel Number Implementation | C # Program to check whether the given number is Lychrel Number or not with given limit on number of iterations . ; Max Iterations ; Function to check whether number is Lychrel Number ; Function to check whether the number is Palindrome ; Function to reverse the number ; Driver program
using System ; class GFG { private static int MAX_ITERATIONS = 20 ; private static bool isLychrel ( long number ) { for ( int i = 0 ; i < MAX_ITERATIONS ; i ++ ) { number = number + reverse ( number ) ; if ( isPalindrome ( number ) ) return false ; } return true ; } private static bool isPalindrome ( long number ) { return number == reverse ( number ) ; } private static long reverse ( long number ) { long reverse = 0 ; while ( number > 0 ) { long remainder = number % 10 ; reverse = ( reverse * 10 ) + remainder ; number = number / 10 ; } return reverse ; } public static void Main ( ) { long number = 295 ; Console . Write ( number + " ▁ is ▁ lychrel ? ▁ " + isLychrel ( number ) ) ; } }
Rectangular ( or Pronic ) Numbers | C # Program to find n - th rectangular number ; Returns n - th rectangular number ; Driver code
using System ; class GFG { static int findRectNum ( int n ) { return n * ( n + 1 ) ; } public static void Main ( ) { int n = 6 ; Console . Write ( findRectNum ( n ) ) ; } }
Program for Muller Method | C # Program to find root of a function , f ( x ) ; function to calculate f ( x ) ; Taking f ( x ) = x ^ 3 + 2 x ^ 2 + 10 x - 20 ; Calculating various constants required to calculate x3 ; Taking the root which is closer to x2 ; checking for resemblance of x3 with x2 till two decimal places ; Driver main function
using System ; class Muller1 { static int MAX_ITERATIONS = 10000 ; static double f ( double x ) { return 1 * Math . Pow ( x , 3 ) + 2 * x * x + 10 * x - 20 ; } static void Muller ( double a , double b , double c ) { int i ; double res ; for ( i = 0 ; ; ++ i ) { double f1 = f ( a ) ; double f2 = f ( b ) ; double f3 = f ( c ) ; double d1 = f1 - f3 ; double d2 = f2 - f3 ; double h1 = a - c ; double h2 = b - c ; double a0 = f3 ; double a1 = ( ( ( d2 * Math . Pow ( h1 , 2 ) ) - ( d1 * Math . Pow ( h2 , 2 ) ) ) / ( ( h1 * h2 ) * ( h1 - h2 ) ) ) ; double a2 = ( ( ( d1 * h2 ) - ( d2 * h1 ) ) / ( ( h1 * h2 ) * ( h1 - h2 ) ) ) ; double x = ( ( - 2 * a0 ) / ( a1 + Math . Abs ( Math . Sqrt ( a1 * a1 - 4 * a0 * a2 ) ) ) ) ; double y = ( ( - 2 * a0 ) / ( a1 - Math . Abs ( Math . Sqrt ( a1 * a1 - 4 * a0 * a2 ) ) ) ) ; if ( x >= y ) res = x + c ; else res = y + c ; double m = res * 100 ; double n = c * 100 ; m = Math . Floor ( m ) ; n = Math . Floor ( n ) ; if ( m == n ) break ; a = b ; b = c ; c = res ; if ( i > MAX_ITERATIONS ) { Console . WriteLine ( " Root ▁ cannot ▁ be ▁ found ▁ using " + " ▁ Muller ' s ▁ method " ) ; break ; } } if ( i <= MAX_ITERATIONS ) Console . WriteLine ( " The ▁ value ▁ of ▁ the ▁ root ▁ is ▁ " + Math . Round ( res , 4 ) ) ; } static void Main ( ) { double a = 0 , b = 1 , c = 2 ; Muller ( a , b , c ) ; } }
Optimized Euler Totient Function for Multiple Evaluations | C # program to efficiently compute values of euler totient function for multiple inputs . ; Stores prime numbers upto MAX - 1 values ; Finds prime numbers upto MAX - 1 and stores them in vector p ; if prime [ i ] is not marked before ; fill vector for every newly encountered prime ; run this loop till square root of MAX , mark the index i * j as not prime ; function to find totient of n ; this loop runs sqrt ( n / ln ( n ) ) times ; subtract multiples of p [ i ] from r ; Remove all occurrences of p [ i ] in n ; when n has prime factor greater than sqrt ( n ) ; Driver code ; preprocess all prime numbers upto 10 ^ 5
using System ; using System . Collections ; class GFG { static int MAX = 100001 ; static ArrayList p = new ArrayList ( ) ; static void sieve ( ) { int [ ] isPrime = new int [ MAX + 1 ] ; for ( int i = 2 ; i <= MAX ; i ++ ) { if ( isPrime [ i ] == 0 ) { p . Add ( i ) ; for ( int j = 2 ; i * j <= MAX ; j ++ ) isPrime [ i * j ] = 1 ; } } } static int phi ( int n ) { int res = n ; for ( int i = 0 ; ( int ) p [ i ] * ( int ) p [ i ] <= n ; i ++ ) { if ( n % ( int ) p [ i ] == 0 ) { res -= ( res / ( int ) p [ i ] ) ; while ( n % ( int ) p [ i ] == 0 ) n /= ( int ) p [ i ] ; } } if ( n > 1 ) res -= ( res / n ) ; return res ; } static void Main ( ) { sieve ( ) ; Console . WriteLine ( phi ( 11 ) ) ; Console . WriteLine ( phi ( 21 ) ) ; Console . WriteLine ( phi ( 31 ) ) ; Console . WriteLine ( phi ( 41 ) ) ; Console . WriteLine ( phi ( 51 ) ) ; Console . WriteLine ( phi ( 61 ) ) ; Console . WriteLine ( phi ( 91 ) ) ; Console . WriteLine ( phi ( 101 ) ) ; } }
Finding n | C # implementation for finding nth number made of prime digits only ; Prints n - th number where each digit is a prime number ; Finding the length of n - th number ; Count of numbers with len - 1 digits ; Count of numbers with i digits ; if i is the length of such number then n < 4 * ( 4 ^ ( i - 1 ) - 1 ) / 3 and n >= 4 * ( 4 ^ i - 1 ) / 3 if a valid i is found break the loop ; check for i + 1 ; Finding ith digit at ith place ; j = 1 means 2 j = 2 means ... j = 4 means 7 ; if prev_count + 4 ^ ( len - i ) is less than n , increase prev_count by 4 ^ ( x - i ) ; else print the ith digit and break ; Driver method
using System ; public class GFG { static void nthprimedigitsnumber ( long n ) { long len = 1 ; long prev_count = 0 ; while ( true ) { long curr_count = ( long ) ( prev_count + Math . Pow ( 4 , len ) ) ; if ( prev_count < n && curr_count >= n ) break ; len ++ ; prev_count = curr_count ; } for ( int i = 1 ; i <= len ; i ++ ) { for ( long j = 1 ; j <= 4 ; j ++ ) { if ( prev_count + Math . Pow ( 4 , len - i ) < n ) prev_count += ( long ) Math . Pow ( 4 , len - i ) ; else { if ( j == 1 ) Console . Write ( "2" ) ; else if ( j == 2 ) Console . Write ( "3" ) ; else if ( j == 3 ) Console . Write ( "5" ) ; else if ( j == 4 ) Console . Write ( "7" ) ; break ; } } } Console . WriteLine ( ) ; } public static void Main ( ) { nthprimedigitsnumber ( 10 ) ; nthprimedigitsnumber ( 21 ) ; } }
Cassiniâ €™ s Identity | C # implementation to demonstrate working of CassiniaTMs Identity ; Returns ( - 1 ) ^ n ; Driver Code
using System ; class GFG { static int cassini ( int n ) { return ( n & 1 ) != 0 ? - 1 : 1 ; } public static void Main ( ) { int n = 5 ; Console . Write ( cassini ( n ) ) ; } }
Find if a number is divisible by every number in a list | C # program which check is a number divided with every element in list or no ; Function which check is a number divided with every element in list or not ; driver program
using System ; class GFG { static bool findNoIsDivisibleOrNot ( int [ ] a , int n ) { for ( int i = 0 ; i < a . Length ; i ++ ) { if ( a [ i ] % n != 0 ) return false ; } return true ; } public static void Main ( ) { int [ ] a = { 14 , 12 , 4 , 18 } ; int n = 2 ; if ( findNoIsDivisibleOrNot ( a , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find a range of composite numbers of given length | C # program to find a range of composite numbers of given length ; method to find factorial of given number ; to print range of length n having all composite integers ; Driver method
using System ; class GFG { static int factorial ( int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } static void printRange ( int n ) { int a = factorial ( n + 2 ) + 2 ; int b = a + n - 1 ; Console . WriteLine ( " [ " + a + " , ▁ " + b + " ] " ) ; } public static void Main ( ) { int n = 3 ; printRange ( n ) ; } }
Find minimum value to assign all array elements so that array product becomes greater | C # program to find minimum value that can be assigned to along elements so that product becomes greater than current product . ; sort the array to apply Binary search ; using log property add every logarithmic value of element to val double val = 0 ; where double is long double ; set left and right extremities to find min value ; multiplying n to mid , to find the correct min value ; Driver code
using System ; public class GFG { static long findMinValue ( long [ ] arr , int n ) { Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { val += ( double ) ( Math . Log ( ( double ) ( arr [ i ] ) ) ) ; } long left = arr [ 0 ] , right = arr [ n - 1 ] ; long ans = 0 ; while ( left <= right ) { long mid = ( left + right ) / 2 ; double temp = ( double ) n * ( double ) ( Math . Log ( ( double ) ( mid ) ) ) ; if ( val < temp ) { ans = mid ; right = mid - 1 ; } else { left = mid + 1 ; } } return ans ; } static public void Main ( ) { long [ ] arr = { 4 , 2 , 1 , 10 , 6 } ; int n = arr . Length ; Console . WriteLine ( findMinValue ( arr , n ) ) ; } }
Find the sum of all the terms in the n | C # implementation to find the sum of all the terms in the nth row of the given series ; method to find the required sum ; sum = n * ( 2 * n ^ 2 + 1 ) ; Driver method
using System ; class Test { static int sumOfTermsInNthRow ( int n ) { int sum = ( int ) ( n * ( 2 * Math . Pow ( n , 2 ) + 1 ) ) ; return sum ; } public static void Main ( ) { int n = 4 ; Console . Write ( " Sum ▁ of ▁ all ▁ the ▁ terms ▁ in ▁ nth ▁ row ▁ = ▁ " + sumOfTermsInNthRow ( n ) ) ; } }
First digit in product of an array of numbers | C # implementation to find first digit of a single number ; Keep dividing by 10 until it is greater than equal to 10 ; Driver method
using System ; public class GFG { static int firstDigit ( int x ) { while ( x >= 10 ) x = x / 10 ; return x ; } public static void Main ( ) { Console . WriteLine ( firstDigit ( 12345 ) ) ; Console . WriteLine ( firstDigit ( 5432 ) ) ; } }
Find the occurrences of digit d in the range [ 0. . n ] | C # program to count appearances of a digit ' d ' in range from [ 0. . n ] ; Initialize result ; Count appearances in numbers starting from d . ; When the last digit is equal to d ; When the first digit is equal to d then ; increment result as well as number ; In case of reverse of number such as 12 and 21 ; Driver code
using System ; public class GFG { static int getOccurence ( int n , int d ) { int result = 0 ; int itr = d ; while ( itr <= n ) { if ( itr % 10 == d ) result ++ ; if ( itr != 0 && itr / 10 == d ) { result ++ ; itr ++ ; } else if ( itr / 10 == d - 1 ) itr = itr + ( 10 - d ) ; else itr = itr + 10 ; } return result ; } public static void Main ( ) { int n = 11 , d = 1 ; Console . Write ( getOccurence ( n , d ) ) ; } }
Numbers having Unique ( or Distinct ) digits | C # code for the above approach ; Function to print unique numbers ; Iterate from l to r ; Convert the no . to String ; Convert String to set using stl ; Output if condition satisfies ; Driver Code ; Input of the lower and higher limits ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void printUnique ( int l , int r ) { for ( int i = l ; i <= r ; i ++ ) { String s = String . Join ( " " , i ) ; HashSet < int > uniDigits = new HashSet < int > ( ) ; foreach ( int c in s . ToCharArray ( ) ) uniDigits . Add ( c ) ; if ( s . Length == uniDigits . Count ) { Console . Write ( i + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int l = 1 , r = 20 ; printUnique ( l , r ) ; } }
Program to calculate the value of sin ( x ) and cos ( x ) using Expansion | C # code for implementing cos function ; Function for calculation ; Converting degrees to radian ; maps the sum along the series ; holds the actual value of sin ( n ) ; Main function
using System ; class GFG { static void cal_cos ( float n ) { float accuracy = ( float ) 0.0001 , x1 , denominator , cosx , cosval ; n = n * ( float ) ( 3.142 / 180.0 ) ; x1 = 1 ; cosx = x1 ; cosval = ( float ) Math . Cos ( n ) ; int i = 1 ; do { denominator = 2 * i * ( 2 * i - 1 ) ; x1 = - x1 * n * n / denominator ; cosx = cosx + x1 ; i = i + 1 ; } while ( accuracy <= cosval - cosx ) ; Console . WriteLine ( cosx ) ; } static void Main ( ) { float n = 30 ; cal_cos ( n ) ; } }