C Program To Find Range of Set of Numbers

Write a C program to find the range of a set of numbers entered through the keyboard. Range is the difference between the smallest and biggest number in the list.

Example: If biggest number in the list is 5 and smallest number in the list is 1. The difference between them is the range. i.e., 5 – 1 = 4. So range = 4.

Related Read:
while loop in C programming
if else statement in C
Relational Operators In C
C Program To Find Absolute Value of a Number

Expected Output for the Input

User Input:
Enter the limit
5
Enter 5 numbers
1
2
3
4
5

Output:
Small Number = 1
Big Number = 5
Range is 4

Logic To Find Range of Set of Numbers

First we ask the user to enter the length or the size of the list, and store it inside the variable limit. If the user enters the list size as 5, then we ask the user to enter 5 numbers.

Next we ask the user to enter the first number. We assign the first number entered by the user to variables small and big. Since user already entered 1 number into the list, we decrement the value of variable limit by 1.

Next we take remaining inputs inside the while loop. For each iteration of the while loop we decrement the value of variable limit by 1, until the value of limit is 0. Once value of limit is zero, control exits while loop.

For each input(inside the while loop), we check if the value entered is bigger than the value present in variable big. If its true, we assign the bigger number to variable big. We also check if the value entered is smaller than the value present in variable small. If its true, we assign the smaller number to variable small.

Once the control exits the while loop, variable big will have the biggest number in the list and variable small will have the smallest number in the list.

Finally, we use below formula to calculate range of the list:
range = big – small;

Video Tutorial: C Program To Find Range of Set of Numbers


[youtube https://www.youtube.com/watch?v=-yN6KsA8d-k]

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

Source Code: C Program To Find Range of Set of Numbers

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

int main()
{
    int small, big, range, num, limit;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Enter %d numbers\n", limit);
    scanf("%d", &num);

    small = big = num;

    limit = limit - 1;

    while(limit)
    {
        scanf("%d", &num);

        if(num > big)
        {
            big = num;
        }

        if(num < small)
        {
            small = num;
        }

        limit--;
    }

    range = big - small;

    printf("Small Number = %d\nBig Number = %d\n", small, big);
    printf("Range is %d\n", abs(range));

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
0
-1
2
3
4
Small Number = -1
Big Number = 4
Range is 5

Output 2:
Enter the limit
6
Enter 6 numbers
10
9
8
7
6
5
Small Number = 5
Big Number = 10
Range is 5

Note: abs() returns the absolute value of a number. Absolute value is always positive.

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 Absolute Value of a Number

Write a C program to find absolute value of a number entered through the keyboard.

Absolute Value
In mathematics, the absolute value or modulus |x| of a real number x is the non-negative value of x without regard to its sign. The absolute value of a number may be thought of as its distance from zero.

Note: abs() is a builtin method/function present in library/header file stdlib.h

Example For Absolute Value of a Number

1. If user enters/inputs a value of 5. Then the distance from 0 to 5 is 5 units. So the absolute value of 5 is 5. Mathematically its written as |5| = 5. Its read as Modulus 5 is 5.

2. If user enters/inputs a value of -5. Then the distance from 0 to -5 is 5 units. So the absolute value of -5 is 5. Mathematically its written as |-5| = 5. Its read as Modulus -5 is 5.

scale

Expected Output for the Input

User Input:
Enter a positive or negative number
-14

Output:
Absolute Value of -14 is 14

Video Tutorial: C Program To Find Absolute Value of a Number


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

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

Source Code: C Program To Find Absolute Value of a Number

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

int main()
{
    int num;

    printf("Enter a positive or negative number\n");
    scanf("%d", &num);

    printf("Absolute Value of |%d| is %d\n", num, abs(num));

    return 0;
}

Output 1:
Enter a positive or negative number
14
Absolute Value of |14| is 14

Output 2:
Enter a positive or negative number
41
Absolute Value of |41| is 41

Output 3:
Enter a positive or negative number
-2
Absolute Value of |-2| is 2

Output 4:
Enter a positive or negative number
2
Absolute Value of |2| is 2

Output 5:
Enter a positive or negative number
-100
Absolute Value of |-100| is 100

Note: abs() works only for integer values and not for floating or double type data.

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