Computer Science, asked by amolbetwar11, 1 year ago

Given the series as follows 2 2 4 8 16 512....... Identify the nth term of the series. Answers can be very large use BigInt in Java

Answers

Answered by nitish8089
11

.................................... code ..................................

..............................language : java........................

// series 2 2 4 8 16 512.......

import java.util.Scanner;

class Series{

public static int oddSeries(int x){

 int y;

 if(x<=1){

  return 2;

 }

 else{

  y=oddSeries(x-2)*oddSeries(x-2);

 }

 return y;

}

public static int evenSeries(int x){

 int a;

 if(x<=2){

  return 2;

 }

 else{

  a=evenSeries(x-2)*evenSeries(x-2)*evenSeries(x-2);

 }

 return a;

}

public static void main(String[] args) {

 Scanner sc=new Scanner(System.in);

 System.out.println("enter the nth digit whose value you want ");

 int n=sc.nextInt();

 if(n>0){

  if(n%2==1){

   int z=Series.oddSeries(n);

   System.out.println(z);

  }

  else{

   int b=Series.evenSeries(n);

   System.out.println(b);

  }

   

 }

 else{

  System.out.println("invalid input");

 }

}

}

Similar questions