# Field Manual for Hackers

## java Write a program using while loop to calculate and print the first ‘N’ Ex=10 Fibonacci number starts with (1,1),2, 3,5,8,13,21,34,55

1. import java.util.Scanner;

class Test{

public static void main(String ar[])
{

Scanner sc=new Scanner(System.in);
int x,n1=1,n2=1,n3=0,loop=1;
System.out.println("Enter Number to print Fibonacci Series");
x=sc.nextInt();
while (loop <= x)
{
n1=n2;
n2=n3;
n3=n1+n2;

System.out.printf(n3+",");
loop++;
}

}
}

2. import java.util.Scanner;

class Test
{

public static void main(String ar[])
{
int num3,num1,num2,terms,i=0;
System.out.print("Enter number of terms: ");
Scanner o= new Scanner(System.in);
terms=o.nextInt();
System.out.print("Enter the first number: ");
num1=o.nextInt();
System.out.print("Enter the second number: ");
num2=o.nextInt();
System.out.print("The series is: "+num1+","+num2);

while(i<terms-2)
{
num3=num1+num2;
System.out.print(","+num3);
num1=num2;
num2=num3;
i++;
}
}
}

3. This comment has been removed by the author.

4. //Fibonacci
import java.util.Scanner;
class Fibonacci
{
public static void main(String ar[])
{
int a=1,b=1,i=1,c=1,Number;
Scanner scan=new Scanner(System.in);
System.out.println("Enter the Number");
Number=scan.nextInt();
while(Number>=i)
{ a=b;
b=c;
c=a+b;
System.out.print(a+" ");
i++;
}
}
}

5. \$num1=\$_POST["num"];
\$i=2;
\$first=1;
\$second=1;
echo "\$first \$second";
while(\$i!=\$num1)
{
\$third=\$first+\$second;
\$first=\$second;
\$second=\$third;
echo " \$third";
\$i++;
}

6. import java.util.Scanner;
class Fibonacci
{
public static void main(String Args[])
{
int x=1,y=1,z=1,num,i=1;
System.out.println("enter the number");
Scanner sc=new Scanner(System.in);
num=sc.nextInt();
while(num>=i)
{
x=y;
y=z;
z=x+y;
System.out.println(x+" ");
i++;
}
}
}

7. import java.util.Scanner;
class fibonacci
{
public static void main(String ar[])
{
Scanner scan = new Scanner(System.in);
int a ,b,c,n;
a=1;
b=1;
System.out.println("how many values");
n = scan.nextInt();
int i=2;
System.out.print("1 " );
System.out.print("1");
while(i<n)
{
c=a+b;
System.out.print(" " +c);
a=b;
b=c;
i++;
}

}
}

8. import java.util.Scanner;
class Fibo{
public static void main(String ar[]){
Scanner obj=new Scanner(System.in);
int num,i=0,sum,a=1,b=1;
System.out.println("enter the number upto which you want the fibonacci");
num=obj.nextInt();
System.out.println(a);
System.out.println(b);
while(i!=num-2){

sum=a+b;
a=b;
b=sum;
System.out.println(sum);
i++;
}
}

}

9. import java.util.Scanner;
class Fibonacci{
public static void main(String ar[]){
int a=0,b=1,c=0,i,n;
Scanner scan=new Scanner(System.in);
System.out.println("ENTER THE NUMBER UPTO YOU WANT THE SERIES");
n=scan.nextInt();
System.out.println("THE SERIES IS= "+a);
System.out.println(+b);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;

System.out.println(+c);
}
}
}

10. import java.util.*;
class Fibonacci{
public static void main(String ar[]){
Scanner s= new Scanner(System.in);
int a=1,b=1,c=0,i=1,n;
System.out.println("enter the number of digits in series");
n=s.nextInt();
System.out.print("series: "+a+","+b);
while(i<=n-2)
{
c=a+b;
System.out.print(","+c);
a=b;
b=c;
i++;
}

}
}

11. package com.example.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Fibonacci extends Activity {

int n,first ,second,next,c;
Button btF1;
EditText etF1,etF2,etF3;
TextView tvF;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fibonacci);

btF1=findViewById(R.id.btF);
etF1=findViewById(R.id.etF);
etF2=findViewById(R.id.etF2);
etF3=findViewById(R.id.etF3);
tvF=findViewById(R.id.tvF);

btF1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
n=Integer.parseInt(etF1.getText().toString());
first=Integer.parseInt(etF2.getText().toString());
second=Integer.parseInt(etF3.getText().toString());

for (c=0;c<n;c++){
next=first+second;
first=second;
second=next;

}
tvF.setText("Result="+next);
}
});
}
}