Posts

Showing posts with the label recursive

Hackerrank Fibonacci Modified Recursive Solution

Image
This is the solution for the Fibonacci Modified Problem found under the dynamic programming section at hackerrank. You need to find the (n+k)th term of the generated series, where nth and (n+1)th term will be supplied as input. The nth and (n+1)th terms, the (n+2)th can be computed by the following relation : Tn+2 = (Tn+1)2 + Tn So, if the first two terms of the series are 0 and 1 and k is 5 then the third term = 1 2 + 0 = 1 fourth term = 1 2 + 1 = 2 fifth term = 2 2 + 1 = 5 Screenshots[Demo] The Code def fibo(a,b,n): if(n==0): return b; else: return fibo(b,b**2+a,n-1) x=raw_input() x=x.split() print fibo(int(x[0]),int(x[1]),int(x[2])-2) Found Bugs, feel free to report them !

A Java Program to find the single digit sum of a number

Image
This program can be used to find the single digit sum of the given number in java. In this post you can find recursive as well as iterative method. Unlike just finding the sum of digits in a given integer, if further finds the same until a single digit is achieved. Screenshots [Demo] The output of both Recursive and Iterative method are displayed above ! The Iterative Method: import java.util.Scanner; class SingleIte { public static void main(String []as) { Scanner sc=new Scanner(System.in); int n,s=0,f=0; System.out.print("Enter The digit: "); n=sc.nextInt(); while(f==0) { while(n>0) { s=s+(n%10); n=n/10; } if(s<10) f=1; else { n=s; s=0; } } System.out.println("The Sigle digit sum is: "+s); } } The Recursive Method: import java.util.Scanner; class SingleRec { public static void singleSum(int n) { int s=0; while(n>0) { s=s+(n%10); n=n/10; } if(s<10) System.ou...

Fibonacci Series using the recursive method

The following program prints the Fibonacci series.This program is implemented using recursive method. The Fibonacci series is a series of numbers in which the current number is the sum of two previous Fibonacci numbers. This program is coded using the C language. Ex: 0,1,1,2,3,5,8,13.... #include<stdio.h> int main() { int n,i; printf("Enter the number of terms : "); scanf("%d",&n); printf("\nThe Fibonacci series is :\n"); for (i=0;i<=n;i++) printf("%d ", Fibonacci(i)); return 0; } int Fibonacci(int n) { if(n==0) return 0; else if(n==1) return 1; else return(Fibonacci(n-1)+Fibonacci(n-2)); } Description: The above program prints the number of terms in Fibonacci Series. Here n is the variable which is user input. It acts as a boundary limit for the generated Fibonacci Series. Output: Case 1: Enter the number of terms : 10 The Fibonacci series is : 0 1 1 2 3 5 8 13 2...

sPad, VI like sample text editor v1.0

Image
sPad is a Btech mini project. In this program you have the access to create view and save files. This is completely based on Command Line Interface. The zip contains a folder sPad and a powerpoint pptx file which will explain all its mechanish. Inside the sPad folder there are 5 header files init,open, pre,save and start  apart form these there is a .cpp file which is the Driver program. you need to compile that file which will genenrate a executable file. Understanding pre.h... This file includes the basic header files namely iostream,fstream and cstring. This file initializes 2 String type variables and a character array name. You can think as using one of the string for secondary memory which is permanent and another as a primary memory which is temporary. Understanding init.h... This file includes two functions namely inp() and hlp() , the inp() can be thought as if converting the primary memory contents to the secondary memory contents. In the hlp(), initially there is ...