Java Program To Check Leap Year
Java Program To Check Leap Year
Here we will write a java program to check whether the input year is a leap
year or not. Before we see the program, lets see how to determine whether
a year is a leap year mathematically:
To determine whether a year is a leap year, follow these steps:
1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
2. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
3. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
4. The year is a leap year (it has 366 days).
5. The year is not a leap year (it has 365 days). Source of these steps.
import java.util.Scanner;
publicclassDemo {
int year;
Scanner scan = newScanner(System.in);
System.out.println("Enter any Year:");
year = scan.nextInt();
scan.close();
boolean isLeap = false;
if(year % 4 == 0)
{
if( year % 100 == 0)
{
if ( year % 400 == 0)
isLeap = true;
else
isLeap = false;
}
else
isLeap = true;
}
else {
isLeap = false;
}
if(isLeap==true)
System.out.println(year + " is a Leap Year.");
else
System.out.println(year + " is not a Leap Year.");
}
}
Output:
import java.util.Scanner;
publicclassDemo{
370 = 33 + 73 + o3
= 27 + 343 + 0
= 370
Let’s write this in a program:
To understand this Program you should have the knowledge of
following Java Programming topics:
number = num;
while (number != 0)
{
temp = number % 10;
total = total + temp*temp*temp;
number /= 10;
}
if(total == num)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
}
Output:
for(;number!=0;number /=10)
{
temp = number %10;
total = total + temp*temp*temp;
}
if(total == num)
System.out.println(num +" is an Armstrong number");
else
System.out.println(num +" is not an Armstrong number");
}
}
Output:
Ënter 3DigitNumber
371
371is an Armstrong number
import java.util.Scanner;
classPrimeCheck
{
publicstaticvoid main(String args[])
{
int temp;
boolean isPrime=true;
Scanner scan=newScanner(System.in);
System.out.println("Enter any number:");
//capture the input in an integer
int num=scan.nextInt();
scan.close();
for(int i=2;i<=num/2;i++)
{
temp=num%i;
if(temp==0)
{
isPrime=false;
break;
}
}
//If isPrime is true then the number is prime else not
if(isPrime)
System.out.println(num +" is a Prime Number");
else
System.out.println(num +" is not a Prime Number");
}
}
Output:
for(int i=2;i<=num/2;i++)
{
temp=num%i;
if(temp==0)
{
isPrime=false;
break;
}
}
with this:
int i=2;
while(i<= num/2)
{
if(num % i ==0)
{
isPrime =false;
break;
}
i++;
}
Program 1:
/**
* @author: BeginnersBook.com
* @description: Program to Calculate Area of square.Program
* will prompt user for entering the side of the square.
*/
import java.util.Scanner;
classSquareAreaDemo{
publicstaticvoid main (String[] args)
{
System.out.println("Enter Side of Square:");
//Capture the user's input
Scanner scanner =newScanner(System.in);
//Storing the captured value in a variable
double side = scanner.nextDouble();
//Area of Square = side*side
double area = side*side;
System.out.println("Area of Square is: "+area);
}
}
Output:
EnterSide of Square:
2.5
Area of Squareis:6.25
Program 2:
/**
* @author: BeginnersBook.com
* @description: Program to Calculate Area of square.
* No user interaction: Side of square is hard-coded in the
* program itself.
*/
classSquareAreaDemo2{
publicstaticvoid main (String[] args)
{
//Value specified in the program itself
double side =4.5;
//Area of Square = side*side
double area = side*side;
System.out.println("Area of Square is: "+area);
}
}
Output:
Area of Squareis:20.25
Java Program to Display Fibonacci Series
using loops
The Fibonacci sequence is a series of numbers where a number is the sum
of previous two numbers. Starting with 0 and 1, the sequence goes 0, 1, 1,
2, 3, 5, 8, 13, 21, and so on. Here we will write three programs to print
fibonacci series 1) using for loop 2) using while loop 3) based on the number
entered by user
If you are new to java, refer this java programming tutorial to start learning
from basics.
FibonacciSeries of 7 numbers:0112358
Example 2: Displaying Fibonacci Sequence using
while loop
publicclassJavaExample{
int i=1;
while(i<=count)
{
System.out.print(num1+" ");
int sumOfPrevTwo = num1 + num2;
num1 = num2;
num2 = sumOfPrevTwo;
i++;
}
}
}
Output:
FibonacciSeries of 7 numbers:0112358
Example 3: Program to display the fibonacci
series based on the user input
This program display the sequence based on the number entered by user.
For example – if user enters 10 then this program displays the series of 10
numbers.
import java.util.Scanner;
publicclassJavaExample{
int i=1;
while(i<=count)
{
System.out.print(num1+" ");
int sumOfPrevTwo = num1 + num2;
num1 = num2;
num2 = sumOfPrevTwo;
i++;
}
}
}
Output:
Factorial of 5is:120
import java.util.Scanner;
publicclassJavaExample{