Finding the Largest Number among a Set of Numbers in C
This program is a simple program that finds the largest number among a set of numbers entered by the user.
- The program starts by including the stdio.h header file, which contains the declarations of the functions used in the program.
- In the main function, the program declares an integer variable "i" for the loop, an integer variable "n" to store the number of elements to be compared, an integer variable "t" to store the greatest number, and an integer array "a" to store the numbers entered by the user.
- It uses the printf function to prompt the user to enter the limit of numbers to be compared, and uses the scanf function to read the input and store it in the "n" variable.
- The program then uses a for loop to take input of 'n' numbers and store them in the array 'a'. Inside the loop, the program uses the printf function to prompt the user to enter a number, and uses the scanf function to read the input and store it in the corresponding element of the array 'a'.
- Then the program uses another for loop to compare each element of the array with the variable 't' which is initially assigned the value of the first element of the array. Inside the loop, the program uses an if statement to check if the current element of the array is greater than 't', if it is then it assigns the value of the current element to 't'
Finally, it uses the printf function to print out the greatest number and returns 0 to indicate that the program has executed successfully.
Source Code
//C Program to print greatest of n numbers in an array.
#include<stdio.h>
int main()
{
int i,n,t,a[100];
printf("\nEnter The Limit : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter The Value : ");
scanf("%d",&a[i]);
}
t=a[0];
for(i=1;i<n;i++)
{
if(t<a[i])
t=a[i];
}
printf("\nThe Greatest No is %d",t);
return 0;
}
To download raw file
Click Here
Output
Enter The Limit : 5
Enter The Value : 23
Enter The Value : 34
Enter The Value : 12
Enter The Value : 3
Enter The Value : 56
The Greatest No is 56