Exploring Arrays and Loops: Finding the Greatest Digit in C Programming

Discover the power of arrays and loops in programming with our latest blog post. We present a clear and concise example in C programming that demonstrates how to efficiently find the largest digit among a series of inputs. Learn our step-by-step approach to gather user input, store it in an array, and employ loops for seamless comparison. Uncover the foundational concepts behind this problem-solving technique and gain insights into the essential role of arrays and loops in programming. Whether you're a beginner or honing your coding skills, this post offers practical knowledge and a deeper understanding of these fundamental programming tools.


#include <stdio.h>
int main()
{
    int a;
    printf("How many digits you want to compare: \n");
    scanf("%d", &a);
    int num[a]; // Declare an array to store the input digits

    // Input loop: Prompt user to enter digits and store them in the array
    for (int i = 0; i < a; i++) // Iterate through the array
    {
        printf("Enter digit %d : ", i + 1);
        scanf("%d", &num[i]);
    }

    int greatest;
    greatest = num[0]; // Initialize the greatest with the first digit

    // Find the greatest digit in the array
    for (int i = 0; i < a; i++) // Iterate through the array
    {
        if (greatest < num[i]) // Compare current digit with greatest
        {
            greatest = num[i]; // Update greatest if a larger digit is found
        }
    }

    printf("Greatest among all is %d", greatest);

    return 0;
}

 

Comments

Popular posts from this blog

Weird Algorithm || Introductory Problem || CSES