C Examples Smallest and Largest

What it means

When we say “find the smallest” or “find the largest” in C:

  • We have a set of numbers (could be two numbers, three numbers, or a list/array).
  • Smallest → the number with the lowest value.
  • Largest → the number with the highest value.

Example:
Numbers: 5, 2, 8 →
Smallest = 2
Largest = 8


Why we need it

We often need to compare values to make decisions in programs.
For example:

  • In marksheets → Find the highest scorer.
  • In finance apps → Find the lowest expense or highest profit.
  • In games → Keep track of the highest score.
  • In sensors → Find the min and max temperature readings.

Without finding smallest/largest:
We cannot decide:

  • Who wins
  • What’s the cheapest option
  • What’s the highest performance
  • Whether a new record is set

Where Smallest and Largest is used

Here are some real-life practical uses:

  1. Banking – Find the largest transaction in a month or the smallest daily balance.
  2. Data Analysis – Minimum and maximum sales in a year.
  3. Weather Apps – Lowest and highest temperatures in a day.
  4. Engineering – Minimum stress and maximum load in a structure.
  5. Sorting – To arrange numbers, we first find the smallest repeatedly.
  6. Gaming – Find the highest score to display on the leaderboard.
  7. Stock Market – Find the day’s highest and lowest stock prices.

Find the Largest number in given number

void main()
{
    int i,n,x,large=0;
    clrscr(); //to clear the screen

    printf(“How many numbers?”);
    scanf(“%d”,&n);
    for(i=0;i<n;++i)
    {
        printf(“nEnter number %d:”,i+1);
        scanf(“%d”,&x);
        if(x>large)
        large=x;
    }

    printf(“nnThe largest number is %d”,large);
    getch(); //to stop the screen
}


Find the Largest & Smallest Number Among Three Numbers

Using Ternary Operator: Example-1

  • This below program for find the largest number. if you wanna check smallest number just the line 9 small=a<b ? (a<c?a:c) : (b<c?b:c);
//www.aruneworld.com

#include<stdio.h>
#include<conio.h>
void main()
{
    int a, b, c, large;
    clrscr();
    printf("Enter any three number: ");
    scanf("%d%d%d",&a,&b,&c);
    large=a>b ? (a>c?a:c) : (b>c?b:c);
    printf("Largest Number is: %d",large);
    getch();
}
/*
Enter any three number: 5 7 2
Largest number is 7
*/

Using Ternary Operator: Example-2

#include<stdio.h>

int main()
{
    int a=2,b=1,c=5;
    int Big = a<b? b<c?c:b: a>c?a:c;
    int Small = a<b? a<c?a:c: b<c?b:c;

    printf("Big\t- %d\nSmall\t- %d", Big,Small);
    
    return 0;
}

Smallest & Largest of N numbers using an array

//www.aruneworld.com
//Write a program to find the smallest of N numbers using array
#include<stdio.h>
int main()
{
  int i, n, a[50], small;
  printf("Give the value of N :\n"); // maximum N value is 50.
  scanf("%d",&n);
  printf("Enter the numbers one by one\n");
  for(i=0;i<n;i++)  scanf("%d",&a[i]);
  small = a[0];
  for(i=1;i<n;i++)
  {
    if(a[i]<small)  small = a[i];
  }
  printf("small number = %d", small);
  int large
  for(i=1;i<n;i++)
  {
     if(a[i]>large) large = a[i];
  }
  printf("largest number = %d", large);
  return 0;
}

/* Output is
Give the value of N :
5
Enter the numbers one by one
10
30
15
24
1
small number = 1
largest number = 30
*/


C program to find the Smallest Number using if loop

Code

#include <stdio.h>
#include <conio.h>

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

    printf("Enter three numbers: ");
    scanf("%d%d%d", &a, &b, &c);

    small = a; // assume a is smallest

    if (b < small)
        small = b;
    if (c < small)
        small = c;

    printf("\nSmallest number is: %d", small);

    getch();
    return 0;
}

Explanation

  1. Variable Declarationint a, b, c, small;
    • a, b, and c → store the three numbers entered by the user.
    • small → will hold the smallest number after comparisons.
  1. User Inputprintf("Enter three numbers: "); scanf("%d%d%d", &a, &b, &c);
    • Prompts the user to enter three integers.
    • Stores them in a, b, and c.
  1. Initial Assumptionsmall = a;
    • At the start, we assume a is the smallest number.
    • This is just a starting guess — we’ll check if b or c is smaller.
  1. Comparison with bif (b < small) small = b;
    • If b is less than the current small (which is a initially),
      update small to be b.
  1. Comparison with cif (c < small) small = c;
    • Similarly, if c is less than the current small,
      update small to be c.
  1. Outputprintf("\nSmallest number is: %d", small);
    • Displays the smallest number found.
  1. Wait for Key Pressgetch();
    • Waits for the user to press a key before closing (used in Turbo C / old compilers).

Key Idea:
We start with an assumption and update only if we find something smaller — this avoids messy nested if structures and makes the code easier to read.


Previous & Next


Please turn AdBlock off, and continue learning

Notice for AdBlock users

Please turn AdBlock off