Java Programs(thread will be updated)
#1
1) Fibonacci series

Code:
Write a java program to print fibonacci series without using recursion and using recursion.

Input: 10

Output: 0 1 1 2 3 5 8 13 21 34

In fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. The first two numbers of fibonacci series are 0 and 1.

Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem.
Most computer programming languages support recursion by allowing a function to call itself within the program text.

Fibonacci Series without using recursion
Spoiler(Show)
Code:
class FibonacciSeries {

    public static void main(String args[]) {
        int n1 = 0, n2 = 1, n3, i, count = 10;
        System.out.print(n1 + " " + n2);  

        for (i = 2; i < count; ++i)
       {
            n3 = n1 + n2;
            System.out.print(" " + n3);
            n1 = n2;
            n2 = n3;
        }

    }
}

Fibonacci Series using recursion
Spoiler(Show)
Code:
class FibonacciSeries {

    static int n1 = 0, n2 = 1, n3 = 0;

    static void printFibonacci(int count) {
        if (count > 0) {
            n3 = n1 + n2;
            n1 = n2;
            n2 = n3;
            System.out.print(" " + n3);
            printFibonacci(count - 1);
        }
    }

    public static void main(String args[]) {
        int count = 10;
        System.out.print(n1 + " " + n2);
        printFibonacci(count - 2);
    }
}
Reply
#2
2) Prime number

Code:
Write a java program to check prime number.

Input: 44

Output: not prime number

Input: 7

Output: prime number

Prime number is a number that is greater than 1 and divided by 1 or itself.

Spoiler(Show)
Code:
import java.util.Scanner;

public class primeNumber {

    public static void isPrimeNumber(int number) {
        int prime = 1;
        for (int i = 2; i < number; i++) {

            if (number % i == 0) {
                System.out.println("Number is not prime");
                prime = 0;
                break;
            }
        }
        if (prime == 1) {
            System.out.println("number is prime");
        }
    }

    public static int chooseNumber() {
        Scanner in = new Scanner(System.in);
        System.out.println("insert a number to check if it's prime ");
        int number = in.nextInt();
        return number;
    }

    public static void main(String[] args) {
        isPrimeNumber(chooseNumber());
    }
}
Reply
#3
3) Palindrome number

Code:
Write a java program to check palindrome number.

Input: 329

Output: not palindrome number

Input: 12321

Output: palindrome number

A palindrome number is a number that is same after reverse.

Spoiler(Show)
Code:
import java.util.Scanner;

public class palindromeNumber {

    public static void isPalindrome(int number) {
        int rev_num = 0;
        int temp = number;
        while (number > 0) {
            rev_num = rev_num * 10 + number % 10;
            number = number / 10;
        }
        if (rev_num == temp) {
            System.out.println("number is palindrome");
        } else {
            System.out.println("number isn't palindrome");
        }
    }

    public static int chooseNumber() {
        Scanner in = new Scanner(System.in);
        System.out.println("insert a number to check if it's palindrome ");
        int number = in.nextInt();
        return number;
    }

    public static void main(String args[]) {
        isPalindrome(chooseNumber());
    }
}
Reply
#4
4) Factorial number

Code:
Write a java program to print factorial of a number.

Input: 5

Output: 120

Input: 6

Output: 720

Factorial of n is the product of all positive descending integers.

Spoiler(Show)
Code:
import java.util.Scanner;

public class factorialNumber {

    public static void getFactorial(int number) {
        int fact = 1;
        for (int i = number; i >= 2; i--) {
            fact = fact * i;
        }
        System.out.println("factorial of " + number + " is : " + fact);
    }

    public static int chooseNumber() {
        Scanner in = new Scanner(System.in);
        System.out.println("insert a number you want to get it's factorial : ");
        int number = in.nextInt();
        return number;
    }

    public static void main(String args[]) {
        getFactorial(chooseNumber());
    }
}
Reply
#5
(06-30-2015, 11:40 AM)Luffy-_- Wrote: 4) Factorial number

Code:
Write a java program to print factorial of a number.

Input: 5

Output: 120

Input: 6

Output: 720

Factorial of n is the product of all positive descending integers.

Spoiler(Show)
Code:
import java.util.Scanner;

public class factorialNumber {

    public static void getFactorial(int number) {
        int fact = 1;
        for (int i = number; i >= 2; i--) {
            fact = fact * i;
        }
        System.out.println("factorial of " + number + " is : " + fact);
    }

    public static int chooseNumber() {
        Scanner in = new Scanner(System.in);
        System.out.println("insert a number you want to get it's factorial : ");
        int number = in.nextInt();
        return number;
    }

    public static void main(String args[]) {
        getFactorial(chooseNumber());
    }
}

you should change a lot of your voids to make them return something
e.g. getFactorial should return an int, because the fact that the method starts with get implies that the method is returning something (which it isnt atm)

so your code could be rewritten to
System.out.println(getFactorial(chooseNumber()));

then you could also format how the factorial will be printed
Reply
#6
thx for the info @badvibes but can you explain why ? with a void method it print what i want without adding System.out.println in my main isn't that better ?
Reply
#7
(07-01-2015, 10:45 AM)Luffy-_- Wrote: thx for the info @badvibes but can you explain why ? with a void method it print what i want without adding System.out.println in my main isn't that better ?
getters should return something, your method names imply they return something but they do not (which breaks the principle of least astonishment) and what happens if later on you don't want it to print to the console in the manner it is in the method; you have to modify the method to do it.

if it's a getter, you can simply change it's format by changing it where System.out.println is called
Reply
#8
oh ok i get it thank you i appreciate your help Smile
Reply
#9
(06-29-2015, 12:06 PM)Terminal Wrote: 2) Prime number

Code:
Write a java program to check prime number.

Input: 44

Output: not prime number

Input: 7

Output: prime number

Prime number is a number that is greater than 1 and divided by 1 or itself.

Spoiler(Show)
Code:
import java.util.Scanner;

public class primeNumber {

    public static void isPrimeNumber(int number) {
        int prime = 1;
        for (int i = 2; i < number; i++) {

            if (number % i == 0) {
                System.out.println("Number is not prime");
                prime = 0;
                break;
            }
        }
        if (prime == 1) {
            System.out.println("number is prime");
        }
    }

    public static int chooseNumber() {
        Scanner in = new Scanner(System.in);
        System.out.println("insert a number to check if it's prime ");
        int number = in.nextInt();
        return number;
    }

    public static void main(String[] args) {
        isPrimeNumber(chooseNumber());
    }
}

Your function demonstrates the very naive way of checking if a number is prime or not.

1. You start at 2, and increment by 1 for the divisibility check. (Note: If divisibility by 2 fails, why should the number be evenly divisible by any other even number [i.e. 4, 6, 8... etc]?)
** 2 is actually the only number which is both even, and prime.
2. You don't need to check up to the number, you actually only need to check for divisibility up to the square root of the number

I'd also suggest you look into prime number generating algorithms such as the sieve of atkins, and others.
Reply
#10
thx for the tipsĀ bitm0de i'm really bad when it's about math but i'll take a look on those algorithms and fix it when i have some free time
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Java sockets 0xRar 3 8,759 12-24-2020, 12:36 PM
Last Post: 0xRar
  Useful resources to become a better Java programmer LOSTINSAUCE 2 14,369 12-21-2020, 09:41 AM
Last Post: 0xRar
  Java won't work properly QMark 2 12,261 02-12-2019, 12:25 AM
Last Post: QMark
  Words with friends(Cheat) Java ImArtisan 0 10,172 03-15-2018, 10:35 AM
Last Post: ImArtisan