Found 7197 Articles for C++

Find the perimeter of a cylinder in C++

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

171 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

94 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

159 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

520 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

Find the length of each string element in the Numpy array in C++

Arnab Chakraborty
Updated on 04-Nov-2019 07:52:59

153 Views

Here we will see how to get the length of each string element in the Numpy Array. Numpy is a library for Numeric Python, and it has very powerful array class. Using this we can store data in an array like structure. To get the length we can follow two different approach, these are like below −Exampleimport numpy as np str_arr = np.array(['Hello', 'Computer', 'Mobile', 'Language', 'Programming', 'Python']) print('The array is like: ', str_arr) len_check = np.vectorize(len) len_arr = len_check(str_arr) print('Respective lengts: ', len_arr)OutputThe array is like: ['Hello' 'Computer' 'Mobile' 'Language' 'Programming' 'Python'] Respective lengts: [ 5 8 6 8 ... Read More

Find the first, second and third minimum elements in an array in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:21:51

251 Views

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

Find the final X and Y when they are Altering under given condition in C++

Arnab Chakraborty
Updated on 08-Jul-2020 07:33:12

108 Views

Consider we have the initial values of two positive integers X and Y. Find the final value of X and Y, such that there will be some alteration as mentioned below −step1 − If X = 0 and Y = 0 then terminate the process, otherwise go to step2step2 − If X >= 2Y, then set X = X – 2Y, and go to step1, otherwise go to step3step3 − If Y >= 2X, then set Y = Y – 2X, and go to step1, otherwise end the process.The number X and Y will be in range [0 and 1018] ... Read More

Find sum of factorials in an array in C++

Arnab Chakraborty
Updated on 04-Nov-2019 07:43:09

240 Views

Consider we have an array A, which is sorted. It has all elements appears twice, but one element is present for only one time. We have to find that element. If the array is [1, 1, 3, 3, 4, 4, 5, 6, 6, 7, 7, 9, 9], so the single element is 5.We will use the binary search approach to solve this. All elements before the single element has their first occurrence at index 0, 2, 4, … and first occurrence at index 1, 3, 5, … but after the single element, all occurrences of the first number will be ... Read More

Find numbers a and b that satisfy the given condition in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:35:58

547 Views

Consider we have an integer n. Our task is to find two numbers a and b, where these three conditions will be satisfied.a mod b = 0a * b > na / b < nIf no pair is found, print -1.For an example, if the number n = 10, then a and b can be a = 90, b = 10. This satisfies given rules.To solve this problem, we will follow these steps −Let b = n. a can be found using these three conditionsa mod b = 0 when a is multiple of ba / b < n, so ... Read More

Find N digits number which is divisible by D in C++

Arnab Chakraborty
Updated on 01-Nov-2019 10:33:46

169 Views

Suppose we have two numbers N and D. We have to find N digit number, that is divisible by D. If N is 3, and D is 5, then the number can be 500. This can be solved easily. If D is 10 and N is 1, then it will be impossible. We can put D, and suppose the D has m number of digits, then attach N – m number of 0s to make it N digit number and divisible by D.Example#include using namespace std; string nDigitDivByD(int n, int d) {    string ans = "";    if (d ... Read More

Advertisements