Powered by Blogger.
Showing posts with label fibonacci series program. Show all posts
Showing posts with label fibonacci series program. Show all posts

Java | Write a Fibonacci Series Program in Java

Fibonacci Program:-

Question-1: Write a Java program that will display the Fibonacci sequence is generated by adding the previous two terms. By starting with 1, 1, and 2, and continue up to the first 10 terms.

Program Solution:

public class Fibonacci{
  public static void main(String[]args){
  int x=1, y=1, z=0;
  for(int i =1; i<=10;i++){
  if(i==1){
  System.out.print(x+” ”+y);
  }
  z=x+y;
  System.out.print(” ”+z);
  x=y;
  y=z;
    }
  }
}

Java | For Loop Statement Program in java

For Loop Statement:

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times.

A for loop is useful when you know how many times a task is to be repeated.

for loop syntax:

for(initialization; condition; increment/decrement){
        statements;
}

For Loop Program:

public class Test {

   public static void main(String args[]) {

      for(int x = 20; x < 30; x = x + 1) {
         System.out.print("value of x : " + x );
         System.out.print("\n");
      }
   }
}

Output:
value of x : 20
value of x : 21
value of x : 22
value of x : 23
value of x : 24
value of x : 25
value of x : 26
value of x : 27
value of x : 28
value of x : 29