Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Program to find largest of N numbers in an array

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 608
    Comment on it

    Finding largest number in an array:

    To find largest number in an array first user will enter the length for the array and then the user will input the elements for the array. There is a max variable taken in the program which is assigned 0th index value of the array. Then we will iterate through the array elements comparing whether max value is less or greater than the array elements if it is less then the array value than that array value will be stored in max variable and after iterating through the whole array we will get the largest element from the array.

    #include<stdio.h>
    void main()
    {
    int arr[50],i,max,length;
      // inputing the number of elements to be in the array
    
    printf("Enter the no of elements in the array"); 
    scanf("%d",&length);        
    printf("Enter the elements in the array");
    for(i=0;i<length;i++)          // inputing the elements in the array
    scanf("%d",&arr[i]);
    printf("Given array is\n");
    for(i=0;i<length;i++)
        printf("%d \t",arr[i]);
    
          // initializing max variable with the first value of the array
         max=a[0];  
    for(i=1;i<length;i++)
    {
        //iterating the array and comparing  the max value with array value
        if(max<arr[i])  
        max=arr[i];
    }
    printf("\t %d",max);    // Printing the largest number from the array
    
    }
    

 1 Comment(s)

  • Well here's the example,
     
    import java.util.Scanner;
    
    public class MaxValue
    {
       public static void main(String[] args)
       {
          int num, maxValue;
          Scanner sc = new Scanner(System.in);
          System.out.print("Please enter number of elements in the array : ");
          num = sc.nextInt();
          int arr[] = new int[num];
          System.out.println("Please enter elements of array : ");
          for(int a = 0; a < num; a++)
          {
             arr[a] = sc.nextInt();
          }
          maxValue = arr[0];
          for(int a = 0; a < num; a++)
          {
             if(maxValue < arr[a])
             {
                maxValue = arr[a];
             }
          }
          System.out.println("Max of array in java : " + maxValue);
          sc.close();
       }
    }
    
    You can find more here in this link, http://www.flowerbrackets.com/java-program-to-find-largest-and-smallest-number-in-an-array/
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: