0% found this document useful (0 votes)
352 views12 pages

Java Program To Check Leap Year

The document discusses several Java programs that demonstrate common programming concepts and tasks. It includes programs to check if a year is a leap year, multiply two numbers, check if a number is Armstrong, check if a number is prime, calculate the area of a square, and display the Fibonacci series using loops. Each program is accompanied by an explanation of the logic and code.

Uploaded by

shankariravi 018
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
352 views12 pages

Java Program To Check Leap Year

The document discusses several Java programs that demonstrate common programming concepts and tasks. It includes programs to check if a year is a leap year, multiply two numbers, check if a number is Armstrong, check if a number is prime, calculate the area of a square, and display the Fibonacci series using loops. Each program is accompanied by an explanation of the logic and code.

Uploaded by

shankariravi 018
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 12

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.

Example: Program to check whether the input


year is leap or not
Here we are using Scanner class to get the input from user and then we
are using if-else statements to write the logic to check leap year. To
understand this program, you should have the knowledge of following
concepts of Core Java Tutorial:
→ If-else statement
→ Read input number in Java program

import java.util.Scanner;
publicclassDemo {

publicstaticvoid main(String[] args) {

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:

Enter any Year:


2001
2001isnot a LeapYear.

Java Program to Multiply Two Numbers


When you start learning java programming, you get these type of problems
in your assignment. Here we will see two Java programs, first program takes
two integer numbers (entered by user) and displays the product of these
numbers. The second program takes any two numbers (can be integer or
floating point) and displays the result.

Example 1: Program to read two integer and


print product of them
This program asks user to enter two integer numbers and displays the
product. To understand how to use scanner to take user input, checkout this
program: Program to read integer from system input.

import java.util.Scanner;

publicclassDemo{

publicstaticvoid main(String[] args){

/* This reads the input provided by user


* using keyboard
*/
Scanner scan =newScanner(System.in);
System.out.print("Enter first number: ");

// This method reads the number provided using keyboard


int num1 = scan.nextInt();

System.out.print("Enter second number: ");


int num2 = scan.nextInt();
// Closing Scanner after the use
scan.close();

// Calculating product of two numbers


int product = num1*num2;

// Displaying the multiplication result


System.out.println("Output: "+product);
}
}
Output:

Enter first number:15


Enter second number:6
Output:90

Java Program to Check Armstrong


Number
Here we will write a java program that checks whether the given number is
Armstrong number or not. We will see the two variation of the same
program. In the first program we will assign the number in the program itself
and in second program user would input the number and the program will
check whether the input number is Armstrong or not.

Before we go through the program, lets see what is an Armstrong number. A


number is called Armstrong number if the following equation holds true for
that number:

xy..z = xn+ yn+.....+ zn


where n denotes the number of digits in the number

For example this is a 3 digit Armstrong number

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:

1. Java while loop


2. Java if..else-if
Example 1: Program to check whether the
given number is Armstrong number
publicclassJavaExample {

publicstaticvoid main(String[] args) {

int num = 370, number, temp, total = 0;

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:

370is an Armstrong number


In the above program we have used while loop, However you can also use
for loop. To use for loop replace the while loop part of the program with this
code:

for( ;number!=0;number /= 10){


temp = number % 10;
total = total + temp*temp*temp;
}

Example 2: Program to check whether the


input number is Armstrong or not
import java.util.Scanner;
publicclassJavaExample{

publicstaticvoid main(String[] args){

int num, number, temp, total =0;


System.out.println("Ënter 3 Digit Number");
Scanner scanner =newScanner(System.in);
num = scanner.nextInt();
scanner.close();
number = num;

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

Java program to check prime number


The number which is only divisible by itself and 1 is known as prime
number, for example 7 is a prime number because it is only divisible by
itself and 1.
This program takes the number (entered by user) and then checks whether
the input number is prime or not. The program then displays the result. If
you are looking for a program that displays the prime number between two
intervals then see: Java program to display prime numbers between 1 to n.

Example: Program to check whether input


number is prime or not
To understand this program you should have the knowledge of for loop, if-
else statements and break statement.

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:

Enter any number:


19
19is a PrimeNumber
Output 2:

Enter any number:


6
6isnot a PrimeNumber
You can also use while loop to check the prime number:
Just replace this part of the code in above program:

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++;
}

Java program to calculate area of Square


In this tutorial we will learn how to calculate area of Square. Following are
the two ways to do it:
1) Program 1: Prompting user for entering the side of the square
2) Program 2: Side of the square is specified in the program’ s source code.

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

To understand these programs, you should have the knowledge of for


loop and while loop.

If you are new to java, refer this java programming tutorial to start learning
from basics.

Example 1: Program to print fibonacci series


using for loop
publicclassJavaExample{

publicstaticvoid main(String[] args){

int count =7, num1 =0, num2 =1;


System.out.print("Fibonacci Series of "+count+" numbers:");

for(int i =1; i <= count;++i)


{
System.out.print(num1+" ");

/* On each iteration, we are assigning second number


* to the first number and assigning the sum of last two
* numbers to the second number
*/
int sumOfPrevTwo = num1 + num2;
num1 = num2;
num2 = sumOfPrevTwo;
}
}
}
Output:

FibonacciSeries of 7 numbers:0112358
Example 2: Displaying Fibonacci Sequence using
while loop
publicclassJavaExample{

publicstaticvoid main(String[] args){


int count =7, num1 =0, num2 =1;
System.out.print("Fibonacci Series of "+count+" numbers:");

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{

publicstaticvoid main(String[] args){

int count, num1 =0, num2 =1;


System.out.println("How may numbers you want in the sequence:");
Scanner scanner =newScanner(System.in);
count = scanner.nextInt();
scanner.close();
System.out.print("Fibonacci Series of "+count+" numbers:");

int i=1;
while(i<=count)
{
System.out.print(num1+" ");
int sumOfPrevTwo = num1 + num2;
num1 = num2;
num2 = sumOfPrevTwo;
i++;
}
}
}
Output:

How may numbers you want in the sequence:


6
FibonacciSeries of 6 numbers:011235
Java Program to Find Factorial using For
and While loop
We will write three java programs to find factorial of a number. 1) using for
loop 2) using while loop 3) finding factorial of a number entered by user.
Before going through the program, lets understand what is factorial:
Factorial of a number n is denoted as n! and the value of n! is: 1 * 2 * 3 * …
(n-1) * n

The same logic we have implemented in our programs using loops. To


understand these programs you should have a basic knowledge of following
topics of java tutorials:

 For loop in Java


 Java – while loop

Example: Finding factorial using for loop


publicclassJavaExample {

publicstaticvoid main(String[] args) {

//We will find the factorial of this number


int number = 5;
long fact = 1;
for(int i = 1; i <= number; i++)
{
fact = fact * i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
Output:

Factorial of 5is: 120


Example 2: Finding Factorial using while loop
publicclassJavaExample {

publicstaticvoid main(String[] args) {

//We will find the factorial of this number


int number = 5;
long fact = 1;
int i = 1;
while(i<=number)
{
fact = fact * i;
i++;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
Output:

Factorial of 5is:120

Example 3: Finding factorial of a number


entered by user
Program finds the factorial of input number using while loop.

import java.util.Scanner;
publicclassJavaExample{

publicstaticvoid main(String[] args){

//We will find the factorial of this number


int number;
System.out.println("Enter the number: ");
Scanner scanner =newScanner(System.in);
number = scanner.nextInt();
scanner.close();
long fact =1;
int i =1;
while(i<=number)
{
fact = fact * i;
i++;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
Output:

Enter the number:


6
Factorial of 6is:720

Java Program to check Even or Odd


number
import java.util.Scanner;
classCheckEvenOdd
{
publicstaticvoid main(String args[])
{
int num;
System.out.println("Enter an Integer number:");

//The input provided by user is stored in num


Scanner input =newScanner(System.in);
num = input.nextInt();

/* If number is divisible by 2 then it's an even number


* else odd number*/
if( num %2==0)
System.out.println("Entered number is even");
else
System.out.println("Entered number is odd");
}
}
Output 1:

Enter an Integer number:


78
Entered number is even
Output 2:

Enter an Integer number:


77
Entered number is odd

You might also like