Question

Write a C++ program to read four whole numbers (integers) and perform the following (1) Display...

Write a C++ program to read four whole numbers (integers) and perform the following

(1) Display the largest of the numbers

(2) Display the smallest of the numbers

(3) Display sum of the even numbers

(4) Display number of even and odd numbers

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include<bits/stdc++.h>


using namespace std;

int main()
{
//array to store 4 numbers
int num[4];
//Initialize largest, smallest, even sums and count of evens
int largest = INT_MIN;
int smallest = INT_MAX;
int evenSum = 0;
int evens = 0;
for(int i=0;i<4;i++){
//Read int
cin>>num[i];
//Update largest and smallest
if(num[i]>largest){
largest = num[i];
}
if(num[i]<smallest){
smallest = num[i];
}
//Find even sum and even numbers count
if(num[i]%2==0){
evenSum = evenSum + num[i];
evens++;
}
}
//Print results
cout<<"Largest of numbers is : "<<largest<<endl;
cout<<"Smallest of numbers is : "<<smallest<<endl;
cout<<"Sum of even numbers : "<<evenSum<<endl;
cout<<"Number of even numbers : "<<evens<<", odd numbers : "<<(4-evens)<<endl;

return 0;
}
OUTPUT :

Add a comment
Know the answer?
Add Answer to:
Write a C++ program to read four whole numbers (integers) and perform the following (1) Display...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel –...

    Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel – sentinel is used to indicate the end of input and must not to be considered as the valid data for the computation). Perform the following operations for the integers entered by the user. Display the sum of the numbers less than 100 Display the smallest of the positive integers Display the largest of the negative integers Display how many integers are between 100 and...

  • Write a Program in C language for: 1. Create a C program to read 7 integers...

    Write a Program in C language for: 1. Create a C program to read 7 integers and store them in an array. Next, the program is to check if the array is symmetric, that is if the first element is equal to the last one, the value of the second one is equal to the value of the last but one, and so on. Since the middle element is not compared with another element, this would not affect the symmetry...

  • Write a JAVA program to read a list of nonnegative integers and to display the largest...

    Write a JAVA program to read a list of nonnegative integers and to display the largest integer, the smallest integer and the average of all the integers. The user indicates the end of the input by entering a negative sentinel value that is not used in finding the largest, smallest and average values. The average should be a value of type double so that it is computed a fractional part. * Java program free from syntax, logic and run time...

  • Write a C++ program that does the following: Asks the user to enter 3 integers, Obtains...

    Write a C++ program that does the following: Asks the user to enter 3 integers, Obtains the numbers from the user, Prints the largest number and then the smallest of the numbers, If the numbers are equal, prints the message: "These numbers are equal." Prints the sum, average, and product of the 3 numbers.

  • write the program in VBA b. Write a program that uses a For loop to perform...

    write the program in VBA b. Write a program that uses a For loop to perform the following steps: a. Prompt the user to input two integers: firstNum and secondNum(firstNum must be less than secondNum). b. Output all odd numbers between firstNum and secondNum. c. Output the sum of all odd numbers between firstNum and secondNum. d. Output all even numbers between firstNum and second Num. e. Output the sum of all even numbers between firstNum and secondNum.

  • I need to write a C++ program that reads two integers from a data filed called...

    I need to write a C++ program that reads two integers from a data filed called "integers.dat" The first integer should be low and the other one high. The program should loop through all integers between the low and high values and include the low and high integers and then display Display the integers in the file Displays the number of integers divisible by 5 OR 6, but not both. Displays the sum of the integers from condition 2 as...

  • Write a program that reads integers ,one per line and displays their sum .Also display all...

    Write a program that reads integers ,one per line and displays their sum .Also display all the numbers read ,each with an annotation giving its percentage contribution to the sum. Use a method that takes the entire array as one argument and returns the sum of the numbers in the array. Hint: Ask user the user for the number of integers to be entered, create an array of that length ,and then fill the array with the integers read. A...

  • Write a complete C++ program to ask the user to enter 4 integers. The program must...

    Write a complete C++ program to ask the user to enter 4 integers. The program must use a function to find the smallest integer among them and print it on the screen. Typical output screen should be as following: Write a complete C++ program to ask the user to enter 4 integers. The program must use a functionto find the smallest integer among them and print it on the screen. Typical output screen should be as following: Note: the code...

  • Write C program to take four integer inputs from user (A, B, C, D) then, your...

    Write C program to take four integer inputs from user (A, B, C, D) then, your program should return the sum of even numbers (if there any even numbers) and the multiplication of the odd numbers (if there any odd numbers). For example: if A=1, B=2, C=3, D=4, your program will return: sum=6, multiplication=3. if A=6, B=2, C=8, D=4, your program will return: sum=20, multiplication=0. if A=1, B=3, C=5, D=4, your program will return: sum=4, multiplication=15.

  • Write a program that prompts the user to read three positive hex integers and display their...

    Write a program that prompts the user to read three positive hex integers and display their sum in both hex and decimal. Your program should prompt the user to read the number again, if the input is incorrect. You must use java.lang.NumberFormatException. (chapter12.AddThreeHexNumbers.java)

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT