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:
- Fibonacci series without using recursion
- Fibonacci series using recursion
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 + ", ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}
Fibonacci Series till 10 terms:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
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
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);
}
}
}
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
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.
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.multiply(BigInteger.valueOf(i));
}
System.out.printf("Factorial of %d = %d", num, factorial);
}
}
Output:
Factorial of 11 = 39916800
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
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.
public class SumNatural {
public static void main(String[] args) {
int num = 110, sum = 0;
for(int i = 1; i <= num; ++i)
{
sum += i;
}
System.out.println("Sum = " + sum);
}
}
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
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.
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.");
}
}
- is not an alphabet.
----- OR -----
n is an alphabet.
----- OR -----
V is an alphabet.
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
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
public class PositiveNegative {
public static void main(String[] args) {
double number = 11.1;
if (number < 0.0)
System.out.println(number + " is a negative number.");
else if ( number > 0.0)
System.out.println(number + " is a positive number.");
else
System.out.println(number + " is 0.");
}
}
11.1 is a positive number.
----- OR -----
-11.1 is a negative number.
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 multiply two floating point numbers
In the below program, we have two floating - point numbers 3.4f and 1.5f stored in variables first and second respectively.
Notice, we have used f after the numbers. This ensures the numbers are float, otherwise they will be assigned typed double.
first and second are then multiplied using the * operator and the result is stored in a new float variable product.
Finally, the result product is printed on the screen using println() function.
public class MultiplyTwoNumbers {
public static void main(String[] args) {
float first = 3.4f;
float second = 1.5f;
float product = first * second;
System.out.println("The product is: " + product);
}
}
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
Write a java program to check leap year
Leap year contains 366 days, which comes once every four years.
Every leap year corresponds to these facts:
- A century year is a year ending with 00. A century year is a leap year only if it is divisible by 400.
- A leap year (except a century year) can be identified if it is exactly divisible by 4.
- A century year should be divisible by 4 and 100 both.
- A non-century year should be divisible only by 4.
Let's find out whether a year is a leap year or not
public class Main {
public static void main(String[] args) {
int year = 2011;
boolean leap = false;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
leap = true;
else
leap = false;
}
else
leap = true;
}
else
leap = false;
if (leap)
System.out.println(year + " is a leap year.");
else
System.out.println(year + " is not a leap year.");
}
}
2011 is not a leap year
--- Another example ---
2012 is a leap year
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
Write a java program to find all roots of a quadratic equation
The standard form of an equation is:
ax2+bx+c=0
Here, a, b and c are real numbers and a can't be equal to 0.
We can calculate the root of a quadratic by using the formula
x=−b±b2−4ac−−−−−−−√2a
The ± sign indicates that there will be two roots:
root1=−b +b2−4ac−−−−−−−√2a
root2=−b -b2−4ac−−−−−−−√2a
The term b2-4ac is known as the determinant of a quadratic equation. It specifies the nature of roots. That is,
- if determinant > 0, roots are real and different
- if determinant < 0, roots are complex and different
- if determinant == 0, roots are real and equal
public class Main {
public static void main(String[] args) {
double a = 4.6, b = 2, c = 4.2;
double root1, root2;
double determinant = b * b - 4 * a * c;
if (determinant > 0) {
root1 = (-b + Math.sqrt(determinant)) / (2 * a);
root2 = (-b - Math.sqrt(determinant)) / (2 * a);
System.out.format("root1 = %.2f and root2 = %.2f", root1, root2);
}
else if (determinant == 0) {
root1 = root2 = -b / (2 * a);
System.out.format("root1 = root2 = %.2f;", root1);
}
else {
double real = -b / (2 * a);
double imaginary = Math.sqrt(-determinant) / (2 * a);
System.out.format("root1 = %.2f+%.2fi", real, imaginary);
System.out.format("\nroot2 = %.2f-%.2fi", real, imaginary);
}
}
}
root1 = -0.22+0.93i
root2 = -0.22-0.93i
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