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 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;
}
Comments
Leave a comment