9.5k views
0 votes
Write a program that asks the user to enter five different, integer numbers. The program then reports the largest number and the smallest number.

Use the if statement, but no loops.

User Andrei Ion
by
7.0k points

1 Answer

4 votes

Answer:

// here is code in C++.

#include <bits/stdc++.h>

using namespace std;

// main function

int main()

{

// variables

int minn=INT_MAX;

int maxx=INT_MIN;

int n1,n2,n3,n4,n5;

cout<<"enter five Numbers:";

//read 5 Numbers

cin>>n1>>n2>>n3>>n4>>n5;

// find maximum

if(n1>maxx)

maxx=n1;

if(n2>maxx)

maxx=n2;

if(n3>maxx)

maxx=n3;

if(n4>maxx)

maxx=n4;

if(n5>maxx)

maxx=n5;

// find minimum

if(n1<minn)

minn=n1;

if(n2<minn)

minn=n2;

if(n3<minn)

minn=n3;

if(n4<minn)

minn=n4;

if(n5<minn)

minn=n5;

// print maximum and minimum

cout<<"maximum of five numbers is: "<<maxx<<endl;

cout<<"minimum of five numbers is: "<<minn<<endl;

return 0;

}

Step-by-step explanation:

Declare two variables "minn" & "maxx" and initialize them with INT_MAX and INT_MIN respectively.Then read the five number from user and compare it with "minn" & "maxx" ,if input is greater than "maxx" then update "maxx" or if input is less than "minn" then update the "minn". After all the inputs, "minn" will have smallest and "maxx" will have largest value.

enter five Numbers:5 78 43 55 12

maximum of five numbers is: 78

minimum of five numbers is: 5

User Thomas Bovee
by
7.1k points