Write a C program for N-Bonacci Number.

function initializeArrayInt(array, length):
    for i = 0 to length - 1:
        array[i] = 0

function bonacciseries(n, m):
    array[m]
    i = 0
    j = 0

    if m >= n:
        initializeArrayInt(array, m)
        array[n - 1] = 1
        i = n
        
        while i < m:
            j = i - n
            while j < i:
                array[i] = array[i] + array[j]
                j = j + 1
            end while
            i = i + 1
        end while
        
        i = 0
        while i < m:
            output array[i]
            output " "
            i = i + 1
        end while
        
    else:
        output "M >= N !!"
    end if

function main():
    n = 0
    m = 0

    output "N-bonacci numbers"
    output "N-bonacci number to calculate?"
    input n
    output "How many series's numbers?"
    input m
    bonacciseries(n, m)

#include <stdio.h>

void initializeArrayInt(int vet[], int length) {
    int i;
    for (i = 0; i < length; i++) {
        vet[i] = 0;
    }
}

void bonacciseries(int n, int m) {
    int a[m];
    int i, j;

    if (m >= n) {
        initializeArrayInt(a, m);
        a[n - 1] = 1;
        i = n;
        while (i < m) {
            j = i - n;
            while (j < i) {
                a[i] = a[i] + a[j];
                j = j + 1;
            }
            i = i + 1;
        }
        i = 0;
        while (i < m) {
            printf("%d ", a[i]);
            i = i + 1;
        }
    } else {
        printf("M >= N !!\n");
    }
}

int main() {
    int n, m;

    printf("N-bonacci numbers\n");
    printf("N-bonacci number to calc?\n");
    scanf("%d", &n);
    printf("How many series's numbers?\n");
    scanf("%d", &m);
    bonacciseries(n, m);

    return 0;
} 

N-bonacci numbers
N-bonacci number to calc?
5
How many series's numbers?
5
0 0 0 0 1

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.