Found 1401 Articles for C

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

sudhir sharma
Updated on 19-Aug-2019 11:41:47

2K+ Views

Given a number, find the difference between sum of odd digits and sum of even digits. Which means we will be count all even digits and all odd digits and the subtracting their sums.SampleInput:12345 Output:3Explanationthe odd digits is 2+4=6 the even digits is 1+3+5=9 odd-even=9-6=3Taking every digit out of number and checking whether the digit is even or odd if even then add it to even sum if not then to odd sum and then take difference of them.Example#include using namespace std; int main() {    int n, r=0;    int diff =0;    int even=0;    int odd=0; ... Read More

C Program for the cube sum of first n natural numbers?

sudhir sharma
Updated on 19-Aug-2019 11:40:22

460 Views

The cube sum of first n natural numbers is the program to add cubes of all natural numbers up to n. It is sum of series 1^3 + 2^3 + …. + n^3 that is sum of cube of n natural numbers.Input:6 Output:441Explanation1^3 + 2^3 + 3^3 + 4^3 + 5^3 + 63 = 441Using For loop to increase the no. and cubing it and taking sum of them.Example#include using namespace std; int main() {    int n = 6;    int sum = 0;    for (int i = 1; i

C Program for the compound interest?

sudhir sharma
Updated on 19-Aug-2019 11:39:05

184 Views

Compound interest is the simple interest that is compounded annually i.e. the interest will be calculated and added to the principal amount every year. This increases the overall interest as compared to simple interest. There is a different mathematical formula to calculate the compound interest. Lets see with the an example,Input:p=5, r=4, t=5 Output:1.083263ExplanationCompound Interest = Principle * (1 + Rate / 100)^time CI=5*(1+4/100)^5 CI=1.083263Example#include #include using namespace std; int main() {    float p, r, t, ci;    p=5;    r=4;    t=5;    ci = p * pow((1 + r / 100), t) - p;    printf("%f", ci);    return 0; }

C/C++ Program for the n-th Fibonacci number?

sudhir sharma
Updated on 19-Aug-2019 11:37:38

607 Views

The Fibonacci sequence is a series where the next term is the sum of the previous two terms.The first two terms of the Fibonacci sequence is 0 followed by 1. In this problem, we will find the nth number in the Fibonacci series. For this we will calculate all the numbers and print the n terms.Input:8 Output:0 1 1 2 3 5 8 13Explanation0+1=1 1+1=2 1+2=3 2+3=5Using For loop to sum of previous two terms for next termExample#include using namespace std; int main() {    int t1=0,t2=1,n,i,nextTerm;    n = 8;    for ( i = 1; i

10’s Complement of a decimal number?

sudhir sharma
Updated on 19-Aug-2019 09:16:32

2K+ Views

9’s complement and 10’s complement are used to make the arithmetic operations in digital system easier. These are used to make computational operations easier using complement implementation and usually trade hardware usage to the program.To obtain the 9’s complement of any number we have to subtract the number with (10n – 1) where n = number of digits in the number, or in a simpler manner we have to subtract each digit of the given decimal number from 9.10’s complement, it is relatively easy to find out the 10’s complement after finding out the 9’s complement of that number. We ... Read More

Average of Squares of n Natural Numbers?

sudhir sharma
Updated on 19-Aug-2019 09:10:15

181 Views

Given a number n, we need to find the average of the square of natural Numbers till n. For this we will first The squares of all the numbers till n. then we will add all these squares and divide them by the number n.Input 3 Output 4.666667Explanation12 + 22 + 32 = 1 + 4 + 9 = 14 14/3 = 4.666667Example#include using namespace std; int main() {    long n , i, sum=0 ,d;    n=3;    for(i=1;i

Add 1 to the number represented as array (Recursive Approach)?

sudhir sharma
Updated on 19-Aug-2019 08:59:09

412 Views

Given an array which is a collection of non-negative number represented as an array of digits, add 1 to the number (increment the number represented by the digits ). The digits are stored such that the most significant digit is the first element of the array.To add 1 to the number represented by digitsGiven array from the end, addition means rounding of the last no 4 to 5.If the last elements 9, make it 0 and carry = 1.For the next iteration check carry and if it adds to 10, do the same as step 2.After adding carry, make carry ... Read More

A square matrix as sum of symmetric and skew-symmetric matrix ?

sudhir sharma
Updated on 19-Aug-2019 08:43:39

792 Views

Symmetric Matrix − A matrix whose transpose is equal to the matrix itself. Then it is called a symmetric matrix.Skew-symmetric matrix − A matrix whose transpose is equal to the negative of the matrix, then it is called a skew-symmetric matrix.The sum of symmetric and skew-symmetric matrix is a square matrix. To find these matrices as the sum we have this formula.Let A be a square matrix. then, A = (½)*(A + A`)+ (½ )*(A - A`), A` is the transpose of the matrix.(½ )(A+ A`) is symmetric matrix.(½ )(A - A`) is a skew-symmetric matrix.Example#include using namespace std; ... Read More

Sum of square-sums of first n natural numbers

sudhir sharma
Updated on 19-Aug-2019 08:41:19

261 Views

The sum of square-sums of the first n natural numbers is finding the sum of sum of squares upto n terms. This series finds the sum of each number upto n, and adds this sums to a sum variable.The sum of square-sum of first 4 natural numbers is −sum = (12) + (12 + 22 ) + (12 + 22 + 32) + (12 + 22 + 32 + 42 ) = 1 + 5 + 14 + 30 = 50There are two methods to find the sum of square-sum of first n natural numbers.1) Using the for loop.In this ... Read More

Sum of square of first n odd numbers

sudhir sharma
Updated on 19-Aug-2019 08:40:00

3K+ Views

The series of squares of first n odd numbers takes squares of of first n odd numbers in series.The series is: 1,9,25,49,81,121…The series can also be written as − 12, 32, 52, 72, 92, 112….The sum of this series has a mathematical formula −n(2n+1)(2n-1)/ 3= n(4n2 - 1)/3Lets take an example,Input: N = 4 Output: sum =Explanation12 + 32 + 52 + 72 = 1 +9+ 25 + 49 = 84Using formula, sum = 4(4(4)2- 1)/3 = 4(64-1)/3 = 4(63)/3 = 4*21 = 84 both these methods are good but the one using mathematical formula is better because it does not use looks which reduces its time complexity.Example#include int main() {    int n = 8;    int sum = 0;    for (int i = 1; i

Advertisements