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

150 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

Print All Palindromes in a Given Range in C++

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

497 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

Find Normal at a Given Point on the Curve in C++

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

160 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

Print Rectangle Pattern in C++

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

888 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 N-th Element from Stern's Diatomic Series in C++

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

159 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

Print Pattern of Numbers in C++

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

629 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

Print 2D Shapes in C++

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

455 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 N Integers with Given Difference Between Product and Sum in C++

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

185 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

Detect Duplicate Values in Primitive Java Array

karthikeya Boyini
Updated on 19-Dec-2019 10:20:51

2K+ Views

To detect the duplicate values in an array you need to compare each element of the array to all the remaining elements, in case of a match you got your duplicate element.One solution to do so you need to use two loops (nested) where the inner loop starts with i+1 (where i is the variable of outer loop) to avoid repetitions in comparison.Exampleimport java.util.Arrays; import java.util.Scanner; public class DetectDuplcate {        public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array that is to ... Read More

Advertisements