39.1k views
3 votes
Write a C+ program to read four numbers (both positive and negative integers) and perform the following.

(1) Display the largest of the numbers
(2) Display the smallest of the numbers
(3) Display sum of the positive numbers between 10 and 99
(4) Display sum of the numbers less than 0 Must use if statement (no loop statements) only.

User Mkalkov
by
7.7k points

1 Answer

6 votes

Final answer:

A C++ program can be written using 'if' conditions to find the largest and smallest of four numbers, the sum of positive numbers between 10 and 99, and the sum of negative numbers.

Step-by-step explanation:

To create a C++ program that reads four numbers and follows the specified instructions, you'd need to use conditional statements. Below is a simple structure for this program. Remember that the actual coding practices might vary with different coding styles or requirements.
#include
using namespace std;
int main() {
int a, b, c, d, largest, smallest, sumPositive = 0, sumNegative = 0;
cout << "Enter four integers: ";
cin >> a >> b >> c >> d;
// Finding the largest number using conditional statements
largest = a;
if (b > largest) largest = b;
if (c > largest) largest = c;
if (d > largest) largest = d;
// Finding the smallest number using conditional statements
smallest = a;
if (b < smallest) smallest = b;
if (c < smallest) smallest = c;
if (d < smallest) smallest = d;
// Sum of positive numbers between 10 and 99
if (a > 9 && a < 100) sumPositive += a;
if (b > 9 && b < 100) sumPositive += b;
if (c > 9 && c < 100) sumPositive += c;
if (d > 9 && d < 100) sumPositive += d;
// Sum of numbers less than 0
if (a < 0) sumNegative += a;
if (b < 0) sumNegative += b;
if (c < 0) sumNegative += c;
if (d < 0) sumNegative += d;
// Displaying results
cout << "The largest number is " << largest << endl;
cout << "The smallest number is " << smallest << endl;
cout << "The sum of positive numbers between 10 and 99 is " << sumPositive << endl;
cout << "The sum of numbers less than 0 is " << sumNegative << endl;
return 0;
}

This program will display the largest and smallest numbers, the sum of positive numbers between 10 and 99, and the sum of numbers less than 0, as required. The 'if' statements handle each condition separately without the need for loops.

User John Greenall
by
8.1k points