Computer Science, asked by samrata824, 3 months ago

Write a menu based program with the following options:-
i) to print the series 2,5,10,17,26 upto n terms
ii) to print the series 20,18,16,..........upto 2.
someone please answer this question immediately!

Answers

Answered by BrainlyProgrammer
3

Question:-

  • Write a menu based program with the following options:-
  • i) to print the series 2,5,10,17,26 upto n terms
  • ii) to print the series 20,18,16,..........upto 2.

_________

Code:-

package Coder;

import java.util.*;

public class SwitchSeries {

public static void main(String[] args) {

/*

*code written by:-

*@swetank232894

*/

Scanner sc=new Scanner (System.in);

int i;

System.out.println("Enter '1' for ser 1, '2' for ser 2");

int ch=sc.nextInt();

switch (ch)

{

case 1:

System.out.println("(i)Series1:-");

//to print:- 2,5,10,17,26

int k=2,d=3;

System.out.print("Enter no. of terms:-");

int n=sc.nextInt();

for(i=1;i<=n;i++)

{

System.out.print(k+" ");

k+=d;

d+=2;

}

break;

case 2:

System.out.println("(ii)Series2:-");

for(i=20;i>=2;i-=2)

System.out.print(i+" ");

break;

default:

System.out.println("Invalid choice");

}

}

}

______

Variable Description:-

  • i:- loop variable
  • k:- to store the numbers to be printed
  • d:- local variable

_____

•Output Attached

Attachments:
Similar questions