codeparrot/xlcost-text-to-code huggingface dataset:
Number of digits in 2 raised to power n | CPP program to find number of digits in 2 ^ n ; Function to find number of digits in 2 ^ n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigits ( int n ) { return ( n * log10 ( 2 ) + 1 ) ; } int main ( ) { int n = 5 ; cout << countDigits ( n ) << endl ; return 0 ; }
Smallest even digits number not less than N | CPP program to print the smallest integer not less than N with all even digits ; function to check if all digits are even of a given number ; iterate for all digits ; if ( ( n % 10 ) % 2 ) if digit is odd ; all digits are even ; function to return the smallest number with all digits even ; iterate till we find a number with all digits even ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int check_digits ( int n ) { while ( n ) { return 0 ; n /= 10 ; } return 1 ; } int smallest_number ( int n ) { for ( int i = n ; ; i ++ ) if ( check_digits ( i ) ) return i ; } int main ( ) { int N = 2397 ; cout << smallest_number ( N ) ; return 0 ; }
Smallest triangular number larger than p | CPP code to find the bucket to choose for picking flowers out of it ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findBucketNo ( int p ) { return ceil ( ( sqrt ( 8 * p + 1 ) - 1 ) / 2 ) ; } int main ( ) { int p = 10 ; cout << findBucketNo ( p ) ; return 0 ; }
LCM of factorial and its neighbors | CPP program to calculate the LCM of N ! and its neighbor ( N - 1 ) ! and ( N + 1 ) ! ; function to calculate the factorial ; returning the factorial of the largest number in the given three consecutive numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int factorial ( unsigned int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } int LCMOfNeighbourFact ( int n ) { return factorial ( n + 1 ) ; } int main ( ) { int N = 5 ; cout << LCMOfNeighbourFact ( N ) << " STRNEWLINE " ; return 0 ; }
Expressing factorial n as sum of consecutive numbers | CPP program to count number of ways we can express a factorial as sum of consecutive numbers ; sieve of Eratosthenes to compute the prime numbers ; Store all prime numbers ; function to calculate the largest power of a prime in a number ; Modular multiplication to avoid the overflow of multiplication Please see below for details https : www . geeksforgeeks . org / how - to - avoid - overflow - in - modular - multiplication / ; Returns count of ways to express n ! as sum of consecutives . ; We skip 2 ( First prime ) as we need to consider only odd primes ; compute the largest power of prime ; if the power of current prime number is zero in N ! , power of primes greater than current prime number will also be zero , so break out from the loop ; multiply the result at every step ; subtract 1 to exclude the case of 1 being an odd divisor ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  50002 NEW_LINE vector < int > primes ; void sieve ( ) { bool isPrime [ MAX ] ; memset ( isPrime , true , sizeof ( isPrime ) ) ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i < MAX ; i += p ) isPrime [ i ] = false ; } } for ( int p = 2 ; p < MAX ; p ++ ) if ( isPrime [ p ] ) primes . push_back ( p ) ; } long long int power ( long long int x , long long int y ) { long long int count = 0 ; long long int z = y ; while ( x >= z ) { count += ( x / z ) ; z *= y ; } return count ; } long long int modMult ( long long int a , long long int b , long long int mod ) { long long int res = 0 ; a = a % mod ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( res + a ) % mod ; a = ( a * 2 ) % mod ; b /= 2 ; } return res % mod ; } long long int countWays ( long long int n , long long int m ) { long long int ans = 1 ; for ( int i = 1 ; i < primes . size ( ) ; i ++ ) { long long int powers = power ( n , primes [ i ] ) ; if ( powers == 0 ) break ; ans = modMult ( ans , powers + 1 , m ) % m ; } if ( ( ( ans - 1 ) % m ) < 0 ) return ( ans - 1 + m ) % m ; else return ( ans - 1 ) % m ; } int main ( ) { sieve ( ) ; long long int n = 4 , m = 7 ; cout << countWays ( n , m ) ; return 0 ; }
Check if the given two numbers are friendly pair or not | Check if the given two number are friendly pair or not . ; Returns sum of all factors of n . ; Traversing through all prime factors . ; THE BELOW STATEMENT MAKES IT BETTER THAN ABOVE METHOD AS WE REDUCE VALUE OF n . ; This condition is to handle the case when n is a prime number greater than 2. ; Function to return gcd of a and b ; Function to check if the given two number are friendly pair or not . ; Finding the sum of factors of n and m ; finding gcd of n and sum of its factors . ; finding gcd of m and sum of its factors . ; checking is numerator and denominator of abundancy index of both number are equal or not . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumofFactors ( int n ) { int res = 1 ; for ( int i = 2 ; i <= 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 ; } int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } bool checkFriendly ( int n , int m ) { int sumFactors_n = sumofFactors ( n ) ; int sumFactors_m = sumofFactors ( m ) ; int gcd_n = gcd ( n , sumFactors_n ) ; int gcd_m = gcd ( m , sumFactors_m ) ; if ( n / gcd_n == m / gcd_m && sumFactors_n / gcd_n == sumFactors_m / gcd_m ) return true ; else return false ; } int main ( ) { int n = 6 , m = 28 ; checkFriendly ( n , m ) ? ( cout << " Yes STRNEWLINE " ) : ( cout << " No STRNEWLINE " ) ; return 0 ; }
Find n | C ++ program to find n - th Fortunate number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to Find primorial of order n ( product of first n prime numbers ) . ; Function to find next prime number greater than n ; Note that difference ( or m ) should be greater than 1. ; loop continuously until isPrime returns true for a number above n ; Ignoring the prime number that is 1 greater than n ; Returns n - th Fortunate number ; Driver function
#include <bits/stdc++.h> 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 ; } long long int primorial ( long long int n ) { long long int p = 2 ; n -- ; for ( int i = 3 ; n != 0 ; i ++ ) { if ( isPrime ( i ) ) { p = p * i ; n -- ; } i ++ ; } return p ; } long long int findNextPrime ( long long int n ) { long long int nextPrime = n + 2 ; while ( true ) { if ( isPrime ( nextPrime ) ) break ; nextPrime ++ ; } return nextPrime ; } long long int fortunateNumber ( int n ) { long long int p = primorial ( n ) ; return findNextPrime ( p ) - p ; } int main ( ) { long long int n = 5 ; cout << fortunateNumber ( n ) << " STRNEWLINE " ; return 0 ; }
Probability for three randomly chosen numbers to be in AP | CPP program to find probability that 3 randomly chosen numbers form AP . ; function to calculate probability ; Driver code to run above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; double procal ( int n ) { return ( 3.0 * n ) / ( 4.0 * ( n * n ) - 1 ) ; } int main ( ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << procal ( n ) ; return 0 ; }
Fermat 's Last Theorem | C ++ program to verify fermat 's last theorem for a given range and n. ; Check if there exists a triplet such that a ^ n + b ^ n = c ^ n ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void testSomeNumbers ( int limit , int n ) { if ( n < 3 ) return ; for ( int a = 1 ; a <= limit ; a ++ ) for ( int b = a ; b <= limit ; b ++ ) { int pow_sum = pow ( a , n ) + pow ( b , n ) ; double c = pow ( pow_sum , 1.0 / n ) ; int c_pow = pow ( ( int ) c , n ) ; if ( c_pow == pow_sum ) { cout << " Count ▁ example ▁ found " ; return ; } } cout << " No ▁ counter ▁ example ▁ within ▁ given " " ▁ range ▁ and ▁ data " ; } int main ( ) { testSomeNumbers ( 10 , 3 ) ; return 0 ; }
Product of given N fractions in reduced form | CPP program to find product of N fractions in reduced form . ; Function to return gcd of a and b ; Print the Product of N fraction in Reduced Form . ; finding the product of all N numerators and denominators . ; Finding GCD of new numerator and denominator ; Converting into reduced form . ; 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 productReduce ( int n , int num [ ] , int den [ ] ) { int new_num = 1 , new_den = 1 ; for ( int i = 0 ; i < n ; i ++ ) { new_num *= num [ i ] ; new_den *= den [ i ] ; } int GCD = gcd ( new_num , new_den ) ; new_num /= GCD ; new_den /= GCD ; cout << new_num << " / " << new_den << endl ; } int main ( ) { int n = 3 ; int num [ ] = { 1 , 2 , 5 } ; int den [ ] = { 2 , 1 , 6 } ; productReduce ( n , num , den ) ; return 0 ; }
Divide a big number into two parts that differ by k | C ++ program to Divide a Big Number into two parts ; Function to adds two Numbers represented as array of character . ; length of string ; initializing extra character position to 0 ; Adding each element of character and storing the carry . ; If remainder remains . ; Function to subtracts two numbers represented by string . ; Finding the length of the string . ; initializing extra character position to 0. ; Substrating each element of character . ; Function divides a number represented by character array a constant . ; Dividing each character element by constant . ; Function to reverses the character array . ; Reversing the array . ; Wrapper Function ; Reversing the character array . ; Adding the each element of both array and storing the sum in array a [ ] . ; Dividing the array a [ ] by 2. ; Reversing the character array to get output . ; Substracting each element of array i . e calculating a = a - b ; Reversing the character array to get output . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100 NEW_LINE void add ( char v1 [ ] , char v2 [ ] ) { int i , d , c = 0 ; int l1 = strlen ( v1 ) ; int l2 = strlen ( v2 ) ; for ( i = l1 ; i < l2 ; i ++ ) v1 [ i ] = '0' ; for ( i = l2 ; i < l1 ; i ++ ) v2 [ i ] = '0' ; for ( i = 0 ; i < l1 i < l2 ; i ++ ) { d = ( v1 [ i ] - '0' ) + ( v2 [ i ] - '0' ) + c ; c = d / 10 ; d %= 10 ; v1 [ i ] = '0' + d ; } while ( c ) { v1 [ i ] = '0' + ( c % 10 ) ; c /= 10 ; i ++ ; } v1 [ i ] = ' \0' ; v2 [ l2 ] = ' \0' ; } void subs ( char v1 [ ] , char v2 [ ] ) { int i , d , c = 0 ; int l1 = strlen ( v1 ) ; int l2 = strlen ( v2 ) ; for ( i = l2 ; i < l1 ; i ++ ) v2 [ i ] = '0' ; for ( i = 0 ; i < l1 ; i ++ ) { d = ( v1 [ i ] - '0' - c ) - ( v2 [ i ] - '0' ) ; if ( d < 0 ) { d += 10 ; c = 1 ; } else c = 0 ; v1 [ i ] = '0' + d ; } v2 [ l2 ] = ' \0' ; i = l1 - 1 ; while ( i > 0 && v1 [ i ] == '0' ) i -- ; v1 [ i + 1 ] = ' \0' ; } int divi ( char v [ ] , int q ) { int i , l = strlen ( v ) ; int c = 0 , d ; for ( i = l - 1 ; i >= 0 ; i -- ) { d = c * 10 + ( v [ i ] - '0' ) ; c = d % q ; d /= q ; v [ i ] = '0' + d ; } i = l - 1 ; while ( i > 0 && v [ i ] == '0' ) i -- ; v [ i + 1 ] = ' \0' ; return c ; } void rev ( char v [ ] ) { int l = strlen ( v ) ; int i ; char cc ; for ( i = 0 ; i < l - 1 - i ; i ++ ) { cc = v [ i ] ; v [ i ] = v [ l - 1 - i ] ; v [ l - i - 1 ] = cc ; } } void divideWithDiffK ( char a [ ] , char k [ ] ) { rev ( a ) ; rev ( k ) ; add ( a , k ) ; divi ( a , 2 ) ; rev ( a ) ; cout << " ▁ " << a ; rev ( a ) ; subs ( a , k ) ; rev ( a ) ; cout << " ▁ " << a ; } int main ( ) { char a [ MAX ] = "100" , k [ MAX ] = "20" ; divideWithDiffK ( a , k ) ; return 0 ; }
Find value of ( n ^ 1 + n ^ 2 + n ^ 3 + n ^ 4 ) mod 5 for given n | finding the value of f ( n ) mod 5 for given n . ; function for f ( n ) mod 5 ; if n % 5 == 1 return 4 ; else return 0 ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fnMod ( int n ) { if ( n % 5 == 1 ) return 4 ; else return 0 ; } int main ( ) { int n = 10 ; cout << fnMod ( n ) << endl ; n = 11 ; cout << fnMod ( n ) << endl ; return 0 ; }
Recursive sum of digits of a number formed by repeated appends | C ++ program to find Sum of digits of a number formed by repeating a number X number of times until sum become single digit . ; return single digit sum of a number . ; Returns recursive sum of digits of a number formed by repeating a number X number of times until sum become single digit . ; Driver program
#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 repeatedNumberSum ( int n , int x ) { int sum = x * digSum ( n ) ; return digSum ( sum ) ; } int main ( ) { int n = 24 , x = 3 ; cout << repeatedNumberSum ( n , x ) << endl ; return 0 ; }
Sum of n digit numbers divisible by a given number | Simple CPP program to sum of n digit divisible numbers . ; Returns sum of n digit numbers divisible by ' number ' ; compute the first and last term ; sum of number which having n digit and divisible by number ; Driver code
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int totalSumDivisibleByNum ( int n , int number ) { int firstnum = pow ( 10 , n - 1 ) ; int lastnum = pow ( 10 , n ) ; int sum = 0 ; for ( int i = firstnum ; i < lastnum ; i ++ ) if ( i % number == 0 ) sum += i ; return sum ; } int main ( ) { int n = 3 , num = 7 ; cout << totalSumDivisibleByNum ( n , num ) << " STRNEWLINE " ; return 0 ; }
Extended Midy 's theorem | C ++ program to demonstrate extended 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 Extended Midy 's theorem ; Dividing repeated part into m parts ; Computing sum of parts . ; Checking for Extended Midy ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string fractionToDecimal ( int numerator , int denominator ) { string res ; unordered_map < int , int > mp ; int rem = numerator % denominator ; while ( ( rem != 0 ) && ( mp . find ( rem ) == mp . end ( ) ) ) { mp [ rem ] = res . length ( ) ; rem = rem * 10 ; int res_part = rem / denominator ; res += to_string ( res_part ) ; rem = rem % denominator ; } return ( rem == 0 ) ? " - 1" : res . substr ( mp [ rem ] ) ; } bool isPrime ( int n ) { for ( int i = 2 ; i <= n / 2 ; i ++ ) if ( n % i == 0 ) return false ; return true ; } void ExtendedMidys ( string str , int n , int m ) { if ( ! isPrime ( n ) ) { cout << " Denominator ▁ is ▁ not ▁ prime , ▁ " << " thus ▁ Extended ▁ Midy ' s ▁ " << " theorem ▁ is ▁ not ▁ applicable " ; return ; } int l = str . length ( ) ; int part1 = 0 , part2 = 0 ; if ( l % 2 == 0 && l % m == 0 ) { int part [ m ] = { 0 } , sum = 0 , res = 0 ; for ( int i = 0 ; i < l ; i ++ ) { int var = i / m ; part [ var ] = part [ var ] * 10 + ( str [ i ] - '0' ) ; } for ( int i = 0 ; i < m ; i ++ ) { sum = sum + part [ i ] ; cout << part [ i ] << " ▁ " ; } cout << endl ; res = pow ( 10 , m ) - 1 ; if ( sum % res == 0 ) cout << " Extended ▁ Midy ' s ▁ theorem ▁ holds ! " ; else cout << " Extended ▁ Midy ' s ▁ theorem " << " ▁ doesn ' t ▁ hold ! " ; } else if ( l % 2 != 0 ) { cout << " The ▁ repeating ▁ decimal ▁ is " << " ▁ of ▁ odd ▁ length ▁ thus ▁ Extended ▁ " << " Midy ' s ▁ theorem ▁ is ▁ not ▁ applicable " ; } else if ( l % m != 0 ) { cout << " The ▁ repeating ▁ decimal ▁ can ▁ " << " not ▁ be ▁ divided ▁ into ▁ m ▁ digits " ; } } int main ( ) { int numr = 1 , denr = 17 , m = 4 ; string res = fractionToDecimal ( numr , denr ) ; if ( res == " - 1" ) cout << " The ▁ fraction ▁ does ▁ not " << " ▁ have ▁ repeating ▁ decimal " ; else { cout << " Repeating ▁ decimal ▁ = ▁ " << res << endl ; ExtendedMidys ( res , denr , m ) ; } return 0 ; }
Count n digit numbers divisible by given number | Simple CPP program to count n digit divisible numbers . ; Returns count of n digit numbers divisible by ' number ' ; compute the first and last term ; count total number of which having n digit and divisible by number ; Driver code
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int numberofterm ( int n , int number ) { int firstnum = pow ( 10 , n - 1 ) ; int lastnum = pow ( 10 , n ) ; int count = 0 ; for ( int i = firstnum ; i < lastnum ; i ++ ) if ( i % number == 0 ) count ++ ; return count ; } int main ( ) { int n = 3 , num = 7 ; cout << numberofterm ( n , num ) << " STRNEWLINE " ; return 0 ; }
N | C ++ program to find N - th term in George Cantor set of rational numbers ; let i = numerator ; let j = denominator ; to keep the check of no . of terms ; loop till k is not equal to n ; check if k is already equal to N then the first term is the required rational number ; loop for traversing from right to left downwards diagonally ; loop for traversing from left to right upwards diagonally ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void georgeCantor ( int n ) { int i = 1 ; int j = 1 ; int k = 1 ; while ( k < n ) { j ++ , k ++ ; if ( k == n ) break ; while ( j > 1 && k < n ) { i ++ , j -- , k ++ ; } if ( k == n ) break ; i ++ , k ++ ; if ( k == n ) break ; while ( i > 1 && k < n ) { i -- , j ++ , k ++ ; } } cout << " N - th ▁ term ▁ : ▁ " << i << " ▁ / ▁ " << j ; } int main ( ) { int n = 15 ; georgeCantor ( n ) ; return 0 ; }
Number is divisible by 29 or not | CPP program to demonstrate above method to check divisibility by 29. ; Returns true if n is divisible by 29 else returns false . ; add the lastdigit * 3 to renaming number until number comes only 2 digit ; return true if number is divisible by 29 another ; Driver Code
#include <iostream> NEW_LINE using namespace std ; bool isDivisible ( long long int n ) { while ( n / 100 ) { int last_digit = n % 10 ; n /= 10 ; n += last_digit * 3 ; } return ( n % 29 == 0 ) ; } int main ( ) { long long int n = 348 ; if ( isDivisible ( n ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Solve the Linear Equation of Single Variable | CPP program to solve the given equation ; Function to solve the given equation ; Traverse the equation ; For cases such as : x , - x , + x ; Flip sign once ' = ' is seen ; There may be a number left in the end ; For infinite solutions ; For no solution ; x = total sum / coeff of x ' - ' sign indicates moving numeric value to right hand side ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string solveEquation ( string equation ) { int n = equation . size ( ) , sign = 1 , coeff = 0 ; int total = 0 , i = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( equation [ j ] == ' + ' equation [ j ] == ' - ' ) { if ( j > i ) total += sign * stoi ( equation . substr ( i , j - i ) ) ; i = j ; } else if ( equation [ j ] == ' x ' ) { if ( ( i == j ) equation [ j - 1 ] == ' + ' ) coeff += sign ; else if ( equation [ j - 1 ] == ' - ' ) coeff -= sign ; else coeff += sign * stoi ( equation . substr ( i , j - i ) ) ; i = j + 1 ; } else if ( equation [ j ] == ' = ' ) { if ( j > i ) total += sign * stoi ( equation . substr ( i , j - i ) ) ; sign = -1 ; i = j + 1 ; } } if ( i < n ) total += sign * stoi ( equation . substr ( i ) ) ; if ( coeff == 0 && total == 0 ) return " Infinite ▁ solutions " ; if ( coeff == 0 && total ) return " No ▁ solution " ; int ans = - total / coeff ; return " x = " + to_string ( ans ) ; } int main ( ) { string equation = " x + 5-3 + x = 6 + x - 2" ; cout << solveEquation ( equation ) ; return 0 ; }
Check if a given number is Pronic | Efficient Approach | C / C ++ program to check if a number is pronic or not ; function to check Pronic Number ; Checking Pronic Number by multiplying consecutive numbers ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool pronic_check ( int n ) { int x = ( int ) ( sqrt ( n ) ) ; if ( x * ( x + 1 ) == n ) return true ; else return false ; } int main ( void ) { int n = 56 ; pronic_check ( n ) == true ? cout << " YES " : cout << " NO " ; return 0 ; }
Check if given number is perfect square | C ++ program for the above approach ; If ceil and floor are equal the number is a perfect square ; Driver Code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void checkperfectsquare ( int n ) { if ( ceil ( ( double ) sqrt ( n ) ) == floor ( ( double ) sqrt ( n ) ) ) { cout << " perfect ▁ square " ; } else { cout << " not ▁ a ▁ perfect ▁ square " ; } } int main ( ) { int n = 49 ; checkperfectsquare ( n ) ; return 0 ; }
Writing power function for large numbers | C ++ program to compute factorial of big numbers ; Maximum number of digits in output ; This function multiplies x with the number represented by res [ ] . res_size is size of res [ ] or number of digits in the number represented by res [ ] . This function uses simple school mathematics for multiplication . This function may value of res_size and returns the new value of res_size ; Initialize carry ; One by one multiply n with individual digits of res [ ] ; Store last digit of ' prod ' in res [ ] ; Put rest in carry ; Put carry in res and increase result size ; This function finds power of a number x ; printing value "1" for power = 0 ; Initialize result ; Multiply x n times ( x ^ n = x * x * x ... . n times ) ; Driver program
#include <iostream> NEW_LINE using namespace std ; #define MAX  100000 NEW_LINE int multiply ( int x , int res [ ] , int res_size ) { int carry = 0 ; for ( int i = 0 ; i < res_size ; i ++ ) { int prod = res [ i ] * x + carry ; res [ i ] = prod % 10 ; carry = prod / 10 ; } while ( carry ) { res [ res_size ] = carry % 10 ; carry = carry / 10 ; res_size ++ ; } return res_size ; } void power ( int x , int n ) { if ( n == 0 ) { cout << "1" ; return ; } int res [ MAX ] ; int res_size = 0 ; int temp = x ; while ( temp != 0 ) { res [ res_size ++ ] = temp % 10 ; temp = temp / 10 ; } for ( int i = 2 ; i <= n ; i ++ ) res_size = multiply ( x , res , res_size ) ; cout << x << " ^ " << n << " ▁ = ▁ " ; for ( int i = res_size - 1 ; i >= 0 ; i -- ) cout << res [ i ] ; } int main ( ) { int exponent = 100 ; int base = 20 ; power ( base , exponent ) ; return 0 ; }
P | CPP program to check if a number is a p - smooth number or not ; function to check if number n is a P - smooth number or not ; prime factorise it by 2 ; if the number is divisible by 2 ; check for all the possible numbers that can divide it ; prime factorize it by i ; stores the maximum if maximum and i , if i divides the number ; if n at the end is a prime number , then it a divisor itself ; Driver program to test above function
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; bool check ( int n , int p ) { int maximum = -1 ; while ( ! ( n % 2 ) ) { maximum = max ( maximum , 2 ) ; n = n / 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { maximum = max ( maximum , i ) ; n = n / i ; } } if ( n > 2 ) maximum = max ( maximum , n ) ; return ( maximum <= p ) ; } int main ( ) { int n = 24 , p = 7 ; if ( check ( n , p ) ) cout << " yes " ; else cout << " no " ; return 0 ; }
Time when minute hand and hour hand coincide | CPP code to find the minute at which the minute hand and hour hand coincide ; function to find the minute ; finding the angle between minute hand and the first hour hand ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find_time ( int h1 ) { int theta = 30 * h1 ; cout << " ( " << ( theta * 2 ) << " / " << "11" << " ) " << " ▁ minutes " ; } int main ( ) { int h1 = 3 ; find_time ( h1 ) ; return 0 ; }