0% found this document useful (0 votes)
10 views13 pages

Number Programms in Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views13 pages

Number Programms in Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Program to Check Prime Number using a for loop

public class Main {

public static void main(String[] args) {

int num = 29;

boolean flag = false;

for (int i = 2; i <= num / 2; ++i) {

if (num % i == 0) {

flag = true;

break;

if (!flag)

System.out.println(num + " is a prime number.");

else

System.out.println(num + " is not a prime number.");

PALPRIME :

import java.io.*;
import java.util.*;

class PalPrimeNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

int number, temp, rem, i;

1
int count = 0;
int sum = 0;
System.out.println("Enter number to check");
number = sc.nextInt();

temp = number;

for(i = 1; i <= temp; i++)


{
if(temp%i == 0)
{
count++; //increment counter when the reminder is 0
}
}

while(number > 0)
{
rem = number%10;

sum = sum*10+rem;

number = number/10;
}

if(temp == sum && count == 2)


{
System.out.println(temp +" is a PalPrime number");
}
else
{
System.out.println(temp +" is not a PalPrime number");
}
}
}

HAPPY NUMBER

Happy number

2
The happy number can be defined as a number which will yield 1 when it is replaced by the
sum of the square of its digits repeatedly. If this process results in an endless cycle of
numbers containing 4, then the number is called an unhappy number.

For example, 32 is a happy number as the process yields 1 as follows

32 + 22 = 13
12 + 32 = 10
12 + 02 = 1

public class HappyNumber


{
public static int isHappyNumber(int num){
int rem = 0, sum = 0;

while(num > 0){


rem = num%10;
sum = sum + (rem*rem);
num = num/10;
}
return sum;
}

public static void main(String[] args) {


int num = 82;
int result = num;

while(result != 1 && result != 4){


result = isHappyNumber(result);
}

if(result == 1)
System.out.println(num + " is a happy number");

else if(result == 4)
System.out.println(num + " is not a happy number");
}
}

PALINDROME

3
class PalindromeExample{
public static void main(String args[]){
int r,sum=0,temp;
int n=454;
temp=n;
while(n>0){
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}

import java.util.*;
class PalindromeExample2
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string/number to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string/number is a palindrome.");
else
System.out.println("Entered string/number isn't a palindrome.");
}
}

4
An Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number
itself. For example:

153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.

Program to check whether the given number is Armstrong


number
public class JavaExample {

public static void 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");

Spy Number

5
A positive integer is called a spy number if the sum and product of its digits are equal. In
other words, a number whose sum and product of all digits are equal is called a spy
number.

Example of Spy Number

Let's take the number 1124 and check whether the number is a spy or not. First, we will split
it into digits (1, 1, 2, 4). After that find the sum and product of all the digits.

Sum=1+1+2+4=8

import java.util.Scanner;
public class SpyNumberExample1
{
public static void main(String args[])
{
int num, product=1, sum=0, lastdigit;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number to check: " );
num=sc.nextInt();
while(num>0)
{
lastdigit=num%10;
sum=sum+lastdigit;
product=product*lastdigit;
num=num/10;
}
if(sum==product)
System.out.println("The given number is a spy number.");
else
System.out.println("The given number is not a spy number.");
}
}

Krishnamurthy Number Java


Krishnamurthy number is another special number in Java. A number is said to
be Krishnamurthy if the factorial sum of all its digits is equal to that
number. Krishnamurthy number is also referred to as a Strong number. Just like

6
Prime and Armstrong numbers, Krishnamurthy number is also frequently asked by the
interviewers but with it's another name, i.e., Strong number.

Let's take an example of Krishnamurthy number.

1. Number = 145
2. = 1! + 4! + 5!
3. = 1 + ( 4 * 3 * 2 * 1 ) + ( 5 * 4 * 3 * 2 * 1 )
4. = 1 + 24 + 120
5. = 145

import Java.util.*;
import java.io.*;
import java.util.Scanner;

class KrishnamurthyNumber {

static int fact(int number)


{
int f = 1;
while (number != 0) {
f = f * number;
number--;
}
return f;
}

static boolean checkNumber(int number)


{
int sum = 0; //initialize sum to 0

int tempNumber = number; //create a copy of the original number

while (tempNumber != 0) {
sum = sum + fact(tempNumber % 10);

tempNumber = tempNumber / 10;


}

if(sum == number)
return true;

7
else
return false;
}

public static void main(String[] args)


{
int n; //initialize variable n

Scanner sc = new Scanner(System.in);

System.out.println("Enter any number:");

n = sc.nextInt();

if (checkNumber(n))
System.out.println(n + " is a krishnamurthy number");
else
System.out.println(n + "is not a krishnamurthy number");
}
}

Harshad number

A number is said to be the Harshad number if it is divisible by the sum of its digit.

For example, if number is 156, then sum of its digit will be 1 + 5 + 6 = 12. Since 156 is
divisible by 12. So, 156 is a Harshad number.

public class HarshadNumber


{
public static void main(String[] args) {
int num = 156;
int rem = 0, sum = 0, n;

n = num;

while(num > 0){


rem = num%10;
sum = sum + rem;

8
num = num/10;
}

if(n%sum == 0)
System.out.println(n + " is a harshad number");
else
System.out.println(n + " is not a harshad number");
}
}

Floyd’s triangle is a triangle with first natural numbers. It is the right arrangement of the
numbers/values or patterns. Basically, it is a left to right arrangement of natural numbers in a right-
angled triangle
Illustration: Suppose if no of rows to be displayed is 5 then the desired output should display 5
rows as:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

import java.util.*;

class GFG {

public static void main(String[] args)


{
int n = 5;

int i, j, k = 1;

for (i = 1; i <= n; i++) {

for (j = 1; j <= i; j++) {

System.out.print(k + " ");

9
k++;
}

System.out.println();
}
}
}

Neon Number
A positive integer whose sum of digits of its square is equal to the number itself is called
a neon number.

import java.util.*;
public class NeonNumberExample1
{
public static void main(String args[])
{
int sum = 0, n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number to check: ");
n = sc.nextInt();
int square = n * n;
while(square != 0)
{
int digit = square % 10;
sum = sum + digit;
square = square / 10;
}
if(n == sum)
System.out.println(n + " is a Neon Number.");
else
System.out.println(n + " is not a Neon Number.");
}
}

What is a perfect number in Java?

10
A number whose sum of factors (excluding the number itself) is equal to the number is
called a perfect number. In other words, if the sum of positive divisors (excluding the
number itself) of a number equals the number itself is called a perfect number. Let's
understood it through an example.

Example of a Perfect Number

Let's take the number 496 and heck it is a perfect number or not.

First, we find the factors of 496 i.e. 1, 2, 4, 8, 16, 31, 62, 124, and 248. Let's find
the sum of factors (1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 +248 = 496). We observe that the
sum of factors is equal to the number itself. Hence, the number 496 is a perfect number.
Similarly,

import java.util.Scanner;
public class PerfectNumberExample1
{
public static void main(String args[])
{
long n, sum=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
n=sc.nextLong();
int i=1;
while(i <= n/2)
{
if(n % i == 0)
{
Sum = sum + i;
}
i++;
}
if(sum==n)
{
System.out.println(n+" is a perfect number.");
else
System.out.println(n+" is not a perfect number.");
}
}

11
Special Number
If the sum of the factorial of digits of a number (N) is equal to the number itself, the number
(N) is called a special number.

The digits of the number are: 1, 4, 5

Factorial of digits:

!1 = 1

!4 = 4*3*2*1 = 24

!5 = 5*4*3*2*1 = 120

Sum of factorial of digits = 1 + 24 + 120 = 145

Compare the sum of the factorial of digits with the given number, i.e. 145 = 145. We
observe that both are equal.

Hence, the given number 145 is a special number.

import java.util.Scanner;
public class SpecialNumberExample1
{
public static void main(String args[])
{
int num, number, last_digit, sum_Of_Fact = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
number = sc.nextInt();
num = number;
while (number > 0)
{
last_digit = number % 10;
int fact=1;
for(int i=1; i<=last_digit; i++)
{
fact=fact*i;
}
sum_Of_Fact = sum_Of_Fact + fact;
number = number / 10;
}
if(num==sum_Of_Fact)

12
{
System.out.println(num+ " is a special number.");
}
else
{
System.out.println(num+ " is not a special number.");
}
}
}

A pronic number is a number which is the product of two consecutive integers, that is, a number of
the form n(n + 1).

The first few pronic numbers are:

0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462 … etc.

import java.util.Scanner;

public class Example13 {

public static void main(String args[])


{
Scanner sc = new Scanner(System.in);
System.out.print("Input a number : ");
int num = sc.nextInt();
int ans = 0;

for(int i=0; i<num; i++)


{
if(i*(i+1) == num)
{
ans = 1;
break;
}
}

13

You might also like