C++ Articles

Page 219 of 597

Program to print an inverse pyramid character pattern in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 333 Views

In this tutorial, we will be discussing a program to print an inverse pyramid character pattern.For this we will be provided with the number of rows containing in the inverted pyramid triangle. Our task is to print the alphabets in the given number of rows to develop the shape of an inverse pyramid.Example#include using namespace std; //printing the inverse pyramid pattern void inv_pyramid(int n){    int i, j, num, gap;    for (i = n; i >= 1; i--) {       for (gap = n - 1; gap >= i; gap--) {          cout

Read More

Find the common nodes in two singly linked list in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 510 Views

Suppose we have two singly-linked lists. We have to find the total number of common nodes in both the singly linked list. So if two lists are like [15, 16, 10, 9, 7, 17], and [15, 16, 40, 6, 9], there are three common nodes.Traverse both lists up to end of the list using two nested loops, for every node in the list, check if it is matched with any node of the second list or not. If a match is found, then increase the counter, and finally return the count.Example#include using namespace std; class Node {    public:   ...

Read More

Find the closest leaf in a Binary Tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 395 Views

Suppose, one binary tree is given. It has leaf nodes at different levels. Another pointer is given, that is pointing to a node. We have to find the distance to the nearest leaf node from the pointed node. Consider the tree is like below −Here leaf nodes are 2, -2 and 6. If the pointer is pointing to node -5, The nearest nodes from -5 will be at distance 1.To solve this, we will traverse the subtree rooted with the given node, and find the closest leaf in the subtree, then store the distance. Now traversing tree starting from the ...

Read More

Find the Diameter or Longest chord of a Circle in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 182 Views

Suppose we have radius r is given. We have to find the diameter or longest chord of the circle. If the radius is 9, and diameter will be 18. This task is extremely simple, we have to find the 2*r, that is the diameter of the circle.Example#include using namespace std; int getDiameter(int r) {    return 2*r; } int main() {    int r = 9;    cout

Read More

Find the K-th minimum element from an array concatenated M times in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 194 Views

Consider we have an array A, and another two integers K and M. We have to find Kth minimum element after concatenating the array to itself M number of times. Suppose the array is like A = [3, 1, 2], K = 4 and M = 3, so after concatenating A, 3 times, it will be [3, 1, 2, 3, 1, 2, 3, 1, 2], the 4th smallest element is 2 here.To solve this problem, we will sort the array A, then return the value, present at index ((K – 1)/M), of the array.Example#include #include using namespace std; int findKSmallestNumber(int ...

Read More

Find the last digit when factorial of A divides factorial of B in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 227 Views

If we have two integers A and B, and B >= A, we have to compute the last digit of the B! / A! When the value of A = 2 and B = 4, then result is 2, 2! = 2 and 4! = 24, so 24/2 = 12. the last digit is 2.As we know that the last digit of factorial will be in set {0, 1, 2, 4, 6}, then follow these steps to solve this problem −We will find the difference between A and Bif diff >=5, then answer is 0otherwise, iterate from (A + 1) ...

Read More

Find smallest values of x and y such that ax – by = 0 in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 201 Views

Suppose we have two values a and b. We have to find x and y, such that ax – by = 0. So if a = 25 and b = 35, then x = 7 and y = 5.To solve this, we have to calculate the LCM of a and b. LCM of a and b will be the smallest value that can make both sides equal. The LCM can be found using GCD of numbers using this formula −LCM (a,b)=(a*b)/GCD(a,b)Example#include #include using namespace std; void getSmallestXY(int a, int b) {    int lcm = (a * b) / __gcd(a, b);    cout

Read More

Find sum of a number and its maximum prime factor in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 236 Views

Suppose we have a positive number n, and we have to find the sum of N and its maximum prime factor. So when the number is 26, then maximum prime factor is 13, so sum will be 26 + 13 = 39.Approach is straight forward. Simply find the max prime factor, and calculate the sum and return.Example#include #include using namespace std; int maxPrimeFact(int n){    int num = n;    int maxPrime = -1;    while (n % 2 == 0) {       maxPrime = 2;       n /= 2;    }    for (int i = 3; i 2)    maxPrime = n;    return maxPrime; } int getRes(int n) {    int sum = maxPrimeFact(n) + n;    return sum; } int main() {    int n = 26;    cout

Read More

Find the center of the circle using endpoints of diameter in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 264 Views

Suppose we have two endpoints of diameter of a circle. These are (x1, y1) and (x2, y2), we have to find the center of the circle. So if two points are (-9, 3) and (5, -7), then the center is at location (-2, -2).We know that the mid points of two points are −$$(x_{m},y_{m})=\left(\frac{(x_{1}+x_{2})}{2},\frac{(y_{1}+y_{2})}{2}\right)$$Example#include using namespace std; class point{    public:       float x, y;       point(float x, float y){          this->x = x;          this->y = y;       }       void display(){          cout

Read More

Find the closest and smaller tidy number in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 180 Views

Suppose we have a number n, we have to find the closest and smaller tidy number of n. So a number is called tidy number, if all of its digits are sorted in non-decreasing order. So if the number is 45000, then the nearest and smaller tidy number will be 44999.To solve this problem, we will traverse the number from end, when the tidy property is violated, then we reduce digit by 1, and make all subsequent digit as 9.Example#include using namespace std; string tidyNum(string number) {    for (int i = number.length()-2; i >= 0; i--) {     ...

Read More
Showing 2181–2190 of 5,962 articles
« Prev 1 217 218 219 220 221 597 Next »
Advertisements