Hafeezul Kareem

Hafeezul Kareem

259 Articles Published

Articles by Hafeezul Kareem

Page 3 of 26

Lagrange’s Interpolation in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 2K+ Views

In this tutorial, we are going to write a program that finds the result for lagranges's interpolation formula.You don't have write any logic for the program. Just convert the formula into code. Let's see the code.Example#include using namespace std; struct Data {    int x, y; }; double interpolate(Data function[], int xi, int n) {    double result = 0;    for (int i = 0; i < n; i++) {       double term = function[i].y;       for (int j = 0; j < n; j++) {          if (j != i) { ...

Read More

Larger of a^b or b^a in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 379 Views

In this tutorial, we are going to write a program that finds out the larger among the ab and baIt's a straightforward problem. Let's see the steps to solve it.Initialise the values of a and b.Take the log of both the values.Compute the values of $b\:\log\:a$ and $a\:\log\:b$Compare the both values.If $a\:\log\:b$ is greater than $b\:\log\:a$, then print bais greater.If $b\:\log\:a$ is greater than $a\:\log\:b$, then print abis greater.Else print both are equal.ExampleLet's see the code.#include using namespace std; int main() {    int a = 4, b = 7;    long double x = (long double) a * ...

Read More

Largest Even and Odd N-digit numbers in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 570 Views

In this tutorial, we are going to write a program that finds the largest even and odd number of n digits number.Let's see the steps to solve the problem.Initialise the number n.The largest odd number is pow(10, n) - 1.The largest even number is odd - 1.ExampleLet's see the code.#include using namespace std; void findEvenAndOddNumbers(int n) {    int odd = pow(10, n) - 1;    int even = odd - 1;    cout

Read More

Largest even digit number not greater than N in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 227 Views

In this tutorial, we are going to write a program that finds the largest number whose digits are all even and not greater than the given n.Let's see the steps to solve the problem.Initialise the number n.Write a loop from i = n .Check whether the digits of current number are all even or not.If the above condition satisfies, then print the number.Else decrement the i.ExampleLet's see the code.#include using namespace std; int allDigitsEven(int n) {    while (n) {       if ((n % 10) % 2){          return 0;       } ...

Read More

Largest K digit number divisible by X in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 222 Views

In this tutorial, we are going to write a program that finds the largest k-digit number that is divisible by x.Let's see the steps to solve the problem.Initialise the x and k.Find the value of pow(10, k) - 1 which is largest k-digit number.Now, remove the remainder value from the above value to get the largest k-digit number that is divisible by x.ExampleLet's see the code.#include using namespace std; int answer(int x, int k) {    int max = pow(10, k) - 1;    return max - (max % x); } int main() {    int x = 45, k = 7;    cout

Read More

Largest N digit number divisible by given three numbers in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 341 Views

In this tutorial, we are going to write a program that finds the largest n-digit number that is divisible by the given three numbers.Let's see the steps to solve the problem.Initialise three numbers along with n.Find the LCM of three numbers.Store the largest number with n-digits.If the largest number is divisible by n, then return it.Else check for the number obtained from subtracting remainder in the above step.ExampleLet's see the code.#include using namespace std; int LCM(int x, int y, int z) {    int ans = ((x * y) / (__gcd(x, y)));    return ((z * ans) / (__gcd(ans, ...

Read More

Largest number less than N with digit sum greater than the digit sum of N in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 441 Views

In this tutorial, we are going to write a program that finds the number less than N with digit sum greater than the digit sum of n.Let's see the steps to solve the problem.Write a function to find the digits sum.Initialise n.Write a loop that iterates from n - 1 to 1.Check the digits sum of current number with the digits sum of n.If the digits sum of current number is greater than n, then return the current number.Move to the next number.ExampleLet's see the code.#include using namespace std; int sumOfDigits(int n) {    int digitsSum = 0;   ...

Read More

Largest number less than X having at most K set bits in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 153 Views

In this tutorial, we are going to write a program that finds the largest number which is less than given x and should have at most k set bits.Let's see the steps to solve the problem.Initialise the numbers x and k.Find the set bits in the number x.Write a loop that iterates set bits count of x - k.Update the value of x with x & (x - 1).Return x.ExampleLet's see the code.#include using namespace std; int largestNumberWithKBits(int x, int k) {    int set_bit_count = __builtin_popcount(x);    if (set_bit_count

Read More

Largest number smaller than or equal to N divisible by K in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 238 Views

In this tutorial, we are going to write a program that finds the number that is smaller than or equal to N and divisible by k.Let's see the steps to solve the problem.Initialise the numbers n and k.Find the remainder with modulo operator.If the remainder is zero, then return n.Else return n - remainder.ExampleLet's see the code.#include using namespace std; int findLargerNumber(int n, int k) {    int remainder = n % k;    if (remainder == 0) {       return n;    }    return n - remainder; } int main() {    int n = 33, k = 5;    cout

Read More

Largest number with binary representation is m 1&rsquo;s and m-1 0&rsquo;s in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 244 Views

In this tutorial, we are going to write a program that finds the largest number with m 1's and m - 1 0's.Let's see the steps to solve the problem.Initialise two variables bits and result with 2 and 1 respectively.Write a loop that iterates from 1 to n.Update the iterating variable value with pow(2, bits) - 1) * (pow(2, bits - 1).If the iterative variable is less than n, then update the result with i.Increment the bits count.Return resutl.ExampleLet's see the code.#include using namespace std; long long getTheNumber(long long n) {    long bits = 2;    long long ...

Read More
Showing 21–30 of 259 articles
« Prev 1 2 3 4 5 26 Next »
Advertisements