Sum or Product
Sum or Product
Send Feedback
Sum or Product
1, then print the sum
2, then print the product
Any other number, then print '-1' (without the quotes)
Line 1 : Integer N
Line 2 : Choice C
Sum or product according to user's choice
1 <= N <= 12
10
1
55
10
2
3628800
10
4
-1
#include<iostream>using namespace std;
int main() { // Write your code here int n,sum=0,prod=1,c; cin>>n>>c; for(int i=1;n>=i;i++){ if(c==1){ sum=sum+i; } else if(c==2){ prod=prod*i; } else{ cout<<-1; //needed to print -1 not return it return 0; } } if(c==1)cout<<sum; //need to one of product or sum depending on c not both else if(c==2)cout<<prod;}
Comments
Post a Comment