text
stringlengths
17
3.65k
code
stringlengths
60
5.26k
Sum of series 2 / 3 | C # program to find sum of given series ; Function to find sum of series up - to n terms ; initializing counter by 1 ; variable to calculate result ; while loop until nth term is not reached ; boolean type variable for checking validation ; Driver Code
using System ; class GFG { static double seriesSum ( int n ) { int i = 1 ; double res = 0.0 ; bool sign = true ; while ( n > 0 ) { n -- ; if ( sign ) { sign = ! sign ; res = res + ( double ) ++ i / ++ i ; } else { sign = ! sign ; res = res - ( double ) ++ i / ++ i ; } } return res ; } public static void Main ( ) { int n = 5 ; Console . Write ( seriesSum ( n ) ) ; } }
Number of Symmetric Relations on a Set | C # program to count total symmetric relations on a set of natural numbers . ; function find the square of n ; Base case ; Return 2 ^ ( n ( n + 1 ) / 2 ) ; Driver code
using System ; class GFG { static int countSymmetric ( int n ) { if ( n == 0 ) return 1 ; return 1 << ( ( n * ( n + 1 ) ) / 2 ) ; } public static void Main ( ) { int n = 3 ; Console . WriteLine ( countSymmetric ( n ) ) ; } }
Program for centered nonagonal number | C # Program to find nth centered nonagonal number . ; Function to find nth centered nonagonal number . ; Formula to find nth centered nonagonal number . ; Driver function .
using System ; class GFG { static int centeredNonagonal ( int n ) { return ( 3 * n - 2 ) * ( 3 * n - 1 ) / 2 ; } public static void Main ( ) { int n = 10 ; Console . Write ( centeredNonagonal ( n ) ) ; } }
Program for Mean Absolute Deviation | C # Program to find mean absolute deviation of given array ; Function to find mean of the array elements . ; Calculate sum of all elements . ; Function to find mean absolute deviation of given elements . ; Calculate the sum of absolute deviation about mean . ; Return mean absolute deviation about mean . ; Driver function .
using System ; class GFG { static float Mean ( float [ ] arr , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; return sum / n ; } static float meanAbsDevtion ( float [ ] arr , int n ) { float absSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) absSum = absSum + Math . Abs ( arr [ i ] - Mean ( arr , n ) ) ; return absSum / n ; } public static void Main ( ) { float [ ] arr = { 10 , 15 , 15 , 17 , 18 , 21 } ; int n = arr . Length ; Console . WriteLine ( meanAbsDevtion ( arr , n ) ) ; } }
Find if it is possible to get a ratio from given ranges of costs and quantities | C # program to find if it is possible to get the ratio r ; Returns true if it is possible to get ratio r from given cost and quantity ranges . ; Calculating cost corresponding to value of i ; Driver Code
using System ; class Ratio { static bool isRatioPossible ( int lowCost , int upCost , int lowQuant , int upQuant , int r ) { for ( int i = lowQuant ; i <= upQuant ; i ++ ) { int ans = i * r ; if ( lowCost <= ans && ans <= upCost ) return true ; } return false ; } public static void Main ( ) { int lowCost = 14 , upCost = 30 , lowQuant = 5 , upQuant = 12 , r = 9 ; if ( isRatioPossible ( lowCost , upCost , lowQuant , upQuant , r ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find N integers with given difference between product and sum | C # code to generate numbers with difference between product and sum is D ; Function to implement calculation ; Driver code
using System ; class GFG { static void findNumbers ( int n , int d ) { for ( int i = 0 ; i < n - 2 ; i ++ ) Console . Write ( "1" + " ▁ " ) ; Console . Write ( "2" + " ▁ " ) ; Console . Write ( n + d ) ; } public static void Main ( ) { int N = 3 , D = 5 ; findNumbers ( N , D ) ; } }
Sum of fourth powers of first n odd natural numbers | C # Program to find the sum of fourth powers of first n odd natural numbers ; calculate the sum of fourth power of first n odd natural numbers ; Driven Program
using System ; class GFG { static long oddNumSum ( int n ) { return ( n * ( 2 * n + 1 ) * ( 24 * n * n * n - 12 * n * n - 14 * n + 7 ) ) / 15 ; } public static void Main ( ) { int n = 4 ; Console . Write ( oddNumSum ( n ) ) ; } }
Trailing number of 0 s in product of two factorials | Java program for count number of trailing zeros in N ! * M ! ; Returns number of zeros in factorial n ; dividing x by powers of 5 and update count ; Returns count of trailing zeros in M ! x N ! ; Driver program
using System ; class GFG { static int trailingZero ( int x ) { int i = 5 , count = 0 ; while ( x > i ) { count = count + x / i ; i = i * 5 ; } return count ; } static int countProductTrailing ( int M , int N ) { return trailingZero ( N ) + trailingZero ( M ) ; } public static void Main ( ) { int N = 67 , M = 98 ; Console . WriteLine ( countProductTrailing ( N , M ) ) ; } }
Trimorphic Number | C # program to check if a number is Trimorphic ; Function to check Trimorphic number ; Store the cube ; Start Comparing digits ; Return false , if any digit of N doesn ' t ▁ match ▁ with ▁ ▁ its ▁ cube ' s digits from last ; Reduce N and cube ; Driver code
using System ; class GFG { static bool isTrimorphic ( int N ) { int cube = N * N * N ; while ( N > 0 ) { if ( N % 10 != cube % 10 ) return false ; N /= 10 ; cube /= 10 ; } return true ; } public static void Main ( ) { int N = 24 ; if ( isTrimorphic ( N ) == true ) Console . Write ( " trimorphic " ) ; else Console . Write ( " not ▁ trimorphic " ) ; } }
Trimorphic Number | C # program to find nth Trimorphic number ; Functions to find nth Trimorphic number ; Comparing the digits ; Return false , if any digit of num doesn ' t ▁ ▁ match ▁ with ▁ its ▁ cube ' s digits from last ; Reduce num and cube ; Check in max int size ; check number is Trimorphic or not ; if counter is equal to the n then return nth number ; Driver code
using System ; class GFG { static int INT_MAX = 2147483647 ; static bool checkTrimorphic ( int num ) { int cube = num * num * num ; while ( num > 0 ) { if ( num % 10 != cube % 10 ) return false ; num /= 10 ; cube /= 10 ; } return true ; } static int nthTrimorphic ( int n ) { int count = 0 ; for ( int i = 0 ; i < INT_MAX ; i ++ ) { if ( checkTrimorphic ( i ) ) count ++ ; if ( count == n ) return i ; } return - 1 ; } static int Main ( ) { int n = 9 ; Console . Write ( nthTrimorphic ( n ) ) ; return 0 ; } }
Find minimum moves to reach target on an infinite line | C # program to find minimum moves to reach target if we can move i steps in i - th move . ; Handling negatives by symmetry ; Keep moving while sum is smaller or difference is odd . ; Driver code
using System ; class GFG { static int reachTarget ( int target ) { target = Math . Abs ( target ) ; int sum = 0 , step = 0 ; while ( sum < target || ( sum - target ) % 2 != 0 ) { step ++ ; sum += step ; } return step ; } public static void Main ( ) { int target = 5 ; Console . WriteLine ( reachTarget ( target ) ) ; } }
Palindromic Selfie Numbers | C # program to find palindromic selfie numbers ; To store all permutations of digits in the number ; int number ; input number ; Function to reverse the digits of a number ; Append it at the beg ; num = num / 10 ; Reduce number until 0 ; Function to check palindromic selfie ; Length of the number required for calculating all permutations of the digits ; Remove the number and its palindrome from the obtained set as this is the LHS of multiplicative equality ; Iterate over all other numbers ; Check for equality x * palin ( x ) = y * palin ( y ) ; flag = true ; Answer found ; If no such number found ; Function to get all possible possible permutations of the digits in num ; Adds the new permutation obtained in the set ; Swap digits to get a different ordering ; Recurse to next pair of digits ; num = swap ( num , l , i ) ; Swap back ; Function that swaps the digits i and j in the num ; Convert int to char array ; Swap the ith and jth character ; Convert back to int and return ; Driver code ; First example , input = 145572 ; Second example , input = 19362 ; Third example , input = 4669
using System ; using System . Collections . Generic ; public class palindrome_selfie { HashSet < int > all_permutes = new HashSet < int > ( ) ; public palindrome_selfie ( int num ) { number = num ; } public int palindrome ( int num ) { int reversednum = 0 ; int d ; while ( num > 0 ) { reversednum = reversednum * 10 + d ; } return reversednum ; } public void palin_selfie ( ) { int l = String . Join ( " " , number ) . Length - 1 ; all_permutes . Remove ( palindrome ( number ) ) ; all_permutes . Remove ( number ) ; foreach ( var number2 in all_permutes ) { if ( number * palindrome ( number ) == number2 * palindrome ( number2 ) ) { Console . WriteLine ( " Palindrome ▁ multiplicative " + " selfie ▁ of ▁ " + number + " ▁ is ▁ : ▁ " + number2 ) ; break ; } } if ( flag == false ) { Console . WriteLine ( " Given ▁ number ▁ has ▁ " + " no ▁ palindrome ▁ selfie . " ) ; } } public void permute ( int num , int l , int r ) { if ( l == r ) all_permutes . Add ( num ) ; else { for ( int i = l ; i <= r ; i ++ ) { num = swap ( num , l , i ) ; permute ( num , l + 1 , r ) ; } } } public int swap ( int num , int i , int j ) { char temp ; char [ ] charArray = String . Join ( " " , num ) . ToCharArray ( ) ; temp = charArray [ i ] ; charArray [ i ] = charArray [ j ] ; charArray [ j ] = temp ; return int . Parse ( String . Join ( " " , charArray ) ) ; } public static void Main ( String [ ] args ) { palindrome_selfie example1 = new palindrome_selfie ( 145572 ) ; example1 . palin_selfie ( ) ; palindrome_selfie example2 = new palindrome_selfie ( 19362 ) ; example2 . palin_selfie ( ) ; palindrome_selfie example3 = new palindrome_selfie ( 4669 ) ; example3 . palin_selfie ( ) ; } }
Sum of fifth powers of the first n natural numbers | C # Program to find the sum of fifth power of first n natural numbers ; calculate the sum of fifth power of first n natural numbers ; Driven Program
using System ; class GFG { static long fifthPowerSum ( int n ) { return ( ( 2 * n * n * n * n * n * n ) + ( 6 * n * n * n * n * n ) + ( 5 * n * n * n * n ) - ( n * n ) ) / 12 ; } public static void Main ( ) { int n = 5 ; Console . Write ( fifthPowerSum ( n ) ) ; } }
Find unit digit of x raised to power y | Efficient Java program to find unit digit of x ^ y . ; Returns unit digit of x raised to power y ; Initialize result as 1 to handle case when y is 0. ; One by one multiply with x mod 10 to avoid overflow . ; Driver program
using System ; class GFG { static int unitDigitXRaisedY ( int x , int y ) { int res = 1 ; for ( int i = 0 ; i < y ; i ++ ) res = ( res * x ) % 10 ; return res ; } public static void Main ( ) { Console . WriteLine ( unitDigitXRaisedY ( 4 , 2 ) ) ; } }
Evaluation of Risk in Investments | C # code for above approach ; List stores the observation in pairs of format ( xi , fi ) , where xi = value of observation ; Function to calculate the summation of fi * xi ; Function to calculate summation fi ; Function to calculate the mean of the set of observations v ; Function to calculate the std deviation of set of observations v ; Get sum of frequencies ; Get the mean of the set of observations ; Driver Code
using System ; using System . Collections . Generic ; class GFG { class pair { public float first , second ; public pair ( float first , float second ) { this . first = first ; this . second = second ; } } static List < pair > List ; static float sigma_fx ( pair [ ] a ) { float sum = 0 ; foreach ( pair i in a ) { sum += i . first * i . second ; } return sum ; } static float sigma_f ( pair [ ] a ) { float sum = 0.0f ; foreach ( pair i in a ) { sum += i . second ; } return sum ; } static float calculate_mean ( pair [ ] a ) { return sigma_fx ( a ) / sigma_f ( a ) ; } static float calculate_std ( pair [ ] a ) { float f = sigma_f ( a ) ; float mean = sigma_fx ( a ) / f ; float sum = 0 ; foreach ( pair i in a ) { sum += ( i . first - mean ) * ( i . first - mean ) * i . second ; } return ( float ) Math . Sqrt ( sum / f ) ; } public static void Main ( String [ ] args ) { pair [ ] A = { new pair ( 0f , 0.1f ) , new pair ( 100f , 0.1f ) , new pair ( 200f , 0.2f ) , new pair ( 333f , 0.3f ) , new pair ( 400f , 0.3f ) } ; pair [ ] B = { new pair ( 100f , 0.1f ) , new pair ( 200f , 0.5f ) , new pair ( 700f , 0.4f ) } ; float avg_A = calculate_mean ( A ) ; float avg_B = calculate_mean ( B ) ; float std_A = calculate_std ( A ) ; float std_B = calculate_std ( B ) ; Console . Write ( " For ▁ Investment ▁ A " + " STRNEWLINE " ) ; Console . Write ( " Average : ▁ " + avg_A + " STRNEWLINE " ) ; Console . Write ( " Standard ▁ Deviation : ▁ " + std_A + " STRNEWLINE " ) ; Console . Write ( " Normalised ▁ Std : ▁ " + std_A / avg_A + " STRNEWLINE " ) ; Console . Write ( " For ▁ Investment ▁ B " + " STRNEWLINE " ) ; Console . Write ( " Average : ▁ " + avg_B + " STRNEWLINE " ) ; Console . Write ( " Standard ▁ Deviation : ▁ " + std_B + " STRNEWLINE " ) ; Console . Write ( " Normalised ▁ Std : ▁ " + std_B / avg_B + " STRNEWLINE " ) ; if ( ( std_B / avg_B ) < ( std_A / avg_A ) ) Console . Write ( " Investment ▁ B ▁ is ▁ less ▁ risky STRNEWLINE " ) ; else Console . Write ( " Investment ▁ A ▁ is ▁ less ▁ risky STRNEWLINE " ) ; } }
Max occurring divisor in an interval | Efficient C # program to find maximum occurring factor in an interval ; function to find max occurring divisor interval [ x , y ] ; if there is only one number in the in the interval , return that number ; otherwise , 2 is the max occurring divisor ; Driver Code
using System ; class GFG { static int findDivisor ( int x , int y ) { if ( x == y ) return y ; return 2 ; } public static void Main ( ) { int x = 3 , y = 16 ; Console . Write ( findDivisor ( x , y ) ) ; } }
Average of Squares of Natural Numbers | C # program to calculate 1 ^ 2 + 2 ^ 2 + 3 ^ 2 + ... average of square number ; Function to calculate average of square number ; Driver code
using System ; public class GFG { static float AvgofSquareN ( int n ) { float sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( i * i ) ; return sum / n ; } static public void Main ( String [ ] args ) { int n = 2 ; Console . WriteLine ( AvgofSquareN ( n ) ) ; } }
Find sum of even factors of a number | Formula based C # program to find sum of all divisors of n . ; Returns sum of all factors of n . ; If n is odd , then there are no even factors . ; Traversing through all prime factors . ; While i divides n , print i and divide n ; here we remove the 2 ^ 0 that is 1. All other factors ; This condition is to handle the case when n is a prime number . ; Driver Code
using System ; public class GfG { public static int sumofFactors ( int n ) { if ( n % 2 != 0 ) return 0 ; int res = 1 ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; if ( i == 2 && count == 1 ) curr_sum = 0 ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } public static void Main ( ) { int n = 18 ; Console . WriteLine ( sumofFactors ( n ) ) ; } }
Find LCM of rational numbers | C # program to find LCM of given array ; get lcm of two numbers ; Finds LCM of numerators ; calculate the lcm of all numerators ; return all numerator lcm ; Get GCD of all the denominators ; calculate the gcd of all the denominators ; return all denominator gcd ; find lcm of all the rational number ; return the LCM of all numerator / GCD of all denominator ; Driver code ; give rational number 2 / 7 , 3 / 14 , 5 / 3 make pair as a numerator and denominator
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 int LCM ( int a , int b ) { return ( a * b ) / ( __gcd ( a , b ) ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static int lcmOfNumerator ( List < pair > vect ) { int lcm = vect [ 0 ] . first ; for ( int i = 1 ; i < vect . Count ; i ++ ) lcm = LCM ( vect [ i ] . first , lcm ) ; return lcm ; } static int gcdOfDemoninators ( List < pair > vect ) { int gcd = vect [ 0 ] . second ; for ( int i = 1 ; i < vect . Count ; i ++ ) gcd = __gcd ( vect [ i ] . second , gcd ) ; return gcd ; } static void lcmOfRationals ( List < pair > vect ) { Console . Write ( lcmOfNumerator ( vect ) + " / " + gcdOfDemoninators ( vect ) ) ; } public static void Main ( String [ ] args ) { List < pair > vect = new List < pair > ( ) ; vect . Add ( new pair ( 2 , 7 ) ) ; vect . Add ( new pair ( 3 , 14 ) ) ; vect . Add ( new pair ( 5 , 3 ) ) ; lcmOfRationals ( vect ) ; } }
Program to determine focal length of a spherical mirror | C # program to determine the focal length of a of a spherical mirror ; Determines focal length of a spherical concave mirror ; Determines focal length of a spherical convex mirror ; Driver function
using System ; class GfG { public static float focal_length_concave ( float R ) { return R / 2 ; } public static float focal_length_convex ( float R ) { return - ( R / 2 ) ; } public static void Main ( String [ ] argc ) { float R = 30 ; Console . Write ( " Focal ▁ length ▁ of " + " spherical ▁ concave " + " mirror ▁ is ▁ : ▁ " + focal_length_concave ( R ) + " ▁ units STRNEWLINE " ) ; Console . Write ( " Focal ▁ length ▁ of " + " spherical ▁ convex " + " mirror ▁ is ▁ : ▁ " + focal_length_convex ( R ) + " ▁ units " ) ; } }
Find sum of odd factors of a number | Formula based C # program to find sum of all divisors of n . ; Returns sum of all factors of n . ; Traversing through all prime factors . ; ignore even factors by removing all powers of 2 ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number . ; Driver code
using System ; class GFG { static int sumofoddFactors ( int n ) { int res = 1 ; while ( n % 2 == 0 ) n = n / 2 ; for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } public static void Main ( String [ ] argc ) { int n = 30 ; Console . Write ( sumofoddFactors ( n ) ) ; } }
Number of non | C # program to find the numbers of non negative integral solutions ; return number of non negative integral solutions ; initialize total = 0 ; Base Case if n = 1 and val >= 0 then it should return 1 ; iterate the loop till equal the val ; total solution of equations and again call the recursive function Solutions ( variable , value ) ; return the total no possible solution ; Driver code
using System ; class GFG { static int countSolutions ( int n , int val ) { int total = 0 ; if ( n == 1 && val >= 0 ) return 1 ; for ( int i = 0 ; i <= val ; i ++ ) { total += countSolutions ( n - 1 , val - i ) ; } return total ; } public static void Main ( ) { int n = 5 ; int val = 20 ; Console . WriteLine ( countSolutions ( n , val ) ) ; } }
Fibonomial coefficient and Fibonomial triangle | C # Program to print Fibonomial Triangle of height n . ; Function to produce Fibonacci Series . ; 0 th and 1 st number of the series are 0 and 1 ; Add the previous 2 numbers in the series and store it ; Function to produce fibonomial coefficient ; Function to print Fibonomial Triangle . ; Finding the fibonacci series . ; to store triangle value . ; initialising the 0 th element of each row and diagonal element equal to 0. ; for each row . ; for each column . ; finding each element using recurrence relation . ; printing the Fibonomial Triangle . ; Driver code
using System ; class GFG { static int N = 6 ; static void fib ( int [ ] f , int n ) { int i ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; for ( i = 2 ; i <= n ; i ++ ) f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; } static void fibcoef ( int [ , ] fc , int [ ] f , int n ) { for ( int i = 0 ; i <= n ; i ++ ) fc [ i , 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { int k = j ; while ( k > 0 ) { k -- ; fc [ i , j ] *= f [ k ] ; } k = 1 ; while ( ( j + 1 ) != k ) fc [ i , j ] /= f [ k ++ ] ; } } } static void printFibonomialTriangle ( int n ) { int [ ] f = new int [ N + 1 ] ; fib ( f , n ) ; int [ , ] dp = new int [ N + 1 , N + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i , 0 ] = dp [ i , i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j < i ; j ++ ) dp [ i , j ] = f [ i - j + 1 ] * dp [ i - 1 , j - 1 ] + f [ j - 1 ] * dp [ i - 1 , j ] ; } for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) Console . Write ( dp [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( ) { int n = 6 ; printFibonomialTriangle ( n ) ; } }
Sum of Arithmetic Geometric Sequence | C # Program to find the sum of first n terms . ; Return the sum of first n term of AGP ; Finding the each term of AGP and adding it to sum . ; Driver Code
using System ; class GFG { static int sumofNterm ( int a , int d , int b , int r , int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( int ) ( ( a + ( i - 1 ) * d ) * ( b * Math . Pow ( r , i - 1 ) ) ) ; return sum ; } public static void Main ( ) { int a = 1 , d = 1 , b = 2 , r = 2 , n = 3 ; Console . Write ( sumofNterm ( a , d , b , r , n ) ) ; } }
Sum of the series 2 + ( 2 + 4 ) + ( 2 + 4 + 6 ) + ( 2 + 4 + 6 + 8 ) + …… + ( 2 + 4 + 6 + 8 + … . + 2 n ) | C # implementation to find the sum of the given series ; function to find the sum of the given series ; first term of each i - th term ; next term ; required sum ; Driver program to test above
using System ; class GFG { static int sumOfTheSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int k = 2 ; for ( int j = 1 ; j <= i ; j ++ ) { sum += k ; k += 2 ; } } return sum ; } public static void Main ( ) { int n = 5 ; Console . Write ( " Sum ▁ = ▁ " + sumOfTheSeries ( n ) ) ; } }
Program to find sum of series 1 * 2 * 3 + 2 * 3 * 4 + 3 * 4 * 5 + . . . + n * ( n + 1 ) * ( n + 2 ) | C # Program to find the sum of series 1 * 2 * 3 + 2 * 3 * 4 + . . . + n * ( n + 1 ) * ( n + 1 ) ; Function to calculate sum of series . ; Driver Code
using System ; public class GfG { static int sumOfSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum = sum + i * ( i + 1 ) * ( i + 2 ) ; return sum ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( sumOfSeries ( n ) ) ; } }
Program to get the Sum of series : 1 | C # program to get the sum of the series ; Function to get the series ; Sum of n - 1 terms starting from 2 nd term ; Driver Code
using System ; class GFG { static double Series ( double x , int n ) { double sum = 1 , term = 1 , fct , j , y = 2 , m ; int i ; for ( i = 1 ; i < n ; i ++ ) { fct = 1 ; for ( j = 1 ; j <= y ; j ++ ) { fct = fct * j ; } term = term * ( - 1 ) ; m = Math . Pow ( x , y ) / fct ; m = m * term ; sum = sum + m ; y += 2 ; } return sum ; } public static void Main ( ) { double x = 9 ; int n = 10 ; Console . Write ( Series ( x , n ) * 10000.0 / 10000.0 ) ; } }
Program to get the Sum of series : 1 | C # program to get the sum of the series ; Function to get the series ; Computing sum of remaining n - 1 terms . ; Driver Code
using System ; class GFG { static float Series ( double x , int n ) { double sum = 1 , term = 1 , fct = 1 ; double p = 1 , multi = 1 ; for ( int i = 1 ; i < n ; i ++ ) { fct = fct * multi * ( multi + 1 ) ; p = p * x * x ; term = ( - 1 ) * term ; multi += 2 ; sum = sum + ( term * p ) / fct ; } return ( float ) sum ; } public static void Main ( ) { double x = 9 ; int n = 10 ; Console . Write ( Series ( x , n ) ) ; } }
Find the number of consecutive zero at the end after multiplying n numbers | C # program to find the number of consecutive zero at the end after multiplying n numbers ; Function to count two 's factor ; Count number of 2 s present in n ; Function to count five 's factor ; Function to count number of zeros ; Count the two 's factor of n number ; Count the five 's factor of n number ; Return the minimum ; driver function
using System ; public class GfG { static int two_factor ( int n ) { int twocount = 0 ; while ( n % 2 == 0 ) { twocount ++ ; n = n / 2 ; } return twocount ; } static int five_factor ( int n ) { int fivecount = 0 ; while ( n % 5 == 0 ) { fivecount ++ ; n = n / 5 ; } return fivecount ; } static int find_con_zero ( int [ ] arr , int n ) { int twocount = 0 ; int fivecount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { twocount += two_factor ( arr [ i ] ) ; fivecount += five_factor ( arr [ i ] ) ; } if ( twocount < fivecount ) return twocount ; else return fivecount ; } public static void Main ( ) { int [ ] arr = { 100 , 10 , 5 , 25 , 35 , 14 } ; int n = 6 ; Console . WriteLine ( find_con_zero ( arr , n ) ) ; } }
First occurrence of a digit in a given fraction | C # program to find first occurrence of c in a / b ; Function to print the first digit ; Reduce the number to its mod ; Traverse for every decimal places ; Get every fraction places when ( a * 10 / b ) / c ; Check if it is equal to the required integer ; Mod the number ; Driver function
using System ; public class GfG { public static int first ( int a , int b , int c ) { a %= b ; for ( int i = 1 ; i <= b ; i ++ ) { a = a * 10 ; if ( a / b == c ) return i ; a %= b ; } return - 1 ; } public static void Main ( ) { int a = 1 , b = 4 , c = 5 ; Console . WriteLine ( first ( a , b , c ) ) ; } }
Minimize the absolute difference of sum of two subsets | C # program for Minimize the absolute difference of sum of two subsets ; function to print difference ; summation of n elements ; if divisible by 4 ; if remainder 1 or 2. In case of remainder 2 , we divide elements from 3 to n in groups of size 4 and put 1 in one group and 2 in group . This also makes difference 1. ; We put elements from 4 to n in groups of size 4. Remaining elements 1 , 2 and 3 can be divided as ( 1 , 2 ) and ( 3 ) . ; Driver program to test above function
using System ; class GFG { static void subsetDifference ( int n ) { int s = n * ( n + 1 ) / 2 ; if ( n % 4 == 0 ) { Console . WriteLine ( " First ▁ " + " subset ▁ sum ▁ = ▁ " + s / 2 ) ; Console . WriteLine ( " Second ▁ " + " subset ▁ sum ▁ = ▁ " + s / 2 ) ; Console . WriteLine ( " Difference " + " ▁ = ▁ " + 0 ) ; } else { if ( n % 4 == 1 n % 4 == 2 ) { Console . WriteLine ( " First ▁ " + " subset ▁ sum ▁ = ▁ " + s / 2 ) ; Console . WriteLine ( " Second ▁ " + " subset ▁ sum ▁ = ▁ " + ( ( s / 2 ) + 1 ) ) ; Console . WriteLine ( " Difference " + " ▁ = ▁ " + 1 ) ; } else { Console . WriteLine ( " First ▁ " + " subset ▁ sum ▁ = ▁ " + s / 2 ) ; Console . WriteLine ( " Second ▁ " + " subset ▁ sum ▁ = ▁ " + s / 2 ) ; Console . WriteLine ( " Difference " + " ▁ = ▁ " + 0 ) ; } } } public static void Main ( ) { int n = 6 ; subsetDifference ( n ) ; } }
Time required to meet in equilateral triangle | C # code to find time taken by animals to meet ; function to calculate time to meet ; Driver Code
using System ; public class GFG { static void timeToMeet ( double s , double v ) { double V = 3 * v / 2 ; double time = s / V ; Console . WriteLine ( ( float ) time ) ; } static public void Main ( ) { double s = 25 , v = 56 ; timeToMeet ( s , v ) ; } }
Sum of the series 1 + ( 1 + 3 ) + ( 1 + 3 + 5 ) + ( 1 + 3 + 5 + 7 ) + Γ’ €¦ Γ’ €¦ + ( 1 + 3 + 5 + 7 + Γ’ €¦ + ( 2 n | C # implementation to find the sum of the given series ; functionn to find the sum of the given series ; first term of each i - th term ; next term ; required sum ; Driver program
using System ; class GFG { static int sumOfTheSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int k = 1 ; for ( int j = 1 ; j <= i ; j ++ ) { sum += k ; k += 2 ; } } return sum ; } public static void Main ( ) { int n = 5 ; Console . Write ( " Sum ▁ = ▁ " + sumOfTheSeries ( n ) ) ; } }
Check if a number can be written as sum of three consecutive integers | C # Code to check if a number can be written as sum of three consecutive integers . ; function to check if a number can be written as sum of three consecutive integer . ; if n is 0 ; if n is positive , increment loop by 1. ; if n is negative , decrement loop by 1. ; Running loop from 0 to n - 2 ; check if sum of three consecutive integer is equal to n . ; Driver program to test above function
using System ; class GFG { static void checksum ( int n ) { if ( n == 0 ) { Console . WriteLine ( " - 1 ▁ 0 ▁ 1" ) ; return ; } int inc ; if ( n > 0 ) inc = 1 ; else inc = - 1 ; for ( int i = 0 ; i <= n - 2 ; i += inc ) { if ( i + i + 1 + i + 2 == n ) { Console . WriteLine ( i + " ▁ " + ( i + 1 ) + " ▁ " + ( i + 2 ) ) ; return ; } } Console . WriteLine ( " - 1" ) ; } public static void Main ( ) { int n = 6 ; checksum ( n ) ; } }
Sum of all divisors from 1 to n | C # program to find sum of all divisor of number up to ' n ' ; Utility function to find sum of all divisor of number up to ' n ' ; Find all divisors of i and add them ; Driver code
using System ; class GFG { static int divisorSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = 1 ; j * j <= i ; ++ j ) { if ( i % j == 0 ) { if ( i / j == j ) sum += j ; else sum += j + i / j ; } } } return sum ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( divisorSum ( n ) ) ; n = 5 ; Console . WriteLine ( divisorSum ( n ) ) ; } }
Program for Binomial Coefficients table | C # program for binomial coefficients ; Function to print binomial table ; B ( m , x ) is 1 if either m or x is is 0. ; Otherwise using recursive formula B ( m , x ) = B ( m , x - 1 ) * ( m - x + 1 ) / x ; Driver Function
using System ; public class GFG { static void printbinomial ( int max ) { for ( int m = 0 ; m <= max ; m ++ ) { Console . Write ( m + " ▁ " ) ; int binom = 1 ; for ( int x = 0 ; x <= m ; x ++ ) { if ( m != 0 && x != 0 ) binom = binom * ( m - x + 1 ) / x ; Console . Write ( binom + " ▁ " ) ; } Console . WriteLine ( ) ; } } static public void Main ( ) { int max = 10 ; printbinomial ( max ) ; } }
Find largest prime factor of a number | C # program to find largest prime factor of number ; function to find largest prime factor ; Initialize the maximum prime factor variable with the lowest one ; Print the number of 2 s that divide n ; equivalent to n /= 2 ; n must be odd at this point ; now we have to iterate only for integers who does not have prime factor 2 and 3 ; This condition is to handle the case when n is a prime number greater than 4 ; Driver code
using System ; class GFG { static long maxPrimeFactors ( long n ) { long maxPrime = - 1 ; while ( n % 2 == 0 ) { maxPrime = 2 ; n >>= 1 ; } while ( n % 3 == 0 ) { maxPrime = 3 ; n = n / 3 ; } for ( int i = 5 ; i <= Math . Sqrt ( n ) ; i += 6 ) { while ( n % i == 0 ) { maxPrime = i ; n = n / i ; } while ( n % ( i + 2 ) == 0 ) { maxPrime = i + 2 ; n = n / ( i + 2 ) ; } } if ( n > 4 ) maxPrime = n ; return maxPrime ; } public static void Main ( ) { long n = 15L ; Console . WriteLine ( maxPrimeFactors ( n ) ) ; n = 25698751364526L ; Console . WriteLine ( maxPrimeFactors ( n ) ) ; } }
Count unset bits in a range | C # implementation to count unset bits in the given range ; Function to get no of set bits in the binary representation of ' n ' ; function to count unset bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; returns number of unset bits in the range ' l ' to ' r ' in ' n ' ; Driver code
using System ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int countUnsetBitsInGivenRange ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return ( r - l + 1 ) - countSetBits ( n & num ) ; } public static void Main ( ) { int n = 80 ; int l = 1 , r = 4 ; Console . Write ( countUnsetBitsInGivenRange ( n , l , r ) ) ; } }
Midy 's theorem | C # implementation as a proof of the Midy 's theorem ; Returns repeating sequence of a fraction . If repeating sequence doesn 't exits, then returns -1 ; Create a map to store already seen remainders remainder is used as key and its position in result is stored as value . Note that we need position for cases like 1 / 6. In this case , the recurring sequence doesn 't start from first remainder. ; Find first remainder ; Keep finding remainder until either remainder becomes 0 or repeats ; Store this remainder ; Multiply remainder with 10 ; Append rem / denr to result ; Update remainder ; Checks whether a number is prime or not ; If all conditions are met , it proves Midy 's theorem ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static String fractionToDecimal ( int numerator , int denominator ) { String res = " " ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int rem = numerator % denominator ; while ( ( rem != 0 ) && ! mp . ContainsKey ( rem ) ) { mp [ rem ] = res . Length ; rem = rem * 10 ; int res_part = rem / denominator ; res += res_part + " " ; rem = rem % denominator ; } return ( rem == 0 ) ? " - 1" : res . Substring ( mp [ rem ] ) ; } static bool isPrime ( int n ) { for ( int i = 2 ; i <= n / 2 ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void Midys ( String str , int n ) { int l = str . Length ; int part1 = 0 , part2 = 0 ; if ( ! isPrime ( n ) ) { Console . Write ( " Denominator ▁ is ▁ not ▁ prime , ▁ " + " thus ▁ Midy ' s ▁ theorem ▁ is ▁ not ▁ " + " applicable " ) ; } else if ( l % 2 == 0 ) { for ( int i = 0 ; i < l / 2 ; i ++ ) { part1 = part1 * 10 + ( str [ i ] - '0' ) ; part2 = part2 * 10 + ( str [ l / 2 + i ] - '0' ) ; } Console . WriteLine ( part1 + " ▁ + ▁ " + part2 + " ▁ = ▁ " + ( part1 + part2 ) ) ; Console . Write ( " Midy ' s ▁ theorem ▁ holds ! " ) ; } else { Console . Write ( " The ▁ repeating ▁ decimal ▁ is ▁ " + " of ▁ odd ▁ length ▁ thus ▁ Midy ' s ▁ " + " theorem ▁ is ▁ not ▁ applicable " ) ; } } public static void Main ( string [ ] args ) { int numr = 2 , denr = 11 ; string res = fractionToDecimal ( numr , denr ) ; if ( res == " - 1" ) Console . Write ( " The ▁ fraction ▁ does ▁ not ▁ " + " have ▁ repeating ▁ decimal " ) ; else { Console . WriteLine ( " Repeating ▁ decimal ▁ = ▁ " + res ) ; Midys ( res , denr ) ; } } }
Sum of fourth power of first n even natural numbers | C # Program to find the sum of fourth powers of first n even natural numbers ; calculate the sum of fourth power of first n even natural numbers ; made even number ; Driven Program
using System ; class GFG { static long evenPowerSum ( int n ) { long sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int j = 2 * i ; sum = sum + ( j * j * j * j ) ; } return sum ; } public static void Main ( ) { int n = 5 ; Console . Write ( evenPowerSum ( n ) ) ; } }
Sum of fourth power of first n even natural numbers | C # Program to find the sum of fourth powers of first n even natural numbers ; calculate the sum of fourth power of first n even natural numbers ; Driven Program
using System ; class GFG { static long evenPowerSum ( int n ) { return ( 8 * n * ( n + 1 ) * ( 2 * n + 1 ) * ( 3 * n * n + 3 * n - 1 ) ) / 15 ; } public static void Main ( ) { int n = 4 ; Console . Write ( evenPowerSum ( n ) ) ; } }
Balanced Prime | C # Program to find Nth Balanced Prime ; Return the Nth balanced prime . ; Sieve of Eratosthenes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; storing all primes ; Finding the Nth balanced Prime ; Driven Program
using System ; using System . Collections . Generic ; public class GFG { static int MAX = 501 ; public static int balancedprime ( int n ) { bool [ ] prime = new bool [ MAX + 1 ] ; for ( int k = 0 ; k < MAX + 1 ; k ++ ) prime [ k ] = 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 ; } } List < int > v = new List < int > ( ) ; for ( int p = 3 ; p <= MAX ; p += 2 ) if ( prime [ p ] ) v . Add ( p ) ; int c = 0 ; for ( int i = 1 ; i < v . Count - 1 ; i ++ ) { if ( ( int ) v [ i ] == ( int ) ( v [ i + 1 ] + v [ i - 1 ] ) / 2 ) c ++ ; if ( c == n ) return ( int ) v [ i ] ; } return 1 ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( balancedprime ( n ) ) ; } }
Smallest integer which has n factors or more | C # program to print the smallest integer with n factors or more ; array to store prime factors ; function to generate all prime factorsof numbers from 1 to 10 ^ 6 ; Initializes all the positions with their value . ; Initializes all multiples of 2 with 2 ; A modified version of Sieve of Eratosthenes to store the smallest prime factor that divides every number . ; check if it has no prime factor . ; Initializes of j starting from i * i ; if it has no prime factor before , then stores the smallest prime divisor ; function to calculate number of factors ; stores the smallest prime number that divides n ; stores the count of number of times a prime number divides n . ; reduces to the next number after prime factorization of n ; false when prime factorization is done ; if the same prime number is dividing n , then we increase the count ; if its a new prime factor that is factorizing n , then we again set c = 1 and change dup to the new prime factor , and apply the formula explained above . ; prime factorizes a number ; for the last prime factor ; function to find the smallest integer with n factors or more . ; check if no of factors is more than n or not ; Driver Code ; generate prime factors of number upto 10 ^ 6
using System ; class GfG { private static int MAX = 1000001 ; private static int [ ] factor = new int [ MAX ] ; public static void generatePrimeFactors ( ) { factor [ 1 ] = 1 ; for ( int i = 2 ; i < MAX ; i ++ ) factor [ i ] = i ; for ( int i = 4 ; i < MAX ; i += 2 ) factor [ i ] = 2 ; for ( int i = 3 ; i * i < MAX ; i ++ ) { if ( factor [ i ] == i ) { for ( int j = i * i ; j < MAX ; j += i ) { if ( factor [ j ] == j ) factor [ j ] = i ; } } } } public static int calculateNoOFactors ( int n ) { if ( n == 1 ) return 1 ; int ans = 1 ; int dup = factor [ n ] ; int c = 1 ; int j = n / factor [ n ] ; while ( j != 1 ) { if ( factor [ j ] == dup ) c += 1 ; else { dup = factor [ j ] ; ans = ans * ( c + 1 ) ; c = 1 ; } j = j / factor [ j ] ; } ans = ans * ( c + 1 ) ; return ans ; } public static int smallest ( int n ) { for ( int i = 1 ; ; i ++ ) if ( calculateNoOFactors ( i ) >= n ) return i ; } public static void Main ( ) { generatePrimeFactors ( ) ; int n = 4 ; Console . Write ( smallest ( n ) ) ; } }
Sum of squares of first n natural numbers | C # Program to find sum of square of first n natural numbers ; Return the sum of square of first n natural numbers ; Iterate i from 1 and n finding square of i and add to sum . ; Driven Program
using System ; class GFG { static int squaresum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( i * i ) ; return sum ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( squaresum ( n ) ) ; } }
Break a number such that sum of maximum divisors of all parts is minimum | C # Code for Break a number such that sum of maximum divisors of all parts is minimum ; Function to check if a number is prime or not . ; If n is an even number ( we can write it as sum of two primes ) ; If n is odd and n - 2 is prime . ; If n is odd , n - 3 must be even . ; Driver program
using System ; class GFG { static bool isPrime ( int n ) { int i = 2 ; while ( i * i <= n ) { if ( n % i == 0 ) return false ; i ++ ; } return true ; } static int minimumSum ( int n ) { if ( isPrime ( n ) ) return 1 ; if ( n % 2 == 0 ) return 2 ; if ( isPrime ( n - 2 ) ) return 2 ; return 3 ; } public static void Main ( ) { int n = 27 ; Console . WriteLine ( minimumSum ( n ) ) ; } }
Find first and last digits of a number | C # Program to find first and last digits of a number ; Find the first digit ; Remove last digit from number till only one digit is left ; return the first digit ; Find the last digit ; return the last digit ; driver function
using System ; public class GfG { public static int firstDigit ( int n ) { while ( n >= 10 ) n /= 10 ; return n ; } public static int lastDigit ( int n ) { return ( n % 10 ) ; } public static void Main ( ) { int n = 98562 ; Console . WriteLine ( firstDigit ( n ) + " ▁ " + lastDigit ( n ) ) ; } }
Find first and last digits of a number | C # program to find first and last digits of a number ; Find the first digit ; Find total number of digits - 1 ; Find first digit ; Return first digit ; Find the last digit ; return the last digit ; Driver program
using System ; class GFG { static int firstDigit ( int n ) { int digits = ( int ) ( Math . Log10 ( n ) ) ; n = ( int ) ( n / ( int ) ( Math . Pow ( 10 , digits ) ) ) ; return n ; } static int lastDigit ( int n ) { return ( n % 10 ) ; } public static void Main ( ) { int n = 98562 ; Console . WriteLine ( firstDigit ( n ) + " ▁ " + lastDigit ( n ) ) ; } }
Express an odd number as sum of prime numbers | C # program to express N as sum of at - most three prime numbers . ; Function to check if a number is prime or not . ; Prints at most three prime numbers whose sum is n . ; if ( isPrime ( n ) ) CASE - I ; else if ( isPrime ( n - 2 ) ) CASE - II ; else CASE - III ; Driver code
using System ; class GFG { public static bool isPrime ( int x ) { if ( x == 0 x == 1 ) return false ; for ( int i = 2 ; i * i <= x ; ++ i ) if ( x % i == 0 ) return false ; return true ; } public static void findPrimes ( int n ) { Console . WriteLine ( n ) ; Console . Write ( 2 + " ▁ " + ( n - 2 ) ) ; { Console . Write ( 3 + " ▁ " ) ; n = n - 3 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime ( i ) && isPrime ( n - i ) ) { Console . WriteLine ( i + " ▁ " + ( n - i ) ) ; break ; } } } } public static void Main ( ) { int n = 27 ; findPrimes ( n ) ; } }
AKS Primality Test | C # code to check if number is prime . This program demonstrates concept behind AKS algorithm and doesn 't implement the actual algorithm (This works only till n = 64) ; array used to store coefficients . ; function to calculate the coefficients of ( x - 1 ) ^ n - ( x ^ n - 1 ) with the help of Pascal 's triangle . ; function to check whether the number is prime or not ; Calculating all the coefficients by the function coef and storing all the coefficients in c array . ; subtracting c [ n ] and adding c [ 0 ] by 1 as ( x - 1 ) ^ n - ( x ^ n - 1 ) , here we are subtracting c [ n ] by 1 and adding 1 in expression . ; checking all the coefficients whether they are divisible by n or not . if n is not prime , then loop breaks and ( i > 0 ) . ; Return true if all coefficients are divisible by n . ; Driver code
using System ; class GFG { static long [ ] c = new long [ 100 ] ; static void coef ( int n ) { c [ 0 ] = 1 ; for ( int i = 0 ; i < n ; c [ 0 ] = - c [ 0 ] , i ++ ) { c [ 1 + i ] = 1 ; for ( int j = i ; j > 0 ; j -- ) c [ j ] = c [ j - 1 ] - c [ j ] ; } } static bool isPrime ( int n ) { coef ( n ) ; c [ 0 ] ++ ; c [ n ] -- ; int i = n ; while ( ( i -- ) > 0 && c [ i ] % n == 0 ) ; return i < 0 ; } public static void Main ( ) { int n = 37 ; if ( isPrime ( n ) ) Console . WriteLine ( " Prime " ) ; else Console . WriteLine ( " Not ▁ Prime " ) ; } }
Motzkin number | C # Program to find Nth Motzkin Number . ; Return the nth Motzkin Number . ; Base Case ; Recursive step ; driver code
using System ; class GFG { public static int motzkin ( int n ) { if ( n == 0 n == 1 ) return 1 ; return ( ( 2 * n + 1 ) * motzkin ( n - 1 ) + ( 3 * n - 3 ) * motzkin ( n - 2 ) ) / ( n + 2 ) ; } public static void Main ( ) { int n = 8 ; Console . WriteLine ( motzkin ( n ) ) ; } }
Sum of the series 0.6 , 0.06 , 0.006 , 0.0006 , ... to n terms | C # program to find sum of 0.6 , 0.06 , 0.006 , 0.0006 , ... to n terms ; function which return the the sum of series ; Driver code
using System ; class GFG { static double sumOfSeries ( int n ) { return ( 0.666 ) * ( 1 - 1 / Math . Pow ( 10 , n ) ) ; } public static void Main ( ) { int n = 2 ; Console . WriteLine ( sumOfSeries ( n ) ) ; } }
Narcissistic number | C # program for checking of Narcissistic number ; function to count digits ; Returns true if n is Narcissistic number ; count the number of digits ; calculates the sum of digits raised to power ; Driver code
using System ; class narcissistic { int countDigit ( int n ) { if ( n == 0 ) return 0 ; return 1 + countDigit ( n / 10 ) ; } bool check ( int n ) { int l = countDigit ( n ) ; int dup = n ; int sum = 0 ; while ( dup > 0 ) { sum += ( int ) Math . Pow ( dup % 10 , l ) ; dup /= 10 ; } return ( n == sum ) ; } public static void Main ( ) { narcissistic obj = new narcissistic ( ) ; int n = 1634 ; if ( obj . check ( n ) ) Console . WriteLine ( " yes " ) ; else Console . WriteLine ( " no " ) ; } }
Sum of squares of first n natural numbers | C # program to calculate 1 ^ 2 + 2 ^ 2 + 3 ^ 2 + ... ; Function to calculate sum ; Driver code
using System ; class GFG { public static int summation ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( i * i ) ; return sum ; } public static void Main ( ) { int n = 2 ; Console . WriteLine ( summation ( n ) ) ; } }
Leyland Number | C # program to print first N Leyland Numbers . ; Print first n Leyland Number . ; Outer loop for x from 2 to n . ; Inner loop for y from 2 to x . ; Calculating x ^ y + y ^ x ; Sorting the all Leyland Number . ; Printing first n Leyland number . ; Driver Code
using System ; using System . Collections ; class GFG { public static void leyland ( int n ) { ArrayList ans = new ArrayList ( ) ; for ( int x = 2 ; x <= n ; x ++ ) { for ( int y = 2 ; y <= x ; y ++ ) { int temp = ( int ) Math . Pow ( x , y ) + ( int ) Math . Pow ( y , x ) ; ans . Add ( temp ) ; } } ans . Sort ( ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } public static void Main ( ) { int n = 6 ; leyland ( n ) ; } }
NicomachusΓ’ €ℒ s Theorem ( Sum of k | C # code to find sum of k - th group of positive odd integers . ; Return the sum of k - th group of positive odd integers . ; Finding first element of kth group . ; Finding the sum . ; Driver program to test above methods
using System ; class GFG { public static int kthgroupsum ( int k ) { int cur = ( k * ( k - 1 ) ) + 1 ; int sum = 0 ; while ( k -- > 0 ) { sum += cur ; cur += 2 ; } return sum ; } public static void Main ( ) { int k = 3 ; Console . WriteLine ( kthgroupsum ( k ) ) ; } }
n | C # program to find n - th term of the series 2 , 12 , 36 , 80 , 150 , . . ; Returns n - th term of the series 2 , 12 , 36 , 80 , 150 ; Driver code
using System ; class GFG { public static int nthTerm ( int n ) { return ( n * n ) + ( n * n * n ) ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( nthTerm ( n ) ) ; } }
Sum of the series 1 , 3 , 6 , 10. . . ( Triangular Numbers ) | C # program to find sum series 1 , 3 , 6 , 10 , 15 , 21. . . and then find its sum ; Function to find the sum of series ; Driver code
using System ; class GFG { static int seriesSum ( int n ) { return ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( seriesSum ( n ) ) ; } }
Calculate speed , distance and time | C # Program to calculate speed distance and time ; Function to calculate speed ; Function to calculate distance traveled ; Function to calculate time taken ; Driver code ; Calling function cal_speed ( ) ; Calling function cal_dis ( ) ; Calling function cal_time ( )
using System ; class GFG { static double cal_speed ( double dist , double time ) { Console . WriteLine ( " ▁ Distance ( km ) ▁ : ▁ " + dist ) ; Console . WriteLine ( " ▁ Time ( hr ) ▁ : ▁ " + time ) ; return dist / time ; } static double cal_dis ( double speed , double time ) { Console . WriteLine ( " ▁ Time ( hr ) ▁ : ▁ " + time ) ; Console . WriteLine ( " ▁ Speed ( km ▁ / ▁ hr ) ▁ : ▁ " + speed ) ; return speed * time ; } static double cal_time ( double dist , double speed ) { Console . WriteLine ( " ▁ Distance ( km ) ▁ : ▁ " + dist ) ; Console . WriteLine ( " ▁ Speed ( km ▁ / ▁ hr ) ▁ : ▁ " + speed ) ; return speed * dist ; } public static void Main ( ) { Console . WriteLine ( " ▁ The ▁ calculated ▁ Speed ( km ▁ / ▁ hr ) ▁ is ▁ : ▁ " + cal_speed ( 45.9 , 2.0 ) ) ; Console . WriteLine ( " ▁ The ▁ calculated ▁ Distance ( km ) ▁ : ▁ " + cal_dis ( 62.9 , 2.5 ) ) ; Console . WriteLine ( " ▁ The ▁ calculated ▁ Time ( hr ) ▁ : ▁ " + cal_time ( 48.0 , 4.5 ) ) ; } }
Find n | C # program to find n - th term of series 1 , 3 , 6 , 10 , 15 , 21. . . ; Function to find the nth term of series ; Loop to add numbers ; Driver code
using System ; class GFG { static int term ( int n ) { int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) ans += i ; return ans ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( term ( n ) ) ; } }
Find the average of first N natural numbers | C # Program to find the Average of first n natural numbers ; Return the average of first n natural numbers ; Driven Program
using System ; class GFG { static float avgOfFirstN ( int n ) { return ( float ) ( 1 + n ) / 2 ; } public static void Main ( ) { int n = 20 ; Console . WriteLine ( avgOfFirstN ( n ) ) ; } }
Find the sum of the series 1 + 11 + 111 + 1111 + ... . . upto n terms | C # program to find the sum of the series 1 + 11 + 111 + 1111 + ... . ; Function for finding summation ; Driver Code
using System ; class GFG { static int summation ( int n ) { int sum ; sum = ( int ) ( Math . Pow ( 10 , n + 1 ) - 10 - ( 9 * n ) ) / 81 ; return sum ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( summation ( n ) ) ; } }
Sum of the Series 1 + x / 1 + x ^ 2 / 2 + x ^ 3 / 3 + . . + x ^ n / n | C # program to find sum of series 1 + x ^ 2 / 2 + x ^ 3 / 3 + ... . + x ^ n / n ; Java code to print the sum of the given series ; Driver code
using System ; class GFG { static float sum ( int x , int n ) { double i , total = 1.0 , multi = x ; for ( i = 1 ; i <= n ; i ++ ) { total = total + multi / i ; multi = multi * x ; } return ( float ) total ; } public static void Main ( ) { int x = 2 ; int n = 5 ; Console . WriteLine ( sum ( x , n ) ) ; } }
Find n | C # program to find the nth term of the series 1 2 2 3 3 3 ... ; function to solve the quadratic equation ; calculating the Nth term ; driver code to check the above function
using System ; class Series { static int term ( int n ) { int x = ( ( ( 1 ) + ( int ) Math . Sqrt ( 1 + ( 8 * n ) ) ) / 2 ) ; return x ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( term ( n ) ) ; } }
Deserium Number | C # program to check whether a number is Deserium number or not ; Returns count of digits in n . ; Returns true if x is Diserium ; Compute powers of digits from right to left . ; If sum of powers is same as given number . ; Driver code
using System ; class Deserium { static int countDigits ( int n ) { int c = 0 ; do { c ++ ; n = n / 10 ; } while ( n != 0 ) ; return c ; } static bool isDeserium ( int x ) { int temp = x ; int p = countDigits ( x ) ; int sum = 0 ; while ( x != 0 ) { int digit = x % 10 ; sum += ( int ) Math . Pow ( digit , p ) ; p -- ; x = x / 10 ; } return ( sum == temp ) ; } public static void Main ( ) { int x = 135 ; if ( isDeserium ( x ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Largest number by which given 3 numbers should be divided such that they leaves same remainder | C # program to find the largest numbers that leads to same remainder when divides given three sorted numbers ; gcd function ; function return number which divides these three number and leaves same remainder . ; We find the differences of all three pairs ; Return GCD of three differences . ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int sameRemainder ( int a , int b , int c ) { int a1 = ( b - a ) , b1 = ( c - b ) , c1 = ( c - a ) ; return gcd ( a1 , gcd ( b1 , c1 ) ) ; } public static void Main ( ) { int a = 62 , b = 132 , c = 237 ; Console . WriteLine ( sameRemainder ( a , b , c ) ) ; } }
Find combined mean and variance of two series | C # program to find combined mean and variance of two series . ; Function to find mean of series . ; Function to find the standard deviation of series . ; Function to find combined variance of two different series . ; mean1 and mean2 are the mean of two arrays . ; sd1 and sd2 are the standard deviation of two array . ; combinedMean is variable to store the combined mean of both array . ; d1_square and d2_square are the combined mean deviation . ; combinedVar is variable to store combined variance of both array . ; Driver code ; Function call to combined mean .
using System ; class GFG { static float mean ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; float mean = ( float ) sum / n ; return mean ; } static float sd ( int [ ] arr , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( arr [ i ] - mean ( arr , n ) ) * ( arr [ i ] - mean ( arr , n ) ) ; float sdd = sum / n ; return sdd ; } static float combinedVariance ( int [ ] arr1 , int [ ] arr2 , int n , int m ) { float mean1 = mean ( arr1 , n ) ; float mean2 = mean ( arr2 , m ) ; Console . Write ( " Mean1 : ▁ " + mean1 + " ▁ " ) ; Console . WriteLine ( " Mean2 : ▁ " + mean2 ) ; float sd1 = sd ( arr1 , n ) ; float sd2 = sd ( arr2 , m ) ; Console . Write ( " StandardDeviation1 : ▁ " + sd1 + " ▁ " ) ; Console . WriteLine ( " StandardDeviation2 : ▁ " + sd2 + " ▁ " ) ; float combinedMean = ( float ) ( n * mean1 + m * mean2 ) / ( n + m ) ; Console . WriteLine ( " Combined ▁ Mean : ▁ " + combinedMean + " ▁ " ) ; float d1_square = ( mean1 - combinedMean ) * ( mean1 - combinedMean ) ; float d2_square = ( mean2 - combinedMean ) * ( mean2 - combinedMean ) ; Console . Write ( " d1 ▁ square : ▁ " + d1_square + " ▁ " ) ; Console . WriteLine ( " d2 _ square : ▁ " + d2_square ) ; float combinedVar = ( n * ( sd1 + d1_square ) + m * ( sd2 + d2_square ) ) / ( n + m ) ; return combinedVar ; } public static void Main ( ) { int [ ] arr1 = { 23 , 45 , 34 , 78 , 12 , 76 , 34 } ; int [ ] arr2 = { 65 , 67 , 34 , 23 , 45 } ; int n = arr1 . Length ; int m = arr2 . Length ; Console . WriteLine ( " Combined ▁ Variance : ▁ " + combinedVariance ( arr1 , arr2 , n , m ) ) ; } }
Check if a large number is divisible by 13 or not | C # program to check whether a number is divisible by 13 or not . ; Returns true if number is divisible by 13 else returns false ; Append required 0 s . at the beginning . ; Same as strcat ( num , "00" ) ; in c . ; Same as strcat ( num , "0" ) ; in c . ; Alternatively add / subtract digits in group of three to result . ; Store group of three numbers in group variable . ; Generate alternate series of plus and minus ; Driver code
using System ; class GFG { static bool checkDivisibility ( string num ) { int length = num . Length ; if ( length == 1 && num [ 0 ] == '0' ) return true ; if ( length % 3 == 1 ) { num += "00" ; length += 2 ; } else if ( length % 3 == 2 ) { num += "0" ; length += 1 ; } int sum = 0 , p = 1 ; for ( int i = length - 1 ; i >= 0 ; i -- ) { int group = 0 ; group += num [ i -- ] - '0' ; group += ( num [ i -- ] - '0' ) * 10 ; group += ( num [ i ] - '0' ) * 100 ; sum = sum + group * p ; p *= ( - 1 ) ; } sum = Math . Abs ( sum ) ; return ( sum % 13 == 0 ) ; } static void Main ( ) { string number = "83959092724" ; if ( checkDivisibility ( number ) ) Console . Write ( number + " ▁ is ▁ divisible ▁ by ▁ 13 . " ) ; else Console . Write ( number + " ▁ is ▁ not ▁ divisible ▁ by ▁ 13 . " ) ; } }
Given two numbers a and b find all x such that a % x = b | C # program to find x such that a % x is equal to b . ; if a is less than b then no solution ; if a is equal to b then every number greater than a will be the solution so its infinity ; count variable store the number of values possible ; checking for both divisor and quotient whether they divide ( a - b ) completely and greater than b . ; Here y is added twice in the last iteration so 1 y should be decremented to get correct solution ; Driver code
using System ; class GFG { static void modularEquation ( int a , int b ) { if ( a < b ) { Console . WriteLine ( " No ▁ solution ▁ possible ▁ " ) ; return ; } if ( a == b ) { Console . WriteLine ( " Infinite ▁ Solution ▁ possible ▁ " ) ; return ; } int count = 0 ; int n = a - b ; int y = ( int ) Math . Sqrt ( a - b ) ; for ( int i = 1 ; i <= y ; ++ i ) { if ( n % i == 0 ) { if ( n / i > b ) count ++ ; if ( i > b ) count ++ ; } } if ( y * y == n && y > b ) count -- ; Console . WriteLine ( count ) ; } public static void Main ( ) { int a = 21 , b = 5 ; modularEquation ( a , b ) ; } }
Count different numbers that can be generated such that there digits sum is equal to ' n ' | C # program to count ways to write ' n ' as sum of digits ; Function to count ' num ' as sum of digits ( 1 , 2 , 3 , 4 ) ; Initialize dp [ ] array ; Base case ; Initialize the current dp [ ] array as '0' ; if i == j then there is only one way to write with element itself ' i ' ; If j == 1 , then there exist two ways , one from '1' and other from '4' ; if i - j is positive then pick the element from ' i - j ' element of dp [ ] array ; Check for modulas ; return the final answer ; Driver code
using System ; public class GFG { static int countWays ( int num ) { int [ ] dp = new int [ num + 1 ] ; int MOD = ( int ) 1E9 + 7 ; dp [ 1 ] = 2 ; for ( int i = 2 ; i <= num ; ++ i ) { dp [ i ] = 0 ; for ( int j = 1 ; j <= 3 ; ++ j ) { if ( i - j == 0 ) dp [ i ] += 1 ; else if ( j == 1 ) dp [ i ] += dp [ i - j ] * 2 ; else if ( i - j > 0 ) dp [ i ] += dp [ i - j ] ; if ( dp [ i ] >= MOD ) dp [ i ] %= MOD ; } } return dp [ num ] ; } static public void Main ( String [ ] args ) { int n = 3 ; Console . WriteLine ( countWays ( n ) ) ; } }
Check whether a number can be represented by sum of two squares | An efficient approach based implementation to find if a number can be written as sum of two squares . ; function to check if there exist two numbers sum of whose squares is n . ; store square value in hashmap ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool sumSquare ( int n ) { Dictionary < int , int > s = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i * i <= n ; ++ i ) { s . Add ( i * i , 1 ) ; if ( s . ContainsKey ( n - i * i ) ) { Console . WriteLine ( ( int ) Math . Sqrt ( n - i * i ) + " ^ 2 ▁ + ▁ " + i + " ^ 2" ) ; return true ; } } return false ; } public static void Main ( String [ ] args ) { int n = 169 ; Console . WriteLine ( sumSquare ( n ) ? " YES " : " NO " ) ; } }
Check whether a number can be represented by sum of two squares | C # program to Check whether a number can be represented by sum of two squares using Fermat Theorem . ; Count all the prime factors . ; If any prime factor of the form ( 4 k + 3 ) ( 4 k + 3 ) occurs an odd number of times . ; If n itself is a prime number and can be expressed in the form of 4 k + 3 we return false . ; Driver Code
using System ; class GFG { public static bool judgeSquareSum ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { int count = 0 ; if ( n % i == 0 ) { while ( n % i == 0 ) { count ++ ; n /= i ; } if ( i % 4 == 3 && count % 2 != 0 ) return false ; } } return n % 4 != 3 ; } static public void Main ( ) { int n = 17 ; if ( judgeSquareSum ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Total no of 1 's in numbers | C # code to count the frequency of 1 in numbers less than or equal to the given number . ; function to count the frequency of 1. ; Driver Code
using System ; class GFG { static int countDigitOne ( int n ) { int countr = 0 ; for ( int i = 1 ; i <= n ; i *= 10 ) { int divider = i * 10 ; countr += ( n / divider ) * i + Math . Min ( Math . Max ( n % divider - i + 1 , 0 ) , i ) ; } return countr ; } public static void Main ( ) { int n = 13 ; Console . WriteLine ( countDigitOne ( n ) ) ; n = 113 ; Console . WriteLine ( countDigitOne ( n ) ) ; n = 205 ; Console . WriteLine ( countDigitOne ( n ) ) ; } }
Divisors of n | C # program to count number of divisors of n ^ 2 which are not divisible by divisor of n ; Function to count divisors of n ^ 2 having no factors of ' n ' ; Increment count of i - th prime divisor ; Find next prime divisor ; Increment count if divisor still remains ; Initialize variable for counting the factors of n ^ 2 and n as ans1 and ans2 respectively ; Range based for - loop ; Use formula as discussed in above ; return the difference of answers ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int factors ( int n ) { Dictionary < int , int > prime = new Dictionary < int , int > ( ) ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; ++ i ) { while ( n % i == 0 ) { if ( prime . ContainsKey ( i ) ) { prime [ i ] = prime [ i ] + 1 ; } else { prime . Add ( i , 1 ) ; } n = n / i ; } } if ( n > 2 ) { if ( prime . ContainsKey ( n ) ) { prime [ n ] = prime [ n ] + 1 ; } else { prime . Add ( n , 1 ) ; } } int ans1 = 1 , ans2 = 1 ; foreach ( KeyValuePair < int , int > it in prime ) { ans1 *= 2 * it . Value + 1 ; ans2 *= it . Value + 1 ; } return ans1 - ans2 ; } public static void Main ( String [ ] args ) { int n = 5 ; Console . WriteLine ( factors ( n ) ) ; n = 8 ; Console . WriteLine ( factors ( n ) ) ; } }
Print digit 's position to be removed to make a number divisible by 6 | C # program to print digit 's position to be removed to make number divisible by 6 ; function to print the number divisible by 6 after exactly removing a digit ; stores the sum of all elements ; traverses the string and converts string to number array and sums up ; if ( a [ n - 1 ] % 2 != 0 ) ODD CHECK ; if second last is odd or sum of n - 1 elements are not divisible by 3. ; second last is even and print n - 1 elements removing last digit ; last digit removed ; counter to check if any element after removing , its sum % 3 == 0 ; traverse till second last element ; to check if any element after removing , its sum % 3 == 0 ; the leftmost element ; break at the leftmost element ; stores the right most element ; if no element has been found as a [ i + 1 ] > a [ i ] ; if second last is even , then remove last if ( sum - last ) % 3 == 0 ; if no element which on removing gives sum % 3 == 0 ; Driver Code
using System ; class GFG { static void greatest ( string s ) { int n = s . Length ; int [ ] a = new int [ n ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = s [ i ] - '0' ; sum += a [ i ] ; } { if ( a [ n - 2 ] % 2 != 0 || ( sum - a [ n - 1 ] ) % 3 != 0 ) { Console . Write ( " - 1" ) ; } else { Console . Write ( n ) ; } } else { int re = sum % 3 ; int del = - 1 ; int flag = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( ( a [ i ] ) % 3 == re ) { if ( a [ i + 1 ] > a [ i ] ) { del = i ; flag = 1 ; break ; } else { del = i ; } } } if ( flag == 0 ) { if ( a [ n - 2 ] % 2 == 0 && re == a [ n - 1 ] % 3 ) del = n - 1 ; } if ( del == - 1 ) Console . Write ( - 1 ) ; else { Console . Write ( del + 1 ) ; } } } public static void Main ( ) { string s = "7510222" ; greatest ( s ) ; } }
Representation of a number in powers of other | C # program to check if m can be represented as powers of w . ; break ; None of 3 worked . ; If m is not zero means , it can 't be represented in terms of powers of w. ; Driver Code
using System ; class GFG { static bool asPowerSum ( int w , int m ) { while ( m > 0 ) { if ( ( m - 1 ) % w == 0 ) m = ( m - 1 ) / w ; else if ( ( m + 1 ) % w == 0 ) m = ( m + 1 ) / w ; else if ( m % w == 0 ) m = m / w ; else } return ( m == 0 ) ; } static public void Main ( ) { int w = 3 , m = 7 ; if ( asPowerSum ( w , m ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Number of digits to be removed to make a number divisible by 3 | C # program to find the minimum number of digits to be removed to make a large number divisible by 3. ; function to count the no of removal of digits to make a very large number divisible by 3 ; add up all the digits of num ; if num is already is divisible by 3 then no digits are to be removed ; if there is single digit , then it is not possible to remove one digit . ; traverse through the number and find out if any number on removal makes the sum divisible by 3 ; if there are two numbers then it is not possible to remove two digits . ; Otherwise we can always make a number multiple of 2 by removing 2 digits . ; Driver Code
using System ; class GFG { static int divisible ( String num ) { int n = num . Length ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( int ) ( num [ i ] ) ; if ( sum % 3 == 0 ) return 0 ; if ( n == 1 ) return - 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( sum % 3 == ( num [ i ] - '0' ) % 3 ) return 1 ; if ( n == 2 ) return - 1 ; return 2 ; } public static void Main ( ) { string num = "1234" ; Console . WriteLine ( divisible ( num ) ) ; } }
Program for dot product and cross product of two vectors | C # implementation for dot product and cross product of two vector . ; Function that return dot product of two vector array . ; Loop for calculate cot product ; Function to find cross product of two vector array . ; Driver code ; dotProduct function call ; crossProduct function call ; Loop that print cross product of two vector array .
using System ; class GFG { static int n = 3 ; static int dotProduct ( int [ ] vect_A , int [ ] vect_B ) { int product = 0 ; for ( int i = 0 ; i < n ; i ++ ) product = product + vect_A [ i ] * vect_B [ i ] ; return product ; } static void crossProduct ( int [ ] vect_A , int [ ] vect_B , int [ ] cross_P ) { cross_P [ 0 ] = vect_A [ 1 ] * vect_B [ 2 ] - vect_A [ 2 ] * vect_B [ 1 ] ; cross_P [ 1 ] = vect_A [ 2 ] * vect_B [ 0 ] - vect_A [ 0 ] * vect_B [ 2 ] ; cross_P [ 2 ] = vect_A [ 0 ] * vect_B [ 1 ] - vect_A [ 1 ] * vect_B [ 0 ] ; } public static void Main ( ) { int [ ] vect_A = { 3 , - 5 , 4 } ; int [ ] vect_B = { 2 , 6 , 5 } ; int [ ] cross_P = new int [ n ] ; Console . Write ( " Dot ▁ product : " ) ; Console . WriteLine ( dotProduct ( vect_A , vect_B ) ) ; Console . Write ( " Cross ▁ product : " ) ; crossProduct ( vect_A , vect_B , cross_P ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( cross_P [ i ] + " ▁ " ) ; } }
Number of n digit numbers that do not contain 9 | C # program to find number of n digit numbers that do not contain 9 as it 's digit ; function to find number of n digit numbers possible ; Driver Code
using System ; public class GFG { static int totalNumber ( int n ) { return 8 * ( int ) Math . Pow ( 9 , n - 1 ) ; } static public void Main ( ) { int n = 3 ; Console . WriteLine ( totalNumber ( n ) ) ; } }
Count ways to express even number Γ’ β‚¬Λœ nΓ’ €ℒ as sum of even integers | C # program to count ways to write number as sum of even integers ; Initialize mod variable as constant ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; 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 ; Return number of ways to write ' n ' as sum of even integers ; Driver code
using System ; class GFG { static int MOD = 1000000007 ; static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( 1 * res * x ) % p ; x = ( 1 * x * x ) % p ; } return res ; } static int countEvenWays ( int n ) { return power ( 2 , n / 2 - 1 , MOD ) ; } public static void Main ( ) { int n = 6 ; Console . WriteLine ( countEvenWays ( n ) ) ; n = 8 ; Console . WriteLine ( countEvenWays ( n ) ) ; } }
Number of steps to convert to prime factors | C # program to count number of steps required to convert an integer array to array of factors . ; array to store prime factors ; function to generate all prime factors of numbers from 1 to 10 ^ 6 ; Initializes all the positions with their value . ; Initializes all multiples of 2 with 2 ; A modified version of Sieve of Eratosthenes to store the smallest prime factor that divides every number . ; check if it has no prime factor . ; Initializes of j starting from i * i ; if it has no prime factor before , then stores the smallest prime divisor ; function to calculate the number of representations ; keep an count of prime factors ; traverse for every element ; count the no of factors ; subtract 1 if Ai is not 1 as the last step wont be taken into count ; Driver code ; call sieve to calculate the factors
using System ; class GFG { static int MAX = 1000001 ; static int [ ] factor = new int [ MAX ] ; static void cal_factor ( ) { factor [ 1 ] = 1 ; for ( int i = 2 ; i < MAX ; i ++ ) factor [ i ] = i ; for ( int i = 4 ; i < MAX ; i += 2 ) factor [ i ] = 2 ; for ( int i = 3 ; i * i < MAX ; i ++ ) { if ( factor [ i ] == i ) { for ( int j = i * i ; j < MAX ; j += i ) { if ( factor [ j ] == j ) factor [ j ] = i ; } } } } static int no_of_representations ( int [ ] a , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int temp = a [ i ] ; int flag = 0 ; while ( factor [ temp ] != 1 ) { flag = - 1 ; count ++ ; temp = temp / factor [ temp ] ; } count += flag ; } return count ; } public static void Main ( ) { cal_factor ( ) ; int [ ] a = { 4 , 4 , 4 } ; int n = a . Length ; Console . WriteLine ( no_of_representations ( a , n ) ) ; } }
Subsequences of size three in an array whose sum is divisible by m | C # program to find count of subsequences of size three divisible by M . ; Three nested loop to find all the sub sequences of length three in the given array A [ ] . ; checking if the sum of the chosen three number is divisible by m . ; Driver code
using System ; class GFG { static int coutSubSeq ( int [ ] A , int N , int M ) { int sum = 0 ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { for ( int k = j + 1 ; k < N ; k ++ ) { sum = A [ i ] + A [ j ] + A [ k ] ; if ( sum % M == 0 ) ans ++ ; } } } return ans ; } public static void Main ( ) { int M = 3 ; int [ ] A = { 1 , 2 , 4 , 3 } ; int N = A . Length ; Console . WriteLine ( coutSubSeq ( A , N , M ) ) ; } }
Subsequences of size three in an array whose sum is divisible by m | C # program to find count of subsequences of size three divisible by M . ; Storing frequencies of all remainders when divided by M . ; including i and j in the sum rem calculate the remainder required to make the sum divisible by M ; if the required number is less than j , we skip as we have already calculated for that value before . As j here starts with i and rem is less than j . ; if satisfies the first case . ; if satisfies the second case ; if satisfies the third case ; Driver code
using System ; class GFG { static int countSubSeq ( int [ ] A , int N , int M ) { int ans = 0 ; int [ ] h = new int [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { A [ i ] = A [ i ] % M ; h [ A [ i ] ] ++ ; } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = i ; j < M ; j ++ ) { int rem = ( M - ( i + j ) % M ) % M ; if ( rem < j ) continue ; if ( i == j && rem == j ) ans += h [ i ] * ( h [ i ] - 1 ) * ( h [ i ] - 2 ) / 6 ; else if ( i == j ) ans += h [ i ] * ( h [ i ] - 1 ) * h [ rem ] / 2 ; else if ( i == rem ) ans += h [ i ] * ( h [ i ] - 1 ) * h [ j ] / 2 ; else if ( rem == j ) ans += h [ j ] * ( h [ j ] - 1 ) * h [ i ] / 2 ; else ans = ans + h [ i ] * h [ j ] * h [ rem ] ; } } return ans ; } public static void Main ( ) { int M = 3 ; int [ ] A = { 1 , 2 , 4 , 3 } ; int N = A . Length ; Console . WriteLine ( countSubSeq ( A , N , M ) ) ; } }
Find n | C # program to find nth term ; utility function ; since first element of the series is 7 , we initialise a variable with 7 ; Using iteration to find nth term ; Driver code
using System ; class GFG { static int findTerm ( int n ) { if ( n == 1 ) return n ; else { int term = 7 ; for ( int i = 2 ; i <= n ; i ++ ) term = term * 2 + ( i - 1 ) ; return term ; } } public static void Main ( ) { int n = 5 ; Console . WriteLine ( findTerm ( n ) ) ; } }
Find n | C # program to find the value at n - th place in the given sequence ; Returns n - th number in sequence 1 , 1 , 2 , 1 , 2 , 3 , 1 , 2 , 4 , ... ; One by one subtract counts elements in different blocks ; Driver code
using System ; class GFG { static int findNumber ( int n ) { n -- ; int i = 1 ; while ( n >= 0 ) { n -= i ; ++ i ; } return ( n + i ) ; } public static void Main ( ) { int n = 3 ; Console . WriteLine ( findNumber ( n ) ) ; } }
Program to find correlation coefficient | C # Program to find correlation coefficient ; function that returns correlation coefficient . ; sum of elements of array X . ; sum of elements of array Y . ; sum of X [ i ] * Y [ i ] . ; sum of square of array elements . ; use formula for calculating correlation coefficient . ; Driver function ; Find the size of array . ; Function call to correlationCoefficient .
using System ; class GFG { static float correlationCoefficient ( int [ ] X , int [ ] Y , int n ) { int sum_X = 0 , sum_Y = 0 , sum_XY = 0 ; int squareSum_X = 0 , squareSum_Y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum_X = sum_X + X [ i ] ; sum_Y = sum_Y + Y [ i ] ; sum_XY = sum_XY + X [ i ] * Y [ i ] ; squareSum_X = squareSum_X + X [ i ] * X [ i ] ; squareSum_Y = squareSum_Y + Y [ i ] * Y [ i ] ; } float corr = ( float ) ( n * sum_XY - sum_X * sum_Y ) / ( float ) ( Math . Sqrt ( ( n * squareSum_X - sum_X * sum_X ) * ( n * squareSum_Y - sum_Y * sum_Y ) ) ) ; return corr ; } public static void Main ( ) { int [ ] X = { 15 , 18 , 21 , 24 , 27 } ; int [ ] Y = { 25 , 25 , 27 , 31 , 32 } ; int n = X . Length ; Console . Write ( Math . Round ( correlationCoefficient ( X , Y , n ) * 1000000.0 ) / 1000000.0 ) ; } }
Find the number of spectators standing in the stadium at time t | C # program to find number of spectators standing at a time ; If the time is less than k then we can print directly t time . ; If the time is n then k spectators are standing . ; Otherwise we calculate the spectators standing . ; Driver code ; Stores the value of n , k and t t is time n & k is the number of specators
using System ; class GFG { static void result ( long n , long k , long t ) { if ( t <= k ) Console . WriteLine ( t ) ; else if ( t <= n ) Console . WriteLine ( k ) ; else { long temp = t - n ; temp = k - temp ; Console . WriteLine ( temp ) ; } } public static void Main ( ) { long n , k , t ; n = 10 ; k = 5 ; t = 12 ; result ( n , k , t ) ; } }
Program for weighted mean of natural numbers . | C # Program to find weighted mean of natural numbers . ; Function to calculate weighted mean . ; Driver program to test the function . ; Take num array and corresponding weight array and initialize it . ; Calculate the size of array . ; Check the size of both array is equal or not .
using System ; class GFG { static float weightedMean ( int [ ] X , int [ ] W , int n ) { int sum = 0 , numWeight = 0 ; for ( int i = 0 ; i < n ; i ++ ) { numWeight = numWeight + X [ i ] * W [ i ] ; sum = sum + W [ i ] ; } return ( float ) ( numWeight ) / sum ; } public static void Main ( ) { int [ ] X = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int [ ] W = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int n = X . Length ; int m = W . Length ; if ( n == m ) Console . WriteLine ( weightedMean ( X , W , n ) ) ; else Console . WriteLine ( " - 1" ) ; } }
Program to find GCD of floating point numbers | C # code for finding the GCD of two floating numbers . ; Recursive function to return gcd of a and b ; base case ; Driver Function .
using System ; class GFG { static float gcd ( double a , double b ) { if ( a < b ) return gcd ( b , a ) ; if ( Math . Abs ( b ) < 0.001 ) return ( float ) a ; else return ( float ) ( gcd ( b , a - Math . Floor ( a / b ) * b ) ) ; } public static void Main ( ) { double a = 1.20 , b = 22.5 ; Console . WriteLine ( gcd ( a , b ) ) ; } }
Program for harmonic mean of numbers | C # program to find harmonic mean of numbers . ; Function that returns harmonic mean . ; Declare sum variables and initialize with zero ; Driver code
using System ; class GFG { static float harmonicMean ( float [ ] arr , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( float ) 1 / arr [ i ] ; return ( float ) n / sum ; } public static void Main ( ) { float [ ] arr = { 13.5f , 14.5f , 14.8f , 15.2f , 16.1f } ; int n = arr . Length ; Console . WriteLine ( harmonicMean ( arr , n ) ) ; } }
Program for harmonic mean of numbers | C # program to find harmonic mean . ; Function that returns harmonic mean . ; Driver code
using System ; class GFG { static float harmonicMean ( int [ ] arr , int [ ] freq , int n ) { float sum = 0 , frequency_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ( float ) freq [ i ] / arr [ i ] ; frequency_sum = frequency_sum + freq [ i ] ; } return ( frequency_sum / sum ) ; } public static void Main ( ) { int [ ] num = { 13 , 14 , 15 , 16 , 17 } ; int [ ] freq = { 2 , 5 , 13 , 7 , 3 } ; int n = num . Length ; Console . WriteLine ( harmonicMean ( num , freq , n ) ) ; } }
First collision point of two series | C # program to calculate the colliding point of two series ; Iterating through n terms of the first series ; x is i - th term of first series ; d is first element of second series and c is common difference for second series . ; If no term of first series is found ; Driver function
using System ; class GFG { static void point ( int a , int b , int c , int d , int n ) { int x , flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { x = b + i * a ; if ( ( x - d ) % c == 0 && x - d >= 0 ) { Console . WriteLine ( x ) ; flag = 1 ; break ; } } if ( flag == 0 ) { Console . WriteLine ( " No ▁ collision ▁ point " ) ; } } public static void Main ( ) { int a = 20 ; int b = 2 ; int c = 9 ; int d = 19 ; int n = 20 ; point ( a , b , c , d , n ) ; } }
Armstrong Numbers between two integers | C # program to find Armstrong numbers in a range ; Prints Armstrong Numbers in given range ; number of digits calculation ; compute sum of nth power of its digits ; checks if number i is equal to the sum of nth power of its digits ; Driver code
using System ; class GFG { static void findArmstrong ( int low , int high ) { for ( int i = low + 1 ; i < high ; ++ i ) { int x = i ; int n = 0 ; while ( x != 0 ) { x /= 10 ; ++ n ; } int pow_sum = 0 ; x = i ; while ( x != 0 ) { int digit = x % 10 ; pow_sum += ( int ) Math . Pow ( digit , n ) ; x /= 10 ; } if ( pow_sum == i ) Console . Write ( i + " ▁ " ) ; } } public static void Main ( ) { int num1 = 100 ; int num2 = 400 ; findArmstrong ( num1 , num2 ) ; Console . WriteLine ( ) ; } }
Pair with maximum GCD from two arrays | C # program to find maximum GCD pair from two arrays ; Find the maximum GCD pair with maximum sum ; array to keep a count of existing elements ; first [ i ] and second [ i ] are going to store maximum multiples of i in a [ ] and b [ ] respectively . ; traverse through the first array to mark the elements in cnt ; Find maximum multiple of every number in first array ; Find maximum multiple of every number in second array . We re - initialise cnt [ ] and traverse through the second array to mark the elements in cnt ; if the multiple is present in the second array then store the max of number or the pre - existing element ; traverse for every elements and checks the maximum N that is present in both the arrays ; Driver Code ; Maximum possible value of elements in both arrays .
using System ; class GFG { static void gcdMax ( int [ ] a , int [ ] b , int n , int N ) { int [ ] cnt = new int [ N ] ; int [ ] first = new int [ N ] ; int [ ] second = new int [ N ] ; for ( int i = 0 ; i < n ; ++ i ) cnt [ a [ i ] ] = 1 ; for ( int i = 1 ; i < N ; ++ i ) for ( int j = i ; j < N ; j += i ) if ( cnt [ j ] > 0 ) first [ i ] = Math . Max ( first [ i ] , j ) ; cnt = new int [ N ] ; for ( int i = 0 ; i < n ; ++ i ) cnt [ b [ i ] ] = 1 ; for ( int i = 1 ; i < N ; ++ i ) for ( int j = i ; j < N ; j += i ) if ( cnt [ j ] > 0 ) second [ i ] = Math . Max ( second [ i ] , j ) ; int x ; for ( x = N - 1 ; x >= 0 ; x -- ) if ( first [ x ] > 0 && second [ x ] > 0 ) break ; Console . WriteLine ( first [ x ] + " ▁ " + second [ x ] ) ; } static int Main ( ) { int [ ] a = { 3 , 1 , 4 , 2 , 8 } ; int [ ] b = { 5 , 2 , 12 , 8 , 3 } ; int n = a . Length ; int N = 20 ; gcdMax ( a , b , n , N ) ; return 0 ; } }
Pierpont Prime | C # program to print Pierpont prime numbers smaller than n . ; Finding all numbers having factor power of 2 and 3 Using sieve ; Storing number of the form 2 ^ i . 3 ^ k + 1. ; Finding prime number using sieve of Eratosthenes . Reusing same array as result of above computations in v . ; Printing n pierpont primes smaller than n ; Driven Program
using System ; using System . Collections ; class GFG { static void printPierpont ( int n ) { bool [ ] arr = new bool [ n + 1 ] ; int two = 1 , three = 1 ; while ( two + 1 < n ) { arr [ two ] = true ; while ( two * three + 1 < n ) { arr [ three ] = true ; arr [ two * three ] = true ; three *= 3 ; } three = 1 ; two *= 2 ; } ArrayList v = new ArrayList ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] ) v . Add ( i + 1 ) ; arr = new bool [ n + 1 ] ; for ( int p = 2 ; p * p < n ; p ++ ) { if ( arr [ p ] == false ) for ( int i = p * 2 ; i < n ; i += p ) arr [ i ] = true ; } for ( int i = 0 ; i < v . Count ; i ++ ) if ( ! arr [ ( int ) v [ i ] ] ) Console . Write ( v [ i ] + " ▁ " ) ; } static void Main ( ) { int n = 200 ; printPierpont ( n ) ; } }
Woodall Number | C # program to check if a number is Woodall or not . ; If number is even , return false . ; If x is 1 , return true . ; While x is divisible by 2 ; Divide x by 2 ; Count the power ; If at any point power and x became equal , return true . ; Driver Code
using System ; class GFG { static bool isWoodall ( int x ) { if ( x % 2 == 0 ) return false ; if ( x == 1 ) return true ; int p = 0 ; while ( x % 2 == 0 ) { x = x / 2 ; p ++ ; if ( p == x ) return true ; } return false ; } public static void Main ( ) { int x = 383 ; if ( isWoodall ( x ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Print k numbers where all pairs are divisible by m | C # program to find a list of k elements from an array such that difference between all of them is divisible by m . ; function to generate k numbers whose difference is divisible by m ; Using an adjacency list like representation to store numbers that lead to same remainder . ; stores the modulus when divided by m ; If we found k elements which have same remainder . ; If we could not find k elements ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void print_result ( int [ ] a , int n , int k , int m ) { List < List < int > > v = new List < List < int > > ( m ) ; for ( int i = 0 ; i < m ; i ++ ) v . Add ( new List < int > ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { int rem = a [ i ] % m ; v [ rem ] . Add ( a [ i ] ) ; if ( v [ rem ] . Count == k ) { for ( int j = 0 ; j < k ; j ++ ) Console . Write ( v [ rem ] [ j ] + " ▁ " ) ; return ; } } Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 8 , 4 } ; int n = a . Length ; print_result ( a , n , 2 , 3 ) ; } }
Largest number less than N whose each digit is prime number | C # program to find the largest number smaller than N whose all digits are prime . ; Number is given as string . ; We stop traversing digits , once it become smaller than current number . For that purpose we use small variable . ; Array indicating if index i ( represents a digit ) is prime or not . ; Store largest ; If there is only one character , return the largest prime less than the number ; If number starts with 1 , return number consisting of 7 ; Traversing each digit from right to left Continue traversing till the number we are forming will become less . ; If digit is prime , copy it simply . ; If not prime , copy the largest prime less than current number ; If not prime , and there is no largest prime less than current prime ; Make current digit as 7 Go left of the digit and make it largest prime less than number . Continue do that until we found a digit which has some largest prime less than it ; If the given number is itself a prime . ; Make last digit as highest prime less than given digit . ; If there is no highest prime less than current digit . ; Once one digit become less than any digit of input replace 7 ( largest 1 digit prime ) till the end of digits of number ; If number include 0 in the beginning , ignore them . Case like 2200 ; Driver Code
using System ; class GFG { static char [ ] PrimeDigitNumber ( char [ ] N , int size ) { char [ ] ans = new char [ size ] ; int ns = 0 ; int small = 0 ; int i ; int [ ] p = { 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 0 , 0 } ; int [ ] prevprime = { 0 , 0 , 0 , 2 , 3 , 3 , 5 , 5 , 7 , 7 } ; if ( size == 1 ) { ans [ 0 ] = ( char ) ( prevprime [ N [ 0 ] - '0' ] + '0' ) ; ans [ 1 ] = ' \0' ; return ans ; } if ( N [ 0 ] == '1' ) { for ( i = 0 ; i < size - 1 ; i ++ ) ans [ i ] = '7' ; ans [ size - 1 ] = ' \0' ; return ans ; } for ( i = 0 ; i < size && small == 0 ; i ++ ) { if ( p [ N [ i ] - '0' ] == 1 ) { ans [ ns ++ ] = N [ i ] ; } else { if ( p [ N [ i ] - '0' ] == 0 && prevprime [ N [ i ] - '0' ] != 0 ) { ans [ ns ++ ] = ( char ) ( prevprime [ N [ i ] - '0' ] + '0' ) ; small = 1 ; } else if ( p [ N [ i ] - '0' ] == 0 && prevprime [ N [ i ] - '0' ] == 0 ) { int j = i ; while ( j > 0 && p [ N [ j ] - '0' ] == 0 && prevprime [ N [ j ] - '0' ] == 0 ) { ans [ j ] = N [ j ] = '7' ; N [ j - 1 ] = ( char ) ( prevprime [ N [ j - 1 ] - '0' ] + '0' ) ; ans [ j - 1 ] = N [ j - 1 ] ; small = 1 ; j -- ; } i = ns ; } } } if ( small == 0 ) { if ( prevprime [ N [ size - 1 ] - '0' ] + '0' != '0' ) ans [ size - 1 ] = ( char ) ( prevprime [ N [ size - 1 ] - '0' ] + '0' ) ; else { int j = size - 1 ; while ( j > 0 && prevprime [ N [ j ] - '0' ] == 0 ) { ans [ j ] = N [ j ] = '7' ; N [ j - 1 ] = ( char ) ( prevprime [ N [ j - 1 ] - '0' ] + '0' ) ; ans [ j - 1 ] = N [ j - 1 ] ; small = 1 ; j -- ; } } } for ( ; ns < size ; ns ++ ) ans [ ns ] = '7' ; ans [ ns ] = ' \0' ; int k = 0 ; while ( ans [ k ] == '0' ) k ++ ; return ans ; } static public void Main ( ) { char [ ] N = "1000" . ToCharArray ( ) ; int size = N . Length ; Console . WriteLine ( PrimeDigitNumber ( N , size ) ) ; } }
Smallest x such that 1 * n , 2 * n , ... x * n have all digits from 1 to 9 | C # program to find x such that 1 * n , 2 * n , 3 * n ... x * n have all digits from 1 to 9 at least once ; Returns smallest value x such that 1 * n , 2 * n , 3 * n ... x * n have all digits from 1 to 9 at least once ; taking temporary array and variable . ; iterate till we get all the 10 digits at least once ; checking all the digits ; dDriver Code
using System ; class GFG { public static int smallestX ( int n ) { int [ ] temp = new int [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) temp [ i ] = 0 ; if ( n == 0 ) return - 1 ; int count = 0 , x = 0 ; for ( x = 1 ; count < 10 ; x ++ ) { int y = x * n ; while ( y > 0 ) { if ( temp [ y % 10 ] == 0 ) { count ++ ; temp [ y % 10 ] = 1 ; } y /= 10 ; } } return x - 1 ; } static void Main ( ) { int n = 5 ; Console . Write ( smallestX ( n ) ) ; } }
Find a number x such that sum of x and its digits is equal to given n . | C # program to find x such that x + digSum ( x ) is equal to n . ; utility function for digit sum ; function for finding x ; iterate from 1 to n . For every no . check if its digit sum with it is equal to n . ; if no such i found return - 1 ; Driver code
using System ; class GFG { static int digSum ( int n ) { int sum = 0 , rem = 0 ; while ( n > 0 ) { rem = n % 10 ; sum += rem ; n /= 10 ; } return sum ; } static int findX ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) if ( i + digSum ( i ) == n ) return i ; return - 1 ; } public static void Main ( ) { int n = 43 ; Console . Write ( " x ▁ = ▁ " + findX ( n ) ) ; } }
9 's complement of a decimal number | C # program to find 9 's complement of a number. ; Driver code
using System ; class GFG { static void complement ( string number1 ) { char [ ] number = number1 . ToCharArray ( ) ; for ( int i = 0 ; i < number . Length ; i ++ ) if ( number [ i ] != ' . ' ) number [ i ] = ( char ) ( ( int ) ( '9' ) - ( int ) ( number [ i ] ) + ( int ) ( '0' ) ) ; System . Console . WriteLine ( "9 ' s ▁ complement ▁ is ▁ : ▁ " + new string ( number ) ) ; } public static void Main ( ) { String number = "345.45" ; complement ( number ) ; } }