Bạn có thể tìm thấy lớn nhất trong số ba số sử dụng câu lệnh if...else
.
Ví dụ 1: Số lớn nhất trong số ba số
// program to find the largest among three numbers
// take input from the user
const num1 = parseFloat[prompt["Enter first number: "]];
const num2 = parseFloat[prompt["Enter second number: "]];
const num3 = parseFloat[prompt["Enter third number: "]];
let largest;
// check the condition
if[num1 >= num2 && num1 >= num3] {
largest = num1;
}
else if [num2 >= num1 && num2 >= num3] {
largest = num2;
}
else {
largest = num3;
}
// display the result
console.log["The largest number is " + largest];
Đầu ra
Enter first number: -7 Enter second number: -5 Enter third number: -1 The largest number is -1
Trong chương trình trên, parseFloat[]
được sử dụng để chuyển đổi chuỗi số thành số. Nếu chuỗi là số nổi, parseFloat[]
sẽ chuyển đổi chuỗi thành số điểm nổi.
Các số được so sánh với nhau sử dụng lớn hơn hoặc bằng với toán tử
Enter first number: -7 Enter second number: -5 Enter third number: -1 The largest number is -11. Và câu lệnh
Enter first number: -7 Enter second number: -5 Enter third number: -1 The largest number is -12 được sử dụng để kiểm tra điều kiện.
Ở đây, logic và
Enter first number: -7 Enter second number: -5 Enter third number: -1 The largest number is -13 cũng được sử dụng để kiểm tra hai điều kiện.
Bạn cũng có thể sử dụng chức năng
Enter first number: -7 Enter second number: -5 Enter third number: -1 The largest number is -14 tích hợp JavaScript để tìm lớn nhất trong số các số.
Ví dụ2: Sử dụng Math.Max []
// program to find the largest among three numbers
// take input from the user
const num1 = parseFloat[prompt["Enter first number: "]];
const num2 = parseFloat[prompt["Enter second number: "]];
const num3 = parseFloat[prompt["Enter third number: "]];
const largest = Math.max[num1, num2, num3];
// display the result
console.log["The largest number is " + largest];
Đầu ra
Enter first number: 5 Enter second number: 5.5 Enter third number: 5.6 The largest number is 5.6
Trong chương trình trên, parseFloat[]
được sử dụng để chuyển đổi chuỗi số thành số. Nếu chuỗi là số nổi, parseFloat[]
sẽ chuyển đổi chuỗi thành số điểm nổi.
Các số được so sánh với nhau sử dụng lớn hơn hoặc bằng với toán tử
Enter first number: -7 Enter second number: -5 Enter third number: -1 The largest number is -11. Và câu lệnh
Enter first number: -7 Enter second number: -5 Enter third number: -1 The largest number is -12 được sử dụng để kiểm tra điều kiện.
Một phương pháp là sắp xếp ba số này và sau đó in cái giữa:
#include
static inline void swap_if_out_of_order [int *p, int *q]
{
if [*p > *q] {
int t = *p;
*p = *q;
*q = t;
}
}
int main [void]
{
int a, b, c;
printf["Enter three integers\n"];
if [scanf["%d%d%d", &a, &b, &c] == 3] {
swap_if_out_of_order[&a, &b];
swap_if_out_of_order[&b, &c];
swap_if_out_of_order[&a, &b];
printf["Second greatest: %d\n", b];
}
}
Hoặc, không cần phân loại, với nhiều nhất ba so sánh:
#include
int main [void]
{
int a, b, c, m;
printf["Enter three integers\n"];
if [scanf["%d%d%d", &a, &b, &c] == 3] {
if [a > b] {
if [b > c] m = b;
else if [a > c] m = c;
else m = a;
} else if [a > c] m = a;
else if [b > c] m = c;
else m = b;
printf["Second greatest: %d\n", m];
}
}
Hoặc, có thể là cách hiệu quả nhất với các hàm tối đa và tối thiểu:
#include
static inline int min [int x, int y] { return x < y ? x : y; }
static inline int max [int x, int y] { return x > y ? x : y; }
int main [void]
{
int a, b, c;
printf["Enter three integers\n"];
if [scanf["%d%d%d", &a, &b, &c] == 3]
printf["Second greatest: %d\n", max[min[a, b], min[max[a, b], c]]];
}
Enter first number: -7 Enter second number: -5 Enter third number: -1 The largest number is -17 Hạn chế số cho đến 2 thập phân
Dưới đây là một chương trình để tìm số lớn thứ hai trong số ba số đầu vào của người dùng bằng cách sử dụng các vòng lặp if-use lồng nhau:
#include
int main[]
{
printf["\n\n\t\tStudytonight - Best place to learn\n\n\n"];
double a, b, c;
printf["Enter 3 numbers:\n"];
scanf["%lf%lf%lf", &a, &b, &c]; //lf is a format specifier to take double as input
// a is the largest
if[a >= b && a >= c]
{
if[b >= c]
{
/*
.2lf restricts the number till
2 decimal places
*/
printf["\n\n%.2lf is the 2nd largest number\n", b];
}
else
{
printf["\n\n%.2lf is the 2nd largest number\n", c];
}
}
else if[b >= a && b >= c]
{
if[a >= c]
{
printf["\n\n%.2lf is the 2nd largest number\n",a];
}
else
{
printf["\n\n%.2lf is the 2nd largest number\n",c];
}
}
// c is the largest number of the three
else if[a >= b]
{
printf["\n\n%.2lf is the 2nd largest number\n", a];
else
{
printf["\n\n%.2lf is the 2nd largest number\n", b];
}
printf["\n\n\t\t\tCoding is Fun !\n\n\n"];
return 0;
}
Đầu ra: