codeparrot/xlcost-text-to-code huggingface dataset:
XOR of all the elements in the given range [ L , R ] | C ++ implementation of the approach ; Function to return the required XOR ; Modulus operator are expensive on most of the computers . n & 3 will be equivalent to n % 4 n % 4 ; If n is a multiple of 4 ; If n % 4 gives remainder 1 ; If n % 4 gives remainder 2 ; If n % 4 gives remainder 3 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long computeXOR ( const int n ) { switch ( n & 3 ) { case 0 : return n ; case 1 : return 1 ; case 2 : return n + 1 ; case 3 : return 0 ; } } int main ( ) { int l = 1 , r = 4 ; cout << ( computeXOR ( r ) ^ computeXOR ( l - 1 ) ) ; return 0 ; }
Find the number of integers from 1 to n which contains digits 0 ' s ▁ and ▁ 1' s only | C ++ program to find the number of integers from 1 to n which contains digits 0 ' s ▁ and ▁ 1' s only ; Function to find the number of integers from 1 to n which contains 0 ' s ▁ and ▁ 1' s only ; If number is greater than n ; otherwise add count this number and call two functions ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumbers ( int x , int n ) { if ( x > n ) return 0 ; return 1 + countNumbers ( x * 10 , n ) + countNumbers ( x * 10 + 1 , n ) ; } int main ( ) { int n = 120 ; cout << countNumbers ( 1 , n ) ; return 0 ; }
Check if factorial of N is divisible by the sum of squares of first N natural numbers | C ++ implementation of the approach ; Function to count number of times prime P divide factorial N ; Lengendre Formula ; Function to find count number of times all prime P divide summation ; Formula for summation of square after removing n and constant 6 ; Loop to traverse over all prime P which divide summation ; If Number itself is a Prime Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; bool checkfact ( int N , int countprime , int prime ) { int countfact = 0 ; if ( prime == 2 prime == 3 ) countfact ++ ; int divide = prime ; while ( N / divide != 0 ) { countfact += N / divide ; divide = divide * divide ; } if ( countfact >= countprime ) return true ; else return false ; } bool check ( int N ) { int sumsquares = ( N + 1 ) * ( 2 * N + 1 ) ; int countprime = 0 ; for ( int i = 2 ; i <= sqrt ( sumsquares ) ; i ++ ) { int flag = 0 ; while ( sumsquares % i == 0 ) { flag = 1 ; countprime ++ ; sumsquares /= i ; } if ( flag ) { if ( ! checkfact ( N - 1 , countprime , i ) ) return false ; countprime = 0 ; } } if ( sumsquares != 1 ) if ( ! checkfact ( N - 1 , 1 , sumsquares ) ) return false ; return true ; } int main ( ) { int N = 5 ; if ( check ( N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Count the number of non | C ++ program to count number of non increasing subarrays ; Initialize result ; Initialize length of current non increasing subarray ; Traverse through the array ; If arr [ i + 1 ] is less than or equal to arr [ i ] , then increment length ; Else Update count and reset length ; If last length is more than 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNonIncreasing ( int arr [ ] , int n ) { int cnt = 0 ; int len = 1 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { if ( arr [ i + 1 ] <= arr [ i ] ) len ++ ; else { cnt += ( ( ( len + 1 ) * len ) / 2 ) ; len = 1 ; } } if ( len > 1 ) cnt += ( ( ( len + 1 ) * len ) / 2 ) ; return cnt ; } int main ( ) { int arr [ ] = { 5 , 2 , 3 , 7 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countNonIncreasing ( arr , n ) ; return 0 ; }
Minimum array elements to be changed to make Recaman 's sequence | C ++ implementation of the approach ; First term of the sequence is always 0 ; Fill remaining terms using recursive formula ; If arr [ i - 1 ] - i is negative or already exists ; Function that returns minimum changes required ; Set to store first n Recaman numbers ; Generate and store first n Recaman numbers ; Insert first n Recaman numbers to set ; If current element of the array is present in the set ; Return the remaining number of elements in the set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int recamanGenerator ( int arr [ ] , int n ) { arr [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int temp = arr [ i - 1 ] - i ; int j ; for ( j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] == temp ) temp < 0 ) { temp = arr [ i - 1 ] + i ; break ; } } arr [ i ] = temp ; } } int recamanArray ( int arr [ ] , int n ) { unordered_set < int > s ; int recaman [ n ] ; recamanGenerator ( recaman , n ) ; for ( int i = 0 ; i < n ; i ++ ) s . insert ( recaman [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { auto it = s . find ( arr [ i ] ) ; if ( it != s . end ( ) ) s . erase ( it ) ; } return s . size ( ) ; } int main ( ) { int arr [ ] = { 7 , 11 , 20 , 4 , 2 , 1 , 8 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << recamanArray ( arr , n ) ; return 0 ; }
Check if product of array containing prime numbers is a perfect square | C ++ implementation of the approach ; Function that returns true if the product of all the array elements is a perfect square ; Update the frequencies of all the array elements ; If frequency of some element in the array is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( int arr [ ] , int n ) { unordered_map < int , int > umap ; for ( int i = 0 ; i < n ; i ++ ) umap [ arr [ i ] ] ++ ; unordered_map < int , int > :: iterator itr ; for ( itr = umap . begin ( ) ; itr != umap . end ( ) ; itr ++ ) if ( ( itr -> second ) % 2 == 1 ) return false ; return true ; } int main ( ) { int arr [ ] = { 2 , 2 , 7 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( isPerfectSquare ( arr , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find the good permutation of first N natural numbers | C ++ implementation of the approach ; Function to print the good permutation of first N natural numbers ; If n is odd ; Otherwise ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int printPermutation ( int n ) { if ( n % 2 != 0 ) cout << -1 ; else for ( int i = 1 ; i <= n / 2 ; i ++ ) cout << 2 * i << " ▁ " << 2 * i - 1 << " ▁ " ; } int main ( ) { int n = 4 ; printPermutation ( n ) ; return 0 ; }
Minimum number operations required to convert n to m | Set | C ++ implementation of the above approach ; Function to find the minimum number of steps ; If n exceeds M ; If N reaches the target ; The minimum of both the states will be the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAXN  10000000 NEW_LINE int minimumSteps ( int n , int m , int a , int b ) { if ( n > m ) return MAXN ; if ( n == m ) return 0 ; return min ( 1 + minimumSteps ( n * a , m , a , b ) , 1 + minimumSteps ( n * b , m , a , b ) ) ; } int main ( ) { int n = 120 , m = 51840 ; int a = 2 , b = 3 ; cout << minimumSteps ( n , m , a , b ) ; return 0 ; }
Minimum number of given operation required to convert n to m | C ++ implementation of the approach ; Function to return the minimum operations required ; Counting all 2 s ; Counting all 3 s ; If q contained only 2 and 3 as the only prime factors then it must be 1 now ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( int n , int m ) { if ( m % n != 0 ) return -1 ; int minOperations = 0 ; int q = m / n ; while ( q % 2 == 0 ) { q = q / 2 ; minOperations ++ ; } while ( q % 3 == 0 ) { q = q / 3 ; minOperations ++ ; } if ( q == 1 ) return minOperations ; return -1 ; } int main ( ) { int n = 120 , m = 51840 ; cout << minOperations ( n , m ) ; return 0 ; }
Sum of Fibonacci Numbers in a range | C ++ implementation of the approach ; Function to return the nth Fibonacci number ; Function to return the required sum ; To store the sum ; Calculate the sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE int fib ( int n ) { double phi = ( 1 + sqrt ( 5 ) ) / 2 ; return round ( pow ( phi , n ) / sqrt ( 5 ) ) ; } ll calculateSum ( int l , int r ) { ll sum = 0 ; for ( int i = l ; i <= r ; i ++ ) sum += fib ( i ) ; return sum ; } int main ( ) { int l = 4 , r = 8 ; cout << calculateSum ( l , r ) ; return 0 ; }
Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone | C ++ Program to find the biggest sphere which is inscribed within a cube which in turn inscribed within a right circular cone ; Function to find the radius of the sphere ; height and radius cannot be negative ; radius of the sphere ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float sphereSide ( float h , float r ) { if ( h < 0 && r < 0 ) return -1 ; float R = ( ( h * r * sqrt ( 2 ) ) / ( h + sqrt ( 2 ) * r ) ) / 2 ; return R ; } int main ( ) { float h = 5 , r = 6 ; cout << sphereSide ( h , r ) << endl ; return 0 ; }
Find the number of ways to divide number into four parts such that a = c and b = d | C ++ implementation for above approach ; Function to find the number of ways to divide N into four parts such that a = c and b = d ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int possibleways ( int n ) { if ( n % 2 == 1 ) return 0 ; else if ( n % 4 == 0 ) return n / 4 - 1 ; else return n / 4 ; } int main ( ) { int n = 20 ; cout << possibleways ( n ) ; return 0 ; }
Count sub | C ++ implementation of the approach ; Function to count sub - arrays whose product is divisible by K ; Calculate the product of the current sub - array ; If product of the current sub - array is divisible by K ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE int countSubarrays ( const int * arr , int n , int K ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { ll product = 1 ; for ( int x = i ; x <= j ; x ++ ) product *= arr [ x ] ; if ( product % K == 0 ) count ++ ; } } return count ; } int main ( ) { int arr [ ] = { 6 , 2 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 4 ; cout << countSubarrays ( arr , n , K ) ; return 0 ; }
Count sub | C ++ implementation of the approach ; Segment tree implemented as an array ; Function to build the segment tree ; Function to query product of sub - array [ l . . r ] in O ( log n ) time ; Function to count sub - arrays whose product is divisible by K ; Query segment tree to find product % k of the sub - array [ i . . j ] ; Driver code ; Build the segment tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE #define MAX  100002 NEW_LINE ll tree [ 4 * MAX ] ; void build ( int node , int start , int end , const int * arr , int k ) { if ( start == end ) { tree [ node ] = ( 1LL * arr [ start ] ) % k ; return ; } int mid = ( start + end ) >> 1 ; build ( 2 * node , start , mid , arr , k ) ; build ( 2 * node + 1 , mid + 1 , end , arr , k ) ; tree [ node ] = ( tree [ 2 * node ] * tree [ 2 * node + 1 ] ) % k ; } ll query ( int node , int start , int end , int l , int r , int k ) { if ( start > end start > r end < l ) { return 1 ; } if ( start >= l && end <= r ) { return tree [ node ] % k ; } int mid = ( start + end ) >> 1 ; ll q1 = query ( 2 * node , start , mid , l , r , k ) ; ll q2 = query ( 2 * node + 1 , mid + 1 , end , l , r , k ) ; return ( q1 * q2 ) % k ; } ll countSubarrays ( const int * arr , int n , int k ) { ll count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { ll product_mod_k = query ( 1 , 0 , n - 1 , i , j , k ) ; if ( product_mod_k == 0 ) { count ++ ; } } } return count ; } int main ( ) { int arr [ ] = { 6 , 2 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 4 ; build ( 1 , 0 , n - 1 , arr , k ) ; cout << countSubarrays ( arr , n , k ) ; return 0 ; }
Find a pair from the given array with maximum nCr value | C ++ implementation of the approach ; Function to print the pair that gives maximum nCr ; This gives the value of N in nCr ; Case 1 : When N is odd ; Case 2 : When N is even ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMaxValPair ( vector < long long > & v , int n ) { sort ( v . begin ( ) , v . end ( ) ) ; long long N = v [ n - 1 ] ; if ( N % 2 == 1 ) { long long first_maxima = N / 2 ; long long second_maxima = first_maxima + 1 ; long long ans1 = 3e18 , ans2 = 3e18 ; long long from_left = -1 , from_right = -1 ; long long from = -1 ; for ( long long i = 0 ; i < n ; ++ i ) { if ( v [ i ] > first_maxima ) { from = i ; break ; } else { long long diff = first_maxima - v [ i ] ; if ( diff < ans1 ) { ans1 = diff ; from_left = v [ i ] ; } } } from_right = v [ from ] ; long long diff1 = first_maxima - from_left ; long long diff2 = from_right - second_maxima ; if ( diff1 < diff2 ) cout << N << " ▁ " << from_left ; else cout << N << " ▁ " << from_right ; } else { long long maxima = N / 2 ; long long ans1 = 3e18 ; long long R = -1 ; for ( long long i = 0 ; i < n - 1 ; ++ i ) { long long diff = abs ( v [ i ] - maxima ) ; if ( diff < ans1 ) { ans1 = diff ; R = v [ i ] ; } } cout << N << " ▁ " << R ; } } int main ( ) { vector < long long > v = { 1 , 1 , 2 , 3 , 6 , 1 } ; int n = v . size ( ) ; printMaxValPair ( v , n ) ; return 0 ; }
Find the number of good permutations | C ++ implementation of the approach ; Function to return the count of good permutations ; For m = 0 , ans is 1 ; If k is greater than 1 ; If k is greater than 2 ; If k is greater than 3 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Permutations ( int n , int k ) { int ans = 1 ; if ( k >= 2 ) ans += ( n ) * ( n - 1 ) / 2 ; if ( k >= 3 ) ans += ( n ) * ( n - 1 ) * ( n - 2 ) * 2 / 6 ; if ( k >= 4 ) ans += ( n ) * ( n - 1 ) * ( n - 2 ) * ( n - 3 ) * 9 / 24 ; return ans ; } int main ( ) { int n = 5 , k = 2 ; cout << Permutations ( n , k ) ; return 0 ; }
Count integers in a range which are divisible by their euler totient value | C ++ implementation of the above approach . ; Function to return a ^ n ; Function to return count of integers that satisfy n % phi ( n ) = 0 ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE using namespace std ; ll power ( ll a , ll n ) { if ( n == 0 ) return 1 ; ll p = power ( a , n / 2 ) ; p = p * p ; if ( n & 1 ) p = p * a ; return p ; } int countIntegers ( ll l , ll r ) { ll ans = 0 , i = 1 ; ll v = power ( 2 , i ) ; while ( v <= r ) { while ( v <= r ) { if ( v >= l ) ans ++ ; v = v * 3 ; } i ++ ; v = power ( 2 , i ) ; } if ( l == 1 ) ans ++ ; return ans ; } int main ( ) { ll l = 12 , r = 21 ; cout << countIntegers ( l , r ) ; return 0 ; }
Number of pairs from the first N natural numbers whose sum is divisible by K | C ++ implementation of the approach ; Function to find the number of pairs from the set of natural numbers up to N whose sum is divisible by K ; Declaring a Hash to store count ; Storing the count of integers with a specific remainder in Hash array ; Check if K is even ; Count of pairs when both integers are divisible by K ; Count of pairs when one remainder is R and other remainder is K - R ; Count of pairs when both the remainders are K / 2 ; Count of pairs when both integers are divisible by K ; Count of pairs when one remainder is R and other remainder is K - R ; Driver code ; Print the count of pairs
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findPairCount ( int N , int K ) { int count = 0 ; int rem [ K ] ; rem [ 0 ] = N / K ; for ( int i = 1 ; i < K ; i ++ ) rem [ i ] = ( N - i ) / K + 1 ; if ( K % 2 == 0 ) { count += ( rem [ 0 ] * ( rem [ 0 ] - 1 ) ) / 2 ; for ( int i = 1 ; i < K / 2 ; i ++ ) count += rem [ i ] * rem [ K - i ] ; count += ( rem [ K / 2 ] * ( rem [ K / 2 ] - 1 ) ) / 2 ; } else { count += ( rem [ 0 ] * ( rem [ 0 ] - 1 ) ) / 2 ; for ( int i = 1 ; i <= K / 2 ; i ++ ) count += rem [ i ] * rem [ K - i ] ; } return count ; } int main ( ) { int N = 10 , K = 4 ; cout << findPairCount ( N , K ) ; return 0 ; }
Find the sum of all Truncatable primes below N | C ++ implementation of the approach ; To check if a number is prime or not ; Sieve of Eratosthenes function to find all prime numbers ; Function to return the sum of all truncatable primes below n ; To store the required sum ; Check every number below n ; Check from right to left ; If number is not prime at any stage ; Check from left to right ; If number is not prime at any stage ; If flag is still true ; Return the required sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  1000005 NEW_LINE bool prime [ N ] ; void sieve ( ) { memset ( prime , true , sizeof prime ) ; prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int i = 2 ; i < N ; i ++ ) if ( prime [ i ] ) for ( int j = i * 2 ; j < N ; j += i ) prime [ j ] = false ; } int sumTruncatablePrimes ( int n ) { int sum = 0 ; for ( int i = 2 ; i < n ; i ++ ) { int num = i ; bool flag = true ; while ( num ) { if ( ! prime [ num ] ) { flag = false ; break ; } num /= 10 ; } num = i ; int power = 10 ; while ( num / power ) { if ( ! prime [ num % power ] ) { flag = false ; break ; } power *= 10 ; } if ( flag ) sum += i ; } return sum ; } int main ( ) { int n = 25 ; sieve ( ) ; cout << sumTruncatablePrimes ( n ) ; return 0 ; }
Smallest and Largest N | C ++ implementation of the approach ; Function to print the largest and the smallest n - digit perfect squares ; Smallest n - digit perfect square ; Largest n - digit perfect square ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nDigitPerfectSquares ( int n ) { cout << pow ( ceil ( sqrt ( pow ( 10 , n - 1 ) ) ) , 2 ) << " ▁ " ; cout << pow ( ceil ( sqrt ( pow ( 10 , n ) ) ) - 1 , 2 ) ; } int main ( ) { int n = 4 ; nDigitPerfectSquares ( n ) ; return 0 ; }
Maximum trace possible for any sub | C ++ implementation of the approach ; Function to return the maximum trace possible for a sub - matrix of the given matrix ; Calculate the trace for each of the sub - matrix with top left corner at cell ( r , s ) ; Update the maximum trace ; Return the maximum trace ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  3 NEW_LINE int MaxTraceSub ( int mat [ ] [ N ] ) { int max_trace = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { int r = i , s = j , trace = 0 ; while ( r < N && s < N ) { trace += mat [ r ] [ s ] ; r ++ ; s ++ ; max_trace = max ( trace , max_trace ) ; } } } return max_trace ; } int main ( ) { int mat [ N ] [ N ] = { { 10 , 2 , 5 } , { 6 , 10 , 4 } , { 2 , 7 , -10 } } ; cout << MaxTraceSub ( mat ) ; return 0 ; }
Check if matrix can be converted to another matrix by transposing square sub | C ++ implementation of the approach ; Function that returns true if matrix1 can be converted to matrix2 with the given operation ; Traverse all the diagonals starting at first column ; Traverse in diagonal ; Store the diagonal elements ; Move up ; Sort the elements ; Check if they are same ; Traverse all the diagonals starting at last row ; Traverse in the diagonal ; Store diagonal elements ; Sort all elements ; Check for same ; If every element matches ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define n  3 NEW_LINE #define m  3 NEW_LINE bool check ( int a [ n ] [ m ] , int b [ n ] [ m ] ) { for ( int i = 0 ; i < n ; i ++ ) { vector < int > v1 , v2 ; int r = i ; int col = 0 ; while ( r >= 0 && col < m ) { v1 . push_back ( a [ r ] [ col ] ) ; v2 . push_back ( b [ r ] [ col ] ) ; r -- ; col ++ ; } sort ( v1 . begin ( ) , v1 . end ( ) ) ; sort ( v2 . begin ( ) , v2 . end ( ) ) ; for ( int i = 0 ; i < v1 . size ( ) ; i ++ ) { if ( v1 [ i ] != v2 [ i ] ) return false ; } } for ( int j = 1 ; j < m ; j ++ ) { vector < int > v1 , v2 ; int r = n - 1 ; int col = j ; while ( r >= 0 && col < m ) { v1 . push_back ( a [ r ] [ col ] ) ; v2 . push_back ( b [ r ] [ col ] ) ; r -- ; col ++ ; } sort ( v1 . begin ( ) , v1 . end ( ) ) ; sort ( v2 . begin ( ) , v2 . end ( ) ) ; for ( int i = 0 ; i < v1 . size ( ) ; i ++ ) { if ( v1 [ i ] != v2 [ i ] ) return false ; } } return true ; } int main ( ) { int a [ n ] [ m ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int b [ n ] [ m ] = { { 1 , 4 , 7 } , { 2 , 5 , 6 } , { 3 , 8 , 9 } } ; if ( check ( a , b ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Last digit of Product of two Large or Small numbers ( a * b ) | C ++ implementation of the above approach ; Fthe unction to print last digit of product a * b ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void lastDigit ( string a , string b ) { int lastDig = ( a [ a . length ( ) - 1 ] - '0' ) * ( b [ b . length ( ) - 1 ] - '0' ) ; cout << lastDig % 10 ; } int main ( ) { string a = "1234567891233" , b = "1234567891233156" ; lastDigit ( a , b ) ; return 0 ; }
Smallest and Largest Palindrome with N Digits | C ++ implementation of the above approach ; Function to print the smallest and largest palindrome with N digits ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPalindrome ( int n ) { if ( n == 1 ) { cout << " Smallest ▁ Palindrome : ▁ 0" << endl ; cout << " Largest ▁ Palindrome : ▁ 9" ; } else { cout << " Smallest ▁ Palindrome : ▁ " << pow ( 10 , n - 1 ) + 1 ; cout << " Largest Palindrome : " } } int main ( ) { int n = 4 ; printPalindrome ( n ) ; return 0 ; }
Addition of two numbers without propagating Carry | C ++ implementation of the above approach ; Function to print sum of 2 numbers without propagating carry ; Reverse a ; Reverse b ; Generate sum Since length of both a and b are same , take any one of them . ; Extract digits from a and b and add ; If sum is single digit ; If sum is not single digit reverse sum ; Extract digits from sum and append to result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int printSum ( int a , int b ) { int res = 0 ; int temp1 = 0 , temp2 = 0 ; while ( a ) { temp1 = temp1 * 10 + ( a % 10 ) ; a /= 10 ; } a = temp1 ; while ( b ) { temp2 = temp2 * 10 + ( b % 10 ) ; b /= 10 ; } b = temp2 ; while ( a ) { int sum = ( a % 10 + b % 10 ) ; if ( sum / 10 == 0 ) res = res * 10 + sum ; else { temp1 = 0 ; while ( sum ) { temp1 = temp1 * 10 + ( sum % 10 ) ; sum /= 10 ; } sum = temp1 ; while ( sum ) { res = res * 10 + ( sum % 10 ) ; sum /= 10 ; } } a /= 10 ; b /= 10 ; } return res ; } int main ( ) { int a = 7752 , b = 8834 ; cout << printSum ( a , b ) ; return 0 ; }
Number of digits before the decimal point in the division of two numbers | C ++ implementation of the approach ; Function to return the number of digits before the decimal in a / b ; Absolute value of a / b ; If result is 0 ; Count number of digits in the result ; Return the required count of digits ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigits ( int a , int b ) { int count = 0 ; int p = abs ( a / b ) ; if ( p == 0 ) return 1 ; while ( p > 0 ) { count ++ ; p = p / 10 ; } return count ; } int main ( ) { int a = 100 ; int b = 10 ; cout << countDigits ( a , b ) ; return 0 ; }
Number of digits before the decimal point in the division of two numbers | C ++ implementation of the approach ; Function to return the number of digits before the decimal in a / b ; Return the required count of digits ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigits ( int a , int b ) { return floor ( log10 ( abs ( a ) ) - log10 ( abs ( b ) ) ) + 1 ; } int main ( ) { int a = 100 ; int b = 10 ; cout << countDigits ( a , b ) ; return 0 ; }
Smallest odd number with N digits | C ++ implementation of the above approach ; Function to return smallest odd with n digits ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestOdd ( int n ) { if ( n == 1 ) return 1 ; return pow ( 10 , n - 1 ) + 1 ; } int main ( ) { int n = 4 ; cout << smallestOdd ( n ) ; return 0 ; }
Largest Even and Odd N | C ++ implementation of the approach ; Function to print the largest n - digit even and odd numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNumbers ( int n ) { int odd = pow ( 10 , n ) - 1 ; int even = odd - 1 ; cout << " Even ▁ = ▁ " << even << endl ; cout << " Odd ▁ = ▁ " << odd ; } int main ( ) { int n = 4 ; findNumbers ( n ) ; return 0 ; }