codeparrot/xlcost-text-to-code huggingface dataset:
Median of two sorted arrays of same size | A C # program to divide and conquer based efficient solution to find median of two sorted arrays of same size . ; This function returns median of ar1 [ ] and ar2 [ ] . Assumptions in this function : Both ar1 [ ] and ar2 [ ] are sorted arrays Both have n elements ; get the median of the first array ; get the median of the second array ; If medians are equal then return either m1 or m2 ; if m1 < m2 then median must exist in ar1 [ m1 ... . ] and ar2 [ ... . m2 ] ; if m1 > m2 then median must exist in ar1 [ ... . m1 ] and ar2 [ m2 ... ] ; Function to get median of a sorted array ; Driver code
using System ; class GfG { static int getMedian ( int [ ] a , int [ ] b , int startA , int startB , int endA , int endB ) { if ( endA - startA == 1 ) { return ( Math . Max ( a [ startA ] , b [ startB ] ) + Math . Min ( a [ endA ] , b [ endB ] ) ) / 2 ; } int m1 = median ( a , startA , endA ) ; int m2 = median ( b , startB , endB ) ; if ( m1 == m2 ) { return m1 ; } else if ( m1 < m2 ) { return getMedian ( a , b , ( endA + startA + 1 ) / 2 , startB , endA , ( endB + startB + 1 ) / 2 ) ; } else { return getMedian ( a , b , startA , ( endB + startB + 1 ) / 2 , ( endA + startA + 1 ) / 2 , endB ) ; } } static int median ( int [ ] arr , int start , int end ) { int n = end - start + 1 ; if ( n % 2 == 0 ) { return ( arr [ start + ( n / 2 ) ] + arr [ start + ( n / 2 - 1 ) ] ) / 2 ; } else { return arr [ start + n / 2 ] ; } } public static void Main ( String [ ] args ) { int [ ] ar1 = { 1 , 2 , 3 , 6 } ; int [ ] ar2 = { 4 , 6 , 8 , 10 } ; int n1 = ar1 . Length ; int n2 = ar2 . Length ; if ( n1 != n2 ) { Console . WriteLine ( " Doesn ' t ▁ work ▁ for ▁ arrays ▁ " + " of ▁ unequal ▁ size " ) ; } else if ( n1 == 0 ) { Console . WriteLine ( " Arrays ▁ are ▁ empty . " ) ; } else if ( n1 == 1 ) { Console . WriteLine ( ( ar1 [ 0 ] + ar2 [ 0 ] ) / 2 ) ; } else { Console . WriteLine ( " Median ▁ is ▁ " + getMedian ( ar1 , ar2 , 0 , 0 , ar1 . Length - 1 , ar2 . Length - 1 ) ) ; } } }
Median of two sorted arrays of same size |  ; This function returns median of ar1 [ ] and ar2 [ ] . Assumptions in this function : Both ar1 [ ] and ar2 [ ] are sorted arrays Both have n elements ; Driver code
using System ; public class GFG { public static int getMedian ( int [ ] ar1 , int [ ] ar2 , int n ) { int j = 0 ; int i = n - 1 ; while ( ar1 [ i ] > ar2 [ j ] && j < n && i > - 1 ) { int temp = ar1 [ i ] ; ar1 [ i ] = ar2 [ j ] ; ar2 [ j ] = temp ; i -- ; j ++ ; } Array . Sort ( ar1 ) ; Array . Sort ( ar2 ) ; return ( ar1 [ n - 1 ] + ar2 [ 0 ] ) / 2 ; } public static void Main ( String [ ] args ) { int [ ] ar1 = { 1 , 12 , 15 , 26 , 38 } ; int [ ] ar2 = { 2 , 13 , 17 , 30 , 45 } ; int n1 = 5 ; int n2 = 5 ; if ( n1 == n2 ) Console . WriteLine ( " Median ▁ is ▁ " + getMedian ( ar1 , ar2 , n1 ) ) ; else Console . WriteLine ( " Doesn ' t ▁ work ▁ for ▁ arrays ▁ of ▁ unequal ▁ size " ) ; } }
How to check if two given line segments intersect ? | C # program to check if two given line segments intersect ; Given three colinear points p , q , r , the function checks if point q lies on line segment ' pr ' ; To find orientation of ordered triplet ( p , q , r ) . The function returns following values 0 -- > p , q and r are colinear 1 -- > Clockwise 2 -- > Counterclockwise ; www . geeksforgeeks . org / orientation - 3 - ordered - points / See https : for details of below formula . ; colinear ; clock or counterclock wise ; The main function that returns true if line segment ' p1q1' and ' p2q2' intersect . ; Find the four orientations needed for general and special cases ; General case ; Special Cases p1 , q1 and p2 are colinear and p2 lies on segment p1q1 ; p1 , q1 and q2 are colinear and q2 lies on segment p1q1 ; p2 , q2 and p1 are colinear and p1 lies on segment p2q2 ; p2 , q2 and q1 are colinear and q1 lies on segment p2q2 ; Doesn 't fall in any of the above cases ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class Point { public int x ; public int y ; public Point ( int x , int y ) { this . x = x ; this . y = y ; } } ; static Boolean onSegment ( Point p , Point q , Point r ) { if ( q . x <= Math . Max ( p . x , r . x ) && q . x >= Math . Min ( p . x , r . x ) && q . y <= Math . Max ( p . y , r . y ) && q . y >= Math . Min ( p . y , r . y ) ) return true ; return false ; } static int orientation ( Point p , Point q , Point r ) { int val = ( q . y - p . y ) * ( r . x - q . x ) - ( q . x - p . x ) * ( r . y - q . y ) ; if ( val == 0 ) return 0 ; return ( val > 0 ) ? 1 : 2 ; } static Boolean doIntersect ( Point p1 , Point q1 , Point p2 , Point q2 ) { int o1 = orientation ( p1 , q1 , p2 ) ; int o2 = orientation ( p1 , q1 , q2 ) ; int o3 = orientation ( p2 , q2 , p1 ) ; int o4 = orientation ( p2 , q2 , q1 ) ; if ( o1 != o2 && o3 != o4 ) return true ; if ( o1 == 0 && onSegment ( p1 , p2 , q1 ) ) return true ; if ( o2 == 0 && onSegment ( p1 , q2 , q1 ) ) return true ; if ( o3 == 0 && onSegment ( p2 , p1 , q2 ) ) return true ; if ( o4 == 0 && onSegment ( p2 , q1 , q2 ) ) return true ; return false ; } public static void Main ( String [ ] args ) { Point p1 = new Point ( 1 , 1 ) ; Point q1 = new Point ( 10 , 1 ) ; Point p2 = new Point ( 1 , 2 ) ; Point q2 = new Point ( 10 , 2 ) ; if ( doIntersect ( p1 , q1 , p2 , q2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; p1 = new Point ( 10 , 1 ) ; q1 = new Point ( 0 , 10 ) ; p2 = new Point ( 0 , 0 ) ; q2 = new Point ( 10 , 10 ) ; if ( doIntersect ( p1 , q1 , p2 , q2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; p1 = new Point ( - 5 , - 5 ) ; q1 = new Point ( 0 , 0 ) ; p2 = new Point ( 1 , 1 ) ; q2 = new Point ( 10 , 10 ) ; ; if ( doIntersect ( p1 , q1 , p2 , q2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Check whether a given point lies inside a triangle or not | C # Code to Check whether a given point lies inside a triangle or not ; A utility function to calculate area of triangle formed by ( x1 , y1 ) ( x2 , y2 ) and ( x3 , y3 ) ; A function to check whether point P ( x , y ) lies inside the triangle formed by A ( x1 , y1 ) , B ( x2 , y2 ) and C ( x3 , y3 ) ; Calculate area of triangle ABC ; Calculate area of triangle PBC ; Calculate area of triangle PAC ; Calculate area of triangle PAB ; Check if sum of A1 , A2 and A3 is same as A ; Driver program to test above function ; Let us check whether the point P ( 10 , 15 ) lies inside the triangle formed by A ( 0 , 0 ) , B ( 20 , 0 ) and C ( 10 , 30 )
using System ; class GFG { static double area ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { return Math . Abs ( ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) / 2.0 ) ; } static bool isInside ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 , int x , int y ) { double A = area ( x1 , y1 , x2 , y2 , x3 , y3 ) ; double A1 = area ( x , y , x2 , y2 , x3 , y3 ) ; double A2 = area ( x1 , y1 , x , y , x3 , y3 ) ; double A3 = area ( x1 , y1 , x2 , y2 , x , y ) ; return ( A == A1 + A2 + A3 ) ; } public static void Main ( ) { if ( isInside ( 0 , 0 , 20 , 0 , 10 , 30 , 10 , 15 ) ) Console . WriteLine ( " Inside " ) ; else Console . WriteLine ( " Not ▁ Inside " ) ; } }
Lucky Numbers | C # program to check Lucky Number ; Returns 1 if n is a lucky no . ohterwise returns 0 ; variable next_position is just for readability of the program we can remove it and use n only ; calculate next position of input no ; driver program
using System ; class GFG { public static int counter = 2 ; static bool isLucky ( int n ) { int next_position = n ; if ( counter > n ) return true ; if ( n % counter == 0 ) return false ; next_position -= next_position / counter ; counter ++ ; return isLucky ( next_position ) ; } public static void Main ( ) { int x = 5 ; if ( isLucky ( x ) ) Console . Write ( x + " ▁ is ▁ a ▁ " + " lucky ▁ no . " ) ; else Console . Write ( x + " ▁ is ▁ not " + " ▁ a ▁ lucky ▁ no . " ) ; } }
Write you own Power without using multiplication ( * ) and division ( / ) operators |  ; Works only if a >= 0 and b >= 0 ; driver program to test above function
using System ; class GFG { static int pow ( int a , int b ) { if ( b == 0 ) return 1 ; int answer = a ; int increment = a ; int i , j ; for ( i = 1 ; i < b ; i ++ ) { for ( j = 1 ; j < a ; j ++ ) { answer += increment ; } increment = answer ; } return answer ; } public static void Main ( ) { Console . Write ( pow ( 5 , 3 ) ) ; } }
Write you own Power without using multiplication ( * ) and division ( / ) operators |  ; A recursive function to get x * y ; A recursive function to get a ^ b Works only if a >= 0 and b >= 0 ; driver program to test above functions
using System ; class GFG { static int multiply ( int x , int y ) { if ( y > 0 ) return ( x + multiply ( x , y - 1 ) ) ; else return 0 ; } static int pow ( int a , int b ) { if ( b > 0 ) return multiply ( a , pow ( a , b - 1 ) ) ; else return 1 ; } public static void Main ( ) { Console . Write ( pow ( 5 , 3 ) ) ; } }
Count numbers that don 't contain 3 | C # program to count numbers that not contain 3 ; Function that returns count of numbers which are in range from 1 to n and not contain 3 as a digit ; Base cases ( Assuming n is not negative ) ; Calculate 10 ^ ( d - 1 ) ( 10 raise to the power d - 1 ) where d is number of digits in n . po will be 100 for n = 578 ; find the most significant digit ( msd is 5 for 578 ) ; For 578 , total will be 4 * count ( 10 ^ 2 - 1 ) + 4 + count ( 78 ) ; For 35 , total will be equal to count ( 29 ) ; Driver program
using System ; class GFG { static int count ( int n ) { if ( n < 3 ) return n ; if ( n >= 3 && n < 10 ) return n - 1 ; int po = 1 ; while ( n / po > 9 ) po = po * 10 ; int msd = n / po ; if ( msd != 3 ) return count ( msd ) * count ( po - 1 ) + count ( msd ) + count ( n % po ) ; else return count ( msd * po - 1 ) ; } public static void Main ( ) { int n = 578 ; Console . Write ( count ( n ) ) ; } }
Number which has the maximum number of distinct prime factors in the range M to N | C # program to print the Number which has the maximum number of distinct prime factors of numbers in range m to n ; Function to return the maximum number ; array to store the number of distinct primes ; true if index ' i ' is a prime ; initializing the number of factors to 0 and ; Used in Sieve ; condition works only x when ' i ' is prime , hence for factors of all prime number , the prime status is changed to false ; Number is prime ; number of factor of a prime number is 1 ; incrementing factorCount all the factors of i ; and changing prime status to false ; Initialize the max and num ; Gets the maximum number ; Gets the maximum number ; Driver code ; Calling function
using System ; class GFG { static int maximumNumberDistinctPrimeRange ( int m , int n ) { long [ ] factorCount = new long [ n + 1 ] ; bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { factorCount [ i ] = 0 ; prime [ i ] = true ; } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] == true ) { factorCount [ i ] = 1 ; for ( int j = i * 2 ; j <= n ; j += i ) { factorCount [ j ] ++ ; prime [ j ] = false ; } } } int max = ( int ) factorCount [ m ] ; int num = m ; for ( int i = m ; i <= n ; i ++ ) { if ( factorCount [ i ] > max ) { max = ( int ) factorCount [ i ] ; num = i ; } } return num ; } public static void Main ( ) { int m = 4 , n = 6 ; Console . WriteLine ( maximumNumberDistinctPrimeRange ( m , n ) ) ; } }
Lexicographic rank of a string | C # program to find lexicographic rank of a string ; A utility function to find factorial of n ; A utility function to count smaller characters on right of arr [ low ] ; A function to find rank of a string in all permutations of characters ; count number of chars smaller than str [ i ] from str [ i + 1 ] to str [ len - 1 ] ; Driver program to test above function
using System ; class GFG { static int fact ( int n ) { return ( n <= 1 ) ? 1 : n * fact ( n - 1 ) ; } static int findSmallerInRight ( string str , int low , int high ) { int countRight = 0 , i ; for ( i = low + 1 ; i <= high ; ++ i ) if ( str [ i ] < str [ low ] ) ++ countRight ; return countRight ; } static int findRank ( string str ) { int len = str . Length ; int mul = fact ( len ) ; int rank = 1 ; int countRight ; for ( int i = 0 ; i < len ; ++ i ) { mul /= len - i ; countRight = findSmallerInRight ( str , i , len - 1 ) ; rank += countRight * mul ; } return rank ; } public static void Main ( ) { string str = " string " ; Console . Write ( findRank ( str ) ) ; } }
Lexicographic rank of a string | A O ( n ) solution for finding rank of string ; all elements of count [ ] are initialized with 0 ; A utility function to find factorial of n ; Construct a count array where value at every index contains count of smaller characters in whole string ; Removes a character ch from count [ ] array constructed by populateAndIncreaseCount ( ) ; A function to find rank of a string in all permutations of characters ; Populate the count array such that count [ i ] contains count of characters which are present in str and are smaller than i ; count number of chars smaller than str [ i ] fron str [ i + 1 ] to str [ len - 1 ] ; Reduce count of characters greater than str [ i ] ; Driver code
using System ; class GFG { static int MAX_CHAR = 256 ; int [ ] count = new int [ MAX_CHAR ] ; static int fact ( int n ) { return ( n <= 1 ) ? 1 : n * fact ( n - 1 ) ; } static void populateAndIncreaseCount ( int [ ] count , char [ ] str ) { int i ; for ( i = 0 ; i < str . Length ; ++ i ) ++ count [ str [ i ] ] ; for ( i = 1 ; i < MAX_CHAR ; ++ i ) count [ i ] += count [ i - 1 ] ; } static void updatecount ( int [ ] count , char ch ) { int i ; for ( i = ch ; i < MAX_CHAR ; ++ i ) -- count [ i ] ; } static int findRank ( char [ ] str ) { int len = str . Length ; int mul = fact ( len ) ; int rank = 1 , i ; populateAndIncreaseCount ( count , str ) ; for ( i = 0 ; i < len ; ++ i ) { mul /= len - i ; rank += count [ str [ i ] - 1 ] * mul ; updatecount ( count , str [ i ] ) ; } return rank ; } public static void Main ( String [ ] args ) { char [ ] str = " string " . ToCharArray ( ) ; Console . WriteLine ( findRank ( str ) ) ; } }
Efficient program to calculate e ^ x | C # efficient program to calculate e raise to the power x ; Function returns approximate value of e ^ x using sum of first n terms of Taylor Series ; initialize sum of series ; driver program
using System ; class GFG { static float exponential ( int n , float x ) { float sum = 1 ; for ( int i = n - 1 ; i > 0 ; -- i ) sum = 1 + x * sum / i ; return sum ; } public static void Main ( ) { int n = 10 ; float x = 1 ; Console . Write ( " e ^ x ▁ = ▁ " + exponential ( n , x ) ) ; } }
Measure one litre using two vessels and infinite water supply | Sample run of the Algo for V1 with capacity 3 and V2 with capacity 7 1. Fill V1 : V1 = 3 , V2 = 0 2. Transfer from V1 to V2 , and fill V1 : V1 = 3 , V2 = 3 2. Transfer from V1 to V2 , and fill V1 : V1 = 3 , V2 = 6 3. Transfer from V1 to V2 , and empty V2 : V1 = 2 , V2 = 0 4. Transfer from V1 to V2 , and fill V1 : V1 = 3 , V2 = 2 5. Transfer from V1 to V2 , and fill V1 : V1 = 3 , V2 = 5 6. Transfer from V1 to V2 , and empty V2 : V1 = 1 , V2 = 0 7. Stop as V1 now contains 1 litre . Note that V2 was made empty in steps 3 and 6 because it became full ; A utility function to get GCD of two numbers ; Class to represent a Vessel ; A vessel has capacity , and current amount of water in it ; Constructor : initializes capacity as given , and current as 0 ; The main function to fill one litre in this vessel . Capacity of V2 must be greater than this vessel and two capacities must be coprime ; solution exists iff a and b are co - prime ; fill A ( smaller vessel ) ; Transfer water from V1 to V2 and reduce current of V1 by the amount equal to transferred water ; Finally , there will be 1 litre in vessel 1 ; Fills vessel with given amount and returns the amount of water transferred to it . If the vessel becomes full , then the vessel is made empty ; If the vessel can accommodate the given amount ; If the vessel cannot accommodate the given amount , then store the amount of water transferred ; Since the vessel becomes full , make the vessel empty so that it can be filled again ; Driver program to test above function ; a must be smaller than b ; Create two vessels of capacities a and b ; Get 1 litre in first vessel
using System ; class GFG { static int gcd ( int a , int b ) { return b > 0 ? gcd ( b , a % b ) : a ; } class Vessel { int capacity , current ; public Vessel ( int capacity ) { this . capacity = capacity ; current = 0 ; } public void makeOneLitre ( Vessel V2 ) { if ( gcd ( capacity , V2 . capacity ) != 1 ) return ; while ( current != 1 ) { if ( current == 0 ) current = capacity ; Console . Write ( " Vessel ▁ 1 : ▁ " + current + " ▁ Vessel ▁ 2 : ▁ " + V2 . current + " STRNEWLINE " ) ; current = current - V2 . transfer ( current ) ; } Console . Write ( " Vessel ▁ 1 : ▁ " + current + " ▁ Vessel ▁ 2 : ▁ " + V2 . current + " STRNEWLINE " ) ; } int transfer ( int amount ) { if ( current + amount < capacity ) { current += amount ; return amount ; } int transferred = capacity - current ; current = 0 ; return transferred ; } } public static void Main ( String [ ] args ) { int a = 3 , b = 7 ; Vessel V1 = new Vessel ( a ) ; Vessel V2 = new Vessel ( b ) ; V1 . makeOneLitre ( V2 ) ; } }
Random number generator in arbitrary probability distribution fashion | C # program to generate random numbers according to given frequency distribution ; Utility function to find ceiling of r in arr [ l . . h ] ; Same as mid = ( l + h ) / 2 ; The main function that returns a random number from arr [ ] according to distribution array defined by freq [ ] . n is size of arrays . ; Create and fill prefix array ; prefix [ n - 1 ] is sum of all frequencies . Generate a random number with value from 1 to this sum ; Find index of ceiling of r in prefix arrat ; Driver Code ; Let us generate 10 random numbers accroding to given distribution
using System ; class GFG { static int findCeil ( int [ ] arr , int r , int l , int h ) { int mid ; while ( l < h ) { mid = l + ( ( h - l ) >> 1 ) ; if ( r > arr [ mid ] ) l = mid + 1 ; else h = mid ; } return ( arr [ l ] >= r ) ? l : - 1 ; } static int myRand ( int [ ] arr , int [ ] freq , int n ) { int [ ] prefix = new int [ n ] ; int i ; prefix [ 0 ] = freq [ 0 ] ; for ( i = 1 ; i < n ; ++ i ) prefix [ i ] = prefix [ i - 1 ] + freq [ i ] ; Random rand = new Random ( ) ; int r = ( ( int ) ( rand . Next ( ) * ( 323567 ) ) % prefix [ n - 1 ] ) + 1 ; int indexc = findCeil ( prefix , r , 0 , n - 1 ) ; return arr [ indexc ] ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int [ ] freq = { 10 , 5 , 20 , 100 } ; int i , n = arr . Length ; for ( i = 0 ; i < 5 ; i ++ ) Console . WriteLine ( myRand ( arr , freq , n ) ) ; } }
How to check if a given number is Fibonacci number ? | C # program to check if x is a perfect square ; A utility function that returns true if x is perfect square ; Returns true if n is a Fibonacci Number , else false ; n is Fibonacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both are a perfect square ; Driver method
using System ; class GFG { static bool isPerfectSquare ( int x ) { int s = ( int ) Math . Sqrt ( x ) ; return ( s * s == x ) ; } static bool isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } public static void Main ( ) { for ( int i = 1 ; i <= 10 ; i ++ ) Console . WriteLine ( isFibonacci ( i ) ? i + " ▁ is ▁ a ▁ Fibonacci ▁ Number " : i + " ▁ is ▁ a ▁ not ▁ Fibonacci ▁ Number " ) ; } }
Count trailing zeroes in factorial of a number | C # program to count trailing 0 s in n ! ; Function to return trailing 0 s in factorial of n ; Initialize result ; Keep dividing n by powers of 5 and update count ; Driver Code
using System ; class GFG { static int findTrailingZeros ( int n ) { int count = 0 ; for ( int i = 5 ; n / i >= 1 ; i *= 5 ) count += n / i ; return count ; } public static void Main ( ) { int n = 100 ; Console . WriteLine ( " Count ▁ of ▁ trailing ▁ 0s ▁ in ▁ " + n + " ! ▁ is ▁ " + findTrailingZeros ( n ) ) ; } }
Program for nth Catalan Number | A recursive C # program to find nth catalan number ; A recursive function to find nth catalan number ; Base case ; catalan ( n ) is sum of catalan ( i ) * catalan ( n - i - 1 ) ; Driver Code
using System ; class GFG { static int catalan ( int n ) { if ( n <= 1 ) { return 1 ; } int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { res += catalan ( i ) * catalan ( n - i - 1 ) ; } return res ; } public static void Main ( ) { for ( int i = 0 ; i < 10 ; i ++ ) Console . Write ( catalan ( i ) + " ▁ " ) ; } }
Program for nth Catalan Number | A dynamic programming based function to find nth Catalan number ; Table to store results of subproblems ; Initialize first two values in table ; Fill entries in catalan [ ] using recursive formula ; Return last entry ; Driver code
using System ; class GFG { static uint catalanDP ( uint n ) { uint [ ] catalan = new uint [ n + 2 ] ; catalan [ 0 ] = catalan [ 1 ] = 1 ; for ( uint i = 2 ; i <= n ; i ++ ) { catalan [ i ] = 0 ; for ( uint j = 0 ; j < i ; j ++ ) catalan [ i ] += catalan [ j ] * catalan [ i - j - 1 ] ; } return catalan [ n ] ; } static void Main ( ) { for ( uint i = 0 ; i < 10 ; i ++ ) Console . Write ( catalanDP ( i ) + " ▁ " ) ; } }
Program for nth Catalan Number | C # program for nth Catalan Number ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver code
using System ; class GFG { static long binomialCoeff ( int n , int k ) { long res = 1 ; if ( k > n - k ) { k = n - k ; } for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static long catalan ( int n ) { long c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } public static void Main ( ) { for ( int i = 0 ; i < 10 ; i ++ ) { Console . Write ( catalan ( i ) + " ▁ " ) ; } } }
Program for nth Catalan Number |  ; Function to print the number ; For the first number C ( 0 ) ; Iterate till N ; Calculate the number and print it ; Driver code ; Function call
using System ; public class GFG { static void catalan ( int n ) { int cat_ = 1 ; Console . Write ( cat_ + " ▁ " ) ; for ( int i = 1 ; i < n ; i ++ ) { cat_ *= ( 4 * i - 2 ) ; cat_ /= ( i + 1 ) ; Console . Write ( cat_ + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int n = 5 ; catalan ( n ) ; } }
Find Excel column name from a given column number | C # program to find Excel column name from a given column number ;  ; Function to print Excel column name for a given column number ; To store result ( Excel column name ) ; Find remainder ; If remainder is 0 , then a ' Z ' must be there in output ; If remainder is non - zero ; Reverse the string and Print result ; Driver code
using System ; class GFG { static String reverse ( String input ) { char [ ] reversedString = input . ToCharArray ( ) ; Array . Reverse ( reversedString ) ; return new String ( reversedString ) ; } private static void printString ( int columnNumber ) { String columnName = " " ; while ( columnNumber > 0 ) { int rem = columnNumber % 26 ; if ( rem == 0 ) { columnName += " Z " ; columnNumber = ( columnNumber / 26 ) - 1 ; } else { columnName += ( char ) ( ( rem - 1 ) + ' A ' ) ; columnNumber = columnNumber / 26 ; } } columnName = reverse ( columnName ) ; Console . WriteLine ( columnName . ToString ( ) ) ; } public static void Main ( String [ ] args ) { printString ( 26 ) ; printString ( 51 ) ; printString ( 52 ) ; printString ( 80 ) ; printString ( 676 ) ; printString ( 702 ) ; printString ( 705 ) ; } }