Programming Articles - Page 2522 of 3366

C Program for Find largest prime factor of a number?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

3K+ Views

In this section, we will see how we can get the largest prime factor of a number in an efficient way. There is a number say n = 1092, we have to get the largest prime factor of this. The prime factors of 1092 are 2, 2, 3, 7, 13. So the largest is 13. To solve this problem, we have to follow this rule −When the number is divisible by 2, then store 2 as largest, and divide the number by 2 repeatedly.Now the number must be odd. Now starting from 3 to square root of the number, if ... Read More

C Program for Extended Euclidean algorithms?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

1K+ Views

Here we will see the extended Euclidean algorithm implemented using C. The extended Euclidean algorithm is also used to get the GCD. This finds integer coefficients of x and y like below −𝑎𝑥+𝑏𝑦 = gcd(𝑎, 𝑏)Here in this algorithm it updates the value of gcd(a, b) using the recursive call like this − gcd(b mod a, a). Let us see the algorithm to get the ideaAlgorithmEuclideanExtended(a, b, x, y)begin    if a is 0, then       x := 0       y := 1       return b    end if    gcd := EuclideanExtended(b mod ... Read More

C Program for efficiently print all prime factors of a given number?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

725 Views

In this section, we will see how we can get all the prime factors of a number in an efficient way. There is a number say n = 1092, we have to get all prime factors of this. The prime factors of 1092 are 2, 2, 3, 7, 13. To solve this problem, we have to follow this rule −When the number is divisible by 2, then print 2, and divide the number by 2 repeatedly.Now the number must be odd. Now starting from 3 to square root of the number, if the number is divisible by current value, then ... Read More

C Program for Difference between sums of odd and even digits?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

621 Views

Suppose we have one long integer. We have to find if the differences between the sum of the odd position digits, and the sum of the even position digits are 0 or not. The positions are start from 0 (left most).For example, suppose a number is 156486. The odd position sum is (5 + 4 + 6) = 15, and even position sum is (1 + 6 + 8) = 15, so they are same.To solve this problem, we can use two different ways. The first way is traversing form start to end and get the sum by alternating the ... Read More

C Program for cube sum of first n natural numbers?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

2K+ Views

In this problem we will see how we can get the sum of cubes of first n natural numbers. Here we are using one for loop, that runs from 1 to n. In each step we are calculating cube of the term and then add it to the sum. This program takes O(n) time to complete. But if we want to solve this in O(1) or constant time, we can use this series formula −AlgorithmcubeNNatural(n)begin    sum := 0    for i in range 1 to n, do       sum := sum + i^3    done    return ... Read More

C Program for compound interest?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

1K+ Views

Here we will see how to get the compound interest by writing one C program. The logic is very easy. Here we need some parameters −P − Principle amountR − Rate of interestT − Time spanThe compound interest formula is like belowExample#include #include float compoundInterest(float P, float T, float R) {    return P*(pow(1+(R/100), T)); } int main() {    float p, t, r;    printf("Enter Princple amount, rate of interest, and time: ");    scanf("%f%f%f", &p, &r, &t);    printf("Interest value: %f", compoundInterest(p, t, r)); }OutputEnter Princple amount, rate of interest, and time: 5000 7.5 3 Interest value: 6211.485352

C/C++ Program for Median of two sorted arrays of same size?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

370 Views

Here we will see how to get the median of two sorted array of the same size. We will use C++ STL to store array elements. After getting two arrays, we will merge them into one. As two arrays of same size are merged, then the final array will always hold even number of elements. We need to take two middle elements, then get the average of them for the median.Algorithmmedian(arr1, arr2)Begin    arr3 := array after merging arr1 and arr2    sort arr3    len := length of arr3    mid := len/2    median := (arr3[mid] + arr3[mid-1])/2 ... Read More

Bisymmetric matrix in C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

207 Views

Here we will see one program that will help to check whether a matrix is bisymmetric or not. The Bisymmetric matrix is one square matrix that are symmetric about both of the major diagonals. The below matrix is an example of bisymmetric matrix.1 2 3 4 5 2 6 7 8 4 3 7 9 7 3 4 8 7 6 2 5 4 3 2 1AlgorithmcheckBiSymmetric(mat, n)Begin    for i in range 0 to n – 1, do       for j in range 0 to i – 1, do          if mat[i, j] is ... Read More

Bash program to find A to the power B?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

3K+ Views

Here we will see how to get the number A raise to the power B using bash script. The logic is very simple. We have to use the ‘**’ operator or power operator to do this. Let us see the following program to understand the concept clearly.Example#!/bin/bash # GNU bash Script a=5 b=6 echo "$(($a ** $b))"Output15625

Bash program to check if the Number is a Palindrome?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

4K+ Views

To check whether a number is palindrome or not, we have to reverse the number, and then if the actual number and the reversed number are same, then this is palindrome. In Bash, performing the reverse operation is very easy. We have to use the ‘rev’ command to do that. Let us see the program to understand clearly.Example#!/bin/bash # GNU bash Script n=12321 rev=$(echo $n | rev) if [ $n -eq $rev ]; then    echo "Number is palindrome" else    echo "Number is not palindrome" fioutlineNumber is palindrome

Advertisements