Write a program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,,n. In javascript example
Answers
Answered by
3
Explanation:
The code is supposed to ask the user whether to find the sum of numbers from 1 to x or finding the factorial of x. After taking the user's input for the ... use.
Mark as Brainliest
Answered by
0
Answer:import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int i,sum=0,pro=1;
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int n1 = s.nextInt();
if(n1==1)
{
for(i=1;i<=n;i++)
{
sum=sum+i;
}
System.out.println(sum);
}
else if(n1==2)
{
for(i=1;i<=n;i++)
{
pro=pro*i;
}
System.out.println(pro);
}
else
System.out.println(-1);
}
}
Similar questions
Art,
6 months ago
Math,
6 months ago
Computer Science,
11 months ago
Computer Science,
11 months ago
Accountancy,
1 year ago