bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

assignment-p-12_v1.c (1473B)


      1 /*
      2  * Author  : Amit Dutta <amitdutta4255@gmail.com>
      3  * Date    : 12 Dec 2025
      4  * Repo    : https://github.com/notamitgamer/bsc
      5  * License : MIT License (See the LICENSE file for details)
      6  */
      7 
      8 /* Write a C program that takes multiple integers as command-line arguments and finds the
      9 maximum and minimum value among them. */
     10 
     11 #include <stdio.h>
     12 #include <stdlib.h>
     13 
     14 // use atoi()
     15 
     16 int main(int argc, char *argv[])
     17 {
     18     int current_val, max_val, min_val, i;
     19     char *endptr;
     20     long first_arg_val;
     21     if (argc < 2)
     22     {
     23         printf("Usage: %s <integer1> <integer2> ...\n", argv[0]);
     24         return 1;
     25     }
     26     first_arg_val = strtol(argv[1], &endptr, 10);
     27     if (*endptr != '\0' || argv[1] == endptr)
     28     {
     29         printf("Error: Argument '%s' is not a valid integer.\n", argv[1]);
     30         return 1;
     31     }
     32     max_val = (int)first_arg_val;
     33     min_val = (int)first_arg_val;
     34     for (i = 2; i < argc; i++)
     35     {
     36         long val = strtol(argv[i], &endptr, 10);
     37         if (*endptr != '\0' || argv[i] == endptr)
     38         {
     39             printf("Error: Argument '%s' is not a valid integer.\n", argv[i]);
     40             return 1;
     41         }
     42         current_val = (int)val;
     43         if (current_val > max_val)
     44         {
     45             max_val = current_val;
     46         }
     47         if (current_val < min_val)
     48         {
     49             min_val = current_val;
     50         }
     51     }
     52     printf("The maximum value is: %d\n", max_val);
     53     printf("The minimum value is: %d\n", min_val);
     54     return 0;
     55 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror