C Program To Find Biggest of Two Numbers using Function

In this video tutorial you’ll learn how to find biggest of two integer numbers using function.

Related Read:
Biggest of Two Numbers: C
Biggest of Two Numbers Using Ternary Operator: C

Video Tutorial: C Program To Find Biggest of Two Numbers using Function


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

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

Source Code: C Program To Find Biggest of Two Numbers using Function

#include<stdio.h>

int biggest(int, int); //  function prototype

int main()
{
    int a, b;

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

    // function call biggest(a, b)
    printf("Biggest of %d and %d is %d\n", a, b, biggest(a, b));

    return 0;
}

//function definition
int biggest(int x, int y)
{
    return( x>y?x:y );
}

Output 1
Enter 2 integer numbers
50
25
Biggest of 50 and 25 is 50

Output 2
Enter 2 integer numbers
-5
-10
Biggest of -5 and -10 is -5

Logic To Find Biggest of 2 Numbers using Function

We ask the user to enter 2 integer numbers. We pass those 2 integer numbers to user defined function biggest. Inside function biggest we use ternary operator to determine the biggest number. Function biggest returns the biggest of the 2 numbers.

x>y?x:y

Here if x is greater than y, x will be returned else y will be returned.

Note: Function biggest returns integer type data. And it takes 2 arguments of type integer. We’re calling function biggest from inside printf() 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 Find First and Second Biggest in N Numbers, without using Arrays, using For Loop

Write a C program to find first and second biggest numbers in a list of N numbers entered by the user, without using arrays and without sorting the list and by using for loop.

Related Read:
For Loop In C Programming Language
Find First and Second Biggest in N Numbers, without using Arrays: C Program

Video Tutorial: C Program To Find First and Second Biggest in N Numbers, without using Arrays, using For Loop


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

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


Source Code: C Program To Find First and Second Biggest in N Numbers, without using Arrays, using For Loop

#include<stdio.h>

int main()
{
    int limit, num, count, fbig = 0, sbig = 0;

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

    printf("Enter %d positive numbers\n", limit);

    for(count = 1; count <= limit; count++)
    {
        scanf("%d", &num);

        if(num > fbig)
        {
            sbig = fbig;
            fbig = num;
        }

        if(num > sbig && num < fbig)
        {
            sbig = num;
        }
    }

    printf("First big = %d\nSecond Big = %d\n", fbig, sbig);

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 positive numbers
1
9
5
3
8
First big = 9
Second Big = 8

Output 2:
Enter the limit
8
Enter 8 positive numbers
1
5
9
3
7
8
6
10
First big = 10
Second Big = 9

Logic To Find First and Second Biggest Number in N Numbers, without using Arrays

First we ask the user to enter length of numbers list. If user enters limit value as 5, then we ask the user to enter 5 numbers. Once the user enters limit value, we iterate the for loop until loop counter variable count is less than or equal to limit. For each iteration of for loop we ask the user to enter a positive integer number. We check if the new number entered by the user is greater than fbig. If true, we swap the value of fbig to sbig and value of num to fbig.

Next we check if the user entered number is greater than sbig and less than fbig, if true, we assign the value of num to sbig.

Once control exits for loop, inside fbig we will have biggest number from the list of numbers entered by the user and sbig will have the second biggest number from the list of numbers entered by the user.

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 Biggest of N Numbers, without using Arrays, using For Loop

Write a C program to find biggest of N numbers without using Arrays and using for loop.

Related Read:
For Loop In C Programming Language
Find Biggest of N Numbers, without using Arrays: C Program

Video Tutorial: C Program To Find Biggest of N Numbers, without using Arrays, using For Loop


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

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


Logic To Find Biggest Number of N numbers, without using Arrays

We ask the user to enter the limit. i.e., the length of the list of numbers. For Example, if user enters limit value as 5, then we accept 5 numbers from the user and then find the biggest and output that number on to the console window.

First we ask the user to enter the limit. If user enters limit value as 5, we iterate the for loop 5 times i.e., until value of count is less than or equal to limit.

We check if its the first iteration of for loop. If true, we assign the first user entered number to variable big.

Inside the for loop, for each iteration we ask the user to enter a number. Next we check if that user entered number is greater than the value present in variable big. If the new number entered by the user is greater than value present in variable big, then we assign the new number entered by the user to the variable big. We keep doing this for each number entered by the user.

Source Code: C Program To Find Biggest of N Numbers, without using Arrays, using For Loop

#include<stdio.h>

int main()
{
    int limit, num, count, big;

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

    printf("Enter %d numbers\n", limit);
    for(count = 1; count <= limit; count++)
    {
        scanf("%d", &num);

        if(num > big || count == 1)
        {
            big = num;
        }
    }

    printf("Biggest number is %d\n", big);

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
1
9
3
7
4
Biggest number is 9

Output 2:
Enter the limit
6
Enter 6 numbers
-2
-5
-6
-9
-1
-4
Biggest number 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

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 Biggest of 3 Numbers using Binary Minus Operator

Lets write a simple C program to find biggest of three numbers using binary minus operator.

Related Read:
if else statement in C
Nested if else Statement In C

Logic To Find Biggest of 3 Numbers using Binary Minus Operator

If user enters a = 1, b = 2, c = 3;

We check if a – b > 0 and a – c > 0
i.e., 1 – 2 > 0 and 1 – 3 > 0
=> -1 > 0 and -2 > 0 (Both these conditions are false).
So a is not the biggest.

In else block we check if b – c > 0
=> 2 – 3 > 0
=> -1 > 0 (which is false)
So b is not the biggest.

In that case whatever number present in variable c is biggest.

Video Tutorial: C Program To Find Biggest of 3 Numbers using Binary Minus Operator


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

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

Source Code: C Program To Find Biggest of 3 Numbers using Binary Minus Operator

#include < stdio.h >

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

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

    if(a-b > 0 && a-c > 0)
    {
        printf("%d is the biggest\n", a);
    }
    else
    {
        if(b-c > 0)
        {
            printf("%d is the biggest\n", b);
        }
        else
        {
            printf("%d is the biggest\n", c);
        }
    }

    return 0;
}

Output 1:
Enter 3 numbers
1
2
3
3 is the biggest

Output 2:
Enter 3 numbers
10
30
20
30 is the biggest

Output 3:
Enter 3 numbers
100
50
75
100 is the biggest

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