C Program To Calculate Percentage Difference Between 2 Numbers

Given two numbers, write a C program to calculate percentage difference between those two numbers.

Formula To Calculate Percentage Difference Between Two Numbers

If user entered numbers are stored in variables a and b. Then the formula to calculate the percentage difference between a and b is:

result = ( |a-b| / ((a+b) / 2.0) ) * 100;

OR

result = ( abs(a-b) / ((a+b) / 2.0) ) * 100;

Note: abs() is a builtin method present in stdlib.h header file, which returns the absolute value.

Related Read:
Basic Arithmetic Operations In C

Check this video tutorial to know more about absolute value:
C Program To Find Absolute Value of a Number

Logic To Calculate Percentage Difference Between 2 Numbers

The percentage difference between two numbers is calculated by dividing the absolute value of the difference between two numbers by the average of those two numbers. And then multiplying the result by 100.

Expected Output for the Input

User Input:
Enter 2 numbers
100
200

Output:
Percentage difference is 66.666664

Video Tutorial: C Program To Calculate Percentage Difference Between 2 Numbers


[youtube https://www.youtube.com/watch?v=L7Vc7-1z9os]

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

Source Code: C Program To Calculate Percentage Difference Between 2 Numbers

#include<stdio.h>
#include<stdlib.h>

int main()
{
    float a, b, result;

    printf("Enter 2 numbers\n");
    scanf("%f%f", &a, &b);

    result = ( abs(a-b) / ( (a+b)/2.0 ) ) * 100;

    printf("Percentage difference is %f\n", result);

    return 0;
}

Output 1:
Enter 2 numbers
50
60
Percentage difference is 18.181818

Output 2:
Enter 2 numbers
60
50
Percentage difference is 18.181818

Output 3:
Enter 2 numbers
75
75
Percentage difference is 0.000000

Output 4:
Enter 2 numbers
75
100
Percentage difference is 28.571428

Output 5:
Enter 2 numbers
150
200
Percentage difference is 28.571428

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

Biggest of Two Numbers: C

In this video tutorial we ask the user to enter 2 integer numbers and using if else and relational operator we check and display the biggest of 2 numbers on the console.

Related Read:
if else statement in C
Relational Operators In C

C Program To Find Biggest of 2 numbers

 
#include < stdio.h >

int main()
{
    int a, b;

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

    if(a > b)
    {
        printf("Biggest of %d and %d is %d\n", a, b, a);
    }
    else
    {
        printf("Biggest of %d and %d is %d\n", a, b, b);
    }

    return 0;
}

Output 1:
Enter 2 numbers
20
30
Biggest of 20 and 30 is 30

Output 2:
Enter 2 numbers
200
300
Biggest of 200 and 300 is 300

Biggest of 2 numbers: C Program


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

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


C Program To Find Biggest of 2 numbers

 
#include < stdio.h >

int main()
{
    int a, b, big;

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

    if(a > b)
    {
        big = a;
    }
    else
    {
       big = b;
    }

    printf("Biggest of %d and %d is %d\n", a, b, big);

    return 0;
}

Output:
Enter 2 numbers
75
100
Biggest of 75 and 100 is 100

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