Found 1339 Articles for C

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

700 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

605 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

357 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

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

Average of ASCII values of characters of a given string?

Arnab Chakraborty
Updated on 02-Jul-2020 07:26:14

865 Views

Here we will see how to count the average of the ASCII values of each character in a given string. Suppose the string is “ABC”. The asci values are 65, 66, 67. So the average of these three is 66.AlgorithmasciiAverage(String)Begin    sum := 0    for each character c in String, do       sum := sum + ASCII of c    done    return sum/length of String EndExample Live Demo#include using namespace std; float asciiAverage(string str){    int sum = 0;    for(int i = 0; i str;    cout

Average of first n odd naturals numbers?

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

269 Views

Here we will see how to get the average of n odd natural numbers. The n is given by the user. To get the ith odd number we can use this formula 2*i+1. Here also we are using this formula. Let us see the algorithm to get the clear idea.AlgorithmavgOddNaturalNumber(n)Begin    sum := 0    for i in range 0 to n-1, do       sum := sum + (2i + 1)    done    return sum/n EndExample Live Demo#include using namespace std; float asciiAverage(string str){    int sum = 0;    for(int i = 0; i str;    cout

Advertisements