# Field Manual for Hackers

## java - Generate a fibonnaci series upto N and count the odd numbers.

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,sum=0;
System.out.println("Enter Number to print Fibonacci Series");
x=sc.nextInt();
while (loop <= x)
{
n1=n2;
n2=n3;
n3=n1+n2;
sum+=n3;
System.out.printf(n3+",");
loop++;
}

System.out.println();
System.out.println("Sum = "+sum);
}
}

2. import java.util.Scanner;

class Test
{

public static void main(String ar[])
{
int num3,num1,num2,terms,i=0,sum=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);
sum=num1+num2;
while(i<terms-2)
{
num3=num1+num2;
System.out.print(","+num3);
num1=num2;
num2=num3;
i++;
sum+=num3;
}
System.out.print("\n The sum of the series is: "+sum);
}
}

3. //Fibonacci
import java.util.Scanner;
class FibonacciN
{
public static void main(String ar[])
{
int a=1,b=1,i=1,c=1,Number,Total=0;
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+" ");
Total=Total+a;
i++;
}
System.out.print("Fibonacci series upto N="+Total);
}
}

4. import java.util.Scanner;
class FibSeries
{
public static void main(String Args[])
{
int x=1,y=1,z=1,num,i=1,sum=0;
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;
sum+=z;
System.out.println(x+" ");
i++;
}
System.out.println("sum="+sum);
}
}

5. import java.util.Scanner;
class A
{
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
int a=0,b=1,c;
int i;
System.out.println(a+""+b);
for(i=2;i<n;i++)
{
c=a+b;
System.out.println(""+c);
a=b;
b=c;
}
}
}

6. import java.util.Scanner;
class fib1 {
public static void main(String ar[])
{
Scanner scan = new Scanner(System.in);
int a ,b,c,n;
a=0;
b=1;
System.out.println("how many values");
n = scan.nextInt();
int i=2;
int sum=0;

System.out.print("0 " );
System.out.print("1");
while(i<n)
{
c=a+b;
System.out.print(" " +c);
if(c%2==0)
{ sum+=c;
}
a=b;
b=c;
i++;

}
System.out.println();
System.out.println("the sum of odd nos is :" +sum);

}
}

output:

how many values
10
0 1 1 2 3 5 8 13 21 34
the sum of odd nos is :44

7. import java.util.Scanner;
class Fibocount{
public static void main(String ar[]){
Scanner obj=new Scanner(System.in);
int num,i=0,sum,a=1,b=1,count=0;
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++;
if(sum%2!=0)
count++;
}
System.out.println("odd nos are"+(count+1));
}
}

8. import java.util.Scanner;
class CountFibo{
public static void main(String ar[]){
int a=0,b=1,c=0,i,n,count=2;
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;
count++;
System.out.println(+c);
}
System.out.println(+count);
}
}

9. import java.util.Scanner;

class FibbonicaUptoN{

public static void main(String ar[]){

Scanner scan=new Scanner(System.in);
int num,first=1,second=1,sum=0;
System.out.println("Enter number");
num=scan.nextInt();

for(int i=1;i<=num;i++){
sum=first+second;
first=second;
second=sum;

}
System.out.println("Result="+sum);

}

}