Fraction sequence with Fibonacci and primes

Asked

Viewed 652 times

0

h. Ask the user for the amount of terms he wants and print the sequence below and the sum of terms. 1 + 1 + 2 + 3 + 5 + 8 +… 2 3 5 7 11 13 * above Fibonacci sequence and below Primes sequence.

I have this exercise to do, but I’m not getting it. When I play the two together, it changes everything. But if I do it separately, I can do the sequence perfectly.

How to solve?

My code so far: http://pastebin.com/ChG2VDTv

#include "stdio.h"

main(){
    int qtd, n1=0, n2=1, f=0, p=2, cont=0, j;
    int a = 0;

    printf("Entre com a quantidade de termos: ");
    scanf("%d", &qtd);

    for(int i=0; i<=qtd; i++){
        cont=0;

        for(j=1; j<=i; j++){
            if (i%j==0){
                cont++;
            }
        }

        if(i!=0){
            if(cont==2){
                //printf("%d ", i);
                printf("%d/%d + ", f, i);
            }
        }

        /*if(i!=0){
            printf("%d + ", f);
        }*/

            n1 = n2;
            n2 = f;
            f = n1+n2;
        }

    return 0;
}
  • 1

    It would be interesting to show working separately. In thesis has no secret to join. Unless I don’t understand something, you make one list and then you make the other, so it shouldn’t be hard to do together.

  • @Michael, I reversed the issue. This is not a forum, and you don’t change the question to [solved]. You can post your solution as a reply and mark as accepted, or if any of the answers answered, just mark the respective answer as accepted (it’s the green V right under the answer score): Here are some cool things to understand the functioning of the site: [help] and Community FAQ.

1 answer

2

Instead of calculating and printing; calculates and puts in arrays. Then prints the arrays.

int arrf[10]; // array para fibonacci
int arrp[10]; // array para primos

// calcula e guarda numeros
for (int k = 0; k < 10; k++) {
    arrf[k] = calculaf(k); // falta a definicao
    arrp[k] = calculap(k); // das funcoes calcula*
}

// imprime linha com numeros fibanacci
for (int k = 0; k < 10; k++) printf("%4d ", arrf[k]);
puts(""); // fim de linha

// imprime linha com numeros primos
for (int k = 0; k < 10; k++) printf("%4d ", arrp[k]);
puts(""); // fim de linha

Browser other questions tagged

You are not signed in. Login or sign up in order to post.