Programa Java para encontrar o maior de três números
Declaração do problema: dados três números x, y e z cujo objetivo é obter o maior entre esses três números.
Exemplo:
Input: x = 7, y = 20, z = 56 Output: 56 // value stored in variable z
Fluxograma para o maior de 3 números:
Algoritmo para encontrar o maior dos três números:
1. Start 2. Read the three numbers to be compared, as A, B and C 3. Check if A is greater than B. 3.1 If true, then check if A is greater than C If true, print 'A' as the greatest number If false, print 'C' as the greatest number 3.2 If false, then check if B is greater than C If true, print 'B' as the greatest number If false, print 'C' as the greatest number 4. End
Abordagens:
- Usando o operador ternário
- Usando if-else
Abordagem 1: usando o operador ternário
A sintaxe do operador condicional:
ans = (conditional expression) ? execute if true : execute if false
- Se a condição for verdadeira, execute a instrução antes dos dois pontos
- Se a condição for falsa, execute uma instrução após dois pontos para
largest = z > (x>y ? x:y) ? z:((x>y) ? x:y);
Ilustração:
x = 5, y= 10, z = 3 largest = 3>(5>10 ? 5:10) ? 3: ((5>10) ? 5:10); largest = 3>10 ? 3 : 10 largest = 10
// Java Program to Find the Biggest of 3 Numbers
// Importing generic Classes/Files
import java.io.*;
class GFG {
// Function to find the biggest of three numbers
static int biggestOfThree(int x, int y, int z)
{
return z > (x > y ? x : y) ? z : ((x > y) ? x : y);
}
// Main driver function
public static void main(String[] args)
{
// Declaring variables for 3 numbers
int a, b, c;
// Variable holding the largest number
int largest;
a = 5;
b = 10;
c = 3;
// Calling the above function in main
largest = biggestOfThree(a, b, c);
// Printing the largest number
System.out.println(largest
+ " is the largest number.");
}
}
10 é o maior número.
Abordagem 2: usando as instruções if-else
Neste método, as instruções if-else irão comparar e verificar o maior número comparando números. 'If' irá verificar se 'x' é maior que 'y' e 'z' ou não. 'else if' irá verificar se 'y' é maior que 'x' e 'z' ou não. E se ambas as condições forem falsas, 'z' será o maior número.
// Java Program to Find the Biggest of 3 Numbers
// Importing generic Classes/Files
import java.io.*;
class GFG {
// Function to find the biggest of three numbers
static int biggestOfThree(int x, int y, int z)
{
// Comparing all 3 numbers
if (x >= y && x >= z)
// Returning 1st number if largest
return x;
// Comparing 2nd no with 1st and 3rd no
else if (y >= x && y >= z)
// Return z if the above conditions are false
return y;
else
// Returning 3rd no, Its sure it is greatest
return z;
}
// Main driver function
public static void main(String[] args)
{
int a, b, c, largest;
// Considering random integers three numbers
a = 5;
b = 10;
c = 3;
// Calling the function in main() body
largest = biggestOfThree(a, b, c);
// Printing the largest number
System.out.println(largest
+ " is the largest number.");
}
}
10 é o maior número.
As postagens do blog Acervo Lima te ajudaram? Nos ajude a manter o blog no ar!
Faça uma doação para manter o blog funcionando.
70% das doações são no valor de R$ 5,00...
Diógenes Lima da Silva