Answer to Question #332689 in C++ for Angel Dingal

Question #332689

Using a while loop write a program that will ask the user to input 5 positive numbers then the program will calculate the sum Ang display all odd numbers the calculate the sum of all odd and even numbers entered




Sample output:




Please enter 5 positive numbers: 1 14 2 13 3



The odd numbers are: 13 3



The sum of odd numbers is: 16



The sum of all numbers is: 33

1
Expert's answer
2022-04-23T03:21:51-0400


	//1 14 2 13 3=1+13+3=17(sum of all odd numbers)
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
  int ar[5];
  cout<<"Please enter 5 positive numbers:";
  int sumOdd=0;
  int sumAll=0;
  for(int i=0;i<5;i++)
    {
        cin>>ar[i];
      sumAll+=ar[i];
      sumOdd+=(ar[i]%2)?ar[i]:0;
    }
  cout<<"The odd numbers are:";
  for(int i=0;i<5;i++)
    {
      if(ar[i]&1)
        cout<<ar[i]<<" ";
    }
  cout<<"\n";
  cout<<"The summ of odd numbers: "<<sumOdd<<endl;
  cout<<"The summ of all numbers: "<<sumAll<<endl;
  
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS