Found 26504 Articles for Server Side Programming

Find sum of sum of all sub-sequences in C++

Arnab Chakraborty
Updated on 19-Dec-2019 10:43:11

248 Views

Consider we have an array A with n elements. We have to find the total sum of the sum of all the subsets of the array. So if the array is like A = [5, 6, 8], then it will be like −SubsetSum5566885, 6116, 8145, 8135, 6, 819Total Sum76As the array has n elements, then we have a 2n number of subsets (including empty). If we observe it clearly, then we can find that each element occurs 2n-1 timesExample Live Demo#include #include using namespace std; int totalSum(int arr[], int n) {    int res = 0;    for (int i = ... Read More

Program to print all the numbers divisible by 3 and 5 in C++

Ayush Gupta
Updated on 19-Dec-2019 10:46:24

1K+ Views

In this tutorial, we will be discussing a program to print all the numbers divisible by 3 and 5 less than the given number.For this we will be given with a number say N. Our task is to print all the numbers less than N which are divisible by both 3 and 5.Example Live Demo#include using namespace std; //printing the numbers divisible by 3 and 5 void print_div(int N){    for (int num = 0; num < N; num++){       if (num % 3 == 0 && num % 5 == 0)       cout

Find sum of digits in factorial of a number in C++

Arnab Chakraborty
Updated on 19-Dec-2019 10:40:11

535 Views

Suppose, we have a number n, our task is to find the sum of digits in then!. Consider n = 5, then n! = 120. So the result will be 3.To solve this problem, we will create a vector to store factorial digits and initialize it with 1. Then multiply 1 to n one by one to the vector. Now sum all the elements in the vector and return the sumExample Live Demo#include #include using namespace std; void vectorMultiply(vector &v, int x) {    int carry = 0, res;    int size = v.size();    for (int i = 0 ; ... Read More

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

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

3K+ 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

142 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

483 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

877 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

149 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

616 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

155 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

Advertisements