Tuesday, 21 November 2017

To find largest among N numbers

Write a C program to find largest number among N numbers using command line arguments.

#include <stdio.h>
int main(int argc, char * argv[])
{
    int i, n, large;
    n = argc;
    if(argc == 1)
        exit(0);
    large = atoi(argv[1]);
    // Loop to store largest number to large
    for(i = 2; i < n; i++)
    {
       if(large < atoi(argv[i]))
           large = atoi(argv[i]);
    }
    //print statement should be according to Question
    printf("Largest element = %d", large);

    return 0;
}


Output:  ProgramName  5  11  23  43  65
              Largest element =   65

No comments:

Post a Comment

Featured Post

TCS Latest Test Patern ( For On-Campus Drives )

Tata Consultancy Services is software services consulting company whose  headquarter is situated in Mumbai, India. It is the Asia’s larges...