Fibonacci Series Program in Java | Armstrong Number in Java

Fibonacci series in Java

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.

There are two ways to write the fibonacci series program in java:

  • Fibonacci Series without using recursion
  • Fibonacci Series using recursion
Source Code:-

import java.util.Scanner;

public class FibonacciSeries {
    public static void main(String[] args) {
        int term, a = 0, b = 1, c;
        System.out.print("Enter term :");
        Scanner sc = new Scanner(System.in);
        term = sc.nextInt();
        for (int i = 1; i <= term; i++) {
            System.out.print(a + " ");
            c = a + b;
            a = b;
            b = c;
        }
    }

}

OUTPUT:-
Enter term :6 0 1 1 2 3 5 PS C:\Users\Tausif\Desktop\60DaysCodingChallenge>


 

Armstrong Number in Java

An Armstrong number is a positive m-digit number that is equal to the sum of the mth powers of their digits. It is also known as plus perfect.

Example of Armstrong Number:-
153: 13 + 53 + 33 = 1 + 125+ 27 = 153
31 = 3
126: 13 + 23 + 53 = 1 + 8 + 216 = 225 (Not an Armstrong Number)

Source Code:-

import java.util.Scanner;

class ArmstrongNumber {
    public static void main(String[] args) {
        int n, temp, arm = 0, rem;
        System.out.print("Enter any number:");
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        temp = n;
        while (n > 0) {
            rem = n % 10;
            arm = (rem * rem * rem) + arm;
            n = n / 10;

        }
        if (temp == arm)
          System.out.print("Armstrong Number");
        else
       System.out.print("Not Armstrong Number");

    }
}


OUTPUT:-
Enter any number:153 Armstrong Number PS C:\Users\Tausif\Desktop\60DaysCodingChallenge>
Enter any number:126
Not Armstrong Number
PS C:\Users\Tausif\Desktop\60DaysCodingChallenge> 


Palindrome Program in Java

Palindrome number in java: A palindrome number is a number that is same after reverse. For example 545, 151, 34543, 343, 171, 48984 are the palindrome numbers.

Source Code

import java.util.Scanner;

public class PalindromeNumber {
    public static void main(String[] args) {
        int n, r, sum = 0, temp;
        System.out.print("Enter any number: ");
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        temp = n;
        while (n > 0) {
            r = n % 10;
            sum = (sum * 10) + r;
            n = n / 10;

        }
        if (temp == sum) {
         System.out.print("Palindrome number");
        } else {
        System.out.print("Not Palindrome number");
        }
    }

}

OUTPUT:-
Enter any number: 151 Palindrome number PS C:\Users\Tausif\Desktop\60DaysCodingChallenge>
Enter any number: 143
Not Palindrome number
PS C:\Users\Tausif\Desktop\60DaysCodingChallenge>



Prime Number Program in Java

 Prime number is a number that is greater than 1 and divided by 1 or itself only. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.

Source Code:-

import java.util.Scanner;

public class PrimeNumber {
    public static void main(String[] args) {
        int n, count = 0;
        System.out.print("Enter any number:");
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            if (n % i == 0) {
                count++;
            }
        }
        if (count == 2) {
          System.out.print("Prime Number");
        } else {
          System.out.print("Not Prime number");
        }

    }

}

OUTPUT:-
Enter any number:5 Prime Number PS C:\Users\Tausif\Desktop\60DaysCodingChallenge>

Tausif

Hi! My name is TAUSIF AHMAD I have completed B.Tech in Computer Science from Maulana Azad National Urdu University Hyderabad. I am always ready to have new experiences meet new people and learn new things. 1. I am very interested in Frontend Development. 2. I love video editing and graphics designing. 3. I enjoy challenges that enables to grow. 4. I am part time Blogger.

2 Comments

Post a Comment
Previous Post Next Post