Calculate Sum, Average, Variance and Standard Deviation: C Program

Write a function that receives 5 integers and returns the sum, average and standard deviation of these numbers. Call this function from main() and print the results in main().

Analyze The Above Problem Statement

1. We need to write a function to calculate the results.
2. Our function receives 5 integers.
3. We need to calculate sum, average or mean and standard deviation inside the function.
4. Problem statement is asking us to return 3 values from the function, which is not possible. But we can work around and use pointers to make it work. So the problem statement is indirectly asking us to use pointers to return more than 1 value from the function.
5. We will not be using arrays in this program since the number of inputs is fixed to 5. We can take 5 variables to store the values entered/input by the user.

Note: Perfect code for above problem statement is present at the end of this blog post / article. In the video we’ve made some modifications to get accurate results for various user inputs. None-the-less, you can find the exact c source code for above problem statement at the end of this blog post.

Related Read:
Basics of Pointers In C Programming Language
Assignment Operators in C

Very Important Note:

Any address preceded by a * (Indirection operator) will fetch the value present at that address.

Video Tutorial: Calculate Sum, Average, Variance and Standard Deviation: C Program


[youtube https://www.youtube.com/watch?v=2CU6uSjyndA]

YouTube Link: https://www.youtube.com/watch?v=2CU6uSjyndA [Watch the Video In Full Screen.]


Source Code: Calculate Sum, Average, Variance and Standard Deviation: C Program

#include<stdio.h>
#include<math.h>

void stand_devi(float, float, float, float, float,
                float*, float*, float*, float*);

int main()
{
    float a, b, c, d, e;
    float sum = 0, avg = 0, sd = 0, vari = 0;

    printf("Enter 5 numbers\n");
    scanf("%f%f%f%f%f", &a, &b, &c, &d, &e);

    stand_devi(a, b, c, d, e, &sum, &avg, &vari, &sd);

    printf("\nSum = %0.2f\n", sum);
    printf("Mean / Average = %0.2f\n", avg);
    printf("Variance = %0.2f\n", vari);
    printf("Standard Deviation = %0.2f\n", sd);

    return 0;
}

void stand_devi(float a, float b, float c, float d, float e,
                float *sum, float *avg, float *v, float *sd)
{
    *sum = a + b + c + d + e;
    *avg = *sum / 5.0;

    *v   += pow( (a - *avg), 2 );
    *v   += pow( (b - *avg), 2 );
    *v   += pow( (c - *avg), 2 );
    *v   += pow( (d - *avg), 2 );
    *v   += pow( (e - *avg), 2 );

    *v   = *v / 5.0;
    *sd  = sqrt(*v);
}

Output:
Enter 5 numbers
50
-5
14
122
41

Sum = 222.00
Mean / Average = 44.40
Variance = 1885.84
Standard Deviation = 43.43

Logic To Calculate Sum, Average, Variance and Standard Deviation

We pass 5 floating point variables and address of 4 floating point variables to the function stand_devi().

Using below formula we calculate sum, average/mean, variance and standard deviation:

*sum = a + b + c + d + e;
*avg = *sum / 5.0;

*variance = ( (a – *avg) x (a – *avg) + (b – *avg) x (b – *avg) + (c – *avg) x (c – *avg) + (d – *avg) x (d – *avg) + (e – *avg) x (e – *avg) ) / 5.0;

*standard_deviation = sqrt(*variance);

Inside stand_devi() function we are calculating sum, average, variance and standard deviation and storing it inside the address of variables sum, avg, vari, sd. So value changes is reflected in the entire program and not just inside stand_devi() method.

C Program Source Code Which Matches the Problem Statement Exactly

#include<stdio.h>
#include<math.h>

void stand_devi(int, int, int, int, int,
                float*, float*, float*);

int main()
{
    int a, b, c, d, e;
    float sum = 0, avg = 0, sd = 0;

    printf("Enter 5 numbers\n");
    scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);

    stand_devi(a, b, c, d, e, &sum, &avg, &sd);

    printf("\nSum = %0.2f\n", sum);
    printf("Mean / Average = %0.2f\n", avg);
    printf("Standard Deviation = %0.2f\n", sd);

    return 0;
}

void stand_devi(int a, int b, int c, int d, int e,
                float *sum, float *avg, float *sd)
{
    float v = 0; // Variance

    *sum = a + b + c + d + e;
    *avg = *sum / 5.0;

     v   += pow( (a - *avg), 2 );
     v   += pow( (b - *avg), 2 );
     v   += pow( (c - *avg), 2 );
     v   += pow( (d - *avg), 2 );
     v   += pow( (e - *avg), 2 );

     v   /= 5.0;

    *sd  = sqrt(v);
}

Output:
Enter 5 numbers
50
69
92
30
-60

Sum = 181.00
Mean / Average = 36.20
Standard Deviation = 52.29

Here we’re following all the things mentioned in the problem statement. We are taking 5 integer values and passing it to our user defined function. Inside the function we are calculating the sum, average and standard deviation and storing it at address of variables sum, avg, and sd. This modification or change is reflected everywhere in the program as we are modifying the value present at address. Addresses are unique and any changes made to the value present at a address reflects everywhere in the program. That’s how we are able to print the result i.e., values of sum, avg and sd inside main() function, after calling stand_devi() function.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Calculate One Number Raised To Another using Function

Write a function power( a, b ), to calculate the value of a raised to b.

Note: In today’s video tutorial lets see 2 methods of calculating value of a raised to b.
1. In first method lets write the entire logic ourselves.
2. In second method lets use the built in method pow() which is present in math.h library file.

Problem Statement

To clarify, the problem statement is: User inputs two numbers through the keyboard. Write a C program to find the value of one number raised to the power of another, using function / method.

Related Read:
Function / Methods In C Programming Language
Calculate Power of a Number: C Program
Calculate Power of a Number using pow(): C Program

Video Tutorial: C Program To Calculate One Number Raised To Another using Function


[youtube https://www.youtube.com/watch?v=NN_m-I_2auE]

YouTube Link: https://www.youtube.com/watch?v=NN_m-I_2auE [Watch the Video In Full Screen.]


Source Code: C Program To Calculate One Number Raised To Another using Function

#include<stdio.h>

int power(int base, int exp)
{
    int count, result = 1;

    for(count = 1; count <= exp; count++)
    {
        result = result * base;
    }

    return(result);
}

int main()
{
    int a, b;

    printf("Enter integer values for base and exponent\n");
    scanf("%d%d", &a, &b);

    printf("%d to the power of %d is %d\n", a, b, power(a, b));

    return 0;
}

Output 1:
Enter integer values for base and exponent
2
4
2 to the power of 4 is 16

Output 2:
Enter integer values for base and exponent
3
2
3 to the power of 2 is 9

Output 3:
Enter integer values for base and exponent
5
2
5 to the power of 2 is 25

Output 4:
Enter integer values for base and exponent
5
3
5 to the power of 3 is 125

Logic

In above code, we ask the user to enter base and exponent values. We pass these 2 values to function power(). Inside power() function/method, we use for loop to multiply the value of base exponent number of times. This would give us the result and we return the result.

Example: In above code, if the user inputs 2 as base and 4 as exponent, then: for loop executes or iterates exponent number of times i.e., 4 times in our case. So 2 is multiplied 4 times.

2 x 2 x 2 x 2 = 16.

Source Code: C Program To Calculate One Number Raised To Another using Function and builtin method pow()

#include<stdio.h>
#include<math.h>

float power(int base, int exp)
{
    return( pow(base, exp) );
}

int main()
{
    int a, b;

    printf("Enter integer values for base and exponent\n");
    scanf("%d%d", &a, &b);

    printf("%d to the power of %d is %0.2f\n", a, b, power(a, b));

    return 0;
}

Output 1:
Enter integer values for base and exponent
2
4
2 to the power of 4 is 16

Output 2:
Enter integer values for base and exponent
3
2
3 to the power of 2 is 9

Output 3:
Enter integer values for base and exponent
5
2
5 to the power of 2 is 25

Output 4:
Enter integer values for base and exponent
5
3
5 to the power of 3 is 125

Logic

Here we ask the user to enter the value of base and exponent. We pass the values of base and exponent to power() method. Inside power() method we use pow() builtin method which is present in math.h header file. We pass base and exponent value to pow() function and it returns the result, which we return to calling function i.e., main method.

Note:
pow() method is present in math.h library, so we include it at the top of our c source code.

Important Note: Note that we are using float as return type in above program(second method), since built in method pow() returns floating point or double type data. If we use integer type data as return type then it would give wrong results for certain inputs.

For example, if you have below code for power() function

int power(int base, int exp)
{
    return( pow(base, exp) );
}

If would return 24 for 5 raise to 2. And 124 as result for 5 raise to 3. Both these results are wrong.

Important Note: As you might have observed already we are not using function prototype in above programs. That is because we are writing function definition at the top before main function from where we are calling power() function. So main method already knows that there is a method called power() in our program.

If we want to write more than 1 function and we need to arrange it properly, we can write function prototype at the top – so that we can know about the functions present in our program in one glance, in one place. And then we can write the function definition anywhere in our program, in any order. Function prototype and function definition orders do not matter.

Also note that, writing function prototype is optional if you write function definition before main.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Armstrong Numbers Between Range using Function

In today’s video tutorial lets find all the Armstrong numbers or Narcissistic numbers between user entered range, using function / method.

An Armstrong number or Narcissistic number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself.

Example 1:
If number = 370
It has 3 digits: 3, 7 and 0. So n = 3.
result = 33 + 73 + 03 = 27 + 343 + 0 = 370.
So the original number 370 is equal to the result. So it’s an Armstrong Number.

Example 2:
If number = 8208
It has 4 digits: 8, 2, 0 and 8. So n = 4.
result = 84 + 24 + 04 + 84 = ‭4096‬ + 16 + 0 + ‭4096‬ = 8208.
So the original number 8208 is equal to the result. So it’s an Armstrong Number.

Related Read:
Swap 2 Numbers Using a Temporary Variable: C
Function / Methods In C Programming Language
C Program to Check Armstrong Number
C Program to print Armstrong Numbers Between Two Integers

Video Tutorial: C Program To Find Armstrong Numbers Between Range using Function


[youtube https://www.youtube.com/watch?v=nsVcZxEvIc8]

YouTube Link: https://www.youtube.com/watch?v=nsVcZxEvIc8 [Watch the Video In Full Screen.]


To count number of digits in a number

    int n = 0, temp;

    temp = num;

    while(temp)
    {
        temp = temp / 10;
        n++;
    }

To Iterate through the digits in the number

        while(num)
        {
            rem = num % 10;
            sum = sum + pow(rem, n);
            num = num / 10;
        }

To know above code logic, please visit and watch the video tutorial present at C Program to Check Armstrong Number.

Full Source Code: C Program To Find Armstrong Numbers Between Range using Function

#include<stdio.h>
#include<math.h>

float armstrong(int);

int main()
{
    int count, start, end, temp;

    printf("Enter start and end values\n");
    scanf("%d%d", &start, &end);

    if(start > end)
    {
        temp = start;
        start= end;
        end  = temp;
    }

    printf("Armstrong numbers between %d and %d are\n", start, end);

    for(count = start; count <= end; count++)
    {
        if(count == armstrong(count))
        {
            printf("%d is an Armstrong number\n", count);
        }
    }

    return 0;
}

float armstrong(int num)
{
    int rem, n = 0, temp;
    float sum = 0.0;

    temp = num;

    while(temp)
    {
        temp = temp / 10;
        n++;
    }

    while(num)
    {
        rem = num % 10;
        sum = sum + pow(rem, n);
        num = num / 10;
    }

    return(sum);
}

Output 1:
Enter start and end values
1
999
Armstrong numbers between 1 and 999 are
1 is an Armstrong number
2 is an Armstrong number
3 is an Armstrong number
4 is an Armstrong number
5 is an Armstrong number
6 is an Armstrong number
7 is an Armstrong number
8 is an Armstrong number
9 is an Armstrong number
153 is an Armstrong number
370 is an Armstrong number
371 is an Armstrong number
407 is an Armstrong number

Output 2:
Enter start and end values
500
99999
Armstrong numbers between 500 and 99999 are
1634 is an Armstrong number
8208 is an Armstrong number
9474 is an Armstrong number
54748 is an Armstrong number
92727 is an Armstrong number
93084 is an Armstrong number

Output 3:
Enter start and end values
1
99999
Armstrong numbers between 1 and 99999 are
1 is an Armstrong number
2 is an Armstrong number
3 is an Armstrong number
4 is an Armstrong number
5 is an Armstrong number
6 is an Armstrong number
7 is an Armstrong number
8 is an Armstrong number
9 is an Armstrong number
153 is an Armstrong number
370 is an Armstrong number
371 is an Armstrong number
407 is an Armstrong number
1634 is an Armstrong number
8208 is an Armstrong number
9474 is an Armstrong number
54748 is an Armstrong number
92727 is an Armstrong number
93084 is an Armstrong number

Output 4:
Enter start and end values
1
500
Armstrong numbers between 1 and 500 are
1 is an Armstrong number
2 is an Armstrong number
3 is an Armstrong number
4 is an Armstrong number
5 is an Armstrong number
6 is an Armstrong number
7 is an Armstrong number
8 is an Armstrong number
9 is an Armstrong number
153 is an Armstrong number
370 is an Armstrong number
371 is an Armstrong number
407 is an Armstrong number

Logic To Find Armstrong Numbers Between Range using Function

We ask the user to enter start and end value i.e., the range. Using for loop we iterate through all the numbers between start and end. For each iteration of for loop, variable count holds the number which we need to check if its Armstrong number or not.

Value of count is passed to function armstrong. Function armstrong counts the number of digits present in the integer number(we store it inside variable n) and then multiplies all the individual digits of the number by n and adds them. The sum is returned back to the calling function.

If value present in count and the value returned by armstrong method are equal, then the value present in count is an Armstrong number and will be displayed on the console window.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Armstrong Numbers Between 1 and 500 using Function

Lets write a C program to find Armstrong number or Narcissistic number from 1 to 500 using function.

Problem Statement
Write a C program to print out all Armstrong numbers or Narcissistic number between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. Hint: Use function / method.

An Armstrong number or Narcissistic number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself.

Example 1:
If number = 153
It has 3 digits: 1, 5 and 3. So n = 3.
result = 13 + 53 + 33 = 1 + 125 + 27 = 153.
So the original number 153 is equal to the result. So it’s an Armstrong Number.

Example 2:
If number = 1634
It has 4 digits: 1, 6, 3 and 4. So n = 4.
result = 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1634.
So the original number 1634 is equal to the result. So it’s an Armstrong Number.

Related Read:
C Program to print Armstrong Numbers between 1 and 500

Video Tutorial: C Program To Find Armstrong Numbers From 1 To 500 using Function


[youtube https://www.youtube.com/watch?v=V0Ws8aS4GwI]

YouTube Link: https://www.youtube.com/watch?v=V0Ws8aS4GwI [Watch the Video In Full Screen.]


To count the digits in a number

    int n = 0, temp;

    temp = num;

    while(temp)
    {
        temp = temp / 10;
        n++;
    }

To Iterate through the digits in the number

    while(num)
    {
        rem = num % 10;
        sum = sum + pow(rem, n);
        num = num / 10;
    }

To know above code logic, please visit and watch the video tutorial present at C Program to Check Armstrong Number.

Full Source Code: C Program To Find Armstrong Numbers Between 1 and 500 using Function

#include<stdio.h>
#include<math.h>

float armstrong(int);

int main()
{
    int count;

    for(count = 1; count <= 500; count++)
    {
        if(count == armstrong(count))
        {
            printf("%d is a Armstrong number\n", count);
        }
    }

    return 0;
}

float armstrong(int num)
{
    int rem, n = 0, temp;
    float sum = 0.0;

    temp = num;

    while(temp)
    {
        temp = temp / 10;
        n++;
    }

    while(num)
    {
        rem = num % 10;
        sum = sum + pow(rem, n);
        num = num / 10;
    }

    return(sum);
}

Output:
1 is a Armstrong number
2 is a Armstrong number
3 is a Armstrong number
4 is a Armstrong number
5 is a Armstrong number
6 is a Armstrong number
7 is a Armstrong number
8 is a Armstrong number
9 is a Armstrong number
153 is a Armstrong number
370 is a Armstrong number
371 is a Armstrong number
407 is a Armstrong number

#include<stdio.h>
#include<math.h>

float armstrong(int);

int main()
{
    int count;

    for(count = 1; count <= 500; count++)
    {
        if(count == armstrong(count))
        {
            printf("%d is a Armstrong number\n", count);
        }
    }

    return 0;
}

float armstrong(int num)
{
    int rem;
    float sum = 0.0;

    while(num)
    {
        rem = num % 10;
        sum = sum + (rem * rem * rem);
        num = num / 10;
    }

    return(sum);
}

Output:
1 is a Armstrong number
153 is a Armstrong number
370 is a Armstrong number
371 is a Armstrong number
407 is a Armstrong number

Logic To Find Armstrong Numbers Between 1 and 500 using Function

Using for loop(inside main method) we pass value(from 1 to 500) to armstrong function one by one. The value is present in variable count.

Function armstrong checks the number of digits present in the number(we call it as n) and then separates individual digits of the number and multiplies all the individual digits n times and adds them all and finally returns the sum.

If the armstrong function returned number and the value present in count are same then the value present in count is an Armstrong number, else we pass on to check the next number selected by for loop.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Check If Sum of Square of Sine and Cosine of an Angle is 1

Write a C program to receive value of an angle in degrees and check whether sum of squares of sine and cosine of this angle is equal to 1.

Related Read:
Basic Arithmetic Operations In C
Calculate Power of a Number using pow(): C Program

Formula To Convert Angle From Degree To Radian

radian = degree x ( PI / 180.0)
where PI is 3.14159265359

Note: In C programming Language, we’ve a builtin constant M_PI which has value of PI.

Expected Output for the Input

User Input:
Enter angle in Degree
45

Output:
Sum of square of sin(45.00) and cos(45.00) is 1

Logic To Check If Sum of Square of Sine and Cosine of an Angle is 1

User enters the angle in degree. We convert angle from degree to radian.
radian = degree x ( PI / 180.0)
Next we use pow() method present in math.h library to sqaure the values of sin(angle) and cos(angle). We add the square of sin(angle) and cos(angle) and store it inside the variable sum, and display appropriate message to the user.

Video Tutorial: C Program To Check If Sum of Square of Sine and Cosine of an Angle is 1


[youtube https://www.youtube.com/watch?v=wYbhYLcAv3Y]

YouTube Link: https://www.youtube.com/watch?v=wYbhYLcAv3Y [Watch the Video In Full Screen.]

Source Code: C Program To Check If Sum of Square of Sine and Cosine of an Angle is 1

#include<stdio.h>
#include<math.h>

int main()
{
    float sum = 0, angle, temp;

    printf("Enter angle in Degree\n");
    scanf("%f", &angle);

    temp   = angle;

    angle  = angle * ( M_PI / 180.0 );

    sum    = (  pow(sin(angle), 2) + pow(cos(angle), 2)  );

    if(sum == 1)
    {
        printf("Sum of square of sin(%0.2f) and cos(%0.2f) is 1\n", temp, temp);
    }
    else
    {
        printf("Sum of square of sin(%0.2f) and cos(%0.2f) is not 1\n", temp, temp);
    }

    return 0;
}

Output 1:
Enter angle in Degree
30
Sum of square of sin(30.00) and cos(30.00) is 1

Output 2:
Enter angle in Degree
45
Sum of square of sin(45.00) and cos(45.00) is 1

Output 3:
Enter angle in Degree
60
Sum of square of sin(60.00) and cos(60.00) is 1

Output 4:
Enter angle in Degree
75
Sum of square of sin(75.00) and cos(75.00) is 1

Output 5:
Enter angle in Degree
90
Sum of square of sin(90.00) and cos(90.00) is 1

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert