codeparrot/xlcost-text-to-code huggingface dataset:
Program to implement Collatz Conjecture | C ++ program to implement Collatz Conjecture ; Function to find if n reaches to 1 or not . ; If there is a cycle formed , we can 't r  reach 1. ; If n is odd then pass n = 3 n + 1 else n = n / 2 ; Wrapper over isToOneRec ( ) ; To store numbers visited using recursive calls . ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isToOneRec ( int n , unordered_set < int > & s ) { if ( n == 1 ) return true ; if ( s . find ( n ) != s . end ( ) ) return false ; return ( n % 2 ) ? isToOneRec ( 3 * n + 1 , s ) : isToOneRec ( n / 2 , s ) ; } bool isToOne ( int n ) { unordered_set < int > s ; return isToOneRec ( n , s ) ; } int main ( ) { int n = 5 ; isToOne ( n ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Program to implement Collatz Conjecture | C ++ program to implement Collatz Conjecture ; Function to find if n reaches to 1 or not . ; Return true if n is positive ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isToOne ( int n ) { return ( n > 0 ) ; } int main ( ) { int n = 5 ; isToOne ( n ) ? cout << " Yes " : cout << " No " ; return 0 ; }
GCD of two numbers formed by n repeating x and y times | C ++ program to print Greatest Common Divisor of number formed by N repeating x times and y times . ; Return the Greatest common Divisor of two numbers . ; Prints Greatest Common Divisor of number formed by n repeating x times and y times . ; Finding GCD of x and y . ; Print n , g times . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } void findgcd ( int n , int x , int y ) { int g = gcd ( x , y ) ; for ( int i = 0 ; i < g ; i ++ ) cout << n ; } int main ( ) { int n = 123 , x = 5 , y = 2 ; findgcd ( n , x , y ) ; return 0 ; }
Count natural numbers whose factorials are divisible by x but not y | C ++ program to count natural numbers whose factorials are divisible by x but not y . ; GCD function to compute the greatest divisor among a and b ; Returns first number whose factorial is divisible by x . ; int i = 1 ; Result ; Remove common factors ; We found first i . ; Count of natural numbers whose factorials are divisible by x but not y . ; Return difference between first natural number whose factorial is divisible by y and first natural number whose factorial is divisible by x . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( ( a % b ) == 0 ) return b ; return gcd ( b , a % b ) ; } int firstFactorialDivisibleNumber ( int x ) { int new_x = x ; for ( i = 1 ; i < x ; i ++ ) { new_x /= gcd ( i , new_x ) ; if ( new_x == 1 ) break ; } return i ; } int countFactorialXNotY ( int x , int y ) { return ( firstFactorialDivisibleNumber ( y ) - firstFactorialDivisibleNumber ( x ) ) ; } int main ( void ) { int x = 15 , y = 25 ; cout << countFactorialXNotY ( x , y ) ; return 0 ; }
Find the first natural number whose factorial is divisible by x | A simple C ++ program to find first natural number whose factorial divides x . ; Returns first number whose factorial divides x . ; int i = 1 ; Result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int firstFactorialDivisibleNumber ( int x ) { int fact = 1 ; for ( i = 1 ; i < x ; i ++ ) { fact = fact * i ; if ( fact % x == 0 ) break ; } return i ; } int main ( void ) { int x = 16 ; cout << firstFactorialDivisibleNumber ( x ) ; return 0 ; }
Find two prime numbers with given sum | C ++ program to find a prime number pair whose sum is equal to given number C ++ program to print super primes less than or equal to n . ; Generate all prime numbers less than n . ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime , else true bool isPrime [ n + 1 ] ; ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Prints a prime pair with given sum ; Generating primes using Sieve ; Traversing all numbers to find first pair ; Driven program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool SieveOfEratosthenes ( int n , bool isPrime [ ] ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) isPrime [ i ] = false ; } } } void findPrimePair ( int n ) { bool isPrime [ n + 1 ] ; SieveOfEratosthenes ( n , isPrime ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime [ i ] && isPrime [ n - i ] ) { cout << i << " ▁ " << ( n - i ) ; return ; } } } int main ( ) { int n = 74 ; findPrimePair ( n ) ; return 0 ; }
Count numbers with same first and last digits | C ++ program to get count of numbers with same start and end digit in an interval ; Utility method to get first digit of x ; method to return count of numbers with same starting and ending digit from 1 upto x ; get ten - spans from 1 to x ; add 9 to consider all 1 digit numbers ; Find first and last digits ; If last digit is greater than first digit then decrease count by 1 ; Method to return count of numbers with same starting and ending digit between start and end ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getFirstDigit ( int x ) { while ( x >= 10 ) x /= 10 ; return x ; } int getCountWithSameStartAndEndFrom1 ( int x ) { if ( x < 10 ) return x ; int tens = x / 10 ; int res = tens + 9 ; int firstDigit = getFirstDigit ( x ) ; int lastDigit = x % 10 ; if ( lastDigit < firstDigit ) res -- ; return res ; } int getCountWithSameStartAndEnd ( int start , int end ) { return getCountWithSameStartAndEndFrom1 ( end ) - getCountWithSameStartAndEndFrom1 ( start - 1 ) ; } int main ( ) { int start = 5 , end = 40 ; cout << getCountWithSameStartAndEnd ( start , end ) ; return 0 ; }
Right | Program to check whether a given number is right - truncatable prime or not . ; Generate all prime numbers less than n . ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime , else true bool isPrime [ n + 1 ] ; ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Returns true if n is right - truncatable , else false ; Generating primes using Sieve ; Checking whether the number remains prime when the last ( " right " ) digit is successively removed ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sieveOfEratosthenes ( int n , bool isPrime [ ] ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) isPrime [ i ] = false ; } } } bool rightTruPrime ( int n ) { bool isPrime [ n + 1 ] ; sieveOfEratosthenes ( n , isPrime ) ; while ( n ) { if ( isPrime [ n ] ) n = n / 10 ; else return false ; } return true ; } int main ( ) { int n = 59399 ; if ( rightTruPrime ( n ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Mersenne Prime | Program to generate mersenne primes ; Generate all prime numbers less than n . ; Initialize all entries of boolean array as true . A value in prime [ i ] will finally be false if i is Not a prime , else true bool prime [ n + 1 ] ; ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to generate mersenne primes less than or equal to n ; Create a boolean array " prime [ 0 . . n ] " ; Generating primes using Sieve ; Generate all numbers of the form 2 ^ k - 1 and smaller than or equal to n . ; Checking whether number is prime and is one less then the power of 2 ; Driven program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void SieveOfEratosthenes ( int n , bool prime [ ] ) { for ( int i = 0 ; i <= n ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } } void mersennePrimes ( int n ) { bool prime [ n + 1 ] ; SieveOfEratosthenes ( n , prime ) ; for ( int k = 2 ; ( ( 1 << k ) - 1 ) <= n ; k ++ ) { long long num = ( 1 << k ) - 1 ; if ( prime [ num ] ) cout << num << " ▁ " ; } } int main ( ) { int n = 31 ; cout << " Mersenne ▁ prime ▁ numbers ▁ smaller ▁ " << " than ▁ or ▁ equal ▁ to ▁ " << n << endl ; mersennePrimes ( n ) ; return 0 ; }
Find sum of modulo K of first N natural number | C ++ program to find sum of modulo K of first N natural numbers . ; Return sum of modulo K of first N natural numbers . ; Iterate from 1 to N && evaluating and adding i % K . ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int N , int K ) { int ans = 0 ; for ( int i = 1 ; i <= N ; i ++ ) ans += ( i % K ) ; return ans ; } int main ( ) { int N = 10 , K = 2 ; cout << findSum ( N , K ) << endl ; return 0 ; }
Find sum of modulo K of first N natural number | C ++ program to find sum of modulo K of first N natural numbers . ; Return sum of modulo K of first N natural numbers . ; Counting the number of times 1 , 2 , . . , K - 1 , 0 sequence occurs . ; Finding the number of elements left which are incomplete of sequence Leads to Case 1 type . ; adding multiplication of number of times 1 , 2 , . . , K - 1 , 0 sequence occurs and sum of first k natural number and sequence from case 1. ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int N , int K ) { int ans = 0 ; int y = N / K ; int x = N % K ; ans = ( K * ( K - 1 ) / 2 ) * y + ( x * ( x + 1 ) ) / 2 ; return ans ; } int main ( ) { int N = 10 , K = 2 ; cout << findSum ( N , K ) << endl ; return 0 ; }
Smallest number to multiply to convert floating point to natural | C ++ program to find the smallest number to multiply to convert a floating point number into natural number . ; Finding GCD of two number ; Returns smallest integer k such that k * str becomes natural . str is an input floating point number ; Find size of string representing a floating point number . ; Below is used to find denominator in fraction form . ; Used to find value of count_after_dot ; To find numerator in fraction form of given number . For example , for 30.25 , numerator would be 3025. ; If there was no dot , then number is already a natural . ; Find denominator in fraction form . For example , for 30.25 , denominator is 100 ; Result is denominator divided by GCD - of - numerator - and - denominator . For example , for 30.25 , result is 100 / GCD ( 3025 , 100 ) = 100 / 25 = 4 ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int findnum ( string & str ) { int n = str . length ( ) ; int count_after_dot = 0 ; bool dot_seen = false ; int num = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] != ' . ' ) { num = num * 10 + ( str [ i ] - '0' ) ; if ( dot_seen == true ) count_after_dot ++ ; } else dot_seen = true ; } if ( dot_seen == false ) return 1 ; int dem = ( int ) pow ( 10 , count_after_dot ) ; return ( dem / gcd ( num , dem ) ) ; } int main ( ) { string str = "5.125" ; cout << findnum ( str ) << endl ; return 0 ; }
Find the maximum number of handshakes | C ++ program to find maximum number of handshakes . ; Calculating the maximum number of handshake using derived formula . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxHandshake ( int n ) { return ( n * ( n - 1 ) ) / 2 ; } int main ( ) { int n = 10 ; cout << maxHandshake ( n ) << endl ; return 0 ; }
Count digits in given number N which divide N | C ++ program to find number of digits in N that divide N . ; Utility function to check divisibility by digit ; ( N [ i ] - '0' ) gives the digit value and form the number ; Function to count digits which appears in N and divide N divide [ 10 ] -- > array which tells that particular digit divides N or not count [ 10 ] -- > counts frequency of digits which divide N ; We initialize all digits of N as not divisible by N . ; start checking divisibility of N by digits 2 to 9 ; if digit divides N then mark it as true ; Now traverse the number string to find and increment result whenever a digit divides N . ; Driver program to run the case
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool divisible ( string N , int digit ) { int ans = 0 ; for ( int i = 0 ; i < N . length ( ) ; i ++ ) { ans = ( ans * 10 + ( N [ i ] - '0' ) ) ; ans %= digit ; } return ( ans == 0 ) ; } int allDigits ( string N ) { bool divide [ 10 ] = { false } ; for ( int digit = 2 ; digit <= 9 ; digit ++ ) { if ( divisible ( N , digit ) ) divide [ digit ] = true ; } int result = 0 ; for ( int i = 0 ; i < N . length ( ) ; i ++ ) { if ( divide [ N [ i ] - '0' ] == true ) result ++ ; } return result ; } int main ( ) { string N = "122324" ; cout << allDigits ( N ) ; return 0 ; }
Aliquot Sequence | C ++ implementation of Optimized approach to generate Aliquot Sequence ; Function to calculate sum of all proper divisors ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; else Otherwise take both ; calculate sum of all proper divisors only ; Function to print Aliquot Sequence for an input n . ; Print the first term ; Calculate next term from previous term ; Print next term ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getSum ( int n ) { for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum - n ; } void printAliquot ( int n ) { printf ( " % d ▁ " , n ) ; unordered_set < int > s ; s . insert ( n ) ; int next = 0 ; while ( n > 0 ) { n = getSum ( n ) ; if ( s . find ( n ) != s . end ( ) ) { cout << " Repeats with " break ; } cout << n << " ▁ " ; s . insert ( n ) ; } } int main ( ) { printAliquot ( 12 ) ; return 0 ; }
Find the prime numbers which can written as sum of most consecutive primes | C ++ program to find Longest Sum of consecutive primes ; 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 . ; function find the prime number which can be written as the sum of the most consecutive primes ; To store maximum length of consecutive primes that can sum to a limit ; The prime number ( or result ) that can be represented as sum of maximum number of primes . ; Consider all lengths of consecutive primes below limit . ; if we cross the limit , then break the loop ; sum_prime [ i ] - sum_prime [ j ] is prime number or not ; Check if sum of current length of consecutives is prime or not . ; update the length and prime number ; Returns the prime number that can written as sum of longest chain of consecutive primes . ; Store prime number in vector ; Calculate sum of prime numbers and store them in sum_prime array . sum_prime [ i ] stores sum of prime numbers from primes [ 0 ] to primes [ i - 1 ] ; Process all queries one by one ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 10000 ; void sieveSundaram ( vector < int > & primes ) { bool marked [ MAX / 2 + 1 ] = { 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 ) ; } int LSCPUtil ( int limit , vector < int > & prime , long long int sum_prime [ ] ) { int max_length = -1 ; int prime_number = -1 ; for ( int i = 0 ; prime [ i ] <= limit ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( sum_prime [ i ] - sum_prime [ j ] > limit ) break ; long long int consSum = sum_prime [ i ] - sum_prime [ j ] ; if ( binary_search ( prime . begin ( ) , prime . end ( ) , consSum ) ) { if ( max_length < i - j + 1 ) { max_length = i - j + 1 ; prime_number = consSum ; } } } } return prime_number ; } void LSCP ( int arr [ ] , int n ) { vector < int > primes ; sieveSundaram ( primes ) ; long long int sum_prime [ primes . size ( ) + 1 ] ; sum_prime [ 0 ] = 0 ; for ( int i = 1 ; i <= primes . size ( ) ; i ++ ) sum_prime [ i ] = primes [ i - 1 ] + sum_prime [ i - 1 ] ; for ( int i = 0 ; i < n ; i ++ ) cout << LSCPUtil ( arr [ i ] , primes , sum_prime ) << " ▁ " ; } int main ( ) { int arr [ ] = { 10 , 30 , 40 , 50 , 1000 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; LSCP ( arr , n ) ; return 0 ; }
Count numbers which can be constructed using two numbers | C ++ program to count all numbers that can be formed using two number numbers x an y ; Returns count of numbers from 1 to n that can be formed using x and y . ; Create an auxiliary array and initialize it as false . An entry arr [ i ] = true is going to mean that i can be formed using x and y ; x and y can be formed using x and y . ; Initialize result ; Traverse all numbers and increment result if a number can be formed using x and y . ; If i can be formed using x and y ; Then i + x and i + y can also be formed using x and y . ; Increment result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNums ( int n , int x , int y ) { vector < bool > arr ( n + 1 , false ) ; if ( x <= n ) arr [ x ] = true ; if ( y <= n ) arr [ y ] = true ; int result = 0 ; for ( int i = min ( x , y ) ; i <= n ; i ++ ) { if ( arr [ i ] ) { if ( i + x <= n ) arr [ i + x ] = true ; if ( i + y <= n ) arr [ i + y ] = true ; result ++ ; } } return result ; } int main ( ) { int n = 15 , x = 5 , y = 7 ; cout << countNums ( n , x , y ) ; return 0 ; }
Emirp numbers | Program to print Emirp numbers less than n ; Function to find reverse of any number ; Sieve method used for generating emirp number ( use of sieve of Eratosthenes ) ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Traverse all prime numbers ; Find reverse a number ; A number is emrip if it is not a palindrome number and its reverse is also prime . ; Mark reverse prime as false so that it 's  not printed again ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int reverse ( int x ) { int rev = 0 ; while ( x > 0 ) { rev = ( rev * 10 ) + x % 10 ; x = x / 10 ; } return rev ; } void printEmirp ( int n ) { bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { int rev = reverse ( p ) ; if ( p != rev && rev <= n && prime [ rev ] ) { cout << p << " ▁ " << rev << " ▁ " ; prime [ rev ] = false ; } } } } int main ( ) { int n = 40 ; printEmirp ( n ) ; return 0 ; }
Abundant Number | An Optimized Solution to check Abundant Number ; Function to calculate sum of divisors ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; else Otherwise take both ; calculate sum of all proper divisors only ; Function to check Abundant Number ; Return true if sum of divisors is greater than n . ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; { sum = sum + i ; sum = sum + ( n / i ) ; } } } sum = sum - n ; return sum ; } bool checkAbundant ( int n ) { return ( getSum ( n ) > n ) ; } int main ( ) { checkAbundant ( 12 ) ? cout << " YES STRNEWLINE " : cout << " NO STRNEWLINE " ; checkAbundant ( 15 ) ? cout << " YES STRNEWLINE " : cout << " NO STRNEWLINE " ; return 0 ; }
Powerful Number | C ++ program to find if a number is powerful or not . ; function to check if the number is powerful ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; if n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime numenr . Since prime numbers are not powerful , we return false if n is not 1. ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } int main ( ) { isPowerful ( 20 ) ? cout << " YES STRNEWLINE " : cout << " NO STRNEWLINE " ; isPowerful ( 27 ) ? cout << " YES STRNEWLINE " : cout << " NO STRNEWLINE " ; return 0 ; }
Deficient Number | C ++ program to implement an Optimized Solution to check Deficient Number ; Function to calculate sum of divisors ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; else Otherwise take both ; Function to check Deficient Number ; Check if sum ( n ) < 2 * n ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divisorsSum ( int n ) { for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { sum = sum + i ; } { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum ; } bool isDeficient ( int n ) { return ( divisorsSum ( n ) < ( 2 * n ) ) ; } int main ( ) { isDeficient ( 12 ) ? cout << " YES STRNEWLINE " : cout << " NO STRNEWLINE " ; isDeficient ( 15 ) ? cout << " YES STRNEWLINE " : cout << " NO STRNEWLINE " ; return 0 ; }
Harshad ( Or Niven ) Number | C ++ implementation of above approach ; Converting integer to string ; Initialising sum to 0 ; Traversing through the string ; Converting character to int ; Comparing number and sum ; Driver Code ; Passing this number to get result function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string checkHarshad ( int n ) { string st = to_string ( n ) ; int sum = 0 ; int length = st . length ( ) ; for ( char i : st ) { sum = sum + ( i - '0' ) ; } if ( n % sum == 0 ) { return " Yes " ; } else { return " No " ; } } int main ( ) { int number = 18 ; cout << checkHarshad ( number ) << endl ; }