Found 7197 Articles for C++

C++ Program to Get Input from the User

Arnab Chakraborty
Updated on 04-Apr-2023 14:14:55

2K+ Views

While writing a program in any language, taking input is a fundamental job that we do in almost all programs. Sometimes we take input directly from the console or take the inputs from the files. Taking inputs from the files is somewhat beneficial because it does not require us to type inputs again and again, or sometimes we can save some good input test cases into a file. However, in this article, we are going to focus on console-based inputs. We will learn different techniques to get inputs from the user in C++. There are a few different approaches to ... Read More

C++ Program to Round a Number to n Decimal Places

Arnab Chakraborty
Updated on 17-Oct-2022 12:11:09

25K+ Views

Representing numbers in outputs is an interesting and also important task while we are writing programs in any language. For integer type (short, long, or medium) type data, it is easy to express the numbers as output. For floating point numbers (float or double type), sometimes we need to round them off to certain decimal places. For example, if we want to represent 52.24568 up to three decimal places, some pre-processing needs to be done. In this article, we shall cover a few techniques to represent a floating-point number up to a certain decimal placed by rounding it off. Among ... Read More

C++ Program to Calculate simple interest and compound interest

Arnab Chakraborty
Updated on 17-Oct-2022 09:22:53

26K+ Views

After calculating interest on the principal amount, simple interest is calculated by taking the principal amount, the rate of interest, and the number of years it will take to calculate interest. The formula is quite easy to understand. The power law is used to calculate compound interest by taking into account the principal amount, the rate of interest, and the period. Here, the formula is likewise not difficult. In this article, we'll talk about how to calculate simple and compound interest values and then use the C++ programming language to put this logic into practice. Simple Interest Simple interest is ... Read More

Finding the second largest element in BST using C++

Prateek Jangid
Updated on 10-Aug-2022 12:03:40

745 Views

In a binary search tree (BST), the second largest element must be returned. In a binary tree, the second element is the largest element. According to the given BST, 13 is the second largest element. Now we are using the C++ approach to solve this problem. We can traverse the tree inorder, and by observation, we can observe that the second largest element in the given BST is 13. The inorder of the tree will be 1 3 4 6 7 8 10 13 14, and we can observe that the elements are in the sorted array. So we ... Read More

C++ program to find the shortest distance between two nodes in BST

Prateek Jangid
Updated on 10-Aug-2022 12:02:23

821 Views

In this article, we are given a BST (binary search tree), and we need to find the shortest distance between 2 given nodes in the BST. Let's have a tree and below are the following scenarios. Let’s assume some simple input and output scenarios Now we have to find the distances between nodes 4 and 13. int key1=13, key2=4; res = solve(root, min(key1, key2), max(key1, key2)); output = 6 The shortest distance is 6, which is through 4->6->3->8->10->14->13(the arrows show a path definition and not anything else). Let’s find another distance from two different nodes in the above ... Read More

C++ program to Replace a Node with Depth in a Binary Tree

Prateek Jangid
Updated on 10-Aug-2022 11:58:31

370 Views

Suppose we have a binary tree and we want to replace the depth of each node with its value. The depth of the node starts from 0 at the root node and increases by 1 for each level we go; for example, we have a binary tree like this; Here we replace, Node Value Depth 1 0 2 1 3 1 4 2 5 2 6 2 7 2 8 3 9 3 We do a simple ... Read More

Replace Each Node in Binary Tree With The Sum Of Its Inorder Predecessor And Successor Using C++

Prateek Jangid
Updated on 10-Aug-2022 11:54:27

362 Views

We are given a binary tree, and we need to replace all the elements with the sum of its inorder predecessor and successor. Inorder is a traversed path in a graph that reads in the order of left node – root node – right node. The method adds the elements in left and right nodes of the parent node and replaces the value with the obtained sum. Suppose we have a tree with the following formation and characters − We can find and store the inorder of the tree in an array. After that, we can again do an ... Read More

Rencontres Number (Counting partial derangements) Using C++

Prateek Jangid
Updated on 10-Aug-2022 11:50:50

311 Views

Given two integers N and k, we need to count the number of derangements where k points are fixed at their position. Given constraints on k are between 0 and n as the number of fixed points when there are n points cannot be more than n. int N=4, k=2; res = solve(N, k); Note that at least conditions don’t hold on k. There has to be precisely and strictly k points on their original index. This is a mathematical problem. Not explaining the proof and explanation of mathematics, we as computer science majors can use the results ... Read More

Remove Edges Connected to a Node Such That The Three Given Nodes are in Different Trees Using C++

Prateek Jangid
Updated on 10-Aug-2022 11:48:01

203 Views

Suppose we are given a binary tree and three nodes in that binary. We have to disconnect one node entirely from the tree. Disconnecting that node leaves us with three different trees. Each of the three given nodes lies in one of them, or each of the given three nodes must not exist in the same tree. Disconnecting a node means that we will remove all the edges from this node to all other nodes. Example For example, let's say we have a tree with three nodes 18, 15, and 17 as shown below − If the task ... Read More

Searching an Element in a Linked List using C++

Prateek Jangid
Updated on 10-Aug-2022 10:09:30

5K+ Views

To search an element in a linked list, we must iterate through the complete list, compare each node with the desired data, and keep searching until a match is obtained. Because a Linked List does not provide random access, we must start the search from the first node. We are given a linked list of integers and an integer key. We need to find if this key exists in our linked list or not. We can do a simple linear search in the linked list and find the key. If present, we can return "Yes"; otherwise, "No" Let us look ... Read More

Advertisements