employer cover photo
employer logo

Pregunta de entrevista de NCR Corporation

Can you write the code to generate the Fibonacci sequence?

Respuesta de la entrevista

Anónimo

20 nov 2018

public class FB { public int maxValue, firstValue, secondValue; public FB(int maxValue,int firstValue, int secondValue){ this.maxValue= maxValue; this.firstValue= firstValue; this.secondValue= secondValue; } public void computeFibonacciSequence (){ while (firstValue <= maxValue) { int sum = firstValue + secondValue; System.out.print(firstValue + " + "+ secondValue+ " = "+ sum); firstValue = secondValue; secondValue = sum; } } public class MainMethod { public static void main(String[] args) { // TODO Auto-generated method stub java.util.Scanner tt = new java.util.Scanner(System.in); System.out.println("Enter Maximum Value"); int a = tt.nextInt(); System.out.println("Enter First Value"); int b = tt.nextInt(); System.out.println("Enter Second Value"); int c = tt.nextInt(); FB test = new FB (a,b, c); test.computeFibonacciSequence(); } } }