Print greatest number using One Dimensional Array in C


This program is written in C programming language and it does the following:

  • It first declares some integer variables s, i, n, a, t and j
  • The program then prompts the user to enter a limit 'n' which determines the size of an integer array 'a'
  • Then, it prompts the user to enter 'n' values which are stored in the array 'a'
  • Next, it uses a nested for loop, the outer loop iterates through the array, and the inner loop compares each element with all other elements in the array.
  • Within the inner for loop, it checks if the current element is less than the next element using an if statement.
  • If true, it assigns the next element to the variable 't'
  • After the nested loop, it prints the value of 't' which will be the greatest value of the array using printf statement
  • Finally, it returns 0 as the value of main function

Source Code

#include<stdio.h>
int main()
{
  int s=0,i,n,a[10],t=0,j;
  printf("ENTER LIMIT:");
  scanf("%d",&n);
  printf("ENTER VALUES:");
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  for(i=0;i<n;i++)
  {
    for(j=0;j<n;j++)
    {
      if(a[i]<a[j])
      {
        t=a[j];
      }
    }
  }
  printf("THE GREATEST VALUE IS:%d",t);
  return 0;
}
To download raw file Click Here

Output

ENTER LIMIT:5
ENTER VALUES:1
2
3
4
5
THE GREATEST VALUE IS 5

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C