0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8 This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order and every even terms is derived from the previous  term using the formula (x/2)

Consider the below series :

0,0,2,1,4,2,6,3,8,4,10,5,12,6,14,7,16,8

  • This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order
  • Every even terms is derived from the previous  term using the formula (x/2)

Write a program to find the nth term in this series.

  1. The value n in a positive integer that should be read from STDIN the nth term that is calculated by the program should be written to STDOUT.
  2. Other than the value of the nth term no other characters /strings or message should be written to STDOUT.

For example

if n=10,the 10 th term in the series is to be derived from the 9th term in the series. The 9th term is 8 so the 10th term is (8/2)=4. Only the value 4 should be printed to STDOUT.

You can assume that the n will not exceed 20,000.

53 comments on “0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8 This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order and every even terms is derived from the previous  term using the formula (x/2)”


  • Pragati Tripathi

    import java.util.Scanner;

    public class q7 {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“enter value of n : “);
    int n = sc.nextInt();
    int a =0;

    if(n%2==0){
    for(int i =0; i<n/2; i++){
    int N = n/2;
    int res = a + (N-1)*1;
    System.out.println(res);
    break;
    }
    }
    else{

    for(int i =0; i<(n/2)+1; i++){
    int N = (n+1)/2;
    int res = a + (N-1)*2;
    System.out.println(res);
    break;
    }
    }
    }
    }


  • 26

    public class Main
    {
    public static void main(String[] args) {
    int num = 16;
    System.out.println((num % 2 == 0) ? (num / 2) – 1 : (num / 2) * 2);
    }
    }


  • Aviral

    Using Java Code :-
    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    System.out.println(“Enter Any Number.”);
    int num=s.nextInt();
    System.out.println(num%2!=0? (int)Math.abs(num-1):(int)Math.abs((num/2)-1));
    }
    }


  • Aviral

    Using Java Code:-
    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    System.out.println(“Enter Any Number.”);
    int num=s.nextInt();
    System.out.println(num%2!=0? (int)Math.abs(num-1):(int)Math.abs((num/2)-1));
    }
    }


  • Aviral

    /******************************************************************************
    Using Java Code
    0,0,2,1,4,2,6,3,8,4,10,5,12,6,14,7,16,8
    odd:- 0 2 4 6 8 10 12 14 16 ….
    even:- 0 1 2 3 4 5 6 7 8….

    *******************************************************************************/
    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    System.out.println(“Enter Any Number.”);
    int num=s.nextInt();
    System.out.println(num%2!=0? (int)Math.abs(num-1):(int)Math.abs((num/2)-1));
    }
    }