• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Java

How to solve any Number program in java

  • Categories Java, Iterative Statements, For & While
Number programs

Watch our video on how to solve any Number Program   –  Click Here

1. Find if number is Deficient Number or not. 

     Deficient Number: Sum of factors is less than the number itself.

            e.g. 21. Factors: 1,3,7 = 11<21.

Code: 

import java.util.Scanner;
public class DeficientNum
{
   public static void main(String[] args) 
   {
      System.out.println("Enter the number:");
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      int sum = 0;
      for(int i = 1; i < n; i++) 
      {
         if (n % i == 0)
            sum = sum + i;
      }
      if (sum < n)
     System.out.println("Yes");
     else
     System.out.println("No");
   }
}

2.  Find if number is a Composite number. its is a number which has more that one factor(excl. 1,n).

 e.g. 8=2,4=2 factors.

Code: 

import java.util.Scanner;
public class CompositeNum
{
public static void main(String[] args)
{
System.out.println("Enter the number:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count = 0;
for(int i = 1; i <= n; i++) { if (n % i == 0) count++; } if (count >3)
System.out.println("Yes");
else
System.out.println("No");
}
}

3. Find if given number is a Abundant number or not.

    Abundant number: sum of factor is greater then the number itself. 

          e.g. 12. Factors: 1, 2, 3, 4, 6 = 16 >12

Code: 

public class AbundantNum
{
public static void main(String[] args)
{
System.out.println("Enter the number:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
for(int i = 1; i < n; i++)
{
if (n % i == 0)
sum = sum + i;
}
if (sum > n)
System.out.println("Yes");
else
System.out.println("No");
}
}

4.  Find if a given number is Pronic number or not.

      Pronic Number: it is the product of two consecutive integers, n(n+1).

            e.g 56= 7 x 8 .

Code: 

import java.util.Scanner;
public class PronicNum
{
public static void main(String[] args)
{
System.out.println("Enter the number:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int fact = 0;
for(int i = 1; i < n; i++)
{
if (n % i == 0)
if (i * (i + 1) == n)
fact = i;
}
if (fact != 0)
System.out.println("Yes");
else
System.out.println("No");
}

5.  Accept a number from user and print if given number is Automorphic number or not.  

     Automorphic number: A Automorphic number is a number which is contained in the last digit(s) of its square.  

For example:    25  in  625

Code:
import java.util.Scanner;
public class AutomorphicNumber
{
    public static void main(){
        Scanner sc = new Scanner(System.in);
        int n;
        System.out.println("Enter any number:");
        n= sc.nextInt();
        int m = n;
        int flag = 0, q = n * n;
        while (n != 0) {
            int d = n%10; 
            int d1 = q%10;
            if (d != d1) 
                flag = 1;
            n = n / 10; 
            q = q / 10;
        }
        if ( flag == 0)
            System.out.println ("yes");
        else
            System.out.println ("no");
    }
}

6. Write Accept a number from user and print if given number is Krishnamurthy number or special number or not.  

KN Special number: A special number is a number whose sum of factorial of digits is equal to the number

For example  145 = 1! + 4! + 5!

Code:
import java.util.Scanner;
public class KNSpecialNumber {
     public static void main()
     {
          Scanner sc = new Scanner(System.in);
          int n;
          System.out.println("Enter any number:");
          n= sc.nextInt();
          int m = n;
          int sum = 0;
          while (n != 0) {
               int d = n%10;
               int fact = 1; 
               for (int i= 1; i<= d; i++) 
                    fact = fact * i;
               sum = sum + fact;
               n = n / 10;
          }
          if ( sum == m)
               System.out.println ("yes");
          else
               System.out.println ("no");
     }
}

7. Accept a number from user and print if given number is Neon number or not.  

Neon number: Sum of digits of square of the number is equal to the number . 

For example:   9:   9*9 = 81, 9= 8+1

Code:
import java.util.Scanner;
public class NeonNumber
{
    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        int n;
        System.out.println("Enter any number:");
        n= sc.nextInt();
        int m = n;
        int sum = 0; 
        n = n * n;
        while (n != 0) {
            int d = n%10;
            sum = sum + d;
            n = n / 10;
        }
        if (sum == m)
            System.out.println ("yes");
        else
            System.out.println ("no");
    }
}

8. Accept a number from user and print if given number is duck number or not.  

Duck number: A Duck number is a number which has zeroes present in it. e.g 402, 280.

Code: 
import java.util.Scanner;
public class DuckNumber
{
    public static void main(){
       Scanner sc = new Scanner(System.in);
       int n;
       System.out.println("Enter any number:");
       n= sc.nextInt();
       int m = n;
       int count = 0;
       while (n != 0) {
          int d = n%10;
          if ( d == 0)
             count++;
          n = n / 10;
       }
       if ( count > 0)
          System.out.println ("yes");
       else
          System.out.println ("no");
    }
}

9. Accept a number from user and print if given number is spy number or not.  

spy number: A spy number is a number which has sum its digits equals the product of the digits.

Example: 123. 1+2+3=1*2*3

Code: 
import java.util.Scanner;
public class SpyNumber
{
    public static void main(){
       Scanner sc = new Scanner(System.in);
       int n;
       System.out.println("Enter any number:");
       n= sc.nextInt();
       int m = n;
       int sum = 0, prod = 1;
       while (n != 0) {
          int d = n%10;
          sum = sum + d;
          prod = prod * d;
          n = n / 10;
       }
       if (sum == prod)
          System.out.println ("yes");
       else
          System.out.println ("no");
    }
}

10. Accept a number from user and print if given number is special number or not.  

Special number: A special number is a number whose Sum of digits plus product of digits it is equal to original number. e.g 

For example    59 = 5 + 9 + (5×9)

                                 =59.     

Code:

import java.util.Scanner;
public class SpecialNumber
{
    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        int n;
        System.out.println("Enter any number:");
        n= sc.nextInt();
        int m = n;
       int sum = 0, prod = 1;
       while (n != 0) {
          int d = n%10;
          sum = sum + d;
          prod = prod * d;
          n = n / 10;
       }
       if ( sum + prod == m)
          System.out.println ("yes");
       else
          System.out.println ("no");     } }

11. Accept a number from user and print if given number is prime number or not? 

  Code: 
import java.util.Scanner;
public class primeNumber
{
   public static void main(String[] args) 
{
System.out.println("Enter the number:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count=0;
for(int i=1; i<=n; i++)
{
if (n % i == 0)
count++;
}
if (count==2)
System.out.println("Yes");
else
System.out.println("No");
}
} 

12. Accept a number from user and print if given number is Perfect number or not.

Perfect numbers : the sum of its positive divisors excluding the number itself is equal to that number.

For example, 28 is a perfect number because, 28 is divisible by 1, 2, 4, 7, 14 and 28 ,and the sum of these values is 1 + 2 + 4 + 7 + 14 = 28.   

Code: 
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.println("\n Please Enter any Number: ");
            n = sc.nextInt();
            for(int i = 1; i < n; i++) {
                  if (n % i == 0)
                        sum = sum + i;
            }
            if (sum == n)
                  System.out.println("Yes");
            else
                  System.out.println("No");
      }
}

13.Check whether a given number is palindrome number or not.

Palindrome means, If the reverse of the number is same of original number

Code:

import java.util.Scanner;
public class palindrome
{
    public static void main()
    {
       Scanner scan = new Scanner(System.in);
     System.out.print("Enter any number: ");
       int n = scan.nextInt();
       int m = n;
       int sum = 0;
       while (n != 0) {
          int d = n%10;
          sum = sum * 10 + d;
          n = n / 10;
       }
       if ( sum == m)
          System.out.println ("yes");
       else
          System.out.println ("no");    
    }
}

14.Write a Program in Java to print GCD of two numbers.

The GCD (Greatest Common Divisor) of two numbers is the largest positive integer number that divides both the numbers without leaving any remainder.

For example. GCD of 30 and 45 is 15.

Code:

import java.util.Scanner;
public class GCD
{
    public static void main(String[] args) {
 
        Scanner sc = new Scanner(System.in);
      int num1, num2, gcd = 1;
      System.out.println("Enter first number:");
      num1= sc.nextInt();
      System.out.println("Enter second number:");
      num2= sc.nextInt();
 
        for(int i = 1; i <= num1 && i <= num2; i++)
        {
            if(num1%i==0 && num2%i==0)
                gcd = i;
        }
 
        System.out.printf("GCD of "+num1+" and "+num2+" is= "+gcd);
    }
 
}

15. Accept a number from user and print if given number is Harshad number or not.

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

For example,     156= 1 + 5 + 6 = 12.

Since 156 is divisible by 12. So, 156 is a Harshad number.

Code:
import java.util.Scanner;
public class HarshadNumber
{
    public static void main(String[] args) { 
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter any number:");
        int n= sc.nextInt();
        int m = n;
        int sum = 0;
        while (n != 0) {
            int d = n%10;
            sum = sum + d;
            n = n / 10;
        }
        if ( m%sum == 0)
            System.out.println ("yes");
        else
            System.out.println ("no");
    } 
}
  • Share:
author avatar
Simply Coding

Previous post

Solve any character pattern program in python
June 5, 2021

Next post

How to solve any sum of series program with nested loops
June 5, 2021

You may also like

Single Linked List Programs in Java
Single Linked List Programs in Java
28 August, 2021
Implementing Stack using Array in Java
Implementing Stack using Array in Java
28 August, 2021
Constructor Programs
Constructor Programs
3 July, 2021

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now