Write a program to take 5 numbers from the user and print the largest number out of the entered numbers.
(please write a beginner's program)
Answers
Answer:
public class LargestOfN {
public static void main(String[] args) {
System.out.println("Welcome to Java Program to find "
+ "largest and smallest number without using array");
System.out.println("Please enter value of N: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int largest = Integer.MIN_VALUE;
int smallest = Integer.MAX_VALUE;
System.out.printf("Please enter %d numbers %n", n);
for (int i = 0; i < n; i++) {
int current = sc.nextInt();
if (current > largest) {
largest = current;
}
Explanation:
#include <stdio.h>
int main() {
int a[10];
int i;
int greatest;
printf("Enter ten values:");
//Store 10 numbers in an array
for (i = 0; i < 10; i++) {
scanf("%d", &a[i]);
}
//Assume that a[0] is greatest
greatest = a[0];
for (i = 0; i < 10; i++) {
if (a[i] > greatest) {
greatest = a[i];
}
}
printf("
Greatest of ten numbers is %d", greatest);
return 0;
}