C Articles - Page 98 of 134

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

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

883 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

281 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

ASCII NUL, ASCII 0 (‘0’) and Numeric literal 0?

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

7K+ Views

Here we will see the ASCII NUL, ASCII 0 and the Numeric Literal 0. The ASCII null is represented as 0x00, and zero is represented as 0x30. The ASCII NUL character is used to denote the end of the string in C or C++. When programmer used ‘0’ (character 0) it is treated as 0x30. This is a hexadecimal number. The decimal equivalent is 48. To put ASCII NUL we have to mention ‘\0’ instead of ‘0’.char asciiNul = ‘\0’; int zero = 0; char zeroChar = ‘0’;The first one is ASCII NUL, second one is numeric 0, and the third one is character 0.

Arrange given numbers to form the biggest number?

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

304 Views

Here we will see how to generate the biggest number by rearranging the given numbers. Suppose there are {45, 74, 23} is given, the program will find the largest number, that is 744523. So each digit will not be arranged. but the whole number will be placed to make largest number.To solve this problem, we will use the string sorting. But the comparison logic is different. The comparing function will take two numbers a and b, then concatenate them to form ab and ba. Among them which one is bigger, that is considered.AlgorithmcompareStrings(a, b)begin    ab := concatenate b with ... Read More

Advertisements