write a program to find sum of even numbers and peoduct of odd numbers from 1 to N
(C++ . For loop)
Answers
Answered by
5
Question:-
Write a program to find sum of even numbers and product of odd numbers from 1 to N
Hint:-
- Loop from 1 to N(limit)
- Each iteration input number and check if even or not
- Yes? Add else Multiply
Approach...
#include <iostream>
using namespace std;
int main() {
int n,a;
cout<<"Enter the limit\n";
cin>>n;
int s=0,p=1;
cout<<"\nEnter "<<n<<" numbers";
for(int i=1;i<=n;i++){
cin>>a;
if(a%2==0)
s+=a;
else
p*=a;
}
cout<<"\nSum="<<s<<"\nProduct="<<p;
return 0;
}
Note:-
- You can also put printing statement inside loop
Sample I/O:-
Enter the limit
5
Enter 5 numbers
1
2
3
4
5
Sum=6
Product=15
Similar questions