employer cover photo
employer logo
employer logo

NCR Corporation

Now known as NCR Voyix

Is this your company?

NCR Corporation interview question

Can you write the code to generate the Fibonacci sequence?

Interview Answer

Anonymous

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(); } } }