
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Write a C program to find out the largest and smallest number in a series
Problem
Let the user enter four series of integers in the console, find out a number which is smallest and largest in a series
Solution
To calculate the small and large number, we use if conditions. The logic we use to find the largest and smallest number is −
if(minno>q) //checking 1st and 2nd number minno=q; else if(maxno&l;q) maxno=q; if(minno>r) //checking 1st and 3rd number minno=r;
Program 1
#include<stdio.h> int main(){ int minno,maxno,p,q,r,s; printf("enter any four numbers:"); scanf("%d%d%d%d",&p,&q,&r,&s); minno=p; maxno=p; if(minno>q) //checking 1st and 2nd number minno=q; else if(maxno<q) maxno=q; if(minno>r) //checking 1st and 3rd number minno=r; else if(maxno<r) maxno=r; if(minno>s) //checking 1st and 4th number minno=s; else if(maxno<s) maxno=s; printf("Largest number from the given 4 numbers is:%d
",maxno); printf("Smallest numbers from the given 4 numbers is:%d",minno); return 0; }
Output
enter any four numbers:34 78 23 12 Largest number from the given 4 numbers is:78 Smallest numbers from the given 4 numbers is:12
Program 2
The below program finds the smallest and largest element in an array −
#include<stdio.h> int main(){ int a[50],i,num,large,small; printf("Enter the number of elements :"); scanf("%d",&num); printf("Input the array elements :
"); for(i=0;i<num;++i) scanf("%d",&a[i]); large=small=a[0]; for(i=1;i<num;++i){ if(a[i]>large) large=a[i]; if(a[i]<small) small=a[i]; } printf("small= %d
",small); printf("large= %d
",large); return 0; }
Output
Enter the number of elements :8 Input the array elements : 1 2 6 4 8 9 3 9 small= 1 large= 9
- Related Articles
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- Program to find Smallest and Largest Word in a String in C++
- C++ program to find smallest and largest number of children in game before start
- Program to find the largest and smallest ASCII valued characters in a string in C++
- C program to find the second largest and smallest numbers in an array
- C# Program to get the smallest and largest element from a list
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- C++ Program to find the smallest digit in a given number
- Find the largest number in a series by using pointers in C language
- Python program to find the smallest number in a list
- C++ program to find minimum possible difference of largest and smallest of crackers
- Python program to find the largest number in a list
- Program to Find Out the Smallest Substring Containing a Specific String in Python
- Python program to find largest number in a list

Advertisements