Program to find largest number among n numbers

0 Suresh Chand June 2, 2021

In this example, We will learn a program to find largest number among n numbers. You must have knowledge in the following topics before looking at this example

Download Application: Learn C Programming with Live Compiler

Algorithm

This is the algorithm of this program

Step 1: Start
Step 2: Initialize 2 integer variables n and i, and two float variables c and largest
Step 3: Take input from user and store in n
Step 4: Take input from user and store in largest
Step 5: Assign i = 2
Step 6: REPEAT this step until i <= n
        Take input from user and store in c
        IF largest < c
                largest = c
        Increase i by 1
Step 7: Print largest
Step 8: End

Flowchart

This is the flowchart of this program

Flowchart : Program to find largest number among n numbers

CODE: Program to find largest number

Now, We will see the code to find largest number among n numbers

#include <stdio.h>

int main()
{
    int n, i;
    float c, largest;

    printf("How many numbers : ");
    scanf("%d", &n);

    printf("\nEnter %d numbers :\n", n);

    printf("\n\tElement 1: ");

    //Here is the important step
    // Initialize largest variable with first element
    scanf("%f", &largest);

    for (i = 2; i <= n; i++)
    {
        printf("\n\tElement %d  : ", i);
        scanf("%f", &c);

        /* 
            if input number is larger than the 
            current largest number
        */
        if (largest < c)
            largest = c; // update big to the larger value
    }

    printf("\nThe largest of the %d numbers is  %f ", n, largest);
    return 0;
}

The output of above program is

How many numbers : 4

Enter 4 numbers :  

        Element 1: 1

        Element 2  : 4

        Element 3  : 7

        Element 4  : 3

The largest of the 4 numbers is  7.000000
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Join Our Newsletter Now

Get the latest tutorials, examples and projects updates.

!

Ads

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Disable Ad Block

Want Same System