Program to find first N Iccanobif Numbers

Given a number N. The task is to find first N Iccanobif Numbers.

Iccanobif Numbers are similar to Fibonacci Numbers. The K-th Iccanobif number can be obtained by addition of previous two numbers after reversing their digits.

The first few Iccanobif Numbers are:

0, 1, 1, 2, 3, 5, 8, 13, 39, 124, 514, 836, …..

Examples:

Input : N = 5
Output : 0 1 1 2 3

Input : N = 9
Output : 0 1 1 2 3 5 8 13 39
Explanation: Upto 8th term, adding previous two 
terms is required, as there is an only single digit. 
For 9th term, adding 31(reversing 8th term) 
and 8 will give 39.


Approach: The idea is to take the first two Iccanobif Numbers as first = 0 and second = 1. Now iterate using a demoPointer N-2 times and every time find reverse of the previous two numbers using the approach discussed in: Reversing digits of a number. Find the sum of the two reversed numbers and update the variables first and second accordingly.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find first
// N Icanobif numbers
  
#include <bits/stdc++.h>
  
using namespace std;
  
// Iterative function to
// reverse digits of num
int reversDigits(int num)
{
    int rev_num = 0;
  
    while (num > 0) {
        rev_num = rev_num * 10 + num % 10;
        num = num / 10;
    }
  
    return rev_num;
}
  
// Function to print first
// N Icanobif Numbers
void icanobifNumbers(int N)
{
    // Initialize first, second numbers
    int first = 0, second = 1;
  
    if (N == 1)
        cout << first;
    else if (N == 2)
        cout << first << " " << second;
    else {
        // Print first two numbers
        cout << first << " " << second << " ";
  
        for (int i = 3; i <= N; i++) {
  
            // Reversing digit of previous
            // two terms and adding them
            int x = reversDigits(first);
            int y = reversDigits(second);
  
            cout << x + y << " ";
  
            int temp = second;
            second = x + y;
            first = temp;
        }
    }
}
  
// Driver Code
int main()
{
    int N = 12;
  
    icanobifNumbers(N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find first
// N Icanobif numbers
  
public class GFG{
  
    // Iterative function to
    // reverse digits of num
    static int reversDigits(int num)
    {
        int rev_num = 0;
      
        while (num > 0) {
            rev_num = rev_num * 10 + num % 10;
            num = num / 10;
        }
      
        return rev_num;
    }
      
    // Function to print first
    // N Icanobif Numbers
    static void icanobifNumbers(int N)
    {
        // Initialize first, second numbers
        int first = 0, second = 1;
      
        if (N == 1)
            System.out.print(first);
        else if (N == 2)
             System.out.print(first + " " + second);
        else {
            // Print first two numbers
            System.out.print(first + " " + second + " ");
      
            for (int i = 3; i <= N; i++) {
      
                // Reversing digit of previous
                // two terms and adding them
                int x = reversDigits(first);
                int y = reversDigits(second);
      
                 System.out.print(x + y + " ");
      
                int temp = second;
                second = x + y;
                first = temp;
            }
        }
    }
      
    // Driver Code
    public static void main(String []args){
        int N = 12;
      
        icanobifNumbers(N);
     }
     // This code is contributed by ANKITRAI1
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find first
# N Icanobif numbers
  
# Iterative function to
# reverse digits of num
def reversedigit(num):
    rev_num = 0
    while num > 0:
        rev_num = rev_num * 10 + num % 10
        num = num // 10
    return rev_num
  
# Function to print first
# N Icanobif Numbers
def icanobifNumbers(N):
  
    # Initialize first, second numbers
    first = 0
    second = 1
    if N == 1:
        print(first)
    elif N == 2:
        print(first, second)
    else:
  
        # Print first two numbers
        print(first, second, end = " ")
        for i in range(3, N + 1):
  
            # Reversing digit of previous
            # two terms and adding them
            x = reversedigit(first)
            y = reversedigit(second)
            print(x + y, end = " ")
            temp = second
            second = x + y
            first = temp
  
# Driver code
N = 12
icanobifNumbers(N)
  
# This code is contributed by Shrikant13

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find first
// N Icanobif numbers
   
using System;
public class GFG{
   
    // Iterative function to
    // reverse digits of num
    static int reversDigits(int num)
    {
        int rev_num = 0;
       
        while (num > 0) {
            rev_num = rev_num * 10 + num % 10;
            num = num / 10;
        }
       
        return rev_num;
    }
       
    // Function to print first
    // N Icanobif Numbers
    static void icanobifNumbers(int N)
    {
        // Initialize first, second numbers
        int first = 0, second = 1;
       
        if (N == 1)
            Console.Write(first);
        else if (N == 2)
             Console.Write(first + " " + second);
        else {
            // Print first two numbers
            Console.Write(first + " " + second + " ");
       
            for (int i = 3; i <= N; i++) {
       
                // Reversing digit of previous
                // two terms and adding them
                int x = reversDigits(first);
                int y = reversDigits(second);
       
                 Console.Write(x + y + " ");
       
                int temp = second;
                second = x + y;
                first = temp;
            }
        }
    }
       
    // Driver Code
    public static void Main(){
        int N = 12;
       
        icanobifNumbers(N);
     }
      
}

chevron_right


PHP

0)
{
$rev_num = ($rev_num * 10) +
($num % 10);
$num = (int)( $num / 10);
}

return $rev_num;
}

// Function to print first
// N Icanobif Numbers
function icanobifNumbers($N)
{
// Initialize first, second numbers
$first = 0;
$second = 1;

if ($N == 1)
echo $first;
else if ($N == 2)
echo $first , ” “, $second;
else
{
// Print first two numbers
echo $first, ” ” , $second, ” “;

for ($i = 3; $i <= $N; $i++) { // Reversing digit of previous // two terms and adding them $x = reversDigits($first); $y = reversDigits($second); echo ($x + $y), " "; $temp = $second; $second = $x + $y; $first = $temp; } } } // Driver Code $N = 12; icanobifNumbers($N); // This code is ccontributed by Tushil. ?>

Output:

0 1 1 2 3 5 8 13 39 124 514 836

Note: For larger value of N, use numbers as string.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.