You are on page 1of 29

Leap Year Program in Java

A day and night is 23 hours, 56 minutes and 4.1 seconds. It takes 365 days, 5 hours,
48 minutes and 45 seconds for the Earth to complete one round of the sun.
In easy terms, the year that the remaining zero is divided by 4 will be a leap year, but
only that century will be a leap year, which is completely divided by 400.

import java.util.Scanner;

public class LeapYear


{
public static void main(String[] args)
{
int year;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a year=");
year = sc.nextInt();
if (((year % 400 == 0) || (year % 4 == 0)) && (year % 100 != 0))
{
System.out.println("Leap year.");
}
else
{
System.out.println("Not a leap year");
}
}
}

Output:

Enter a year=1600
Not a leap year
Greatest Number Program in Java
import java.util.Scanner;
public class GreatestNumber {
public static void main(String[] args) {
int n1, n2, n3;
Scanner sc = new Scanner(System.in);

System.out.print("Enter n1 number=");
n1 = sc.nextInt();
System.out.print("Enter n2 number=");
n2 = sc.nextInt();
System.out.print("Enter n3 number=");
n3 = sc.nextInt();

if (n1 > n2) {


if (n1 > n3) {
System.out.println("n1 is greatest.");
} else {
System.out.println("n3 is greatest.");
}
} else {
if (n2 > n3) {
System.out.println("n2 is greatest.");
} else {
System.out.println("n3 is greatest.");
}
}
}
}
Output:

Enter n1 number=15
Enter n2 number=18
Enter n3 number=10
n2 is greatest.
Even Odd Program in Java
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
int number;
Scanner sc = new Scanner(System.in);

System.out.print("Enter your number=");


number = sc.nextInt();

if (number % 2 == 0) {
System.out.println("Number is even.");
} else {
System.out.println("Number is odd.");
}
}
}
Output:

Enter your number=5


Number is odd.
Armstrong Number Program in Java
Armstrong Number is a positive number if it is equal to the sum of cubes
of its digits is called Armstrong number and if its sum is not equal to the
number then its not a Armstrong number.
Armstrong Number Program is very popular in java, c language, python etc.

Examples: 153 is Armstrong

(1*1*1)+(5*5*5)+(3*3*3) = 153

import java.util.Scanner;

/**
* @author AFAQ
*/
public class ArmstrongNumber
{
public static void main(String[] args)
{
int n,
cubeSum = 0, num, r;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
cubeSum = cubeSum + (r * r * r);
num = num / 10;
}
if (n == cubeSum)
{
System.out.println("Armstrong Number");
}
else
{
System.out.println("Not Armstrong Number");
}
}
}

Output:

Enter number=153
Armstrong Number
Buzz Number Program in Java

A number is said to be Buzz Number if it ends with 7 or is divisible by 7.


Example: 1007 is a Buzz Number.

import java.util.Scanner;
public class BuzzNumber
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter n=");
n = sc.nextInt();
if (n % 10 == 7 || n % 7 == 0)
{
System.out.println("Buzz number");
}
else
{
System.out.println("Not Buzz number");
}
}
}
Output:

Enter n=147
Buzz number
CoPrime Numbers Program in Java
Two integers a and b are said to be relatively prime, mutually prime, or coprime if the
only positive integer that divides both of them is 1. Example: 13 and 15 are co prime.

import java.util.Scanner;
public class CoPrimeNumbers
{
public static void main(String[] args)
{
int a, b, gcd = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a=");
a = sc.nextInt();
System.out.print("Enter b=");
b = sc.nextInt();
int min, max;
min = a;
if (min > b)
{
min = b;
max = a;
}
else
{ min = a;
max = b;
}
while (max > min)
{ int r = max % min;
if (r == 0)
{
gcd = min;
break;
}
else
{ max = min;
min = r;
}
}
if (gcd == 1)
{
System.out.println("Co Prime Numbers");
}
else
{
System.out.println("Not Co Prime Numbers");
}
}
}
Output:

Enter a=13
Enter b=15
Co Prime Numbers

Disarium Number Program in Java


A number is called Disarium number if the sum of its power of the
positions from left to right is equal to the number.
Example:

11 + 32 + 53 = 1 + 9 + 125 = 135

import java.util.Scanner;

public class DisariumNumber


{
public static void main(String[] args)
{
int r, n, num,digits=0,
sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
digits++;
num = num / 10;
}
num = n;
while (num > 0)
{
r = num % 10;
sum = sum + (int)Math.pow(r, digits);
num = num / 10;
digits--;
}

if(n==sum)
{
System.out.println("Disarium Number");
}
else
{
System.out.println("Not Disarium Number");
}

}
}
Output:

Enter number=135
Disarium Number

Factors Program in Java


Factor a number or algebraic expression that divides another number or
expression evenly—i.e., with no remainder. For example, 3 and 6 are
factors of 12 because 12 ÷ 3 = 4 exactly and 12 ÷ 6 = 2 exactly. The other
factors of 12 are 1, 2, 4, 6 and 12.
Factors of 12: 1, 2, 3, 4, 6, 12.
import java.util.Scanner;

public class Factors


{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
{
System.out.println(i);
}
}
}
}
Output:

Enter number=6
1
2
3
6

Fibonacci Series Program in Java


A series of numbers in which each number ( Fibonacci number ) is the
sum of the two preceding numbers. The simplest is the series 0, 1, 1, 2, 3,
5, 8, etc.
import java.util.Scanner;

public class FibonacciSeries


{
public static void main(String[] args)
{
// TODO code application logic here
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of series=");
n = sc.nextInt();
int a = 0,
b = 1, c;
for (int i = 1; i <= n; i++)
{
System.out.println(a);
c = a + b;
a = b;
b = c;
}
}
}
Output:

Enter number of series=10


0
1
1
2
3
5
8
13
21
34

Floyd Triangle Program in Java


1
23
456
7 8 9 10
11 12 13 14 15

public class FloydTriangle


{
public static void main(String[] args)
{
int k = 1;
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(k + " ");
k++;
}
System.out.println();
}
}
}
Output:

1
23
456
7 8 9 10
11 12 13 14 15

Greatest Common Divisor Program in Java


the greatest common divisor (gcd) of two or more integers, which are not
all zero, is the largest positive integer that divides each of the integers.
For example, the gcd of 8 and 12 is 4.
import java.util.Scanner;

public class GCDProgram


{
public static void main(String[] args)
{
int a, b, gcd = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a=");
a = sc.nextInt();
System.out.print("Enter b=");
b = sc.nextInt();
int min, max;
min = a;
if (min > b)
{
min = b;
max = a;
}
else
{
min = a;
max = b;
}
while (max > min)
{
int r = max % min;
if (r == 0)
{
gcd = min;
break;
}
else
{
max = min;
min = r;
}
}
System.out.println("GCD=" + gcd);
}
}
Output:

Enter a=15
Enter b=18
GCD=3

Least Common Multiple Program in Java


The least common multiple, lowest common multiple, or smallest
common multiple of two integers a and b, usually denoted by LCM(a, b),
is the smallest positive integer that is divisible by both a and b.
import java.util.Scanner;

public class LCMProgram


{
public static void main(String[] args)
{
int a, b, gcd = 1, lcm = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a=");
a = sc.nextInt();
System.out.print("Enter b=");
b = sc.nextInt();
int min, max;
min = a;
if (min > b)
{
min = b;
max = a;
}
else
{
min = a;
max = b;
}
while (max > min)
{
int r = max % min;
if (r == 0)
{
gcd = min;
break;
}
else
{
max = min;
min = r;
}
}
lcm = (a * b) / gcd;
System.out.println("LCM:" + lcm);
}
}
Output:

Enter a=15
Enter b=18
LCM:90

Neon Number Program in Java


A neon number is a number where the sum of digits of square of the
number is equal to the number. For example if the input number is 9, its
square is 9*9 = 81 and sum of the digits is 9. i.e. 9 is a neon number.
import java.util.Scanner;

public class NeonNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int n,
sqr = 1,
sum = 0, r;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
sqr = n * n;
while (sqr > 0)
{
r = sqr % 10;
sum = sum + r;
sqr = sqr / 10;
}
if (n == sum)
{
System.out.println("Neon Number");
}
else
{
System.out.println("Not Neon Number");
}
}
}
Output:

Enter number=9
Neon Number

Palindrome Number Program in Java


A palindromic number is a number that remains the same when its digits
are reversed. Like 16461, for example,
import java.util.Scanner;

public class PalindromeNumber


{
public static void main(String[] args)
{
int n, num, r,
rev = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
rev = (rev * 10) + r;
num = num / 10;
}
if (n == rev)
{
System.out.println("Palindrome Number");
}
else
{
System.out.println("Not Palindrome Number");
}
}
}
Output:

Enter number=12321
Palindrome Number

Perfect Number Program in Java


A perfect number is a positive integer that is equal to the sum of its
positive divisors, excluding the number itself. For instance, 6 has divisors
1, 2 and 3, and 1 + 2 + 3 = 6, so 6 is a perfect number.
import java.util.Scanner;

public class PerfectNumber


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

Output:

Enter number=6
Perfect Number

Prime Number Program in Java


A prime number is a natural number greater than 1 that cannot be
formed by multiplying two smaller natural numbers. A natural number
greater than 1 that is not prime is called a composite number. For
example, 5 is prime because the only ways of writing it as a product, 1 ×
5 or 5 × 1, involve 5 itself.
import java.util.Scanner;

public class PrimeNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int n,
i = 2;
boolean flag = true;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
while (n > i)
{
if (n % 2 == 0)
{
flag = false;
break;
}
i++;
}
if (flag)
{
System.out.println("Number is prime.");
}
else
{
System.out.println("Number is not prime.");
}
}
}
Output:

Enter number=17
Number is prime.
Prime Number Up to N Terms Program in
Java
import java.util.Scanner;

public class PrimeNumberUptoN


{
public static void main(String[] args)
{
int size,c=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of prime=");
size = sc.nextInt();
int n=2;
while(c<=size)
{
boolean flag = true;
for(int i=2;i < n;i++)
{
if (n % i == 0)
{
flag = false;
break;
}
}
if (flag)
{
System.out.println("Number is prime="+n);
c++;
}
n++;
}

}
}
Output:

Enter size of prime=5


Number is prime=2
Number is prime=3
Number is prime=5
Number is prime=7
Number is prime=11
Number is prime=13
Reverse Number Program in Java
If a number=1234, then reverse of number is 4321.
import java.util.Scanner;

public class ReverceNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int n, num, r,
rev = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
rev = (rev * 10) + r;
num = num / 10;
}
System.out.println("Reverce of Number=" + rev);
}
}

Output:

Enter number=1234
Reverce of Number=4321
Spy Number Program in Java
A spy number is a number where the sum of its digits equals the product
of its digits. For example, 1124 is a spy number, the sum of its digits is
1+1+2+4=8 and the product of its digits is 1*1*2*4=8.
import java.util.Scanner;

public class SpyNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int r, n, num, mul = 1, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
sum = sum + r;
mul = mul * r;
num = num / 10;
}
if (mul == sum)
{
System.out.println("Spy Number");
}
else
{
System.out.println("Not Spy Number");
}
}
}
Output:

Enter number=123
Spy Number
Sum of Digits Program in Java
If a number enter 5462 then sum of theses digits 5+4+6+2 = 17.
import java.util.Scanner;

public class SumofDigits


{
public static void main(String[] args)
{
// TODO code application logic here
int r, n, num,
sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
sum = sum + r;
num = num / 10;
}
System.out.println("Sum of digit=" + sum);
}
}
Output:

Enter number=1234
Sum of digit=10
Twin Prime Program in Java
A twin prime is a prime number that is either 2 less or 2 more than
another prime number—for example, either member of the twin prime
pair (41, 43). In other words, a twin prime is a prime that has a prime gap
of two.
import java.util.Scanner;

public class TwinPrime


{
public static void main(String[] args)
{
int a, b;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a=");
a = sc.nextInt();
System.out.print("Enter b=");
b = sc.nextInt();
if (prime(a) && prime(b) && (Math.abs(a - b) == 2))
{
System.out.println("Twin Prime");
}
else
{
System.out.println("Not Twin Prime");
}
}
static boolean prime(int n)
{
// TODO code application logic here
int i = 2;
boolean flag = true;
while (n > i)
{
if (n % 2 == 0)
{
flag = false;
break;
}
i++;
}
return flag;
}
}
Output:

Enter a=5
Enter b=7
Twin Prime
Twisted Prime Program in Java
A number is called a twisted prime number if it is a prime number and
reverse of this number is also a prime number.
import java.util.Scanner;

public class TwistedPrime


{
public static void main(String[] args)
{
// TODO code application logic here
int n, num, r,
rev = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
rev = (rev * 10) + r;
num = num / 10;
}
if (prime(n) && prime(rev))
{
System.out.println("Twisted Prime");
}
else
{
System.out.println("Not Twisted Prime");
}
}
static boolean prime(int n)
{
// TODO code application logic here
int i = 2;
boolean flag = true;
while (n > i)
{
if (n % 2 == 0)
{
flag = false;
break;
}
i++;
}
return flag;
}
}

Output:

Enter number=137
Twisted Prime
Number Series Program in Java
import java.util.Scanner;

public class NumberSeries


{
public static void main(String[] args)
{
int n, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms=");
n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
sum = sum + i;
if (n == i)
{
System.out.println(i);
}
else
{
System.out.print(i + "+");
}
}
System.out.println("Sum of series=" + sum);
}
}
Output:

Enter number of terms=5


1+2+3+4+5
Sum of series=15
Even Number Series Program in Java
import java.util.Scanner;

public class EvenNumberSeries


{
public static void main(String[] args)
{
int n, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms=");
n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
int t = i * 2;
sum = sum + t;
if (n == i)
{
System.out.println(i * 2);
}
else
{
System.out.print((i * 2) + "+");
}
}
System.out.println("Sum of even series=" + sum);
}
}
Output:

Enter number of terms=5


2+4+6+8+10
Sum of even series=30
Odd Number Series Program in Java
import java.util.Scanner;

public class OddNumberSeries


{
public static void main(String[] args)
{
int n, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms=");
n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
int t = i * 2 - 1;
sum = sum + t;
if (n == i)
{
System.out.println(t);
}
else
{
System.out.print(t + "+");
}
}
System.out.println("Sum of even series=" + sum);
}
}
Output:

Enter number of terms=5


1+3+5+7+9
Sum of even series=25
Square Number Series Program in Java
import java.util.Scanner;

public class SquareNumberSeries


{
public static void main(String[] args)
{
int n,sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms=");
n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
int t=i*i;
sum=sum+t;
if (n == i)
{
System.out.println(t);
}
else
{
System.out.print(t+"+");
}
}
System.out.println("Sum of series="+sum);
}
}
Output:

Enter number of terms=5


1+4+9+16+25
Sum of series=55
Loop based output questions
int a, b;
for (a = 6, b = 4; a <= 24; a = a + 6)
{
if (a % b == 0)
break;
}
System.out.println(a);
Output:

12

int i = 1;
int d = 5;
do
{
d = d * 2;
System.out.println(d);
i++;
}
while (i <= 5);
Output:

10
20
40
80
160

for (int i = 3; i <= 4; i++)


{
for (int j = 2; j < i; j++)
{
System.out.print("");
}
System.out.println("WIN");
}
Output:

WIN
WIN
Sum of Series Program
Example – 1 + (1+2) + (1+2+3) + …… n

import java.util.*;

class seriesprogram {

// Function to find sum of given series


static int sumOfSeries(int n)
{
int sum = 0;
for (int i = 1 ; i <= n ; i++)
for (int j = 1 ; j <= i ; j++)
sum += j;
return sum;
}

/* Driver program to test above function */


public static void main(String[] args)
{
int n = 10;
System.out.println(sumOfSeries(n));

}
}

Series Program

Example – 1 + 12 + 123 + 1234 + 12345 + ……

import java.util.Scanner;
public class Series
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
int s = 0, c; // s for terms of series, c for counter to
generate n terms
for (c = 1; c <= n; c++) {
s = s * 10 + c;
System.out.print(s + " ");
}
}
}

You might also like