Prime Number: Void Int Boolean Int
Prime Number: Void Int Boolean Int
Prime Number: Void Int Boolean Int
class PalindromeExample{
public static void main(String args[]){
int r,sum=0,temp;
int n=454;//It is the number variable to be checked for palindrome
temp=n;
while(n>0){
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}
Perfect number
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;
//executes until the condition becomes false
while(i <= n/2)
{
if(n % i == 0)
{
//calculates the sum of factors
sum = sum + i;
} //end of if
//after each iteration, increments the value of variable i by 1
i++;
} //end of while
//compares sum with the number
if(sum==n)
{
//prints if sum and n are equal
System.out.println(n+" is a perfect number.");
} //end of if
else
//prints if sum and n are not equal
System.out.println(n+" is not a perfect number.");
}
}
Diserium number
import java.util.Scanner;
class Test {
// Function to check Automorphic number
static boolean isAutomorphic(int N)
{
// Store the square
int sq = N * N;
// Start Comparing digits
while (N > 0) {
// Return false, if any digit of N doesn't
// match with its square's digits from last
if (N % 10 != sq % 10)
return false;
// Reduce N and square
N /= 10;
sq /= 10;
}
return true;
}
// Driver method
public static void main(String[] args)
{
int N = 5;
System.out.println(isAutomorphic(N) ? "Automorphic" : "Not
Automorphic");
}
}
Armstrong number
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber % 10;
result += Math.pow(remainder, 3);
originalNumber /= 10;
}
if(result == number)
System.out.println(number + " is an Armstrong number.");
else
System.out.println(number + " is not an Armstrong number.");
}
}
Krishnamurthy number
import java.util.Scanner;
//create KrishnamurthyNumber class to check whether the given number is a
Krishnamurthy number or not
class KrishnamurthyNumber {
// create fact() method to calculate the factorial of the number
static int fact(int number)
{
int f = 1;
while (number != 0) {
f = f * number;
number--;
}
return f;
}
// create checkNumber() method that returns true when it founds number
krishnamurthy
static boolean checkNumber(int number)
{
int sum = 0; //initialize sum to 0
int tempNumber = number; //create a copy of the original number
//perform operation until tempNumber will not equal to 0
while (tempNumber != 0) {
// calculate the factorial of the last digit of the tempNumber and
then add it to the sum
sum = sum + fact(tempNumber % 10);
// replace the value of tempNumber by tempNumber/10
tempNumber = tempNumber / 10;
}
// Check whether the number is equal to the sum or not. If both are
equal, number is krishnamurthy number
if(sum == number)
return true;
else
return false;
}
// main() method start
public static void main(String[] args)
{
int n; //initialize variable n
//create scanner class object to read data from user
Scanner sc = new Scanner(System.in);
//custom message
System.out.println("Enter any number:");
//store user entered value into variable n
n = sc.nextInt();
if (checkNumber(n))
System.out.println(n + " is a krishnamurthy number");
else
System.out.println(n + "is not a krishnamurthy number");
}
}
Mobius number
class ReverseNumber {
public static void main(String[] args) {
int num = 1234, reversed = 0;
System.out.println("Original Number: " + num);
// run loop until num becomes 0
while(num != 0) {
// get last digit from num
int digit = num % 10;
reversed = reversed * 10 + digit;
// remove the last digit from num
num /= 10;
}
System.out.println("Reversed Number: " + reversed);
}
}
Fibonacci numbers
class FibonacciExample1{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);//printing 0 and 1
for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already
printed
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}}
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: ");
//reads an integer from the user
number = sc.nextInt();
num = number;
//executes until the condition becomes false
while (number > 0)
{
//finds the alst digit from the given number
last_digit = number % 10;
//variable stores factorial
int fact=1;
//factorial logic
for(int i=1; i<=last_digit; i++)
{
//calculates factorial
fact=fact*i;
}
//calculates the sum of all factorials
sum_Of_Fact = sum_Of_Fact + fact;
//divides the number by 10 and removes the last digit from the number
number = number / 10;
}
//compares the sum with the given number
if(num==sum_Of_Fact)
{
System.out.println(num+ " is a special number.");
}
else
{
System.out.println(num+ " is not a special number.");
}
}
}
Magic number
import java.util.Scanner;
public class MagicNumberExample1
{
public static void main(String args[])
{
int n, remainder = 1, number, sum = 0;
//creating a constructor of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number you want to check: ");
//reading an integer form the user
n = sc.nextInt();
//assigning the entered number in the variable num
number = n;
//outer while loop
while (number > 9) //while(number > 0 || sum > 9)
{
//inner while loop
while (number > 0)
{
//determines the remainder
remainder = number % 10;
sum = sum + remainder;
//divides the number by 10 and removes the last digit of the number
number = number / 10;
}
number = sum;
sum = 0;
}
if (number == 1)
{
System.out.println("The given number is a magic number.");
}
else
{
System.out.println("The given number is not a magic number.");
}
}
}