codeparrot/xlcost-text-to-code huggingface dataset:
Smith Number | C ++ program to check whether a number is Smith Number or not . ; array to store all prime less than and equal to 10 ^ 6 ; utility function for sieve of sundaram ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half This array is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Returns true if n is a Smith number , else false . ; Find sum the digits of prime factors of n ; If primes [ i ] is a prime factor , add its digits to pDigitSum . ; If n != 1 then one prime factor still to be summed up ; ; All prime factors digits summed up Now sum the original number digits ; If sum of digits in prime factors and sum of digits in original number are same , then return true . Else return false . ; Driver code ; Finding all prime numbers before limit . These numbers are used to find prime factors .
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 10000 ; vector < int > primes ; void sieveSundaram ( ) { bool marked [ MAX / 2 + 100 ] = { 0 } ; for ( int i = 1 ; i <= ( sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = true ; primes . push_back ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . push_back ( 2 * i + 1 ) ; } bool isSmith ( int n ) { int original_no = n ; int pDigitSum = 0 ; for ( int i = 0 ; primes [ i ] <= n / 2 ; i ++ ) { while ( n % primes [ i ] == 0 ) { int p = primes [ i ] ; n = n / p ; while ( p > 0 ) { pDigitSum += ( p % 10 ) ; p = p / 10 ; } } } if ( n != 1 && n != original_no ) { while ( n > 0 ) { pDigitSum = pDigitSum + n % 10 ; n = n / 10 ; } } int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits = sumDigits + original_no % 10 ; original_no = original_no / 10 ; } return ( pDigitSum == sumDigits ) ; } int main ( ) { sieveSundaram ( ) ; cout << " Printing ▁ first ▁ few ▁ Smith ▁ Numbers " " ▁ using ▁ isSmith ( ) n " ; for ( int i = 1 ; i < 500 ; i ++ ) if ( isSmith ( i ) ) cout << i << " ▁ " ; return 0 ; }
Kaprekar Number | C ++ program to check if a number is Kaprekar number or not ; Returns true if n is a Kaprekar number , else false ; Count number of digits in square ; Split the square at different poitns and see if sum of any pair of splitted numbers is equal to n . ; To avoid numbers like 10 , 100 , 1000 ( These are not Karprekar numbers ; Find sum of current parts and compare with n ; compare with original number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool iskaprekar ( int n ) { if ( n == 1 ) return true ; int sq_n = n * n ; int count_digits = 0 ; while ( sq_n ) { count_digits ++ ; sq_n /= 10 ; } for ( int r_digits = 1 ; r_digits < count_digits ; r_digits ++ ) { int eq_parts = pow ( 10 , r_digits ) ; if ( eq_parts == n ) continue ; int sum = sq_n / eq_parts + sq_n % eq_parts ; if ( sum == n ) return true ; } return false ; } int main ( ) { cout << " Printing ▁ first ▁ few ▁ Kaprekar ▁ Numbers " " ▁ using ▁ iskaprekar ( ) STRNEWLINE " ; for ( int i = 1 ; i < 10000 ; i ++ ) if ( iskaprekar ( i ) ) cout << i << " ▁ " ; return 0 ; }
Keith Number | C ++ program to check if a number is Keith or not ; Returns true if x is Keith , else false . ; Store all digits of x in a vector " terms " Also find number of digits and store in " n " . ; int temp = x , n = 0 ; n is number of digits in x ; To get digits in right order ( from MSB to LSB ) ; Keep finding next trms of a sequence generated using digits of x until we either reach x or a number greate than x ; Next term is sum of previous n terms ; When the control comes out of the while loop , either the next_term is equal to the number or greater than it . If next_term is equal to x , then x is a Keith number , else not ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isKeith ( int x ) { vector < int > terms ; while ( temp > 0 ) { terms . push_back ( temp % 10 ) ; temp = temp / 10 ; n ++ ; } reverse ( terms . begin ( ) , terms . end ( ) ) ; int next_term = 0 , i = n ; while ( next_term < x ) { next_term = 0 ; for ( int j = 1 ; j <= n ; j ++ ) next_term += terms [ i - j ] ; terms . push_back ( next_term ) ; i ++ ; } return ( next_term == x ) ; } int main ( ) { isKeith ( 14 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; isKeith ( 12 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; isKeith ( 197 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; return 0 ; }
Check if a number can be expressed as a sum of consecutive numbers | C ++ program to check if a number can be expressed as sum of consecutive numbers ; This function returns true if n can be expressed sum of consecutive . ; We basically return true if n is a power of two ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canBeSumofConsec ( unsigned int n ) { return ( ( n & ( n - 1 ) ) && n ) ; } int main ( ) { unsigned int n = 15 ; canBeSumofConsec ( n ) ? cout << " true " : cout << " false " ; return 0 ; }
Check if a number can be expressed as a sum of consecutive numbers |  ; Updating n with 2 n ; ( n & ( n - 1 ) ) = > Checking whether we can write 2 n as 2 ^ k if yes ( can 't represent 2n as 2^k) then answer 1  if no (can represent 2n as 2^k) then answer 0
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int canBeSumofConsec ( long long int n ) { n = 2 * n ; return ( ( n & ( n - 1 ) ) != 0 ) ; } int main ( ) { long long int n = 10 ; cout << canBeSumofConsec ( n ) << " STRNEWLINE " ; }
Even Fibonacci Numbers Sum | Find the sum of all the even - valued terms in the Fibonacci sequence which do not exceed given limit . ; Returns sum of even Fibonacci numbers which are less than or equal to given limit . ; Initialize first two even prime numbers and their sum ; calculating sum of even Fibonacci value ; get next even value of Fibonacci sequence ; If we go beyond limit , we break loop ; Move to next even number and update sum ; Driver code
#include <iostream> NEW_LINE using namespace std ; int evenFibSum ( int limit ) { if ( limit < 2 ) return 0 ; long long int ef1 = 0 , ef2 = 2 ; long long int sum = ef1 + ef2 ; while ( ef2 <= limit ) { long long int ef3 = 4 * ef2 + ef1 ; if ( ef3 > limit ) break ; ef1 = ef2 ; ef2 = ef3 ; sum += ef2 ; } return sum ; } int main ( ) { int limit = 400 ; cout << evenFibSum ( limit ) ; return 0 ; }
Find numbers with K odd divisors in a given range | C ++ program to count numbers with k odd divisors in a range . ; Utility function to check if number is perfect square or not ; Utility Function to return count of divisors of a number ; Note that this loop runs till square root ; If divisors are equal , count it only once ; Otherwise print both ; Function to calculate all divisors having exactly k divisors between a and b ; calculate only for perfect square numbers ; check if number is perfect square or not ; total divisors of number equals to k or not ; Driver program to run the case
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfect ( int n ) { int s = sqrt ( n ) ; return ( s * s == n ) ; } int divisorsCount ( int n ) { int count = 0 ; for ( int i = 1 ; i <= sqrt ( n ) + 1 ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) count += 1 ; else count += 2 ; } } return count ; } int kDivisors ( int a , int b , int k ) { for ( int i = a ; i <= b ; i ++ ) { if ( isPerfect ( i ) ) if ( divisors ( i ) == k ) count ++ ; } return count ; } int main ( ) { int a = 2 , b = 49 , k = 3 ; cout << kDivisors ( a , b , k ) ; return 0 ; }
Nth Even Fibonacci Number | C ++ code to find Even Fibonacci Series using normal Recursion ; Function which return nth even fibonnaci number ; calculation of Fn = 4 * ( Fn - 1 ) + Fn - 2 ; Driver Code
#include <iostream> NEW_LINE using namespace std ; long int evenFib ( int n ) { if ( n < 1 ) return n ; if ( n == 1 ) return 2 ; return ( ( 4 * evenFib ( n - 1 ) ) + evenFib ( n - 2 ) ) ; } int main ( ) { int n = 7 ; cout << evenFib ( n ) ; return 0 ; }
N 'th Smart Number | C ++ implementation to find n 'th smart number ; Limit on result ; Function to calculate n 'th smart number ; Initialize all numbers as not prime ; iterate to mark all primes and smart number ; Traverse all numbers till maximum limit ; ' i ' is maked as prime number because it is not multiple of any other prime ; mark all multiples of ' i ' as non prime ; If i is the third prime factor of j then add it to result as it has at least three prime factors . ; Sort all smart numbers ; return n 'th smart number ; Driver program to run the case
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 3000 ; int smartNumber ( int n ) { int primes [ MAX ] = { 0 } ; vector < int > result ; for ( int i = 2 ; i < MAX ; i ++ ) { if ( primes [ i ] == 0 ) { primes [ i ] = 1 ; for ( int j = i * 2 ; j < MAX ; j = j + i ) { primes [ j ] -= 1 ; if ( ( primes [ j ] + 3 ) == 0 ) result . push_back ( j ) ; } } } sort ( result . begin ( ) , result . end ( ) ) ; return result [ n - 1 ] ; } int main ( ) { int n = 50 ; cout << smartNumber ( n ) ; return 0 ; }
Repeated subtraction among two numbers | C ++ program to count of steps until one of the two numbers become 0. ; Returns count of steps before one of the numbers become 0 after repeated subtractions . ; If y divides x , then simply return x / y . ; Else recur . Note that this function works even if x is smaller than y because in that case first recursive call exchanges roles of x and y . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSteps ( int x , int y ) { if ( x % y == 0 ) return x / y ; return x / y + countSteps ( y , x % y ) ; } int main ( ) { int x = 100 , y = 19 ; cout << countSteps ( x , y ) ; return 0 ; }
Common Divisors of Two Numbers | C ++ implementation of program ; Map to store the count of each prime factor of a ; Function that calculate the count of each prime factor of a number ; Function to calculate all common divisors of two given numbers a , b -- > input integer numbers ; Find count of each prime factor of a ; stores number of common divisors ; Find the count of prime factors of b using distinct prime factors of a ; Prime factor of common divisor has minimum cnt of both a and b ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; map < int , int > ma ; void primeFactorize ( int a ) { for ( int i = 2 ; i * i <= a ; i += 2 ) { int cnt = 0 ; while ( a % i == 0 ) { cnt ++ ; a /= i ; } ma [ i ] = cnt ; } if ( a > 1 ) { ma [ a ] = 1 ; } } int commDiv ( int a , int b ) { primeFactorize ( a ) ; int res = 1 ; for ( auto m = ma . begin ( ) ; m != ma . end ( ) ; m ++ ) { int cnt = 0 ; int key = m -> first ; int value = m -> second ; while ( b % key == 0 ) { b /= key ; cnt ++ ; } res *= ( min ( cnt , value ) + 1 ) ; } return res ; } int main ( ) { int a = 12 , b = 24 ; cout << commDiv ( a , b ) << endl ; return 0 ; }
Count number of solutions of x ^ 2 = 1 ( mod p ) in given range | C ++ program to count number of values that satisfy x ^ 2 = 1 mod p where x lies in range [ 1 , n ] ; Initialize result ; Traverse all numbers smaller than given number p . Note that we don 't  traverse from 1 to n, but 1 to p ; If x is a solution , then count all numbers of the form x + i * p such that x + i * p is in range [ 1 , n ] ; The largest number in the form of x + p * i in range [ 1 , n ] ; Add count of numbers of the form x + p * i . 1 is added for x itself . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long ll ; int findCountOfSolutions ( int n , int p ) { ll ans = 0 ; for ( ll x = 1 ; x < p ; x ++ ) { if ( ( x * x ) % p == 1 ) { ll last = x + p * ( n / p ) ; if ( last > n ) last -= p ; ans += ( ( last - x ) / p + 1 ) ; } } return ans ; } int main ( ) { ll n = 10 , p = 5 ; printf ( " % lld STRNEWLINE " , findCountOfSolutions ( n , p ) ) ; return 0 ; }
Happy Number | method return true if n is Happy Number numSquareSum method is given in below detailed code snippet
int isHappyNumber ( int n ) { set < int > st ; while ( 1 ) { n = numSquareSum ( n ) ; if ( n == 1 ) return true ; if ( st . find ( n ) != st . end ( ) ) return false ; st . insert ( n ) ; } }
Kaprekar Constant | C ++ program to demonstrate working of Kaprekar constant ; This function checks validity of kaprekar ' s ▁ constant . ▁ It ▁ returns ▁ kaprekar ' s constant for any four digit number " n " such that all digits of n are not same . ; Store current n as previous number ; Get four digits of given number ; Sort all four digits in ascending order And giet in the form of number " asc " ; Get all four dgits in descending order in the form of number " desc " ; Get the difference of two numbers ; If difference is same as previous , we have reached kaprekar 's constant ; Else recur ; A wrapper over kaprekarRec ( ) ; Driver code ; Trying few four digit numbers , we always get 6174
#include <bits/stdc++.h> NEW_LINE using namespace std ; int kaprekarRec ( int n , int & prev ) { if ( n == 0 ) return 0 ; prev = n ; int digits [ 4 ] ; for ( int i = 0 ; i < 4 ; i ++ ) { digits [ i ] = n % 10 ; n = n / 10 ; } sort ( digits , digits + 4 ) ; int asc = 0 ; for ( int i = 0 ; i < 4 ; i ++ ) asc = asc * 10 + digits [ i ] ; sort ( digits , digits + 4 , std :: greater < int > ( ) ) ; int desc = 0 ; for ( int i = 0 ; i < 4 ; i ++ ) desc = desc * 10 + digits [ i ] ; int diff = abs ( asc - desc ) ; if ( diff == prev ) return diff ; return kaprekarRec ( diff , prev ) ; } int kaprekar ( int n ) { int prev = 0 ; return kaprekarRec ( n , prev ) ; } int main ( ) { cout << kaprekar ( 1000 ) << endl ; cout << kaprekar ( 1112 ) << endl ; cout << kaprekar ( 9812 ) << endl ; return 0 ; }
Bakhshali Approximation for computing square roots | This program gives result approximated to 5 decimal places . ; This will be the nearest perfect square to s ; This is the sqrt of pSq ; Find the nearest perfect square to s ; calculate d ; calculate P ; calculate A ; calculate sqrt ( S ) . ; Driver program to test above function
#include <iostream> NEW_LINE float sqroot ( float s ) { int pSq = 0 ; int N = 0 ; for ( int i = static_cast < int > ( s ) ; i > 0 ; i -- ) { for ( int j = 1 ; j < i ; j ++ ) { if ( j * j == i ) { pSq = i ; N = j ; break ; } } if ( pSq > 0 ) break ; } float d = s - pSq ; float P = d / ( 2.0 * N ) ; float A = N + P ; float sqrt_of_s = A - ( ( P * P ) / ( 2.0 * A ) ) ; return sqrt_of_s ; } int main ( ) { float num = 9.2345 ; float sqroot_of_num = sqroot ( num ) ; std :: cout << " Square ▁ root ▁ of ▁ " << num << " ▁ = ▁ " << sqroot_of_num ; return 0 ; }
Breaking an Integer to get Maximum Product | C / C ++ program to find maximum product by breaking the Integer ; method return x ^ a in log ( a ) time ; Method returns maximum product obtained by breaking N ; base case 2 = 1 + 1 ; base case 3 = 2 + 1 ; breaking based on mod with 3 ; If divides evenly , then break into all 3 ; If division gives mod as 1 , then break as 4 + power of 3 for remaining part ; If division gives mod as 2 , then break as 2 + power of 3 for remaining part ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int x , int a ) { int res = 1 ; while ( a ) { if ( a & 1 ) res = res * x ; x = x * x ; a >>= 1 ; } return res ; } int breakInteger ( int N ) { if ( N == 2 ) return 1 ; if ( N == 3 ) return 2 ; int maxProduct ; switch ( N % 3 ) { case 0 : maxProduct = power ( 3 , N / 3 ) ; break ; case 1 : maxProduct = 2 * 2 * power ( 3 , ( N / 3 ) - 1 ) ; break ; case 2 : maxProduct = 2 * power ( 3 , N / 3 ) ; break ; } return maxProduct ; } int main ( ) { int maxProduct = breakInteger ( 10 ) ; cout << maxProduct << endl ; return 0 ; }
Finding sum of digits of a number until sum becomes single digit | C ++ program to find sum of digits of a number until sum becomes single digit . ; Loop to do sum while sum is not less than or equal to 9 ; Driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digSum ( int n ) { int sum = 0 ; while ( n > 0 sum > 9 ) { if ( n == 0 ) { n = sum ; sum = 0 ; } sum += n % 10 ; n /= 10 ; } return sum ; } int main ( ) { int n = 1234 ; cout << digSum ( n ) ; return 0 ; }
Finding sum of digits of a number until sum becomes single digit | Driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digSum ( int n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } int main ( ) { int n = 9999 ; cout << digSum ( n ) ; return 0 ; }
Multiples of 3 or 7 | A better C ++ program to find count of all numbers that multiples ; Returns count of all numbers smaller than or equal to n and multiples of 3 or 7 or both ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countMultiples ( int n ) { return n / 3 + n / 7 - n / 21 ; } int main ( ) { cout << " Count ▁ = ▁ " << countMultiples ( 25 ) ; }
Find Last Digit of a ^ b for Large Numbers | C ++ code to find last digit of a ^ b ; Function to find b % a ; Initialize result ; calculating mod of b with a to make b like 0 <= b < a ; return mod ; return modulo ; function to find last digit of a ^ b ; if a and b both are 0 ; if exponent is 0 ; if base is 0 ; if exponent is divisible by 4 that means last digit will be pow ( a , 4 ) % 10. if exponent is not divisible by 4 that means last digit will be pow ( a , b % 4 ) % 10 ; Find last digit in ' a ' and compute its exponent ; Return last digit of result ; Driver program to run test case
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Modulo ( int a , char b [ ] ) { int mod = 0 ; for ( int i = 0 ; i < strlen ( b ) ; i ++ ) mod = ( mod * 10 + b [ i ] - '0' ) % a ; } int LastDigit ( char a [ ] , char b [ ] ) { int len_a = strlen ( a ) , len_b = strlen ( b ) ; if ( len_a == 1 && len_b == 1 && b [ 0 ] == '0' && a [ 0 ] == '0' ) return 1 ; if ( len_b == 1 && b [ 0 ] == '0' ) return 1 ; if ( len_a == 1 && a [ 0 ] == '0' ) return 0 ; int exp = ( Modulo ( 4 , b ) == 0 ) ? 4 : Modulo ( 4 , b ) ; int res = pow ( a [ len_a - 1 ] - '0' , exp ) ; return res % 10 ; } int main ( ) { char a [ ] = "117" , b [ ] = "3" ; cout << LastDigit ( a , b ) ; return 0 ; }
Reverse and Add Function | C ++ Program to implement reverse and add function ; Iterative function to reverse digits of num ; Function to check whether he number is palindrome or not ; Reverse and Add Function ; Reversing the digits of the number ; Adding the reversed number with the original ; Checking whether the number is palindrome or not ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long reversDigits ( long long num ) { long long rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } bool isPalindrome ( long long num ) { return ( reversDigits ( num ) == num ) ; } void ReverseandAdd ( long long num ) { long long rev_num = 0 ; while ( num <= 4294967295 ) { rev_num = reversDigits ( num ) ; num = num + rev_num ; if ( isPalindrome ( num ) ) { printf ( " % lld STRNEWLINE " , num ) ; break ; } else if ( num > 4294967295 ) { printf ( " No ▁ palindrome ▁ exist " ) ; } } } int main ( ) { ReverseandAdd ( 195 ) ; ReverseandAdd ( 265 ) ; return 0 ; }
Stein 's Algorithm for finding GCD | Recursive C ++ program to implement Stein 's Algorithm ; Function to implement Stein 's Algorithm ; GCD ( 0 , b ) == b ; GCD ( a , 0 ) == a , GCD ( 0 , 0 ) == 0 ; look for factors of 2 if ( ~ a & 1 ) a is even ; if ( b & 1 ) b is odd ; else both a and b are even ; if ( ~ b & 1 ) a is odd , b is even ; reduce larger number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == b ) return a ; if ( a == 0 ) return b ; if ( b == 0 ) return a ; { return gcd ( a >> 1 , b ) ; return gcd ( a >> 1 , b >> 1 ) << 1 ; } return gcd ( a , b >> 1 ) ; if ( a > b ) return gcd ( ( a - b ) >> 1 , b ) ; return gcd ( ( b - a ) >> 1 , a ) ; } int main ( ) { int a = 34 , b = 17 ; printf ( " Gcd ▁ of ▁ given ▁ numbers ▁ is ▁ % d STRNEWLINE " , gcd ( a , b ) ) ; return 0 ; }
Sub | C ++ program to answer multiple queries of divisibility by 3 in substrings of a number ; Array to store the sum of digits ; Utility function to evaluate a character 's integer value ; This function receives the string representation of the number and precomputes the sum array ; This function receives l and r representing the indices and prints the required output ; Driver function to check the program
#include <iostream> NEW_LINE using namespace std ; int sum [ 1000005 ] ; int toInt ( char x ) { return int ( x ) - '0' ; } void prepareSum ( string s ) { sum [ 0 ] = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) sum [ i + 1 ] = sum [ i ] + toInt ( s [ i ] ) ; } void query ( int l , int r ) { if ( ( sum [ r + 1 ] - sum [ l ] ) % 3 == 0 ) cout << " Divisible ▁ by ▁ 3 STRNEWLINE " ; else cout << " Not ▁ divisible ▁ by ▁ 3 STRNEWLINE " ; } int main ( ) { string n = "12468236544" ; prepareSum ( n ) ; query ( 0 , 1 ) ; query ( 1 , 2 ) ; query ( 3 , 6 ) ; query ( 0 , 10 ) ; return 0 ; }
Print all n | A C ++ recursive program to print all n - digit numbers whose sum of digits equals to given sum ; n , sum -- > value of inputs out -- > output array index -- > index of next digit to be filled in output array ; Base case ; If number becomes N - digit ; if sum of its digits is equal to given sum , print it ; Traverse through every digit . Note that here we ' re ▁ considering ▁ leading ▁ 0' s as digits ; append current digit to number ; recurse for next digit with reduced sum ; This is mainly a wrapper over findNDigitNumsUtil . It explicitly handles leading digit ; output array to store N - digit numbers ; fill 1 st position by every digit from 1 to 9 and calls findNDigitNumsUtil ( ) for remaining positions ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNDigitNumsUtil ( int n , int sum , char * out , int index ) { if ( index > n sum < 0 ) return ; if ( index == n ) { if ( sum == 0 ) { out [ index ] = ' \0' ; cout << out << " ▁ " ; } return ; } for ( int i = 0 ; i <= 9 ; i ++ ) { out [ index ] = i + '0' ; findNDigitNumsUtil ( n , sum - i , out , index + 1 ) ; } } void findNDigitNums ( int n , int sum ) { char out [ n + 1 ] ; for ( int i = 1 ; i <= 9 ; i ++ ) { out [ 0 ] = i + '0' ; findNDigitNumsUtil ( n , sum - i , out , 1 ) ; } } int main ( ) { int n = 2 , sum = 3 ; findNDigitNums ( n , sum ) ; return 0 ; }