Programming Articles - Page 2515 of 3363

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

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

354 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

188 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

266 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

322 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

311 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

299 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

172 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

Add 1 to number represented as array (Recursive Approach)?

Arnab Chakraborty
Updated on 31-Jul-2019 12:24:28

345 Views

In this section we will see one interesting problem. Suppose one number is given. We have to increase this number by 1. This is extremely simple task. But here we will place the number as an array. each digit of that number is placed as an element of the array. If the number is 512, then it will be stored as {5, 1, 2}. And also we have to increase the number using recursive approach. Let us see the algorithm to get the clear idea.Algorithmincrement(arr, n, index)Initially the default value of index is 0begin    if index < n, then ... Read More

Add 1 to a number represented as linked list?

Arnab Chakraborty
Updated on 31-Jul-2019 12:20:04

243 Views

Here we will see how to add 1 with a number stored into a linked list. In the linked list, each digit of the numbers is stored. If the number is 512, then it will be stored like below −512 = (5)-->(1)-->(2)-->NULLWe are providing the list into the increment function. That will return another list after adding 1 with it. Here we are using the C++ STL linked list. Let us see the algorithm to bet better idea.AlgorithmincrementList(l1)Begin    carry := 1    res := an empty list    for each node n from l1, scan from last to first, ... Read More

Advertisements