Found 1401 Articles for C

An Interesting Method to Generate Binary Numbers from 1 to n?

Arnab Chakraborty
Updated on 02-Jul-2020 09:41:31

287 Views

Here we will see one interesting method for generating binary numbers from 1 to n. Here we are using queue. Initially the queue will hold first binary number ‘1’. Now repeatedly delete element from queue, and print it, and append 0 at the end of the front item, and append 1 at the end of the front time, and insert them into the queue. Let us see the algorithm to get the idea.AlgorithmgenBinaryNumbers(n)Begin    define empty queue.    insert 1 into the queue    while n is not 0, do       delete element from queue and store it ... Read More

An efficient way to check whether n-th Fibonacci number is multiple of 10?

Arnab Chakraborty
Updated on 31-Jul-2019 13:20:58

158 Views

Here we will see one efficient way to check whether the nth Fibonacci term is multiple of 10 or not. Suppose the Fibonacci terms are {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987}. So here 15th (Counting from 0) Fibonacci term is divisible by 10. For 16 it will return true.One easiest method is generating Fibonacci numbers up to given term, and check whether it is divisible by 10 or not? But this solution is not good, because it will not work for larger terms.Another good approach is like below −Fibonacci terms ... Read More

Amazing stuff with system() in C / C++?

Arnab Chakraborty
Updated on 31-Jul-2019 13:18:06

770 Views

Here we will see some amazing results by using the system() function in C or C++. The system function is present in Windows, Linux and MAC operating systems. This function is used to execute the system commands that can be written in Command line.Here we will see two usages if system function in C or C++. The first one is getting the IP configuration details using C++ program.Example#include #include using namespace std; int main() {    system("C:\Windows\System32\ipconfig"); }OutputWindows IP Configuration Ethernet adapter Local Area Connection:    Connection-specific DNS Suffix . : domain.name    Link-local IPv6 Address . . ... Read More

All possible strings of any length that can be formed from a given string?

Arnab Chakraborty
Updated on 31-Jul-2019 13:14:53

548 Views

In this section we will see how to generate all possible strings of any length, this will take each combination of characters to make string. For example, if the string is ABC, then it will generate − {A, B, C, AB, BA, BC, CB, CA, AC, ABC, ACB, BAC, BCA, CAB, CBA}Let us see the example to get the idea.AlgorithmprintAllString(str)Begin    n := length of the string str    count is 2^n – 1    for each number 0 to count, do       sub_str := empty string       for j in range 0 to n, do ... Read More

All possible numbers of N digits and base B without leading zeros?

Arnab Chakraborty
Updated on 31-Jul-2019 13:11:26

153 Views

Here we will see one problem, We have N and base B. Our task is to count all N digit numbers of base B without any leading 0s. So if N is 2 and B is 2 there will be four numbers 00, 01, 10, 11. So only two of them are valid for this section. These are 10, 11, there are no leading 0s.If the base is B, then there are 0 to B – 1 different digits. So BN number of different N digit values can be generated (including leading 0s). The first digit is 0m if we ... Read More

All possible co-prime distinct element pairs within a range [L, R]?

Arnab Chakraborty
Updated on 31-Jul-2019 13:08:45

192 Views

Here we will see how to count number of co-prime pairs from the range, where a number will not appear more than a single pair.Before discussing the logic, let us see what are the co-prime numbers? The co-prime numbers are those numbers which has only one positive integer divisor, that is 1. In other words, we can say the GCD of these two numbers is 1.Here we are providing the lower and upper limit. If the lower and upper limits are 1 and 6, then there are three pairs. These are (1, 2), (3, 4) and (5, 6)The approach for ... Read More

All possible binary numbers of length n with equal sum in both halves?

Arnab Chakraborty
Updated on 31-Jul-2019 13:06:08

161 Views

Here we will see all possible binary numbers of n bit (n is given by the user) where the sum of each half is same. For example, if the number is 10001 here 10 and 01 are same because their sum is same, and they are in the different halves. Here we will generate all numbers of that type.AlgorithmgenAllBinEqualSumHalf(n, left, right, diff)left and right are initially empty, diff is holding difference between left and rightBegin    if n is 0, then       if diff is 0, then          print left + right       ... Read More

All palindrome numbers in a list?

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

197 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?

Arnab Chakraborty
Updated on 31-Jul-2019 12:58:13

265 Views

Here we will see how to add two numbers stored into separate 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 two lists of this type, our task is to add them and get the result after calculating the sum. Here we are using the C++ STL linked list. Let us see the algorithm to bet better idea.AlgorithmaddListNumbers(l1, l2)Begin    Adjust the l1 and l2 lengths by adding leading 0s with the smaller one    carry := 0   ... Read More

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

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

223 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

Advertisements