Divide Every Element of One Array by Another Array in C++

Hafeezul Kareem
Updated on 27-Jan-2021 12:49:35

1K+ Views

In this tutorial, we are going to write a program that divides one array of elements by another array of elements.Here, we are following a simple method to complete the problem. Let's see the steps to solve the problem.Initialize the two arrays.Iterate through the second array and find the product of the elements.Iterate through the first array and divide each element with a product of the second array elements.ExampleLet's see the code. Live Demo#include using namespace std; void divideArrOneWithTwo(int arr_one[], int arr_two[], int n, int m) {    int arr_two_elements_product = 1;    for (int i = 0; i < ... Read More

Divide a String in N Equal Parts in C++

Hafeezul Kareem
Updated on 27-Jan-2021 12:48:23

1K+ Views

In this tutorial, we are going to write a program that divides the given string into N equal parts.If we can't divide the string into N equal parts, then print the same thing. Let's see the steps to solve the problem.Initialize the string and N.Find the length of the string using the size method.Check whether the string can be divided into N parts or not.If string can't divide into N equal parts, then print a message.Else iterate through the string and print each part.ExampleLet's see the code. Live Demo#include using namespace std; void divideTheString(string str, int n) {    int ... Read More

Divide a Number into Two Parts in C++ Program

Hafeezul Kareem
Updated on 27-Jan-2021 12:46:26

747 Views

In this tutorial, we are going to write a program that divides the given number into two partsIt's a straightforward problem to solve. We can get a number by diving the given number. And we can get the second number by subtracting the result from the total.If the given number is n, then the two numbers area = n / 2 b = n - aExampleLet's see the code. Live Demo#include using namespace std; void divideTheNumber(int n) {    int a = n / 2;    int b = n - a;    cout

Divide a Big Number into Two Parts Differing by K in C++

Hafeezul Kareem
Updated on 27-Jan-2021 12:46:05

184 Views

In this tutorial, we are going to write a program that divides a number into two parts with a difference of k.Let's see an example.Inputn = 100 k = 30Output65 35Here, we need to understand a little bit of math before diving into the problem. Let's see it.We have a + b = n and a - b = k. By adding the two equations, we geta = (n + k)/2 b = n - aExampleThat's it. We have n and k. And there is nothing more in it. Let's see the code Live Demo#include using namespace std; void divideTheNumber(int ... Read More

Distributing All Balls Without Repetition in C++ Program

Hafeezul Kareem
Updated on 27-Jan-2021 12:44:38

261 Views

In this tutorial, we are going to learn how to distribute n balls for k students without hurting anyone.The idea is simple, we have n balls in different colors that need to be distributed to the students. We don't have to give more than one ball of the same color to any student. If it is possible for a student to get more than one ball of the same color, then distribution should not happen.Let's see an example.Inputn = 10 k = 5 ballsColors = "rrrgbrbgbr"OutputYesNo color is more than the number of students (k). So, no student will get ... Read More

Element-wise Sum of Two Arrays in C++

Hafeezul Kareem
Updated on 27-Jan-2021 12:41:24

291 Views

In this tutorial, we are going to write a program that finds the sun of two array elements and store them into a separate array.We have given two arrays and we need to add the corresponding index elements from the two arrays. If the sum is not single digits, then extract the digits from the number and store them in the new array.Let's see an example.Inputarr_one = {1, 2, 32, 4, 5} arr_two = {1, 52, 3}Output2 5 4 3 5 4 5Let's see the steps to solve the problem.Initialize two arrays with dummy data.We are using the vector to ... Read More

Digital Root and Repeated Digital Sum in C++

Hafeezul Kareem
Updated on 27-Jan-2021 12:40:39

1K+ Views

In this tutorial, we are going to learn how to find the digital root of a given number.The digital root is the sum of a number of digits (until the sum of the digits becomes a single digit).We are given an integer in string format. And we have to find the sum of the digits repeatedly until the sum becomes a single digit.Let's see the steps to solve the problem.Initialize an integer in the string format.Iterate through the number and add each digit to the sum variable.If the sum is 0, then print 0.Else if the sum is divisible by ... Read More

Different Substrings in a String that Start and End with Given Strings in C++

Hafeezul Kareem
Updated on 27-Jan-2021 12:39:44

323 Views

In this tutorial, we are going to write a program that finds the total number of substrings that starts and ends with the given strings.We are given one string and two substrings. We need to find the different substrings count that starts and ends with the given two substrings. Let's see an example.Inputstr = "getmesomecoffee" start = "m" end = "e"Output6There are a total of 6 different substrings in the given string. They are me, mesome, mesomemecoffe, mesomemecoffee, mecoffe, mecoffee.Let's see the steps to solve the problem.Initialize the strings.Iterate over the str and find the start and end substrings indexes. ... Read More

Different Possible Marks for N Questions and Negative Marking in C++ Program

Hafeezul Kareem
Updated on 27-Jan-2021 12:36:21

363 Views

In this tutorial, we are going to write a program that finds different possible marks for the given n questions with positive and negative marking.Let's say we have 10 questions and each carries 2 marks for correct answers and -1 marks for a negative answer. Our aim is to find all the possible ways in which a student can score in the exam.Let's see the steps to solve the problem.Initialize the number of questions, positive marks for the correct answer and negative marks for the wrong answer.Initialize a set to store the possible marks.Write two inner loops from 0 to ... Read More

Diagonally Dominant Matrix in C++ Program

Hafeezul Kareem
Updated on 27-Jan-2021 12:35:32

429 Views

In this tutorial, we are going to write a program that helps us to find whether the given matrix is diagonally dominant or not.The matrix is called a diagonally dominant matrix if the sum of elements in the matrix other than the diagonal element is less than the diagonal matrix. Let's see an example.421 352 247The above matrix is a diagonally dominant matrix. Because4 > 2 + 1 5 ≥ 3 + 2 7 > 4 + 2All the diagonal elements are greater than or equal to the sum of the non-diagonal elements in the same row.Let's see the steps ... Read More

Advertisements