Program to Find the Largest number amongst the numbers entered by the user in C

Posted in

Program to Find the Largest number amongst the numbers entered by the user in C
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    Problem Statement

    According to the problem statement, we need to write a program that prints or finds the largest number entered by the users amongst a list of numbers. The program must be written in such as way, it first asks the user to enter the number of elements or values the users want to enter. And after entering all those numbers we need to print the largest one. For Example, if the user chooses to enter 5 numbers

    Input

    How many numbers you want to enter? : 5
    Enter Number 1: 23
    Enter Number 2: 25
    Enter Number 3: 45
    Enter Number 4: 70
    Enter Number 5: 80

    Output

    The largest entered number is 80

    Algorithm

    • Initialize a variable largest with a random minimum value.
    • Ask the user to enter the number of elements n he/she wants to enter.
    • Create a for loop from range 1 to n .
    • Inside the loop ask users to enter the number.
    • After user enter the number check if the entered number is larger than the largest number, if yes save the entered number to the largest variable.

    C Program to Find the Largest number amongst the numbers entered by the user

    #include <stdio.h>
    
    int main()
    {	
    	int num, element, largest =-999999999, i;
    	
    	//input the number
    	printf("How many numbers you want to enter?: ");
    	scanf("%d", &num);
    	
    	for(i=1; i<=num;i++) { printf("Enter Number %d: ", i); scanf("%d", &element); //check if the entered number is greater than largest number if(element> largest)
    		{
    			largest = element;
    		}
    	}
    	
    	printf("The largest number is %d", largest);
    	
    
    	return 0;
    }

    Output

    How many numbers you want to enter?: 5
    Enter Number 1: 23
    Enter Number 2: 535
    Enter Number 3: 223
    Enter Number 4: 75
    Enter Number 5: 23
    The largest number is 535

    C++ Program to find the largest number amongst the numbers entered by the user

    #include<iostream>
    
    using namespace std;
    int main()
    {
    	int num, element, largest =-999999999;
    	
    	//input the number
    	cout<<"How many numbers you want to enter?: "; cin>>num;
    	
    	for(int i=1; i<=num;i++)
    	{
    		cout<<"Enter Number "<< i<<": "; cin>>element;
    		
    		//check if the entered number is greater than largest number
    		if(element> largest)
    		{
    			largest = element;
    		}
    	}
    	
    	cout<<"The largest number is "<< largest;
    	
    	return 0; 
    }

    Output

    How many numbers you want to enter?: 6
    Enter Number 1: 234
    Enter Number 2: 25
    Enter Number 3: 74
    Enter Number 4: 165
    Enter Number 5: 236
    Enter Number 6: 234
    The largest number is 236

    Python Program to Find the Largest number amongst the numbers entered by the user

    # input the number
    num = int(input("How many elements you wants to enter?: "))
    
    # initialize with minimum value
    largest =float("-inf")
    
    for i in range(1, num+1):
        element = int(input(f"Enter Number {i}: "))
    
        # check if the entered number is larger than largest value
        if(element> largest):
            largest=element
    
    print("The largest numbers is:", largest)

    Output:

    How many elements you wants to enter?: 5
    Enter Number 1: -24
    Enter Number 2: -535
    Enter Number 3: -43
    Enter Number 4: -223
    Enter Number 5: -4643
    The largest numbers is: -24

    Wrapping Up!

    Now let's wrap up our programming article on finding the largest number amongst the numbers entered by the user. The solution starts with initializing the initial value for the largest variable. It is very important to initialize as minimum as value to the initial largest variable. Do not initialize it with zero because in some cases the user may enter all the negative numbers, so their zero will become the largest number that's what we do not want from our program. We want that it must print the largest number from the user-entered values.

    People are also reading:

    Leave a Comment on this Post

    0 Comments