Finding Smallest Number Among Three (Using C and function)

I got this problem while learning C in school and its worth learning. And you can use same method for C++ also.


Write a program that takes three integers as input and prints the smallest and largest of these numbers. The main program must prompt for integers and read them. Write the functions largest() and smallest() that receive the entered numbers as their parameters. Correspondingly, the functions shall return values corresponding to their names.



#include <stdio.h>

int largest(int first, int second, int third); //making the function name and giving parameters
int smallest(int first, int second, int third);

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

    printf("Enter the 1. number: ");
    scanf("%d",& a);
    printf("Enter the 2. number: ");
    scanf("%d",& b);
    printf("Enter the 3. number: ");
    scanf("%d",& c);
    largestNumber=largest(a,b,c); //storing the input value in function parameters
    smallestNumber =smallest(a,b,c);
    printf("Among the numbers you entered,\t the largest was %d and the smallest was %d.", largestNumber,smallestNumber);

    return 0;
}

int largest(int first, int second, int third) // calling the function
{
    int d;
    d = first;
    if (d<second)
        d = second;
    if (d<third)
        d = third;
    return d; // returns the largest value to the function                           
}

int smallest(int first, int second, int third)
{
    int d;
    d = first;
    if (d>second)
        d = second;
    if (d>third)
        d = third;
    return d;
}





Comments

Popular posts from this blog

What is LDR and how it works with circuit to try on?

Use of 4 Strain Gauge Sensors and LabView DAQ to Measure and Calculate Weigth

POWER SUPPLY (5A Constant Current Source)