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 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 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)

  • Write a MIPS program that reads (with an appropriate prompt) a sequence of 20 integers and...

    Write a MIPS program that reads (with an appropriate prompt) a sequence of 20 integers and stores them in an array, and then calls the following two functions and prints the results in a readable format. The two functions are: smallestLargest: computes the smallest and the largest values in the array. oddEven: computes the number of even integers and the number of odd integers.

  • WRITE A PROGRAM IN C++ THAT DECLARES AN INTEGER VECTOR V 1.)INITIALIZE THE VECTOR, V, WITH...

    WRITE A PROGRAM IN C++ THAT DECLARES AN INTEGER VECTOR V 1.)INITIALIZE THE VECTOR, V, WITH 10 INTEGERS WITH VALUES FROM 1 TO 10 2.)OUTPUT THE VECTOR IN DESCENDING ORDER, THEN ASCENDING ORDER 2.)ADD ALL THE EVEN NUMBERS 3.)ADD ALL THE ODD NUMBERS 4.)OUTPUT THE SUM OF EVEN INTEGERS 5.)OUTPUT THE SUM OF ODD INTEGERS 7.)OUTPUT THE PRODUCT OF EVEN INTEGERS 8.)OUTPUT THE PRODUCT OF ODD INTEGERS SAMPLE: Vector: 2 4 3 5 2 3 8 9 1 10 Ascending...

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
ADVERTISEMENT