Skip to content

Instantly share code, notes, and snippets.

@marioluan
Created October 30, 2012 18:35
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marioluan/3982122 to your computer and use it in GitHub Desktop.
Save marioluan/3982122 to your computer and use it in GitHub Desktop.
Programa em Java para imprimir a série de FIBONACCI até o X termo
/* Construir um algoritmo para imprimir a série de FIBONACCI até o 10º termo.
* A série de FIBONACCI é formada pela sequência:
*0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55...
*/
package cap14;
public class Exercicio02 {
public static void main(String[] args){
int num1 = 1, num2 = 0;
System.out.println(num2);
System.out.println(num1);
for(int i = 0; i < 8; i++){
num1 = num1 + num2;
num2 = num1 - num2;
System.out.println(num1);
}
}
}
@alairneto
Copy link

Muito bom!

@matheusrenner22
Copy link

Very Good!

@PuppeJr
Copy link

PuppeJr commented Jul 30, 2021

Thank you so much yeah

@marcioauau2005
Copy link

marcioauau2005 commented Sep 29, 2021

Eu gostaria de saber como faz para colocar a sequência na horizontal.

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package exercicio13;

/**
*

/* Imprima os 30 primeiros elementos da série de Fibonacci. A sé-
rie é a seguinte: 1, 1, 2, 3, 5, 8, 13, 21 etc. Para calculá-la, o primei-
ro e segundo elementos valem 1, daí por diante, cada elemento
vale a soma dos dois elementos anteriores (ex: 8 = 5 + 3).
*/

public class Exercicio13 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    
    int n1 = 1;
    int n2 = 0;
    
         
    for (int i = 1; i <= 30; i++){
      if (i < 2){
          
          System.out.println(1);
          
      } else {
        n1 = n1 + n2;
        n2 = n1 - n2;
       
    
    System.out.println(n1);
      }
    }
    }
          
}

@EvertonMachado1
Copy link

/**

  • @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here

    int n1 = 1;
    int n2 = 0;

    for (int i = 1; i <= 30; i++){
    if (i < 2){

       System.out.println(1);
    

    } else {
    n1 = n1 + n2;
    n2 = n1 - n2;

    /* O comando System.out.println pula uma linha e o comando system.out.print mantem na mesma.
    então se utilizar o comando print vai manter mas os valores vão estar colados e por isso você tem que
    adicionar um espaço a caixa de texto ficando assim:
    */
    System.out.print(n1 +" ");
    }
    }
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment