Found 7347 Articles for C++

Program for weighted mean of natural numbers in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:16:26

809 Views

Given with an array of natural numbers and one more array containing the weights of the corresponding natural numbers and the task is to calculate the weighted mean of natural numbers.There is a formula which is used for calculating the weighted mean of natural numbers.$$\overline{x}=\frac{\displaystyle\sum\limits_{i=1}^n (x_{i*}w_{i})}{\displaystyle\sum\limits_{i=1}^n w_{i}}$$Where, x is the natural number and w is the weighted associated with that natural number.Input X[] = {11, 22, 43, 34, 25, 16} W[] = {12, 12, 43, 54, 75, 16}Output weighted mean is : 29.3019Explanation (11*12 + 22*12 + 43*43 + 34*54 + 25*75 + 16*16) / (12 + 12 + 43 + 54 +75 ... Read More

Program to check if water tank overflows when n solid balls are dipped in the water tank in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:13:55

184 Views

Given with the radius and height of cylindrical water tank, ‘n’ number of spherical solid balls with the radius and the volume of water in the tank and the task is to check whether the tank will overflow or not when balls are dipped in the tank.Formula to calculate the volumeCylinder 3.14 * r * r * hWhere, r is radius of tank and h is the height of the tankSphere (4/3) * 3.14 * R * R * RWhere, R is radius of sphere ballInput tank_height = 5 tank_radius = 2 water_volume = 10 capacity = 10 ball_radius = 2Output It will overflowApproach ... Read More

Program to check if an Array is Palindrome or not using STL in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:12:04

2K+ Views

Given an array arr[n] of n integers, the task is to find whether array is a palindrome or not. We have to do the stated task using STL in C++.In C++ there is a feature of STL(Standard Template Library), it is a set of C++ template classes which are used to provide the data structures and several functions such as stacks, queues, lists, etc. To use these one must have knowledge of template classes.Palindrome is a sequence which is read the same from the front or rear of the sequence. Some simple examples of a palindrome are − MADAM, RACECAR, ... Read More

Program to check if three points are collinear in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:09:33

2K+ Views

Given with three different valued points and the task is to check whether the points are collinear or not.Points are said to be collinear if they lie on the same line and they are not collinear if they are on the different lines. Given below is the figure of collinear and non-collinear points.Input x1 = 1, x2 = 2, x3 = 3, y1 = 1, y2 = 4, y3 = 5Output no points are not collinearInput x1 = 1, y1 = 1, x2 = 1, y2 = 4, x3 = 1, y3 = 5Output points are collinearApproach used in the below program is as ... Read More

Program to check whether the given number is Buzz Number or not in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:07:45

1K+ Views

Given with a number ‘n’ and the task is to determine whether the given positive integer is a buzz number or not and display the result as an output.What is Buzz Number?For being a buzz number there are two conditions either of which must be true −Number should end with digit 7 e.g. 27, 657, etc.Number should be divisible by 7 e.g 63, 49, etc.Inputnumber: 49Outputit’s a buzz numberExplanation − since the number is divisible by 7 so it’s a buzz numberInputnumber: 29Outputit’s not a buzz numberExplanation − since the number is neither divisible by 7 nor end with digit ... Read More

Program to check whether a number is Proth number or not in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:05:54

613 Views

Given with a number ‘n’ and the task is to determine whether the given positive integer is a proth or not and display the result as an output.What is Proth Number?A proth number is given by$$N=k\cdot\:2^{n}+1$$Where, n is a positive integer and k is a odd positive integerThe first few proth numbers are given below −3, 5, 9, 13, 17, 25, 33, 41, 49, 57, 65, 81, 97.......Inputnumber: 17Outputits a proth numberInputnumber: 18Outputits not a proth numberApproach used in the given program is as followsInput the number to check for the conditionApply the given formula to check whether its a ... Read More

Program to check bitnoicity of an array in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:03:01

350 Views

Given an array arr[N] of N integers, the task is to check whether the given array is bitonic or not. If the given array is bitonic then print “Yes its a bitonic array”, else print “No its not a bitonic array”.A Bitonic array is when the array is in strictly increasing order first and then in strictly decreasing order.Like this array arr[] = {1, 2, 3, 4, 2, -1, -5} is a bitonic array, because till 4 it is in strictly increasing order and after 4 it is in strictly decreasing order.Input arr[] = {1, 3, 5, 4, 2, 0}Output Yes its ... Read More

Program for average of an array(Iterative and Recursive) in C++

Sunidhi Bansal
Updated on 13-Aug-2020 06:59:49

1K+ Views

Given an array of N integers arr[N], the task is to find the average of arr[N]. To achieve the result we can either use iterative approach or the recursive approach. We will be showing both in the given solution.Average of an array will be sum of all the elements of an array divided by the number of elements.Iterative methodIn iterative approach, we use loops like for-loop, while-loop or do-while loop which executes the statements till the condition holds true which means 1.Let’s take an example and then discuss how it can be obtained using iterative approach.Input arr[] = {1, 2, 4, ... Read More

Program to add two binary strings in C++

Sunidhi Bansal
Updated on 13-Aug-2020 06:56:19

10K+ Views

Given two strings with binary number, we have to find the result obtained by adding those two binary strings and return the result as a binary string.Binary numbers are those numbers which are expressed either as 0 or 1. While adding 2 binary numbers there is binary addition rule which is to be taken care of.0+0 → 0 0+1 → 1 1+0 → 1 1+1 → 0, carry 1Input str1 = {“11”}, str2 = {“1”}Output “100”Input str1 = {“110”}, str2 = {“1”}Output “111”Approach used below is as follows to solve the problemTraverse both the string from lastAdd the binary of two numbersIf there are ... Read More

Product of all prime nodes in a Singly Linked List in C++

Sunidhi Bansal
Updated on 13-Aug-2020 06:54:05

210 Views

Given with n nodes and the task is to print the product of all the prime nodes in a linked list. Prime nodes are the ones that will have prime values as their count locations.Input 10 20 30 40 50Output 4, 00, 000Explanation − 10 is at index value 1 which is non-prime so it will be skipped. Moving to 20 with index value 2 which is a prime number so it will be considered. Similarly, 40 and 50 are at prime index locations.Product − 20*40*50 = 4, 00, 000In the above diagram, red coloured nodes represents the prime nodesApproach used below ... Read More

Advertisements