Fibonacci series in Java


Java Fibonacci series is a number series, the sum of the previous two numbers result is the next number.
e.g: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc.
we can start our series from 0 or 1, it is our choice.

 There are two ways to write the Fibonacci series in Java:


=> Fibonacci series using for & while loop / without method recursion
=> Fibonacci series using method recursion

Let’s check the first Fibonacci series using for loop



public class FibonacciTest {     
       public static void main(String[] args) {       
              int number=10;
              int f1=0,f2=1,f3=0;       
              //System.out.print(f1); // print zero          
              for(int i=1; i<=number; i++){
                    
                     f3=f1+f2;
                     f1=f2;
                     f2=f3;
                     f3=f1;
                     System.out.print(" "+f3);
              }            
       }
}


Output:


1 1 2 3 5 8 13 21 34 55


Understanding the flow of the program


int number=10;
int f1=0,f2=1,f3=0;       
for(int i=1; i<=number; i++)
Repeat loop until condition false 

f3=f1+f2;
f1=f2;
f2=f3;
f3=f1;
System.out.println( f3);
f3=0+1
f1=1
f2=1
f3=1
1
f3=1+1
f1=1
f2=2
f3=1
1
f3=1+2
f1=2
f2=3
f3=2
2
f3=2+3
f1=3
f2=5
f3=3
3
f3=3+5
f1=5
f2=8
f3=5
5
f3=5+8
f1=8
f2=13
f3=8
8
f3 store next print value


f3 store current print value
Continue like this……

I hope you guys understood the flow of this program J

Fibonacci series using method recursion


Method recursion is a method that calls itself continuously.


public class FibonacciTest {     
        static int f1=0,f2=1,f3=0;   
        static void printSeries(int count){   
           if(count<=10){   
                f3=f1+f2;   
                f1=f2;   
                f2=f3;
                f3=f1;
                System.out.print(" "+f3);
                count++;
                printSeries(count);   
            }   
        }   
        public static void main(String args[]){   
        
        // System.out.print(f1);//print 0   
               printSeries(1);
        } 
}


The Fibonacci program using method recursion is given the same output as above

Output:



1 1 2 3 5 8 13 21 34 55


Here we took static variables and methods because we can call it directly in the main method without creating the object.

The flow of this program is the same as the above program but here we used method recursion and repeat the if condition until false.



7 comments:

  1. Thank you, good explanation

    ReplyDelete
  2. function processRequest(config) {
    console.log("Request is " + JSON.stringify(config.data));

    config = padWithZeroes(config);
    config = hashPin(config);
    var checkSum = getCheckSum(config.data, config);
    console.log('returned checksum is ' + checkSum);
    config.data.checkSum = "" + checkSum;
    config.data = encrypt(JSON.stringify(config.data));
    config.headers = {
    'Access-Control-Allow-Origin': true,
    "Accept": "application/json, text/plain, */*",
    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
    };
    console.log('The request object is ' + JSON.stringify(config));
    return config;
    }

    function encrypt(dataToEncrypt) {
    var iterationCount = 1000;
    var keySize = 128;
    // var plaintext = $('#plaintext').val();
    var passphrase = 'abc';

    var iv = CryptoJS.lib.WordArray.random(128 / 8)
    .toString(CryptoJS.enc.Hex);
    console.log("IV = " + iv);
    var salt = CryptoJS.lib.WordArray.random(128 / 8)
    .toString(CryptoJS.enc.Hex);
    console.log("SALT = " + salt);

    // var checkSum = CryptoJS.MD5(plaintext);
    // console.log("checkSum = " + checkSum);

    var aesUtil = new AesUtil(keySize, iterationCount);
    var ciphertext = aesUtil.encrypt(salt, iv, passphrase,
    dataToEncrypt);
    console.log("CIPHERTEXT = " + ciphertext);

    return serializeData({
    token1: iv,
    token2: salt,
    inputString: ciphertext
    });
    // var aesUtil = new AesUtil(128, 1000);
    // var decryptTxt = aesUtil.decrypt('27b06e2129bd1949ed6aad22d12f2dab', '4beceeff2a24df6ff0b24b42dcb80047', 'abc', 'fFvH5XB8SzixsNz2ebzEOA==');
    // console.log("decryptTxt = " + decryptTxt);


    }

    ReplyDelete
  3. https://www.codeproject.com/Articles/198646/Javascript-RSA-Encryption-and-Java-Decryption
    https://www.linkedin.com/pulse/jshtml5-java-encryption-using-aes-128bit256bit-subhadip-pal
    http://cryptojs.altervista.org/secretkey/doc/doc_aes_java.html
    http://drvijayy2k2.blogspot.in/2015/03/password-encrypt-in-javascript-and.html
    https://github.com/mpetersen/aes-example
    https://www.codeday.top/2017/07/14/27193.html
    https://coderanch.com/t/637480/Java-encryption-javascript-decryption
    http://www.code2learn.com/2011/06/encryption-and-decryption-of-data-using.html

    ReplyDelete
  4. http://www.jeejava.com/spring-boot-mvc-with-tiles-and-ajax/#fruitfulCommentError
    https://stackoverflow.com/questions/12197230/how-to-render-tile-views-in-spring-mvc-via-ajax
    https://www.springbyexample.org/examples/dynamic-tiles-spring-mvc-module.html
    https://stackoverflow.com/questions/18712719/integrating-spring-mvc-3-ajax-and-apache-tiles
    https://richardbarabe.wordpress.com/2009/02/23/apache-tiles-2-integration-with-spring-mvc/
    http://icodingclub.blogspot.in/2013/12/tutorial-tiles-3-with-spring-3-with-jsp.html
    http://www.stackoverflowtr.com/questions/25057478/tiles-wildcards-and-spring-mvc
    http://maciekmoczkowski.blogspot.in/2009/09/partial-rendering-with-spring-mvc-and.html
    https://stackoverflow.com/questions/36663038/spring-thymeleaf-header-and-footer-dynamic-includes

    ReplyDelete
  5. https://docs.spring.io/spring-webflow/docs/2.5.0.BUILD-SNAPSHOT/reference/html/spring-js.html
    https://github.com/Codingpedia/podcastpedia

    http://websystique.com/springmvc/spring-4-mvc-apache-tiles-3-annotation-based-example/ -- NEED TO CHECK CLEAR
    pablo.anecico.com/cgblog/10/15/Render-fragments-with-Spring-MVC-Tiles-and-Ajax

    ReplyDelete
  6. https://stackoverflow.com/questions/9266560/springmvc-jquery-tiles-and-partial-rerendering

    ReplyDelete
  7. https://www.marcus-povey.co.uk/2013/03/11/automatic-create-and-modified-timestamps-in-mysql/
    https://medium.com/@bengarvey/use-an-updated-at-column-in-your-mysql-table-and-make-it-update-automatically-6bf010873e6a
    https://dba.stackexchange.com/questions/56424/column-auto-updated-after-24-hours-in-mysql

    ReplyDelete