codeparrot/xlcost-text-to-code huggingface dataset:
Program for Chocolate and Wrapper Puzzle | Efficient C ++ program to find maximum number of chocolates ; Returns maximum number of chocolates we can eat with given money , price of chocolate and number of wrapprices required to get a chocolate . ; Corner case ; First find number of chocolates that can be purchased with the given amount ; Now just add number of chocolates with the chocolates gained by wrapprices ; Driver code ; total money ; cost of each candy ; no of wrappers needs to be ; exchanged for one chocolate .
#include <iostream> NEW_LINE using namespace std ; int countMaxChoco ( int money , int price , int wrap ) { if ( money < price ) return 0 ; int choc = money / price ; choc = choc + ( choc - 1 ) / ( wrap - 1 ) ; return choc ; } int main ( ) { int money = 15 ; int price = 1 ; int wrap = 3 ; cout << countMaxChoco ( money , price , wrap ) ; return 0 ; }
Check if possible to move from given coordinate to desired coordinate | C ++ program to check if it is possible to reach ( a , b ) from ( x , y ) . ; Returns GCD of i and j ; Returns true if it is possible to go to ( a , b ) from ( x , y ) ; Find absolute values of all as sign doesn 't  matter. ; If gcd is equal then it is possible to reach . Else not possible . ; Driven Program ; Converting coordinate into positive integer
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int i , int j ) { if ( i == j ) return i ; if ( i > j ) return gcd ( i - j , j ) ; return gcd ( i , j - i ) ; } bool ispossible ( int x , int y , int a , int b ) { x = abs ( x ) , y = abs ( y ) , a = abs ( a ) , b = abs ( b ) ; return ( gcd ( x , y ) == gcd ( a , b ) ) ; } int main ( ) { int x = 35 , y = 15 ; int a = 20 , b = 25 ; ( ispossible ( x , y , a , b ) ) ? ( cout << " Yes " ) : ( cout << " No " ) ; return 0 ; }
Equidigital Numbers | C ++ Program to find Equidigital Numbers till n ; Array to store all prime less than and equal to MAX . ; Utility function for sieve of sundaram ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half This array is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Returns true if n is a Equidigital number , else false . ; Count digits in original number ; Count all digits in prime factors of n pDigit is going to hold this value . ; Count powers of p in n ; If primes [ i ] is a prime factor , ; Count the power of prime factors ; Add its digits to pDigit . ; Add digits of power of prime factors to pDigit . ; If n != 1 then one prime factor still to be summed up ; ; If digits in prime factors and digits in original number are same , then return true . Else return false . ; Driver code ; Finding all prime numbers before limit . These numbers are used to find prime factors .
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 10000 ; vector < int > primes ; void sieveSundaram ( ) { bool marked [ MAX / 2 + 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 ) ; } bool isEquidigital ( int n ) { if ( n == 1 ) return true ; int original_no = n ; int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits ++ ; original_no = original_no / 10 ; } int pDigit = 0 , count_exp = 0 , p ; for ( int i = 0 ; primes [ i ] <= n / 2 ; i ++ ) { while ( n % primes [ i ] == 0 ) { p = primes [ i ] ; n = n / p ; count_exp ++ ; } while ( p > 0 ) { pDigit ++ ; p = p / 10 ; } while ( count_exp > 1 ) { pDigit ++ ; count_exp = count_exp / 10 ; } } if ( n != 1 ) { while ( n > 0 ) { pDigit ++ ; n = n / 10 ; } } return ( pDigit == sumDigits ) ; } int main ( ) { sieveSundaram ( ) ; cout << " Printing ▁ first ▁ few ▁ Equidigital ▁ Numbers " " ▁ using ▁ isEquidigital ( ) STRNEWLINE " ; for ( int i = 1 ; i < 20 ; i ++ ) if ( isEquidigital ( i ) ) cout << i << " ▁ " ; return 0 ; }
Maximum sum of distinct numbers with LCM as N | C / C ++ program to get maximum sum of Numbers with condition that their LCM should be N ; Method returns maximum sum f distinct number whose LCM is N ; find all divisors which divides ' N ' ; if ' i ' is divisor of ' N ' ; if both divisors are same then add it only once else add both ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMaximumSumWithLCMN ( int N ) { int sum = 0 ; int LIM = sqrt ( N ) ; for ( int i = 1 ; i <= LIM ; i ++ ) { if ( N % i == 0 ) { if ( i == ( N / i ) ) sum += i ; else sum += ( i + N / i ) ; } } return sum ; } int main ( ) { int N = 12 ; cout << getMaximumSumWithLCMN ( N ) << endl ; return 0 ; }
LCM of First n Natural Numbers | C ++ program to find LCM of First N Natural Numbers . ; to calculate hcf ; to calculate lcm ; lcm ( a , b ) = ( a * b ) / hcf ( a , b ) ; assign a = lcm of n , n - 1 ; b = b - 1 ; Driver code ; base case ; Function call pass n , n - 1 in function to find LCM of first n natural number
#include <bits/stdc++.h> NEW_LINE using namespace std ; int hcf ( int a , int b ) { if ( b == 0 ) return a ; return hcf ( b , a % b ) ; } int findlcm ( int a , int b ) { if ( b == 1 ) return a ; a = ( a * b ) / hcf ( a , b ) ; b -= 1 ; return findlcm ( a , b ) ; } int main ( ) { int n = 7 ; if ( n < 3 ) cout << n ; else cout << findlcm ( n , n - 1 ) ; return 0 ; }
Smallest number divisible by first n numbers | C ++ program to find smallest number evenly divisible by all numbers 1 to n ; Function returns the lcm of first n numbers ; Driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long lcm ( long long n ) { long long ans = 1 ; for ( long long i = 1 ; i <= n ; i ++ ) ans = ( ans * i ) / ( __gcd ( ans , i ) ) ; return ans ; } int main ( ) { long long n = 20 ; cout << lcm ( n ) ; return 0 ; }
Trapezoidal Rule for Approximate Value of Definite Integral | C ++ program to implement Trapezoidal rule ; A sample function whose definite integral 's approximate value is computed using Trapezoidal rule ; Declaring the function f ( x ) = 1 / ( 1 + x * x ) ; Function to evaluate the value of integral ; Grid spacing ; Computing sum of first and last terms in above formula ; Adding middle terms in above formula ; h / 2 indicates ( b - a ) / 2 n . Multiplying h / 2 with s . ; Driver program to test above function ; Range of definite integral ; Number of grids . Higher value means more accuracy
#include <stdio.h> NEW_LINE float y ( float x ) { return 1 / ( 1 + x * x ) ; } float trapezoidal ( float a , float b , float n ) { float h = ( b - a ) / n ; float s = y ( a ) + y ( b ) ; for ( int i = 1 ; i < n ; i ++ ) s += 2 * y ( a + i * h ) ; return ( h / 2 ) * s ; } int main ( ) { float x0 = 0 ; float xn = 1 ; int n = 6 ; printf ( " Value ▁ of ▁ integral ▁ is ▁ % 6.4f STRNEWLINE " , trapezoidal ( x0 , xn , n ) ) ; return 0 ; }
Breaking an Integer to get Maximum Product |  ; The main function that returns the max possible product ; n equals to 2 or 3 must be handled explicitly ; Keep removing parts of size 3 while n is greater than 4 ; Keep multiplying 3 to res ; The last part multiplied by previous parts ; Driver program to test above functions
#include <iostream> NEW_LINE using namespace std ; int maxProd ( int n ) { if ( n == 2 n == 3 ) return ( n - 1 ) ; int res = 1 ; while ( n > 4 ) { n -= 3 ; res *= 3 ; } return ( n * res ) ; } int main ( ) { cout << " Maximum ▁ Product ▁ is ▁ " << maxProd ( 45 ) ; return 0 ; }
Finding number of digits in n 'th Fibonacci number | C ++ program to find number of digits in nth Fibonacci number ; This function returns the number of digits in nth Fibonacci number after ceiling it Formula used ( n * log ( phi ) - ( log 5 ) / 2 ) ; using phi = 1.6180339887498948 ; Driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long numberOfDigits ( long long n ) { if ( n == 1 ) return 1 ; long double d = ( n * log10 ( 1.6180339887498948 ) ) - ( ( log10 ( 5 ) ) / 2 ) ; return ceil ( d ) ; } int main ( ) { long long i ; for ( i = 1 ; i <= 10 ; i ++ ) cout << " Number ▁ of ▁ Digits ▁ in ▁ F ( " << i << " ) ▁ - ▁ " << numberOfDigits ( i ) << " STRNEWLINE " ; return 0 ; }
Number of elements with odd factors in given range | C ++ program to count number of odd squares in given range [ n , m ] ; Function to count odd squares ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOddSquares ( int n , int m ) { return ( int ) pow ( m , 0.5 ) - ( int ) pow ( n - 1 , 0.5 ) ; } int main ( ) { int n = 5 , m = 100 ; cout << " Count ▁ is ▁ " << countOddSquares ( n , m ) ; return 0 ; }
K 'th Boom Number | C ++ program to find K 'th Boom number ; This function uses queue data structure to K 'th Boom number ; Create an empty queue of strings ; Enqueue an empty string ; counter for K 'th element ; This loop checks the value of count to become equal to K when value of count will be equals to k we will print the Boom number ; current Boom number ; Store current Boom number before changing it ; Append "2" to string s1 and enqueue it ; check if count == k ; K 'th Boom number ; Append "3" to string s2 and enqueue it . Note that s2 contains the previous front ; check if count == k ; K 'th Boom number ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long int ll ; void boomNumber ( ll k ) { queue < string > q ; q . push ( " " ) ; ll count = 0 ; while ( count <= k ) { string s1 = q . front ( ) ; q . pop ( ) ; string s2 = s1 ; q . push ( s1 . append ( "2" ) ) ; count ++ ; if ( count == k ) { cout << s1 << endl ; break ; } q . push ( s2 . append ( "3" ) ) ; count ++ ; if ( count == k ) { cout << s2 << endl ; break ; } } return ; } int main ( ) { ll k = 1000000 ; boomNumber ( k ) ; return 0 ; }
Check if a number exists having exactly N factors and K prime factors | C ++ program to check if it is possible to make a number having total N factors and K prime factors ; Function to compute the number of factors of the given number ; Vector to store the prime factors ; While there are no two multiples in the number , divide it by 2 ; If the size is already greater than K , then return true ; Computing the remaining divisors of the number ; If n is divisible by i , then it is a divisor ; If the size is already greater than K , then return true ; If the size is already greater than K , then return true ; If none of the above conditions satisfies , then return false ; Function to check if it is possible to make a number having total N factors and K prime factors ; If total divisors are less than the number of prime divisors , then print No ; Find the number of factors of n ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool factors ( int n , int k ) { vector < int > v ; while ( n % 2 == 0 ) { v . push_back ( 2 ) ; n /= 2 ; } if ( v . size ( ) >= k ) return true ; for ( int i = 3 ; i * i <= n ; i += 2 ) { while ( n % i == 0 ) { n = n / i ; v . push_back ( i ) ; } if ( v . size ( ) >= k ) return true ; } if ( n > 2 ) v . push_back ( n ) ; if ( v . size ( ) >= k ) return true ; return false ; } void operation ( int n , int k ) { bool answered = false ; if ( n < k ) { answered = true ; cout << " No " << " STRNEWLINE " ; } bool ok = factors ( n , k ) ; if ( ! ok && ! answered ) { answered = true ; cout << " No " << " STRNEWLINE " ; } if ( ok && ! answered ) cout << " Yes " << " STRNEWLINE " ; } int main ( ) { int n = 4 ; int k = 2 ; operation ( n , k ) ; return 0 ; }
To Generate a One Time Password or Unique Identification URL | A C / C ++ Program to generate OTP ( One Time Password ) ; A Function to generate a unique OTP everytime ; All possible characters of my OTP ; String to hold my OTP ; Driver Program to test above functions ; For different values each time we run the code ; Declare the length of OTP
#include <bits/stdc++.h> NEW_LINE using namespace std ; string generateOTP ( int len ) { string str = " abcdefghijklmnopqrstuvwxyzABCD " " EFGHIJKLMNOPQRSTUVWXYZ0123456789" ; int n = str . length ( ) ; string OTP ; for ( int i = 1 ; i <= len ; i ++ ) OTP . push_back ( str [ rand ( ) % n ] ) ; return ( OTP ) ; } int main ( ) { srand ( time ( NULL ) ) ; int len = 6 ; printf ( " Your ▁ OTP ▁ is ▁ - ▁ % s " , generateOTP ( len ) . c_str ( ) ) ; return ( 0 ) ; }
No of Factors of n ! | C ++ program to count number of factors of n ; Sieve of Eratosthenes to mark all prime number in array prime as 1 ; Initialize all numbers as prime ; Mark composites ; Returns the highest exponent of p in n ! ; Returns the no of factors in n ! ; ans stores the no of factors in n ! ; Find all primes upto n ; Multiply exponent ( of primes ) added with 1 ; if p is a prime then p is also a prime factor of n ! ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long int ll ; void sieve ( int n , bool prime [ ] ) { for ( int i = 1 ; i <= n ; i ++ ) prime [ i ] = 1 ; prime [ 1 ] = 0 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * i ; j <= n ; j += i ) prime [ j ] = 0 ; } } } int expFactor ( int n , int p ) { int x = p ; int exponent = 0 ; while ( ( n / x ) > 0 ) { exponent += n / x ; x *= p ; } return exponent ; } ll countFactors ( int n ) { ll ans = 1 ; bool prime [ n + 1 ] ; sieve ( n , prime ) ; for ( int p = 1 ; p <= n ; p ++ ) { if ( prime [ p ] == 1 ) ans *= ( expFactor ( n , p ) + 1 ) ; } return ans ; } int main ( ) { int n = 16 ; printf ( " Count ▁ of ▁ factors ▁ of ▁ % d ! ▁ is ▁ % lld STRNEWLINE " , n , countFactors ( n ) ) ; return 0 ; }
LCM of given array elements |  ; recursive implementation ; lcm ( a , b ) = ( a * b / gcd ( a , b ) ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LcmOfArray ( vector < int > arr , int idx ) { if ( idx == arr . size ( ) - 1 ) { return arr [ idx ] ; } int a = arr [ idx ] ; int b = LcmOfArray ( arr , idx + 1 ) ; return ( a * b / __gcd ( a , b ) ) ; } int main ( ) { vector < int > arr = { 1 , 2 , 8 , 3 } ; cout << LcmOfArray ( arr , 0 ) << " STRNEWLINE " ; arr = { 2 , 7 , 3 , 9 , 4 } ; cout << LcmOfArray ( arr , 0 ) << " STRNEWLINE " ; return 0 ; }
Check if a number is a power of another number | C ++ program to check if a number is power of another number ; Returns 1 if y is a power of x ; The only power of 1 is 1 itself ; Repeatedly comput power of x ; Check if power of x becomes y ; Driver program to test above function ; check the result for true / false and print
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPower ( int x , long int y ) { if ( x == 1 ) return ( y == 1 ) ; long int pow = 1 ; while ( pow < y ) pow *= x ; return ( pow == y ) ; } int main ( ) { check the result for true / false and print cout << isPower ( 10 , 1 ) << endl ; cout << isPower ( 1 , 20 ) << endl ; cout << isPower ( 2 , 128 ) << endl ; cout << isPower ( 2 , 30 ) << endl ; return 0 ; }
Number of perfect squares between two given numbers | An Efficient Method to count squares between a and b ; An efficient solution to count square between a and b ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSquares ( int a , int b ) { return ( floor ( sqrt ( b ) ) - ceil ( sqrt ( a ) ) + 1 ) ; } int main ( ) { int a = 9 , b = 25 ; cout << " Count ▁ of ▁ squares ▁ is ▁ " << countSquares ( a , b ) ; return 0 ; }
Count positive integers with 0 as a digit and maximum ' d ' digits | C ++ program to find the count of natural numbers upto a given number of digits that contain atleast one zero ; Utility function to calculate the count of natural numbers upto a given number of digits that contain atleast one zero ; Sum of two GP series ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCountUpto ( int d ) { int GP1_Sum = 9 * ( ( pow ( 10 , d ) - 1 ) / 9 ) ; int GP2_Sum = 9 * ( ( pow ( 9 , d ) - 1 ) / 8 ) ; return GP1_Sum - GP2_Sum ; } int main ( ) { int d = 1 ; cout << findCountUpto ( d ) << endl ; d = 2 ; cout << findCountUpto ( d ) << endl ; d = 4 ; cout << findCountUpto ( d ) << endl ; return 0 ; }
Check if count of divisors is even or odd | Naive Solution to find if count of divisors is even or odd ; Function to count the divisors ; Initialize count of divisors ; Note that this loop runs till square root ; If divisors are equal increment count by one Otherwise increment count by 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countDivisors ( int n ) { int count = 0 ; for ( int i = 1 ; i <= sqrt ( n ) + 1 ; i ++ ) { if ( n % i == 0 ) count += ( n / i == i ) ? 1 : 2 ; } if ( count % 2 == 0 ) cout << " Even " << endl ; else cout << " Odd " << endl ; } int main ( ) { cout << " The ▁ count ▁ of ▁ divisor : ▁ " ; countDivisors ( 10 ) ; return 0 ; }
Gray to Binary and Binary to Gray conversion | C ++ program for above approach ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int greyConverter ( int n ) { return n ^ ( n >> 1 ) ; } int main ( ) { int n = 3 ; cout << greyConverter ( n ) << endl ; n = 9 ; cout << greyConverter ( n ) << endl ; return 0 ; }
Compute n ! under modulo p | C ++ program to Returns n % p using Sieve of Eratosthenes ; Returns largest power of p that divides n ! ; Initialize result ; Calculate x = n / p + n / ( p ^ 2 ) + n / ( p ^ 3 ) + ... . ; Utility function to do modular exponentiation . It returns ( x ^ y ) % p ; Initialize result Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Returns n ! % p ; Use Sieve of Eratosthenes to find all primes smaller than n ; Consider all primes found by Sieve ; Find the largest power of prime ' i ' that divides n ; Multiply result with ( i ^ k ) % p ; Driver method
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largestPower ( int n , int p ) { int x = 0 ; while ( n ) { n /= p ; x += n ; } return x ; } int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } int modFact ( int n , int p ) { if ( n >= p ) return 0 ; int res = 1 ; bool isPrime [ n + 1 ] ; memset ( isPrime , 1 , sizeof ( isPrime ) ) ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = 2 * i ; j <= n ; j += i ) isPrime [ j ] = 0 ; } } for ( int i = 2 ; i <= n ; i ++ ) { if ( isPrime [ i ] ) { int k = largestPower ( n , i ) ; res = ( res * power ( i , k , p ) ) % p ; } } return res ; } int main ( ) { int n = 25 , p = 29 ; cout << modFact ( n , p ) ; return 0 ; }
Count number of squares in a rectangle | C ++ program to count squares in a rectangle of size m x n ; Returns count of all squares in a rectangle of size m x n ; If n is smaller , swap m and n ; Now n is greater dimension , apply formula ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int countSquares ( int m , int n ) { if ( n < m ) swap ( m , n ) ; return m * ( m + 1 ) * ( 2 * m + 1 ) / 6 + ( n - m ) * m * ( m + 1 ) / 2 ; } int main ( ) { int m = 4 , n = 3 ; cout << " Count ▁ of ▁ squares ▁ is ▁ " << countSquares ( m , n ) ; }
Add two numbers using ++ and / or | C ++ program to add two numbers using ++ ; Returns value of x + y without using + ; If y is positive , y times add 1 to x ; If y is negative , y times subtract 1 from x ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int add ( int x , int y ) { while ( y > 0 && y -- ) x ++ ; while ( y < 0 && y ++ ) x -- ; return x ; } int main ( ) { cout << add ( 43 , 23 ) << endl ; cout << add ( 43 , -23 ) << endl ; return 0 ; }
Count factorial numbers in a given range | Program to count factorial numbers in given range ; Function to count factorial ; Find the first factorial number ' fact ' greater than or equal to ' low ' ; Count factorial numbers in range [ low , high ] ; Return the count ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int countFact ( int low , int high ) { int fact = 1 , x = 1 ; while ( fact < low ) { fact = fact * x ; x ++ ; } int res = 0 ; while ( fact <= high ) { res ++ ; fact = fact * x ; x ++ ; } return res ; } int main ( ) { cout << " Count ▁ is ▁ " << countFact ( 2 , 720 ) ; return 0 ; }
Find number of days between two given dates | C ++ program two find number of days between two given dates ; A date has day ' d ' , month ' m ' and year ' y ' ; To store number of days in all months from January to Dec . ; This function counts number of leap years before the given date ; Check if the current year needs to be considered for the count of leap years or not ; An year is a leap year if it is a multiple of 4 , multiple of 400 and not a multiple of 100. ; This function returns number of days between two given dates ; initialize count using years and day ; Add days for months in given date ; Since every leap year is of 366 days , Add a day for every leap year ; SIMILARLY , COUNT TOTAL NUMBER OF DAYS BEFORE ' dt2' ; return difference between two counts ; Driver code ; Function call
#include <iostream> NEW_LINE using namespace std ; struct Date { int d , m , y ; } ; const int monthDays [ 12 ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; int countLeapYears ( Date d ) { int years = d . y ; if ( d . m <= 2 ) years -- ; return years / 4 - years / 100 + years / 400 ; } int getDifference ( Date dt1 , Date dt2 ) { long int n1 = dt1 . y * 365 + dt1 . d ; for ( int i = 0 ; i < dt1 . m - 1 ; i ++ ) n1 += monthDays [ i ] ; n1 += countLeapYears ( dt1 ) ; long int n2 = dt2 . y * 365 + dt2 . d ; for ( int i = 0 ; i < dt2 . m - 1 ; i ++ ) n2 += monthDays [ i ] ; n2 += countLeapYears ( dt2 ) ; return ( n2 - n1 ) ; } int main ( ) { Date dt1 = { 1 , 2 , 2000 } ; Date dt2 = { 1 , 2 , 2004 } ; cout << " Difference ▁ between ▁ two ▁ dates ▁ is ▁ " << getDifference ( dt1 , dt2 ) ; return 0 ; }
Find length of period in decimal value of 1 / n | C ++ program to find length of period of 1 / n without using map or hash ; Function to find length of period in 1 / n ; Find the ( n + 1 ) th remainder after decimal point in value of 1 / n ; Store ( n + 1 ) th remainder ; Count the number of remainders before next occurrence of ( n + 1 ) ' th ▁ remainder ▁ ' d ' ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int getPeriod ( int n ) { int rem = 1 ; for ( int i = 1 ; i <= n + 1 ; i ++ ) rem = ( 10 * rem ) % n ; int d = rem ; int count = 0 ; do { rem = ( 10 * rem ) % n ; count ++ ; } while ( rem != d ) ; return count ; } int main ( ) { cout << getPeriod ( 3 ) << endl ; cout << getPeriod ( 7 ) << endl ; return 0 ; }
Replace all â €˜ 0 â €™ with â €˜ 5 â €™ in an input Integer |  ; Returns the number to be added to the input to replace all zeroes with five ; Amount to be added ; Unit decimal place ; A number divisible by 10 , then this is a zero occurrence in the input ; Move one decimal place ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateAddedValue ( int number ) { int result = 0 ; int decimalPlace = 1 ; if ( number == 0 ) { result += ( 5 * decimalPlace ) ; } while ( number > 0 ) { if ( number % 10 == 0 ) { result += ( 5 * decimalPlace ) ; } number /= 10 ; decimalPlace *= 10 ; } return result ; } int replace0with5 ( int number ) { return number += calculateAddedValue ( number ) ; } int main ( ) { cout << replace0with5 ( 1020 ) ; }
Program to find remainder without using modulo or % operator | C ++ program to find remainder without using modulo operator ; This function returns remainder of num / divisor without using % ( modulo ) operator ; Driver program to test above functions
#include <iostream> NEW_LINE using namespace std ; int getRemainder ( int num , int divisor ) { return ( num - divisor * ( num / divisor ) ) ; } int main ( ) { cout << getRemainder ( 100 , 7 ) ; return 0 ; }