program to find the greatest among ten numbers.

29 Dec 2022 Balmiki Kumar 0 C Programming

Finding the Greatest Number among Ten: C Program Example

A program to find the greatest among ten numbers is a computer program that takes ten input numbers as input from the user and returns the largest number among them. The program compares the ten input numbers using logical conditions such as if-else statements or loops, and finds the largest number based on the comparison. This type of program is commonly used in many applications where it is necessary to find the highest value among a large number of inputs, such as finding the highest score in a game or finding the largest number in a dataset. The program may use an array to store the input numbers and a loop to iterate through the array and find the largest number.

C Program: Find the Largest Number among Ten

#include<stdio.h>
int main() {
 int a[10];
 int i;
 int greatest;
 printf("Enter ten values:");
 //Store 10 numbers in an array
 for (i = 0; i < 10; i++) {
 scanf("%d", &a[i]);
 }
 //Assume that a[0] is greatest
 greatest = a[0];
 for (i = 0; i < 10; i++) {
 if (a[i] > greatest) {
 greatest = a[i];
 }
 }
 printf("\nGreatest of ten numbers is %d", greatest);
 return 0;
}
 

Output:

Enter ten values: 2 53 65 3 88 8 14 5 77 64
Greatest of ten numbers is 88

Explanation with an example:

Entered values are 2, 53, 65, 3, 88, 8, 14, 5, 77, 64 They are stored in an array of size 10. let a[] be an array holding these values. /* how the greatest among ten numbers is found */ Let us consider a variable 'greatest'. At the beginning of the loop, variable 'greatest' is assinged with the value of first element in the array greatest=a[0]. Here variable 'greatest' is assigned 2 as a[0]=2. Below loop is executed until end of the array 'a[]';. for(i=0; i<10; i++) { if(a[i]>greatest) { greatest= a[i]; } } For each value of 'i', value of a[i] is compared with value of variable 'greatest'. If any value greater than the value of 'greatest' is encountered, it would be replaced by a[i]. After completion of 'for' loop, the value of variable 'greatest' holds the greatest number in the array. In this case 88 is the greatest of all the numbers. M

 

C program that finds the greatest among ten numbers:

#include <stdio.h>

int main() {
    int numbers[10];
    int max = 0;

    printf("Enter ten numbers:\n");

    // Input ten numbers
    for (int i = 0; i < 10; i++) {
        printf("Enter number %d: ", i + 1);
        scanf("%d", &numbers[i]);

        // Update max if current number is greater
        if (numbers[i] > max) {
            max = numbers[i];
        }
    }

    printf("The greatest number is: %d\n", max);

    return 0;
}

In this program, you input ten numbers and keep track of the greatest number encountered so far. At the end of the input loop, the value of max will hold the greatest number among the ten input numbers, which is then displayed.

Feel free to run this program and input the ten numbers to see the result

BY: Balmiki Kumar

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.