Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Wednesday, June 21, 2017

Recursive sequence Geeks solution


Problem: F(n)= (1) +(2*3) + (4*5*6) ... n.
find F(n)


import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
 {
     public static void check(int n,long sum,int time,int number)
     {
         long sum2=1;
         if(n==1)
         System.out.println("1");
         else if(n==time-1)
         System.out.println(sum);
         else
         {
         for(int i=0;i<time;i++)
         {
             sum2*=number++;
         }
       
       check(n,sum+sum2,++time,number)  ;
     }
       
     }
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
   check(ab.nextInt(),1,2,2);
}
}
}

0 Comments:

Post a Comment

Stats