Prime Number: Void Int Boolean Int

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Prime number

public class Main {

    public static void main(String[] args) {


 
      int num = 29;
      boolean flag = false;
      for (int i = 2; i <= num / 2; ++i) {
        // condition for nonprime number
        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.");
    }
  }
Palindrome number

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;

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");
        }
       
    }
}
Double decker number

public class NumberDoubleValueExample1 {  


    public static void main(String[] args) {      
         //Get a number as integer  
         Number x = 123456;      
         //Print their value as double  
         System.out.println("x as integer : " +x );  
         System.out.print("x as double: " + x.doubleValue());        
         }  
}  
Automorphic number

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

public class Armstrong {

    public static void main(String[] args) {

        int number = 371, originalNumber, remainder, result = 0;

        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

public class GFG {


     
    // C# Program to evaluate Mobius
    // Function: M(N) = 1 if N = 1
    // M(N) = 0 if any prime factor
    // of N is contained twice
    // M(N) = (-1)^(no of distinct
    // prime factors)
 
    // Function to check if n is
    // prime or not
    static boolean isPrime(int n)
    {
        if (n < 2)
            return false;
        for (int i = 2; i * i <= n; i++)
            if (n % i == 0)
                return false;
        return true;
    }
 
    static int mobius(int N)
    {
        // Base Case
        if (N == 1)
            return 1;
 
        // For a prime factor i check if
        // i^2 is also a factor.
        int p = 0;
        for (int i = 1; i <= N; i++) {
            if (N % i == 0 && isPrime(i)) {
 
                // Check if N is divisible by i^2
                if (N % (i * i) == 0)
                    return 0;
                else
 
                    // i occurs only once, increase f
                    p++;
            }
        }
 
        // All prime factors are contained only
        // once Return 1 if p is even else -1
        return (p % 2 != 0) ? -1 : 1;
    }
 
    // Driver code
    static public void main(String[] args)
    {
        int N = 17;
        System.out.println("Mobius Functions M(N) at " +
                      " N = " + N + " is: "    + mobius(N));
        System.out.println("Mobius Functions M(N) at " +
                        " N = " + 25 + " is: " + mobius(25));
        System.out.println("Mobius Functions M(N) at " +
                          " N = " + 6 + " is: " + mobius(6));
    }
}
 
// This code is contributed by vt_m
Reverse 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.");  
}  
}  
}  

You might also like