codeparrot/xlcost-text-to-code huggingface dataset:
Numbers having difference with digit sum more than s | Program to find number of integer such that integer - digSum > s ; function for digit sum ; function to calculate count of integer s . t . integer - digSum > s ; if n < s no integer possible ; iterate for s range and then calculate total count of such integer if starting integer is found ; if no integer found return 0 ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digitSum ( long long int n ) { int digSum = 0 ; while ( n ) { digSum += n % 10 ; n /= 10 ; } return digSum ; } long long int countInteger ( long long int n , long long int s ) { if ( n < s ) return 0 ; for ( long long int i = s ; i <= min ( n , s + 163 ) ; i ++ ) if ( ( i - digitSum ( i ) ) > s ) return ( n - i + 1 ) ; return 0 ; } int main ( ) { long long int n = 1000 , s = 100 ; cout << countInteger ( n , s ) ; return 0 ; }
Division without using ' / ' operator | CPP program to divide a number by other without using / operator ; Function to find division without using ' / ' operator ; Handling negative numbers ; if num1 is greater than equal to num2 subtract num2 from num1 and increase quotient by one . ; checking if neg equals to 1 then making quotient negative ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int division ( int num1 , int num2 ) { if ( num1 == 0 ) return 0 ; if ( num2 == 0 ) return INT_MAX ; bool negResult = false ; if ( num1 < 0 ) { num1 = - num1 ; if ( num2 < 0 ) num2 = - num2 ; else negResult = true ; } else if ( num2 < 0 ) { num2 = - num2 ; negResult = true ; } int quotient = 0 ; while ( num1 >= num2 ) { num1 = num1 - num2 ; quotient ++ ; } if ( negResult ) quotient = - quotient ; return quotient ; } int main ( ) { int num1 = 13 , num2 = 2 ; cout << division ( num1 , num2 ) ; ; return 0 ; }
Nonagonal number | CPP Program find first n nonagonal number . ; Function to find nonagonal number series . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Nonagonal ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { cout << i * ( 7 * i - 5 ) / 2 ; cout << " ▁ " ; } } int main ( ) { int n = 10 ; Nonagonal ( n ) ; return 0 ; }
Find n | Program to calculate nth term of a series ; func for calualtion ; for summation of square of first n - natural nos . ; summation of first n natural nos . ; return result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int seriesFunc ( int n ) { int sumSquare = ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; int sumNatural = ( n * ( n + 1 ) / 2 ) ; return ( sumSquare + sumNatural + 1 ) ; } int main ( ) { int n = 8 ; cout << seriesFunc ( n ) << endl ; n = 13 ; cout << seriesFunc ( 13 ) ; return 0 ; }
Program to check Plus Perfect Number | CPP implementation to check if the number is plus perfect or not ; function to check plus perfect number ; calculating number of digits ; calculating plus perfect number ; checking whether number is plus perfect or not ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkplusperfect ( int x ) { int temp = x ; int n = 0 ; while ( x != 0 ) { x /= 10 ; n ++ ; } x = temp ; int sum = 0 ; while ( x != 0 ) { sum += pow ( x % 10 , n ) ; x /= 10 ; } return ( sum == temp ) ; } int main ( ) { int x = 9474 ; if ( checkplusperfect ( x ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Number of distinct subsets of a set | CPP program to count number of distinct subsets in an array of distinct numbers ; Returns 2 ^ n ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int subsetCount ( int arr [ ] , int n ) { return 1 << n ; } int main ( ) { int A [ ] = { 1 , 2 , 3 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << subsetCount ( A , n ) ; return 0 ; }
Program to calculate GST from original and net prices | CPP Program to compute GST from original and net prices . ; return value after calculate GST % ; Driver program to test above functions
#include <iostream> NEW_LINE using namespace std ; float Calculate_GST ( float org_cost , float N_price ) { return ( ( ( N_price - org_cost ) * 100 ) / org_cost ) ; } int main ( ) { float org_cost = 100 ; float N_price = 120 ; cout << " GST ▁ = ▁ " << Calculate_GST ( org_cost , N_price ) << " ▁ % ▁ " ; return 0 ; }
Centered hexagonal number | Program to find nth centered hexadecimal number . ; Function to find centered hexadecimal number . ; Formula to calculate nth centered hexadecimal number and return it into main function . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int centeredHexagonalNumber ( int n ) { return 3 * n * ( n - 1 ) + 1 ; } int main ( ) { int n = 10 ; cout << n << " th ▁ centered ▁ hexagonal ▁ number : ▁ " ; cout << centeredHexagonalNumber ( n ) ; return 0 ; }
Find the distance covered to collect items at equal distances | C ++ program to calculate the distance for given problem ; function to calculate the distance ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_distance ( int n ) { return n * ( ( 3 * n ) + 7 ) ; } int main ( ) { int n = 5 ; cout << " Distance ▁ = ▁ " << find_distance ( n ) ; return 0 ; }
Twin Prime Numbers | CPP program to check twin prime ; Please refer below post for details of this function https : goo . gl / Wv3fGv ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Returns true if n1 and n2 are twin primes ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } bool twinPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && abs ( n1 - n2 ) == 2 ) ; } int main ( ) { int n1 = 11 , n2 = 13 ; if ( twinPrime ( n1 , n2 ) ) cout << " Twin ▁ Prime " << endl ; else cout << endl << " Not ▁ Twin ▁ Prime " << endl ; return 0 ; }
Sum of the sequence 2 , 22 , 222 , ... ... ... | CPP program to find sum of series 2 , 22 , 222 , . . ; function which return the the sum of series ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float sumOfSeries ( int n ) { return 0.0246 * ( pow ( 10 , n ) - 1 - ( 9 * n ) ) ; } int main ( ) { int n = 3 ; cout << sumOfSeries ( n ) ; return 0 ; }
Find sum of even index binomial coefficients | CPP Program to find sum even indexed Binomial Coefficient . ; Returns value of even indexed Binomial Coefficient Sum which is 2 raised to power n - 1. ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int evenbinomialCoeffSum ( int n ) { return ( 1 << ( n - 1 ) ) ; } int main ( ) { int n = 4 ; printf ( " % d " , evenbinomialCoeffSum ( n ) ) ; return 0 ; }
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 ; required sum ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfTheSeries ( int n ) { return ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ; } int main ( ) { int n = 5 ; cout << " Sum ▁ = ▁ " << sumOfTheSeries ( n ) ; return 0 ; }
Sum of the series 1 + ( 1 + 2 ) + ( 1 + 2 + 3 ) + ( 1 + 2 + 3 + 4 ) + ... ... + ( 1 + 2 + 3 + 4 + ... + n ) | CPP program to find sum of given series ; Function to find sum of given series ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= i ; j ++ ) sum += j ; return sum ; } int main ( ) { int n = 10 ; cout << sumOfSeries ( n ) ; return 0 ; }
Number of triangles after N moves | C ++ program to calculate the number of equilateral triangles ; function to calculate number of triangles in Nth step ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfTriangles ( int n ) { int ans = 2 * ( pow ( 3 , n ) ) - 1 ; return ans ; } int main ( ) { int n = 2 ; cout << numberOfTriangles ( n ) ; return 0 ; }
Motzkin number | CPP Program to find Nth Motzkin Number . ; Return the nth Motzkin Number . ; Base case ; Finding i - th Motzkin number . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int motzkin ( int n ) { int dp [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = ( ( 2 * i + 1 ) * dp [ i - 1 ] + ( 3 * i - 3 ) * dp [ i - 2 ] ) / ( i + 2 ) ; return dp [ n ] ; } int main ( ) { int n = 8 ; cout << motzkin ( n ) << endl ; return 0 ; }
NicomachusÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¾¢ s Theorem ( Sum of k | Efficient CPP program to find sum of k - th group of positive odd integers . ; Return the sum of kth group of positive odd integer . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int kthgroupsum ( int k ) { return k * k * k ; } int main ( ) { int k = 3 ; cout << kthgroupsum ( k ) << endl ; return 0 ; }
Find x , y , z that satisfy 2 / n = 1 / x + 1 / y + 1 / z | CPP program to find x y z that satisfies 2 / n = 1 / x + 1 / y + 1 / z ... ; function to find x y and z that satisfy given equation . ; driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printXYZ ( int n ) { if ( n == 1 ) cout << -1 ; else cout << " x ▁ is ▁ " << n << " STRNEWLINE y ▁ is ▁ " << n + 1 << " STRNEWLINE z ▁ is ▁ " << n * ( n + 1 ) ; } int main ( ) { int n = 7 ; printXYZ ( n ) ; return 0 ; }
Find n | CPP program to find the n - th term in series 1 3 6 10 . . . ; Function to find nth term ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int term ( int n ) { return n * ( n + 1 ) / 2 ; } int main ( ) { int n = 4 ; cout << term ( n ) ; return 0 ; }
Find Harmonic mean using Arithmetic mean and Geometric mean | C ++ implementation of compution of arithmetic mean , geometric mean and harmonic mean ; Function to calculate arithmetic mean , geometric mean and harmonic mean ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; double compute ( int a , int b ) { double AM , GM , HM ; AM = ( a + b ) / 2 ; GM = sqrt ( a * b ) ; HM = ( GM * GM ) / AM ; return HM ; } int main ( ) { int a = 5 , b = 15 ; double HM = compute ( a , b ) ; cout << " Harmonic ▁ Mean ▁ between ▁ " << a << " ▁ and ▁ " << b << " ▁ is ▁ " << HM ; return 0 ; }
Find n | Program to find n - th element in the series 9 , 33 , 73 , 128. . ; Returns n - th element of the series ; driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int series ( int n ) { return ( 8 * n * n ) + 1 ; } int main ( ) { int n = 5 ; cout << series ( n ) ; return 0 ; }
Check if a number is divisible by all prime divisors of another number | CPP program to find if all prime factors of y divide x . ; Returns true if all prime factors of y divide x . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivisible ( int x , int y ) { if ( y == 1 ) return true ; if ( __gcd ( x , y ) == 1 ) return false ; return isDivisible ( x , y / gcd ) ; } int main ( ) { int x = 18 , y = 12 ; if ( isDivisible ( x , y ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Program to find Sum of a Series a ^ 1 / 1 ! + a ^ 2 / 2 ! + a ^ 3 / 3 ! + a ^ 4 / 4 ! + Ã ¢ â ‚¬¦ Ã ¢ â ‚¬¦ . + a ^ n / n ! | CPP program to print the sum of series ; function to calculate sum of given series ; multiply ( a / i ) to previous term ; store result in res ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; double sumOfSeries ( double a , double num ) { double res = 0 , prev = 1 ; for ( int i = 1 ; i <= num ; i ++ ) { prev *= ( a / i ) ; res = res + prev ; } return ( res ) ; } int main ( ) { double n = 5 , a = 2 ; cout << sumOfSeries ( a , n ) ; return 0 ; }
Program for Celsius To Fahrenheit conversion | CPP program to convert Celsius scale to Fahrenheit scale ; function to convert Celsius scale to Fahrenheit scale ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float Cel_To_Fah ( float n ) { return ( ( n * 9.0 / 5.0 ) + 32.0 ) ; } int main ( ) { float n = 20.0 ; cout << Cel_To_Fah ( n ) ; return 0 ; }
Series with largest GCD and sum equals to n | CPP program to find the series with largest GCD and sum equals to n ; function to generate and print the sequence ; stores the maximum gcd that can be possible of sequence . ; if maximum gcd comes out to be zero then not possible ; the smallest gcd possible is 1 ; traverse the array to find out the max gcd possible ; checks if the number is divisible or not ; checks if x is smaller then the max gcd possible and x is greater then the resultant gcd till now , then r = x ; checks if n / x is smaller than the max gcd possible and n / x is greater then the resultant gcd till now , then r = x ; traverses and prints d , 2d , 3d , ... , ( k - 1 ) d , ; computes the last element of the sequence n - s . ; prints the last element ; driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print_sequence ( int n , int k ) { int b = n / ( k * ( k + 1 ) / 2 ) ; if ( b == 0 ) { cout << -1 << endl ; } else { int r = 1 ; for ( int x = 1 ; x * x <= n ; x ++ ) { if ( n % x != 0 ) continue ; if ( x <= b && x > r ) r = x ; if ( n / x <= b && n / x > r ) r = n / x ; } for ( int i = 1 ; i < k ; i ++ ) cout << r * i << " ▁ " ; int res = n - ( r * ( k * ( k - 1 ) / 2 ) ) ; cout << res << endl ; } } int main ( ) { int n = 24 ; int k = 4 ; print_sequence ( n , k ) ; n = 24 , k = 5 ; print_sequence ( n , k ) ; n = 6 , k = 4 ; print_sequence ( n , k ) ; }
Number of compositions of a natural number | C ++ program to find the total number of compositions of a natural number ; Return 2 raised to power ( n - 1 ) ; Driver Code
#include <iostream> NEW_LINE using namespace std ; #define ull  unsigned long long NEW_LINE ull countCompositions ( ull n ) { return ( 1L ) << ( n - 1 ) ; } int main ( ) { ull n = 4 ; cout << countCompositions ( n ) << " STRNEWLINE " ; return 0 ; }
Program to count digits in an integer ( 4 Different Methods ) | Recursive C ++ program to count number of digits in a number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigit ( long long n ) { if ( n / 10 == 0 ) return 1 ; return 1 + countDigit ( n / 10 ) ; } int main ( void ) { long long n = 345289467 ; cout << " Number ▁ of ▁ digits ▁ : " << countDigit ( n ) ; return 0 ; }
Tribonacci Numbers | A DP based CPP program to print first n Tribonacci numbers . ; Driver code
#include <iostream> NEW_LINE using namespace std ; int printTrib ( int n ) { int dp [ n ] ; dp [ 0 ] = dp [ 1 ] = 0 ; dp [ 2 ] = 1 ; for ( int i = 3 ; i < n ; i ++ ) dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + dp [ i - 3 ] ; for ( int i = 0 ; i < n ; i ++ ) cout << dp [ i ] << " ▁ " ; } int main ( ) { int n = 10 ; printTrib ( n ) ; return 0 ; }
Tribonacci Numbers | Program to print first n tribonacci numbers Matrix Multiplication function for 3 * 3 matrix ; Recursive function to raise the matrix T to the power n ; base condition . ; recursively call to square the matrix ; calculating square of the matrix T ; if n is odd multiply it one time with M ; base condition ; T [ 0 ] [ 0 ] contains the tribonacci number so return it ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void multiply ( int T [ 3 ] [ 3 ] , int M [ 3 ] [ 3 ] ) { int a , b , c , d , e , f , g , h , i ; a = T [ 0 ] [ 0 ] * M [ 0 ] [ 0 ] + T [ 0 ] [ 1 ] * M [ 1 ] [ 0 ] + T [ 0 ] [ 2 ] * M [ 2 ] [ 0 ] ; b = T [ 0 ] [ 0 ] * M [ 0 ] [ 1 ] + T [ 0 ] [ 1 ] * M [ 1 ] [ 1 ] + T [ 0 ] [ 2 ] * M [ 2 ] [ 1 ] ; c = T [ 0 ] [ 0 ] * M [ 0 ] [ 2 ] + T [ 0 ] [ 1 ] * M [ 1 ] [ 2 ] + T [ 0 ] [ 2 ] * M [ 2 ] [ 2 ] ; d = T [ 1 ] [ 0 ] * M [ 0 ] [ 0 ] + T [ 1 ] [ 1 ] * M [ 1 ] [ 0 ] + T [ 1 ] [ 2 ] * M [ 2 ] [ 0 ] ; e = T [ 1 ] [ 0 ] * M [ 0 ] [ 1 ] + T [ 1 ] [ 1 ] * M [ 1 ] [ 1 ] + T [ 1 ] [ 2 ] * M [ 2 ] [ 1 ] ; f = T [ 1 ] [ 0 ] * M [ 0 ] [ 2 ] + T [ 1 ] [ 1 ] * M [ 1 ] [ 2 ] + T [ 1 ] [ 2 ] * M [ 2 ] [ 2 ] ; g = T [ 2 ] [ 0 ] * M [ 0 ] [ 0 ] + T [ 2 ] [ 1 ] * M [ 1 ] [ 0 ] + T [ 2 ] [ 2 ] * M [ 2 ] [ 0 ] ; h = T [ 2 ] [ 0 ] * M [ 0 ] [ 1 ] + T [ 2 ] [ 1 ] * M [ 1 ] [ 1 ] + T [ 2 ] [ 2 ] * M [ 2 ] [ 1 ] ; i = T [ 2 ] [ 0 ] * M [ 0 ] [ 2 ] + T [ 2 ] [ 1 ] * M [ 1 ] [ 2 ] + T [ 2 ] [ 2 ] * M [ 2 ] [ 2 ] ; T [ 0 ] [ 0 ] = a ; T [ 0 ] [ 1 ] = b ; T [ 0 ] [ 2 ] = c ; T [ 1 ] [ 0 ] = d ; T [ 1 ] [ 1 ] = e ; T [ 1 ] [ 2 ] = f ; T [ 2 ] [ 0 ] = g ; T [ 2 ] [ 1 ] = h ; T [ 2 ] [ 2 ] = i ; } void power ( int T [ 3 ] [ 3 ] , int n ) { if ( n == 0 n == 1 ) return ; int M [ 3 ] [ 3 ] = { { 1 , 1 , 1 } , { 1 , 0 , 0 } , { 0 , 1 , 0 } } ; power ( T , n / 2 ) ; multiply ( T , T ) ; if ( n % 2 ) multiply ( T , M ) ; } int tribonacci ( int n ) { int T [ 3 ] [ 3 ] = { { 1 , 1 , 1 } , { 1 , 0 , 0 } , { 0 , 1 , 0 } } ; if ( n == 0 n == 1 ) return 0 ; else power ( T , n - 2 ) ; return T [ 0 ] [ 0 ] ; } int main ( ) { int n = 10 ; for ( int i = 0 ; i < n ; i ++ ) cout << tribonacci ( i ) << " ▁ " ; cout << endl ; return 0 ; }
Geometric mean ( Two Methods ) | Program to calculate the geometric mean of the given array elements . ; function to calculate geometric mean and return float value . ; declare sum variable and initialize it to 1. ; Compute the sum of all the elements in the array . ; compute geometric mean through formula antilog ( ( ( log ( 1 ) + log ( 2 ) + . . . + log ( n ) ) / n ) and return the value to main function . ; Driver function ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; float geometricMean ( int arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + log ( arr [ i ] ) ; sum = sum / n ; return exp ( sum ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << geometricMean ( arr , n ) ; return 0 ; }
Smallest number k such that the product of digits of k is equal to n | C ++ implementation to find smallest number k such that the product of digits of k is equal to n ; function to find smallest number k such that the product of digits of k is equal to n ; if ' n ' is a single digit number , then it is the required number ; stack the store the digits ; repeatedly divide ' n ' by the numbers from 9 to 2 until all the numbers are used or ' n ' > 1 ; save the digit ' i ' that divides ' n ' onto the stack ; if true , then no number ' k ' can be formed ; pop digits from the stack ' digits ' and add them to ' k ' ; required smallest number ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int smallestNumber ( int n ) { if ( n >= 0 && n <= 9 ) return n ; stack < int > digits ; for ( int i = 9 ; i >= 2 && n > 1 ; i -- ) { while ( n % i == 0 ) { digits . push ( i ) ; n = n / i ; } } if ( n != 1 ) return -1 ; long long int k = 0 ; while ( ! digits . empty ( ) ) { k = k * 10 + digits . top ( ) ; digits . pop ( ) ; } return k ; } int main ( ) { int n = 100 ; cout << smallestNumber ( n ) ; return 0 ; }
Check if a number is magic ( Recursive sum of digits is 1 ) | CPP program to check if a number is Magic number . ; Note that the loop continues if n is 0 and sum is non - zero . It stops when n becomes 0 and sum becomes single digit . ; Return true if sum becomes 1. ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isMagic ( int n ) { int sum = 0 ; while ( n > 0 sum > 9 ) { if ( n == 0 ) { n = sum ; sum = 0 ; } sum += n % 10 ; n /= 10 ; } return ( sum == 1 ) ; } int main ( ) { int n = 1234 ; if ( isMagic ( n ) ) cout << " Magic ▁ Number " ; else cout << " Not ▁ a ▁ magic ▁ Number " ; return 0 ; }
Check if a number is magic ( Recursive sum of digits is 1 ) | C ++ program to check Whether the number is Magic or not . ; Accepting sample input ; Condition to check Magic number
#include <iostream> NEW_LINE using namespace std ; int main ( ) { int x = 1234 ; if ( x % 9 == 1 ) cout << ( " Magic ▁ Number " ) ; else cout << ( " Not ▁ a ▁ Magic ▁ Number " ) ; return 0 ; }
Rearrangement of a number which is also divisible by it | CPP program for finding rearrangement of n that is divisible by n ; perform hashing for given n ; perform hashing ; check whether any arrangement exists ; Create a hash for given number n The hash is of size 10 and stores count of each digit in n . ; check for all possible multipliers ; check hash table for both . Please refer below link for help of equal ( ) https : www . geeksforgeeks . org / stdequal - in - cpp / ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void storeDigitCounts ( int n , vector < int > & hash ) { while ( n ) { hash [ n % 10 ] ++ ; n /= 10 ; } } int rearrange ( int n ) { vector < int > hash_n ( 10 , 0 ) ; storeDigitCounts ( n , hash_n ) ; for ( int mult = 2 ; mult < 10 ; mult ++ ) { int curr = n * mult ; vector < int > hash_curr ( 10 , 0 ) ; storeDigitCounts ( curr , hash_curr ) ; if ( equal ( hash_n . begin ( ) , hash_n . end ( ) , hash_curr . begin ( ) ) ) return curr ; } return -1 ; } int main ( ) { int n = 10035 ; cout << rearrange ( n ) ; return 0 ; }
Sylvester 's sequence | CPP program to print terms of Sylvester 's sequence ; To store the product . ; To store the current number . ; Loop till n . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  1000000007 NEW_LINE void printSequence ( int n ) { int a = 1 ; int ans = 2 ; for ( int i = 1 ; i <= n ; i ++ ) { cout << ans << " ▁ " ; ans = ( ( a % N ) * ( ans % N ) ) % N ; a = ans ; ans = ( ans + 1 ) % N ; } } int main ( ) { int n = 6 ; printSequence ( n ) ; return 0 ; }
Program to find sum of first n natural numbers | CPP program to find sum of first n natural numbers . ; Returns sum of first n natural numbers ; Driver code
#include <iostream> NEW_LINE using namespace std ; int findSum ( int n ) { int sum = 0 ; for ( int x = 1 ; x <= n ; x ++ ) sum = sum + x ; return sum ; } int main ( ) { int n = 5 ; cout << findSum ( n ) ; return 0 ; }
Hailstone Numbers | C ++ program to generate hailstone numbers and calculate steps required to reduce them to 1 ; function to print hailstone numbers and to calculate the number of steps required ; N is initially 1. ; N is reduced to 1. ; If N is Even . ; N is Odd . ; Driver code ; Function to generate Hailstone Numbers ; Output : Number of Steps
#include <bits/stdc++.h> NEW_LINE using namespace std ; int HailstoneNumbers ( int N ) { static int c ; cout << N << " ▁ " ; if ( N == 1 && c == 0 ) { return c ; } else if ( N == 1 && c != 0 ) { c ++ ; return c ; } else if ( N % 2 == 0 ) { c ++ ; HailstoneNumbers ( N / 2 ) ; } else if ( N % 2 != 0 ) { c ++ ; HailstoneNumbers ( 3 * N + 1 ) ; } } int main ( ) { int N = 7 ; int x ; x = HailstoneNumbers ( N ) ; cout << endl ; cout << " Number ▁ of ▁ Steps : ▁ " << x ; return 0 ; }
Find m | CPP program to find m - th summation ; Function to return mth summation ; base case ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int SUM ( int n , int m ) { if ( m == 1 ) return ( n * ( n + 1 ) / 2 ) ; int sum = SUM ( n , m - 1 ) ; return ( sum * ( sum + 1 ) / 2 ) ; } int main ( ) { int n = 5 ; int m = 3 ; cout << " SUM ( " << n << " , ▁ " << m << " ) : ▁ " << SUM ( n , m ) ; return 0 ; }
Count number of digits after decimal on dividing a number | CPP program to count digits after dot when a number is divided by another . ; int ans = 0 ; Initialize result ; calculating remainder ; if this remainder appeared before then the numbers are irrational and would not converge to a solution the digits after decimal will be infinite ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int x , int y ) { unordered_map < int , int > m ; while ( x % y != 0 ) { x = x % y ; ans ++ ; if ( m . find ( x ) != m . end ( ) ) return -1 ; m [ x ] = 1 ; x = x * 10 ; } return ans ; } int main ( ) { int res = count ( 1 , 2 ) ; ( res == -1 ) ? cout << " INF " : cout << res ; cout << endl ; res = count ( 5 , 3 ) ; ( res == -1 ) ? cout << " INF " : cout << res ; cout << endl ; res = count ( 3 , 5 ) ; ( res == -1 ) ? cout << " INF " : cout << res ; return 0 ; }