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:
- Banking – Find the largest transaction in a month or the smallest daily balance.
- Data Analysis – Minimum and maximum sales in a year.
- Weather Apps – Lowest and highest temperatures in a day.
- Engineering – Minimum stress and maximum load in a structure.
- Sorting – To arrange numbers, we first find the smallest repeatedly.
- Gaming – Find the highest score to display on the leaderboard.
- 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
- Question : Write a program to find the smallest of N numbers using array.
- Code in GitHub : Find_Smallest_Number_using_Array.c
//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
- Variable Declaration
int a, b, c, small;a,b, andc→ store the three numbers entered by the user.small→ will hold the smallest number after comparisons.
- User Input
printf("Enter three numbers: "); scanf("%d%d%d", &a, &b, &c);- Prompts the user to enter three integers.
- Stores them in
a,b, andc.
- Initial Assumption
small = a;- At the start, we assume
ais the smallest number. - This is just a starting guess — we’ll check if
borcis smaller.
- At the start, we assume
- Comparison with
bif (b < small) small = b;- If
bis less than the currentsmall(which isainitially),
updatesmallto beb.
- If
- Comparison with
cif (c < small) small = c;- Similarly, if
cis less than the currentsmall,
updatesmallto bec.
- Similarly, if
- Output
printf("\nSmallest number is: %d", small);- Displays the smallest number found.
- Wait for Key Press
getch();- 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
- Previous : C Examples Fibonacci Sequence
- C Examples Random Number Generator (Git Code)
- Next : C Examples Ascending and Decending Order (Sorting & Algorithms)