Fibonacci Sequence Using Methods in Java

import java.util.Scanner;

/* AniBlogger12
 * Methods 
 */
public class methods7 {
	// method for returning fibonacci sequence
	public static int fibonacci(int num) {
		// if the number is 0, return 0
		if (num == 0) {
			return 0;
			// or else if the number is 1, return 1
		} else if (num == 1) {
			return 1;
			// else return the sum of the 2 last numbers to get the next term in the
			// sequence
		} else {
			return fibonacci(num - 1) + fibonacci(num - 2);
		}
	}

	// main
	public static void main(String[] args) {
		// get keyboard
		Scanner kb = new Scanner(System.in);
		// prompt user
		System.out.println("Please enter the number of terms you want to display the fibonacci series:");
		// user input
		int num = kb.nextInt();
		// result variable
		int result = 0;
		// for loop
		for (int i = 0; i < num; i++) {
			// result is equal to the fibonacci of (i)
			result = fibonacci(i);
			// print out result
			System.out.println(result);
		}

	}
}

Binary Addition

import java.util.Scanner;

/*AniBlogger12
 * Binary Addition Program
 */
public class binaryAddition {

	public static void main(String[] args) {
		// get keyboard
		Scanner keyboard = new Scanner(System.in);
		// prompt user
		System.out.println("Enter a binary number.");
		// user integer number input
		int num1 = keyboard.nextInt();
		// convert the int number to a string
		String number1 = Integer.toString(num1);
		// make length variable and set it equal to the length of the number entered
		int length1 = number1.length();
		// prompt user asking for another binary number
		System.out.println("Enter another binary number.");
		// user input for another binary number
		int num2 = keyboard.nextInt();
		// convert the int number to a string
		String number2 = Integer.toString(num2);
		// make length variable and set it equal to the length of the number entered
		int length2 = number2.length();
		// add the two binary numbers like adding normal numbers
		int add = num1 + num2;
		// convert sum to a string (this is not the answer)
		String notanswer = Integer.toString(add);
		// add a zero to the string variable
		notanswer = "0" + notanswer;
		// find length of this
		int length = notanswer.length();
		// make a string digit variable
		String digit = "";
		// make an answer variable
		String answer = "";
		// make and initialize a carry variable
		int carry = 0;
		// if the length of the first num is greater than the length of the second num
		if (length1 > length2) {
			// for loop
			// iterate forward
			for (int i = 0; i <= length1; i++) {
				// take part of the digit from length - i - 1th position to length -ith position
				digit = notanswer.substring(length - i - 1, length - i);
				// if first digit is 0 and there is nothing to carry over
				if (digit.equals("0") && carry == 0) {
					// add a 0 to the answer
					answer = "0" + answer;
					// carry stays 0
					carry = 0;

				}
				// or else if the first digit is 0 and carry is 1
				else if (digit.equals("0") && carry == 1) {
					// add 1 to the answer
					answer = "1" + answer;
					// carry becomes 0
					carry = 0;

				}
				// or else if the first digit is 1 and carry is 0
				else if (digit.equals("1") && carry == 0) {
					// add 1 to the answer
					answer = "1" + answer;
					// carry over 0
					carry = 0;

				}
				// else if the first digit is 1 and carry is 1
				else if (digit.equals("1") && carry == 1) {
					// add 0 to answer
					answer = "0" + answer;
					// carry over 1
					carry = 1;

				}
				// else if the first digit is 2 and carry is 0
				else if (digit.equals("2") && carry == 0) {
					// add 0
					answer = "0" + answer;
					// carry over 1
					carry = 1;

				}
				// else if the first digit is 2 and carry is 1
				else if (digit.equals("2") && carry == 1) {
					// add 1 to answer
					answer = "1" + answer;
					// carry over 1
					carry = 1;

				}

			}

		}
		// or else
		// if first number is smaller than second number
		else if (length1 < length2) {
			// for-loop
			for (int i = 0; i <= length2; i++) {
				// same as last time
				digit = notanswer.substring(length - i - 1, length - i);
				// same as before
				if (digit.equals("0") && carry == 0) {

					answer = "0" + answer;

					carry = 0;

				}

				else if (digit.equals("0") && carry == 1) {

					answer = "1" + answer;

					carry = 0;

				}

				else if (digit.equals("1") && carry == 0) {

					answer = "1" + answer;

					carry = 0;

				}

				else if (digit.equals("1") && carry == 1) {

					answer = "0" + answer;

					carry = 1;

				}

				else if (digit.equals("2") && carry == 0) {

					answer = "0" + answer;

					carry = 1;

				}

				else if (digit.equals("2") && carry == 1) {

					answer = "1" + answer;

					carry = 1;

				}

			}

		}
		// or else if both are equal
		else if (length1 == length2) {
			// for-loop
			for (int i = 0; i <= length2; i++) {
				// same as before
				digit = notanswer.substring(length - i - 1, length - i);

				if (digit.equals("0") && carry == 0) {

					answer = "0" + answer;

					carry = 0;

				}

				else if (digit.equals("0") && carry == 1) {

					answer = "1" + answer;

					carry = 0;

				}

				else if (digit.equals("1") && carry == 0) {

					answer = "1" + answer;

					carry = 0;

				}

				else if (digit.equals("1") && carry == 1) {

					answer = "0" + answer;

					carry = 1;

				}

				else if (digit.equals("2") && carry == 0) {

					answer = "0" + answer;

					carry = 1;

				}

				else if (digit.equals("2") && carry == 1) {

					answer = "1" + answer;

					carry = 1;

				}

			}

		}
		// print out the final sum
		System.out.println(answer);

	}

}

Looping Examples-Graphics with the Turtle Library

# Example #1:
# import the turtle method
import turtle
# get the Screen
window = turtle.Screen
# initialize Turtle Variable
bob = turtle.Turtle()
# Make speed fast (0 is the fastest)
bob.speed(0)
# for loop
# start = 0, stop = 250, iteration = 1 (you don't have to put this because if no iteration is typed in the range, it assumes the iteration is 1.
for i in range(0,250):
	# move bob forward and iterate by 1 to make bob loop
	bob.forward(i+1)
        # we are trying to make the base hexogonal and iterate it
        # we use bob.left(60) to turn bob 60 degrees (the angle of each 
        side of a hexagon)
	bob.left(60)

Output:

# Example #2:
# import Turtle Method
import turtle
# get Screen
window = turtle.Screen
# Initialize Turtle (I named mine 'bob')
bob = turtle.Turtle()
# make speed fast (zero = fastest)
bob.speed(0)
# for-loop
# start = 0, end = 500, iteration = 1
for i in range(0,500):
	# move bob forward and iterate by 1
	bob.forward(i+1)
        # move bob left by 65 degrees (always go a couple of degrees off from the angle of a side of a hexagon if you want to create the hexagonal spiral like shape
	bob.left(65)

Output:

Design a site like this with WordPress.com
Get started