Found 1401 Articles for C

Print number of words, vowels and frequency of each character

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

262 Views

Input a string and find the total number of words, vowels and frequency of a character enter by a userInput : enter s string : I love my MOM      Enter a charcter of which you want to find a frequency: M    Total frequency of M : 2    Total number of vowels : 4    Total number of words : 4AlgorithmSTART Step 1 Declare array of string, ch, i, freq to 0, vow to 0, word to 0 Step 2 Input a string and a character ch Step 3 Loop for from i to 0 and str[i]!=’\o’ ... Read More

Print n numbers such that their sum is a perfect square

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

132 Views

Given with n numbers program must find those n number whose sum is a perfect squareInput : 5 Output : 1 3 5 7 9 1+3+5+7+9=25 i.e (5)^2AlgorithmSTART    Step 1 : Declare a Macro for size let’s say of 5 and i to 1    Step 2: loop While till i printing (2*i)-1 Step       Step 2.2 -> incrementing i with 1 Step    Step3-> End loop While STOPExample#include # define SIZE 5 int main() {    int i=1;    while(i

C Program for Find sum of odd factors of a number?

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

952 Views

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

C Program for Find largest prime factor of a number?

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

2K+ 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

778 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

501 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

473 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

804 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

285 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

Advertisements