Skip to content

Instantly share code, notes, and snippets.

@h26k2
Created September 17, 2019 15:45
Show Gist options
  • Save h26k2/89b45ab3d85583a5e92cc97542961a4b to your computer and use it in GitHub Desktop.
Save h26k2/89b45ab3d85583a5e92cc97542961a4b to your computer and use it in GitHub Desktop.
OOP-Assignment for printing Fibonacci series and swapping two numbers using methods
package work;
import java.util.Scanner;
public class Work {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter num1 : ");
int n1 = input.nextInt();
System.out.println("Enter num2 : ");
int n2 = input.nextInt();
swapWithTemp(n1,n2);
swapWithoutTemp(n1,n2);
System.out.println("Enter max no of range you want to display fibonacci series : ");
int r = input.nextInt();
fibonacciSeries(r);
}
public static void fibonacciSeries(int range){
int num1 , num2 , next;
num1 = 0;
num2 = 1;
System.out.println("**************************** FIBONACCI SERIES IS ********************");
System.out.println(num1);
System.out.println(num2);
for(int i=1 ; i<=range-2 ; i++){
next = num1 + num2;
System.out.println(next);
num1 = num2;
num2 = next;
}
}
public static void swapWithTemp(int a , int b){
System.out.println("******************** SWAPPING WITH TEMPORARY VARIABLE ******************");
int temp;
System.out.println("value of a " + a);
System.out.println("value of b "+b);
temp = a;
a = b;
b = temp;
System.out.println("value of a " + a);
System.out.println("value of b "+b);
}
public static void swapWithoutTemp(int a ,int b){
System.out.println("************** SWAPPING WITHOUT TEMP ************");
System.out.println("Value of a "+a);
System.out.println("Value of b "+b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("Value of a"+a);
System.out.println("Value of b"+b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment