program in java to Print the given series based on the user's choice:
3 6 12 24 48 96....upto n terms
2 5 10 17 26 37.. Upto n
pls anwer fast
Answers
Answer:
Program:-
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int ch,n;
System.out.println("Enter 1 for series 1:");
System.out.println("Enter 2 for series 2:");
System.out.println("Enter your choice");
ch=in.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter the limit:");
int temp=3;
n=in.nextInt();
System.out.println("The series:");
for(int i=1;i<=n;i++)
{
System.out.print(temp+" ");
temp=temp*2;
}
break;
case 2:
int t=1;
System.out.println("Enter the limit:");
n=in.nextInt();
for(int i=1;i<=n;i++)
{
System.out.print((t*t+1)+" ");
t++;
}
break;
default:
System.out.println("Wrong Choice");
}
in.close();
}
}
- The first photo is the output for 1st series.
- The second photo is the output for 2nd series.
- The third photo is the output for Invalid choice.
Also the case 2 you can also do like this:-
case 2:
int t=2,c=3;
System.out.println("Enter the limit:");
n=in.nextInt();
for(int i=1;i<=n;i++)
{
System.out.print(t+" ");
t=t+c;
c=c+2;
}
break;
This will also print the same series
2 5 10 17 26 37...