Found 7347 Articles for C++

Balance pans using given weights that are powers of a number in C++ program

sudhir sharma
Updated on 09-Jul-2020 05:51:39

87 Views

STATEMENT − Balance pans using given weights that are powers of a number.DESCRIPTION − In this problem we are given a pan based weighing machine. we are given a weight T and some other weights whose values are powers of a number a. We need to balance the pans using the given weights.Now, based on this we have this equation, T + (some power of a) = (some other power of a)Now, we should remember that there is exactly one weight corresponding to a power value.Example, T = 12 : a = 4Using the below values, we can balance the ... Read More

Bakhshali Approximation for computing square roots in C program

sudhir sharma
Updated on 09-Jul-2020 05:52:06

517 Views

Bakhshali approximation is a method of computing the square root of a number which is not a perfect square. Now, lets brush-related terms to easily understand the concept.Square root of a number x is a number that satisfies the following condition, y2 = x.Perfect Square is a number whose square roots are w. For example 16 is perfect square as its roots are 4 and 4.There are multiple methods defined mathematically to find the square root of a number. In this tutorial, we are going to learn about Bakhshali approximation to find the square root of a number.It is a ... Read More

Find the smallest number X such that X! contains at least Y trailing zeros in C++

Arnab Chakraborty
Updated on 04-Nov-2019 10:06:31

74 Views

We have to take a number Y, we will find smallest number X, such that X! contains at least Y number of training zeros. For example, if Y = 2, then the value of X = 10. As X! = 3228800. It has Y number of zeros.We can solve this using binary search. The number of trailing zeros in N! is given by the count of the factors 5 in N!. X can be found using binary search in range [0, 5*Y]Example #include using namespace std; int factorCount(int n, int X) {    if (X < n)       ... Read More

Find the smallest and second smallest elements in an array in C++

Arnab Chakraborty
Updated on 04-Nov-2019 10:03:23

2K+ Views

Suppose we have an array of n elements. We have to find the first, second smallest elements in the array. First smallest is the minimum of the array, second smallest is minimum but larger than the first smallest number.Scan through each element, then check the element, and relate the condition for first, and second smallest elements conditions to solve this problem.Example #include using namespace std; int getTwoSmallest(int arr[], int n) {    int first = INT_MAX, sec = INT_MAX;    for (int i = 0; i < n; i++) {       if (arr[i] < first) {     ... Read More

Find the product of last N nodes of the given Linked List in C++

Arnab Chakraborty
Updated on 04-Nov-2019 09:59:11

100 Views

Consider we have few elements in a linked list. We have to find the multiplication result of last n number of elements. The value of n is also given. So if the list is like [5, 7, 3, 5, 6, 9], and n = 3, then result will be 5 * 6 * 9 = 270.The process is straight forward. We simply read the current element starting from left side, then add the elements into stack. After filling up the stack, remove n elements and multiply them with the prod. (initially prod is 1), when n number of elements are ... Read More

Find the product of first k nodes of the given Linked List in C++

Arnab Chakraborty
Updated on 04-Nov-2019 08:17:25

67 Views

Consider we have few elements in a linked list. We have to find the multiplication result of first k number of elements. The value of k is also given. So if the list is like [5, 7, 3, 5, 6, 9], and k = 3, then result will be 5 * 7 * 3 = 105.The processes is straight forward. We simply read the current element starting from left side, then multiply it with the prod. (initially prod is 1), when k number of elements are traversed, then stop.Example#include #include using namespace std;    class Node{       public: ... Read More

Find the perimeter of a cylinder in C++

Arnab Chakraborty
Updated on 04-Nov-2019 08:07:19

116 Views

Suppose we have the diameter and the height of the cylinder, we have to find the perimeter of the cylinder. As the perimeter is the outline of two dimensional object, then we cannot find the perimeter of one three dimensional object directly. We can make a cross section of the cylinder, and convert it as rectangle, then find the perimeter. The two sides of the rectangular cross section are the diameter, and the height. So perimeter is −p=(2*d)+(2*h)Example#include using namespace std; int getCylinderPerimeter(int d, int h) {    return (2*d) + (2*h); } int main() {    int diameter = ... Read More

Find the other-end coordinates of diameter in a circler in C++

Arnab Chakraborty
Updated on 04-Nov-2019 08:03:14

56 Views

Suppose we have the center coordinate and one coordinate point on the perimeter of the circle. We have to find the another point on the perimeter. Consider the center points are (p, q), and one given point is (a, b). We have to find the point (x, y). As we know that the center is the middle point of the diameter. So we can write them like −(p, q)=(a+x/2, b+y/2)Or from this the (x, y) can be expressed as −x=2p-a, y=2q-bExample#include using namespace std; int getCylinderPerimeter(int d, int h) {    return (2*d) + (2*h); } int main() {   ... Read More

Find the Number which contain the digit d in C++

Arnab Chakraborty
Updated on 04-Nov-2019 08:01:20

92 Views

Consider we have a digit d, and the upper limit n. we have to find all numbers that contains d in range 0 to n. So if n = 20, and digit is 3, then the numbers will be [3, 13].To solve this problem, we will take every number as string, then if the digit is present in the string, the number will be printed, otherwise ignored.Example#include using namespace std; int getAllNumWithDigit(int n, int d) {    string str = "";    str += to_string(d);    char ch = str[0];    string p = "";    p += ch;    for (int i = 0; i

Find the Next perfect square greater than a given number in C++

Arnab Chakraborty
Updated on 04-Nov-2019 07:55:45

422 Views

Suppose we have a number n. our task is to find next perfect square number of n. So if the number n = 1000, then the next perfect square number is 1024 = 322.To solve this, we have get the square root of the given number n, then take the floor of it, after that display the square of the (floor value + 1)Example#include #include using namespace std; int justGreaterPerfectSq(int n) {    int sq_root = sqrt(n);    return (sq_root + 1)*(sq_root + 1);    } int main() {    int n = 1000;    cout

Advertisements