Found 7197 Articles for C++

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

Hafeezul Kareem
Updated on 09-Apr-2021 13:29:56

289 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. Live Demo#include using namespace std; int LCM(int x, int y, int z) {    int ans = ((x * y) / (__gcd(x, y)));    return ((z * ans) / ... Read More

Largest K digit number divisible by X in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:29:35

173 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. Live Demo#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

Largest gap in an array in C++

Hafeezul Kareem
Updated on 03-Dec-2024 21:59:29

298 Views

In this tutorial, we are going to write a program that finds the largest difference between the two elements in the given array. Approach Let's look at the approach we will follow to solve this problem. Firstly, we will initialize the array. Then we will find or search for the max and min elements in the array. And will return max - min. Example Here is the following code for finding the largest gap in an array in C++. #include using namespace std; ... Read More

Largest even number possible by using one swap operation in given number in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:21:14

723 Views

In this tutorial, we are going to write a program that finds the largest possible even number with a single swap of digits.Let's see the steps to solve the problem.Initialise the number in string format.Iterate over the given number.Find the even digit that is less than or equal to the last digit of the number.Break the loop if you find the desired even digit.If the even digit doesn't exist, then return the given number.Swap the last digit with the even digit you found in the above step.Return the numberExample Live Demo#include using namespace std; string getLargestEvenNumber(string number, int n) { ... Read More

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

Hafeezul Kareem
Updated on 09-Apr-2021 13:20:05

182 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. Live Demo#include using namespace std; int allDigitsEven(int n) {    while (n) {       if ((n % 10) % 2){          return 0;       ... Read More

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

Hafeezul Kareem
Updated on 09-Apr-2021 13:19:28

522 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. Live Demo#include using namespace std; void findEvenAndOddNumbers(int n) {    int odd = pow(10, n) - 1;    int even = odd - 1;    cout

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

Hafeezul Kareem
Updated on 09-Apr-2021 13:18:53

318 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. Live Demo#include using namespace std; int main() {    int a = 4, b = 7;    long double x = (long double) a ... Read More

Lagrange’s Interpolation in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:16:03

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 Live Demo#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

Lagrange’s four square theorem in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:11:26

402 Views

In this tutorial, we are going to learn about largrange's four square theorem.The lagranges's four square theorem states that every natural number can be written as sum of squares of 4 numbers.The following code finds the 4 numbers which satisfies the above condition for the given number n.ExampleLet's see the code. Live Demo#include using namespace std; void printSquareCombinations(int n) {    for (int i = 0; i * i

kth smallest/largest in a small range unsorted array in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:10:55

129 Views

In this tutorial, we are going to write a program that finds the k-th smallest number in the unsorted array.Let's see the steps to solve the problem.Initialise the array and k.Sort the array using sort method.Return the value from the array with the index k - 1.Let's see the code.Example Live Demo#include using namespace std; int findKthSmallestNumber(int arr[], int n, int k) {    sort(arr, arr + n);    return arr[k - 1]; } int main() {    int arr[] = { 3, 5, 23, 4, 15, 16, 87, 99 }, k = 5;    cout

Advertisements