Programming Articles - Page 2517 of 3366

All palindrome numbers in a list?

Arnab Chakraborty
Updated on 31-Jul-2019 13:00:48

341 Views

Here we will see one simple problem. We have to find all numbers that are palindrome in nature in a given list. The approach is simple, take each number from list and check it is palindrome or not, and print the number.AlgorithmgetAllPalindrome(arr, n)Begin    for each element e in arr, do       if e is palindrome, then          print e       end if    done EndExample#include #include using namespace std; bool isPalindrome(int n){    int reverse = 0, t;    t = n;    while (t != 0){       ... Read More

Add two numbers represented by linked lists?

Nishu Kumari
Updated on 31-Jul-2019 12:58:13

381 Views

We're given two singly linked lists, where each node stores one digit of a number. The digits are arranged from left to right, just like how we normally write numbers. For example: 7 -> 2 -> 4 -> 3 represents the number 7243. Our task is to add these two numbers and return the sum as a new linked list in the same (left-to-right) order. The input number can contain zeroes at the start, but in the output, there should not be any leading zeros. Let's understand this with a diagram given below - Scenario 1 Input: List 1 ... Read More

C/C++ Program to Count trailing zeroes in factorial of a number?

Arnab Chakraborty
Updated on 31-Jul-2019 12:54:40

335 Views

Here we will see how to calculate the number of trailing 0s for the result of factorial of any number. So if the n = 5, then 5! = 120. There is only one trailing 0. For 20!, it will be 4 zeros as 20! = 2432902008176640000.The easiest approach is just calculating the factorial and count the 0s. But this approach fails for large value of n. So we will follow another approach. The trailing zeros will be there, if the prime factors are 2 and 5. If we count the 2s and 5s we can get the result. For ... Read More

C/C++ Program to check whether it is possible to make a divisible by 3 number using all digits in an array?

Arnab Chakraborty
Updated on 31-Jul-2019 12:51:07

173 Views

In this section we will see if one array is given with n numbers, we have to check if we make a number using all of the elements of these numbers, that number will be divisible by 3 or not. If the array elements are {15, 24, 23, 13}, then the we can make integer like 15242313. It will be divisible by 3.AlgorithmcheckDivThree(arr)Begin    rem := 0    for each element e in arr, do       rem := (rem + e) mod 3    done    if rem is 0, then       return true    end ... Read More

C++ string class and its applications?

Arnab Chakraborty
Updated on 31-Jul-2019 12:48:26

129 Views

The C++ has the String class. That is different than the traditional C strings. The C string is actually the character array. In C++, the string class has few different properties. It has different functions, that can be used to perform different tasks. Here we will see the important features of the String class.In the first section we will see how the constructors of the string class works in different way. Let us see by example.Example#include using namespace std; int main() {    string str("This is a string");    cout

C++ Program to Print Matrix in Z form?

Arnab Chakraborty
Updated on 31-Jul-2019 12:43:25

252 Views

Here we will see how to print the matrix elements in Z form. So if the array is like below −5 8 7 1 2 3 6 4 1 7 8 9 4 8 1 5Then it will be printed like: 5, 8, 7, 1, 6, 7, 4, 8, 1, 5AlgorithmprintMatrixZ(mat)Begin    print the first row    i := 1, j := n-2    while i < n and j >= 0, do       print mat[i, j]       i := i + 1, j := j - 1    done    print the last row EndExample#include #define MAX 4 using namespace std; void printMatrixZ(int mat[][MAX], int n){    for(int i = 0; i

C++ Program for Zeckendorf's Theorem?

Arnab Chakraborty
Updated on 31-Jul-2019 12:40:35

300 Views

Here we will see how to check whether the given sum is found by adding some nonneighbouring Fibonacci numbers or not, if so, what are the numbers? For example if the give sum value is 10, this is sum of 8 and 2. Both 8 and 2 are Fibonacci terms and they are not adjacent. Let us see the algorithm to get the idea.AlgorithmnonNeighbourFibo(sum)Begin    while sum > 0, do       fibo := greatest Fibonacci term but not greater than sum       print fibo       sum := sum - fibo    done EndExample#include using ... Read More

C++ Program for BogoSort or Permutation Sort?

Arnab Chakraborty
Updated on 31-Jul-2019 12:38:27

292 Views

Here we will see another sorting algorithm called the Bogo Sort. This sort is also known as Permutation sort, Stupid sort, slow sort etc. This sorting algorithm is particularly ineffective sorting technique. This comes under the generate and test paradigm. It repeatedly generates a permutation until it is sorted. The conception is very straight forward. Until the list is sorted just shuffle the elements.AlgorithmbogoSort(array, n)Begin    while the arr is not sorted, do       shuffle arr    done EndExample#include #include using namespace std; bool isSorted(int arr[], int n) { //check whether the list is sorted    or not ... Read More

C++ Internals?

Arnab Chakraborty
Updated on 31-Jul-2019 12:32:50

283 Views

Here we will see the Class internals. Before that we will see Default constructors, which are related to internals. The default constructor is one constructor (defined by user or compiler) that does not take any argument. Now the question comes, why the default constructor is used?If the default constructor is not given, the compiler will implicitly declare default constructor. Default constructors are used to initialize some class internals. It will not affect the data member of the class. The compiler inserts default constructor in some different situations. Suppose a class is derived from another class with default constructor, or one ... Read More

Add elements of given arrays with given constraints?

Arnab Chakraborty
Updated on 02-Jul-2020 09:29:33

160 Views

Here we will see one problem. We will add two array elements and store them into another array. But we will follow some constraints. These constraints are like below −Addition should be stated from 0th index of both of the arraySplit the sum if it is more than one-digit number, and place each digit to the corresponding locationsRemaining digits of the larger input array will be stored at output arrayLet us see the algorithm to get the idea.AlgorithmaddArrayConstraints(arr1, arr2)Begin    define empty vector out    i := 0    while i is less than both arr1.length and arr2.length, do   ... Read More

Advertisements