Found 7347 Articles for C++

Program to print all substrings of a given string in C++

Ayush Gupta
Updated on 19-Dec-2019 10:38:57

2K+ Views

In this tutorial, we will be discussing a program to print all the substring of a given string.For this we will be given with a string or an array of characters. Our task is to print all the substrings of that particular string.Example Live Demo#include using namespace std; //printing all the substrings void print_substr(char str[], int n){    for (int len = 1; len

Find reminder of array multiplication divided by n in C++

Arnab Chakraborty
Updated on 19-Dec-2019 10:37:48

88 Views

Suppose we have an array of n elements called A. We have to print the remainder after multiply all the numbers divided by n. Suppose A = [100, 10, 5, 25, 35, 14], and n = 11. The output is 9. So the value of 100 * 10 * 5 * 25 * 35 * 14 mod 11 = 9.First, we have to take the remainder of each number, then multiply the remainder with the current result. After multiplication, again take the remainder to avoid overflow.Example Live Demo#include #include using namespace std; int getRemainder(int a[], int size, int n) {    int mul = 1;    for(int i = 0; i

Program to print all palindromes in a given range in C++

Ayush Gupta
Updated on 19-Dec-2019 10:36:25

355 Views

In this tutorial, we will be discussing a program to print all palindromes in a given range.For this we will be given the mathematical range in which the palindromes are to be found. Our task is to find all the palindromes in that range and print it back.Example Live Demo#include using namespace std; //checking if the number is a palindrome int is_palin(int n){    int rev = 0;    for (int i = n; i > 0; i /= 10)    rev = rev*10 + i%10;    return (n==rev); } void countPal(int min, int max){    for (int i = min; i

Program to print a rectangle pattern in C++

Ayush Gupta
Updated on 19-Dec-2019 10:33:32

719 Views

In this tutorial, we will be discussing a program to print a given rectangular pattern.For this we will be given with the height and the breath of the rectangle. Our task is to print the rectangle with the given dimensions using the “@” character.Example Live Demo#include using namespace std; void print_rec(int h, int w){    for (int i=0; i

Find normal at a given point on the curve in C++

Arnab Chakraborty
Updated on 19-Dec-2019 10:34:57

109 Views

Suppose we have a curve like y = x(A - x), we have to find the normal at a given point (x, y) on that curve. Here A is an integer number, x and y are also integers.To solve this, we have the check that the given point is on the curve or not, if so, then find the differentiation of that curve, so it will be −$$\frac{\text{d}y}{\text{d}x}=A-2x$$Then put x and y into the dy/dx, then find the normal using this equation −$$Y-y=-\lgroup\frac{\text{d}x}{\text{d}y}\rgroup*\lgroup X-x \rgroup$$Example Live Demo#include using namespace std; void getNormal(int A, int x, int y) {    int differentiation ... Read More

Program to print a pattern of numbers in C++

Ayush Gupta
Updated on 19-Dec-2019 10:28:59

507 Views

In this tutorial, we will be discussing a program to print a given pattern of numbers.Our task is to make use of looping structure in the code and print the given pattern − 1 232 34543 4567654 567898765Example Live Demo#include using namespace std; int main(){    int n = 5, i, j, num = 1, gap;    gap = n - 1;    for ( j = 1 ; j

Find n-th element from Stern’s Diatomic Series in C++

Arnab Chakraborty
Updated on 19-Dec-2019 10:31:16

86 Views

Here we will see how to find the nth term in Stern’s Diatomic series. The series is like 0, 1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, 4, … This is also known as fusc function. This series can be defined as −𝑝(𝑛)=$p\lgroup\frac{n}{2}\rgroup$ 𝑤ℎ𝑒𝑛 𝑛 𝑖𝑠 𝑒𝑣𝑒𝑛𝑝(𝑛)=$p\lgroup\frac{n-1}{2}\rgroup+p\lgroup\frac{n+1}{2}\rgroup$ 𝑤ℎ𝑒𝑛 𝑛 𝑖𝑠 𝑜𝑑𝑑𝑝(0)=0 𝑎𝑛𝑑 𝑝(1)=1Here we will use the Dynamic programming approach to reduce the number of computations. After saving the base case for p(0) and p(1), we will iterate from index i = 2 to n, and compute p(i)Example Live Demo#include using namespace std; int findTerm(int ... Read More

Find N integers with given difference between product and sum in C++

Arnab Chakraborty
Updated on 19-Dec-2019 10:21:28

123 Views

Suppose we have two integers N and D. We have to find a set of N integers, where the difference between their sum and product is the same as D. Suppose the N = 3, and D = 5, then the output will be 1, 2, 8. Here the sum is 1 + 2 + 8 = 11, and product is 1 * 2 * 8 = 16, the difference between 16 and 11 is 5.We have to solve this problem; we will use one tricky method. Here we will try to find N–2 number of 1s, one 2, and ... Read More

Program to print 2D shapes in C++

Ayush Gupta
Updated on 19-Dec-2019 10:25:20

361 Views

In this tutorial, we will be discussing a program to print out 2D shapes.For this we will be provided with the various parameters required to make a shape such as radius, side length and side breadth, etc. And our task is to print a shape accordingly with no thickness.Example Live Demo#include using namespace std; void print_circle(int radius){    for (int i = 0; i

Find multiple of x closest to or a ^ b (a raised to power b) in C++

Arnab Chakraborty
Updated on 19-Dec-2019 10:19:15

169 Views

Suppose we have three values, a, b and x. We have to find one multiple of x, that is nearest to ab. Suppose the numbers are x = 4, a = 3, b = 3, then the output will be 28, as this is nearest to 33 = 27The approach is simple; we have to follow these conditions −If b < 0, and a = 1, then ab turns out to be 1 and hence, the closest multiple of x becomes either 0 or x.If b < 0 and a > 1, then, ab, turns out to be less than ... Read More

Advertisements