Found 7347 Articles for C++

Tribonacci Word in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:17:14

148 Views

The Tribonacci Word is a sequence of digits. This is similar to the Fibonacci Words. Tribonacci Word is constructed by repeated concatenation of three previous stringsT(n) = T(n - 1) + T(n - 2) + T(n - 3)The first few strings to start, are {1, 12, 1213} So the next one will be 1213 + 12 + 1 = 1213121Algorithmtribonacci_word(n): Begin    first := 1, second := 12, third := 1213    print first, second, third    for i in range 3 to n, do       temp := third       third := third + second + ... Read More

Tribonacci Numbers in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:13:29

1K+ Views

Here we will see how to generate the Tribonacci numbers using C++. The Tribonacci numbers are similar to the Fibonacci numbers, but here we are generating a term by adding three previous terms. Suppose we want to generate T(n), then the formula will be like below −T(n) = T(n - 1) + T(n - 2) + T(n - 3)The first few numbers to start, are {0, 1, 1}Algorithmtribonacci(n): Begin    first := 0, second := 1, third := 1    print first, second, third    for i in range n – 3, do       next := first + ... Read More

Tetranacci Numbers in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:10:21

138 Views

Here we will see how to generate the Tetranacci numbers using C++. The Tetranacci numbers are similar to the Fibonacci numbers, but here we are generating a term by adding four previous terms. Suppose we want to generate T(n), then the formula will be like below −T(n) = T(n - 1) + T(n - 2) + T(n - 3) + T(n - 4)The first few numbers to start, are {0, 1, 1, 2}Algorithmtetranacci(n): Begin    first := 0, second := 1, third := 1, fourth := 2    print first, second, third, fourth    for i in range n – ... Read More

Sort the numbers according to their sum of digits in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:05:21

309 Views

In this section we will see how to sort numbers according to their sum of digits. So if a number has lesser sum of digits, then that will be placed at first, then next number will be placed with larger sum of digits.data = {14, 129, 501, 23, 0, 145}after sorting, they will be −data = {0, 14, 23, 501, 145, 129}Here we will create our own comparison logic to sort them. That comparison logic will be used in the sort function in C++ STL.Algorithmcompare(num1, num2): Begin    if sum of digits of num1 < sum of digits of num2, ... Read More

Sort an array of strings according to string lengths in C++

Arnab Chakraborty
Updated on 25-Sep-2019 11:51:12

794 Views

Here we will see how to sort a list of strings based on their lengths. So if a string has less number of characters, then that will be placed first, then other longer strings will be placed. Suppose the strings arestr_list = {“Hello”, “ABC”, “Programming”, “Length”, “Population”}after sorting, they will be −str_list = {“ABC”, “Hello”, “Length”, “Population”, “Programming”}Here we will create our own comparison logic to sort them. That comparison logic will be used in the sort function in C++ STL.Algorithmcompare(str1, str2): Begin    if length of str1 < length of str2, then       return 1    return ... Read More

Sort an array according to the order defined by another array in C++

Arnab Chakraborty
Updated on 25-Sep-2019 11:47:56

201 Views

In this section we will see another sorting problem. Suppose we have two arrays A1 and A2. We have to sort A1 in such a way that the relative order among the elements will be same as those are in A2. If some elements are not present in A2, then they will be appended after the sorted elements. Suppose A1 and A2 are the following −A1 = {2, 1, 2, 1, 7, 5, 9, 3, 8, 6, 8} A2 = {2, 1, 8, 3}After the sorting A1 will be like below −A1 = {2, 2, 1, 1, 8, 8, 3, ... Read More

Sort an array according to count of set bits in C++

Arnab Chakraborty
Updated on 25-Sep-2019 11:34:23

298 Views

Here we will see one interesting problem to sort an array based on the set-bits. When an element in the array has higher number of set-bits, then that will be placed before another element which has lower number of set bits. Suppose some numbers are 12, 15, 7. So the set bits are basically number of 1’s in their binary representation. These are 1100 (12), 1111 (15), and 0111 (7). So after sorting it will be look like this −1111, 0111, 1100 (15, 7, 12)Here we have to find the number of set-bits at first. Then we will use the ... Read More

Program for Area Of Square in C++

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

893 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

95 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

460 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

Advertisements