-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSum or Product
45 lines (35 loc) · 948 Bytes
/
Sum or Product
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
********* Problem Statment **********
Write a program that asks the user for a number N and a choice C. And then give them the possibility to choose between computing the
sum and computing the product of all integers in the range 1 to N (both inclusive).
****** Solution (Java Code) *******
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n,c,i,sum=0,mul=1;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
c=sc.nextInt();
if(c==1)
{
while(n<13 && n>0)
{
sum=sum+n;
n--;
}
System.out.println(sum);
}
else if(c==2)
{
while(n>0)
{
mul=mul*n;
n--;
}
System.out.println(mul);
}
else
{
System.out.println(-1);
}
}
}