Programmator
Programmator
  • Home
  • Download
  • Social
    • YouTube
    • Instagram
    • Facebook
    • Telegram
  • Features
    • C/C++
    • Java
  • Contact Us

Write a java program to display Fibonacci series

In this program, Fibonacci series means to next number is the sum 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:

  1. Fibonacci series without using recursion
  2. Fibonacci series using recursion

  • Program:

class Main {
  public static void main(String[] args) {

    int n = 10, firstTerm = 0, secondTerm = 1;
    System.out.println("Fibonacci Series till " + n + " terms:");

    for (int i = 1; i <= n; ++i) {
      System.out.print(firstTerm + ", ");

      // compute the next term
      int nextTerm = firstTerm + secondTerm;
      firstTerm = secondTerm;
      secondTerm = nextTerm;
    }
  }
}

  • Output:

Fibonacci Series till 10 terms:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here
Java program to check whether a given number is even or odd Here
Java program to check whether an alphabet is vowel or consonant Here 
Java program to find the largest among three numbers Here
Java program to find all roots of a quadratic equation Here
Java program to check leap year Here
Write a java program to multiply two floating point numbers Here
Java program to check whether a number is positive or negative Here
Java program to check whether a character is alphabet or not Here
Java program to calculate the sum of natural numbers Here
Java program to find factorial of a number Here
Java program to generate multiplication table Here

How to generate multiplication table in java?

In this program, we will understand the logic to print table and also implement that logic in java programs.

A table is a sequence of number that are generated using multiplication. We enter an integer as input of which we want to print the table. After that, we multiply the entered by 1 to 10, respectively on by one.

Suppose we have entered 5 as input then the table of 5 will be:

5*1 = 5
5*2= 10
5*3 =15

  • Program:

public class MultiplicationTable {

    public static void main(String[] args) {

        int num = 8;
        for(int i = 1; i <= 10; ++i)
        {
            System.out.printf("%d * %d = %d \n", num, i, num * i);
        }
    }
}

  • Output:

8 * 1 = 8 
8 * 2 = 16 
8 * 3 = 24 
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here
Java program to check whether a given number is even or odd Here
Java program to check whether an alphabet is vowel or consonant Here 
Java program to find the largest among three numbers Here
Java program to find all roots of a quadratic equation Here
Java program to check leap year Here
Write a java program to multiply two floating point numbers Here
Java program to check whether a number is positive or negative Here
Java program to check whether a character is alphabet or not Here
Java program to calculate the sum of natural numbers Here
Java program to find factorial of a number Here

Write a java program to find factorial of a number

Here, we will used for loop to loop through all numbers between 1 and the given number num (10), and the product of each number till num is stored in a variable factorial.

We will used long instead of int to store large results of factorial. However, it's still not big enough to store the value of bigger numbers (say 100).

For results that cannot be stored in a long variable, we use BigInteger variable in java.math library.

  • Program:

import java.math.BigInteger;

public class Factorial {

    public static void main(String[] args) {

        int num = 11;
        BigInteger factorial = BigInteger.ONE;
        for(int i = 1; i <= num; ++i)
        {
            // factorial = factorial * i;
            factorial = factorial.multiply(BigInteger.valueOf(i));
        }
        System.out.printf("Factorial of %d = %d", num, factorial);
    }
}

Output:

Factorial of 11 = 39916800

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here
Java program to check whether a given number is even or odd Here
Java program to check whether an alphabet is vowel or consonant Here 
Java program to find the largest among three numbers Here
Java program to find all roots of a quadratic equation Here
Java program to check leap year Here
Write a java program to multiply two floating point numbers Here
Java program to check whether a number is positive or negative Here
Java program to check whether a character is alphabet or not Here
Java program to calculate the sum of natural numbers Here

Write a java program to calculate the sum of natural numbers using for loop

The natural numbers are the numbers that include all the positive integers from 1 to infinity. For example, 1, 2, 3, 4, 5, ...., n. When we add these numbers together, we get the sum of natural numbers.

In this section, we will create the following programs:

  • Java program to find the sum of the first 100 natural numbers
  • Java program to find the sum of n natural numbers
  • Java program to find the sum of n natural numbers using the function

We can also find the sum of n natural number using the mathematical formula:

Sum of n natural numbers = n*(n+1)/2

Suppose we want to find the sum of the 100 natural numbers. By putting the value in the above formula, we get:

Sum = 100*100+1/2 = 100*50.5 = 5050

Here, we are going to use for loop to find the sum of natural numbers.

  • Program:

public class SumNatural {

    public static void main(String[] args) {

        int num = 110, sum = 0;

        for(int i = 1; i <= num; ++i)
        {
            // sum = sum + i;
            sum += i;
        }

        System.out.println("Sum = " + sum);
    }
}

  • Output:

Sum = 6105

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here
Java program to check whether a given number is even or odd Here
Java program to check whether an alphabet is vowel or consonant Here 
Java program to find the largest among three numbers Here
Java program to find all roots of a quadratic equation Here
Java program to check leap year Here
Write a java program to multiply two floating point numbers Here
Java program to check whether a number is positive or negative Here
Java program to check whether a character is alphabet or not Here

Write a java program to check whether a character is alphabet or not

Here, the char variable stores the ASCII value of a character (number between 0 and 127) rather than the character itself.

The ASCII value of lowercase alphabets are from 97 to 122. And the ASCII value of uppercase alphabets are from 65 to 90. That is, alphabet a is stored as 97 and alphabet z is stored as 122. similarly, alphabet A is stored as 65 and alphabet Z is stored as 90.

Now, when we compare variable c between 'a' to 'z' and 'A' to 'Z', the variable is compared with the ASCII value of the alphabets 97 to 122 and 65 to 90 respectively.

since the ASCII value of * does not fall in between the ASCII value of alphabets. Hence, the program outputs * is not an alphabet.

  • Program:

public class Alphabet {

    public static void main(String[] args) {

        char c = '-';

        if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
            System.out.println(c + " is an alphabet.");
        else
            System.out.println(c + " is not an alphabet.");
    }
}

  • Output:

- is not an alphabet.

----- OR -----

n is an alphabet.

----- OR -----

V is an alphabet.
 
  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here
Java program to check whether a given number is even or odd Here
Java program to check whether an alphabet is vowel or consonant Here 
Java program to find the largest among three numbers Here
Java program to find all roots of a quadratic equation Here
Java program to check leap year Here
Write a java program to multiply two floating point numbers Here
Java program to check whether a number is positive or negative Here

Write a java program to check if number is positive or negative

Here, we will write java program to checks whether the specified number is positive or negative using the if else statements.

  • If a number is less than zero, then it is a negative number
  • If a number is greater than zero, then it is a negative number
  • If a number is equal to zero, then it is neither negative nor positive number

  • Program:

public class PositiveNegative {

    public static void main(String[] args) {

        double number = 11.1;

        // true if number is less than 0
        if (number < 0.0)
            System.out.println(number + " is a negative number.");

        // true if number is greater than 0
        else if ( number > 0.0)
            System.out.println(number + " is a positive number.");

        // if both test expression is evaluated to false
        else
            System.out.println(number + " is 0.");
    }
}

  • Output:


11.1 is a positive number.

----- OR -----

-11.1 is a negative number.

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here
Java program to check whether a given number is even or odd Here
Java program to check whether an alphabet is vowel or consonant Here
Newer Posts Older Posts Home

ABOUT

I could look back at my life and get a good story out of it. It's a picture of somebody trying to figure things out.

SUBSCRIBE & FOLLOW

POPULAR POSTS

  • Write a C program to enter a distance in to kilometer and convert it in to meter, feet, inches and centimeter
  • Write a C program to computer Fahrenheit from centigrade ( f=1.8*c+32 )
  • Write a C program to interchange two numbers
  • Write a C program to read and store the roll no and marks of 20 students using array
  • Write a C program to evaluate the series sum=1-x+x^2/2!-x^3/3!+x^4/4!......-x^9/9!

Advertisement

👆 Shopping 👆

Powered by Blogger

Archive

  • February 20241
  • January 20242
  • November 20234
  • October 20236
  • September 20236
  • August 20235
  • April 20231
  • March 20231
  • October 20221
  • August 20223
  • July 20222
  • May 20223
  • April 20222
  • March 20221
  • January 20221
  • December 20216
  • November 20214
  • October 20211
  • September 20216
  • August 202127
  • July 20216

Report Abuse

Copyright

  • Home
  • About us
  • Privacy Policy
  • Disclaimer
  • Contact Us

About Me

Nilesh Patel
View my complete profile

Copyright © Programmator. Designed by OddThemes