Fibonacci Series - Reverse & Sum

 Fibonacci Series - Reverse & Sum 

PROBLEM STATEMENT :

The program must accept an integer N as the input. The program must print the first N terms in the following series. 0, 1, 1, 2, 3, 5, 8, 13, 39, 124, 514, 836, 1053, 4139, ... and so on. In the above series, the first term is always 0, the second term is always 1 and the remaining terms are formed by adding the reverse of the previous two terms. 

Boundary Condition(s): 

1 <= N <= 50 

Input Format: 

The first line contains N. Output Format: The first line contains the first N terms in the above-mentioned series separated by a space. 

Example Input/Output 1: 
Input: () 
11
Output: 0 1 1 2 3 5 8 13 39 124 514

Explanation: 

Here N=11 and the first two terms in the series are 0 and 1. 

The 3rd term in the series is 1 (0 + 1). 

The 4th term in the series is 2 (1 + 1). 

The 5th term in the series is 3 (1 + 2). 

The 6th term in the series is 5 (2 + 3). 

The 7th term in the series is 8 (3 + 5). 

The 8th term in the series is 13 (5 + 8). 

The 9th term in the series is 39 (8 + 31). 

The 10th term in the series is 124 (31 + 93). 

The 11th term in the series is 514 (93 + 421). 

Hence the output is 0 1 1 2 3 5 8 13 39 124 514 

Example Input/Output 2: 
Input: () 
5 
Output: 0 1 1 2 3





                    


            1)    LEARN THRICE 

                                👇 

            2)    THINK TWICE

                                👇 

            3)    APPLY ONCE




SOLUTION :

C (Programming Language)


      

#include<stdio.h> #include<stdlib.h> long reverse(long N) { long revNum=0; while(N!=0) { revNum = revNum*10+(N%10); N/=10; } return revNum; } int main() { int N; scanf("%d",&N); long first=0,second=1,next=1; for(int i=1;i<=N;i++) { printf("%ld ",first); next = reverse(first)+reverse(second); first = second; second = next; } }



C++ (CPP)

       

#include <bits/stdc++.h> using namespace std; long long int rev(long long int a) { long long int r=0; while(a>0) { r = r*10 + a%10; a/=10; } return r; } int main(int argc, char** argv) { int n; long long int a=0,b=1,c=0; cin>>n; for(int i=0;i<n;i++) { cout<<c<<" "; a=b; b=c; c = rev(a)+rev(b); } }



JAVA

       

import java.util.*; public class Hello { public static long reverse(long n) { long a=0; while(n>0) { long r=n%10; a=a*10+r; n/=10; } return a; } public static void main(String[] args) { ArrayList<Long> arr=new ArrayList<>(); Scanner s=new Scanner(System.in); int n=s.nextInt(); arr.add((long)0); arr.add((long)1); for(int i=2;i<n;i++) { arr.add(reverse(arr.get(i-1))+reverse(arr.get(i-2))); } for(int i=0;i<n;i++) System.out.print(arr.get(i)+" "); } }



PYTHON

       

n=int(input()) l=[0,1] for i in range(2,n): rev1=int(str(l[-1])[::-1]) rev2=int(str(l[-2])[::-1]) l.append(rev1+rev2) print(*l[:n])



Never Stop Learning !!


Comments

Popular posts from this blog

DP (1) - Count number of ways to cover a distance

Zero Insert After K Times One

Left Number Twice Right