C++ Articles - Page 577 of 719

Program for Area Of Square in C++

Sunidhi Bansal
Updated on 23-Sep-2019 11:53:37

2K+ Views

We are given with a side of a rectangle and our task is to print the area of the square from that side.Square is 2-D plain figure which have 4 sides and forms 4 angles of 90degree each and all the sides are of equal shape. In other words we can say that the square is a form of rectangle with equal sides.Given below is representation of a square −The Area of square is Side x SideExampleInput: 6 Output: 36 As the side is 6 so the output is 6*6=36 Input: 12 Output: 144AlgorithmSTART    Step 1-> Declare a function ... Read More

C++ Program for Area Of Square after N-th fold

Sunidhi Bansal
Updated on 23-Sep-2019 11:50:02

194 Views

Given a side of a square and the number of fold, we have to find the Area of square after number of folds.A square is a 2-D shape like rectangle where all the sides are equal. And it’s all angles are equal to 90 degrees.While folding a square we −Fold the square from top left side of the triangle to the bottom of the right side forming a triangle.The second fold will be folding from up to downside.The third fold is folding again from left to right.And likewise we follow the above steps.ExamplesInput: side = 23, fold = 4 Output: ... Read More

Program to check if N is a Pentagonal Number in C++

Sunidhi Bansal
Updated on 23-Sep-2019 11:37:11

580 Views

Given with a number N the task is to check whether the number is a pentagonal number or not. Numbers that can be arranged to form a pentagon is a pentagonal number as these numbers can be used as points to form a pentagon. For example, some of pentagonal numbers are 1, 5, 12, 22, 35, 51....We can use formula to check whether the number is a pentagonal number or not$$p(n)=\frac{\text{3}*n^2-n}{\text{2}}$$Where, n is the number of points pentagonal will haveExampleInput-: n=22 Output-: 22 is pentagonal number Input-: n=23 Output-: 23 is not a pentagonal numberAlgorithmStart Step 1 -> declare function ... Read More

Program to convert speed in km/hr to m/sec and vice versa in C++

Sunidhi Bansal
Updated on 23-Sep-2019 11:00:19

790 Views

Conversion Formulas −1 km/hr = 5/18 m/sec or 0.277778 m/sec 1 m/sec = 18/5 km/hr or 3.6 km/hrExampleInput-: km = 60.00    mk = 70.00 Output-: speed in meter per second = 16.6667    speed in km per hour = 252AlgorithmStart Step 1 -> Declare function to convert km/hr into m/sec    float km_m(float km)       return (0.277778 * km) Step 2 -> Declare function to convert m/sec into km/hr    float m_km(float mk)       return (3.6 * mk) step 3 -> In main()    declare variable as float km = 60.0 and float mk = ... Read More

Program to check if two given matrices are identical in C++

Sunidhi Bansal
Updated on 23-Sep-2019 10:55:03

1K+ Views

Given two matrix M1[r][c] and M2[r][c] with ‘r’ number of rows and ‘c’ number of columns, we have to check that the both given matrices are identical or not. If they are identical then print “Matrices are identical” else print “Matrices are not identical”Identical MatrixTwo matrices M1 and M2 are be called identical when −Number of rows and columns of both matrices are same.The values of M1[i][j] are equal to M2[i][j].Like in the given figure below both matrices m1 and m2 of 3x3 are identical −$$M1[3][3]=\begin{bmatrix} 1 & 2 & 3 \ 4 & 5 & 6 \ 7 & ... Read More

Minimum flips required to maximize a number with k set bits in C++.

Narendra Kumar
Updated on 23-Sep-2019 10:49:41

186 Views

Problem statementGiven two numbers n and k, we need to find the minimum number of flips required to maximize given number by flipping its bits such that the resulting number has exactly k set bits. Please note input must satify condition that k < number of bits in n.ExampleLet us suppose n = 9 and k = 2Binary representation of 9 is − 1001. It contains 4 bits.Largest 4 digit binary number with 2 set bits is − 1100 i.e. 12To convert 1001 to 1100 we have to flip highlighed 2 bitsAlgorithm1. Count the number of bits in n. Let ... Read More

Minimum flips in two binary arrays so that their XOR is equal to another array in C++.

Narendra Kumar
Updated on 23-Sep-2019 10:41:53

219 Views

Problem statementGiven three arrays with 0’s and 1’s of size n, the task is to find minimum flip of bits in the first and second array such that the XOR of i’th index bit of first and second array is equal to i’th index bit of the third array.Please note that we can only flip at most p bits of array 1 and at most q bits of array 2. Also rearranging array elements is not allowed.Let us suppose p = 2 and q = 5arr1[] = {1, 0, 1, 1, 0, 1, 0} arr2[] = {0, 1, 0, 1, ... Read More

Minimum element in a max heap in C++.

Narendra Kumar
Updated on 23-Sep-2019 10:34:50

421 Views

Problem statementFind the element with the least value in max heap.Let us consider below max heap.In max heap value of the root node is always greater than its childer. Because of this property, we can conclude that value will be present in one of the leaf nodes. If heap contains n nodes then there will be ceil(n/2) leaves.Max heap is a complete binary tree hence it can be represented in an array. In such heap the first leaf will be present after floor(n/2) index. So in our example, the first leave will be present at index 5.AlgorithmWe can use the ... Read More

Minimum delete operations to make all elements of array same in C++.

Narendra Kumar
Updated on 23-Sep-2019 10:29:22

225 Views

Problem statementGiven an array of n elements such that elements may repeat. We can delete any number of elements from array. The task is to find a minimum number of elements to be deleted from array to make it equal.arr[] = {10, 8, 10, 7, 10, -1, -4, 12}We have to delete highlighted 5 elements to make all array elements the same.Algorithm1. Count frequency of each element 2. Find maximum frequecy among the frequencies. Let us call this as maxFrequncy 3. Elements to be deleted: n – maxFrequecy where n is size of an arrayExample#include #include #include ... Read More

Minimum and Maximum Prime Numbers of a Singly Linked List in C++.

Narendra Kumar
Updated on 23-Sep-2019 10:25:58

200 Views

Problem statementGiven a linked list of n positive integers. We have to find the prime number with minimum and maximum value.If the given list is −10 -> 4 -> 1 -> 12 -> 13 -> 7 -> 6 -> 2 -> 27 -> 33 then minimum prime number is 2 and maximum prime number is 13Algorithm1. Find maximum number from given number. Let us call it maxNumber 2. Generate prime numbers from 1 to maxNumber and store them in a dynamic array 3. Iterate linked list and use dynamic array to find prime number with minimum and maximum valueExample#include ... Read More

Advertisements