//How to find greatest number from an array.
import java.util.*;
class M
{
public static void main(String args[])
{
int a[]=new int[5];
int largest_number=0;
System.out.println("Enter the 5 values of an array:");
Scanner sc=new Scanner(System.in);
for(int i=0;i<=4;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<4;i++)
{
if(a[i]>a[i+1])
{
a[i+1]=a[i];
largest_number=a[i+1];
}
else
{
}
}
System.out.println("largest number is: "+largest_number);
}
}
OUTPUT:
Enter the 5 values of an array:
5
2
8
1
7
largest number is: 8
import java.util.*;
class M
{
public static void main(String args[])
{
int a[]=new int[5];
int largest_number=0;
System.out.println("Enter the 5 values of an array:");
Scanner sc=new Scanner(System.in);
for(int i=0;i<=4;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<4;i++)
{
if(a[i]>a[i+1])
{
a[i+1]=a[i];
largest_number=a[i+1];
}
else
{
}
}
System.out.println("largest number is: "+largest_number);
}
}
OUTPUT:
Enter the 5 values of an array:
5
2
8
1
7
largest number is: 8
No comments:
Post a Comment