Skip to main content

This program checks whether a number is a Special Number or not in JAVA

 This program checks whether a number is a Special Number or not.

A number is said to be a special number when the sum of the factorial of its digits is equal to the number itself.

Example- 145 is a Special Number as 1!+4!+5!=145.

Program-

/*import java.util.*;

public class SpecialNumberCheck

{

    public static void main(String args[])

    {

        Scanner ob=new Scanner(System.in);

        System.out.println("Enter the number to be checked.");

        int a=ob.nextInt();

        int sum=0,b=a;

        while(a!=0)//or a>0

        {

            int rem=a%10;

int fact=1;

            for(int i=1;i<=rem;i++)

            {

                fact=fact*i;

            }

            sum=sum+fact;

            a=a/10;

        }

        if(b==sum)

        {

        System.out.println(a+" is a Special Number.");

    }

    else

    {

        System.out.println(a+" is not a Special Number.");

     }

    }

}*/

//____________________________

Description-

The program checks if the number is a Special Number. We input the number through the Scanner class and

the sum is a variable that stores the sum of the factorial of digits. A temp variable is taken. The while loop

calculates the sum of the factorial digits.

Inside the loop factorial of the last digit is calculated and added to the sum. Every time 'fact' is initialized to 1

as for every digit we have its own factorial. the variable temp is divided by 10 each time so that we can

get the last digit and then the second last digit and so on.

If the sum equals a number then the resultant statements are printed.

import java.util.*;

public class SpecialNumberCheck

{

    public static void main(String args[])

    {

        int sum=0,a,i,b;

        Scanner ob=new Scanner(System.in);

        System.out.println("Enter the number to be checked.");

        a=ob.nextInt();

        b=a;//initialize a variable equal to the number as we can not

            // modify original number since we will require it later

        while(b>0)//or a>0

        {

            int rem=a%10;

            sum=sum+rem;

            b=b/10;

        }

        if (a % sum == 0) {


            System.out.println(a+" is a Niven Number.");

        }

        else

        {

            System.out.println(a+" is not a Niven Number.");

        }

    }

}




Comments

Popular posts from this blog

Difference between Primary Key , Foreign Key & Candidate Key

 Last Updated: 23-02-2021 Primary Key: A primary key is used to ensure data in the specific column is unique. It is a column that cannot have NULL values. It is either an existing table column or a column that is specifically generated by the database according to a defined sequence. Example: Refer to the figure – STUD_NO, as well as STUD_PHONE both, are candidate keys for relation STUDENT but STUD_NO can be chosen as the primary key (only one out of many candidate keys). Foreign Key: A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables. It is a column (or columns) that references a column (most often the primary key) of another table. Example: Refer to the figure – STUD_NO in STUDENT_COURSE is a foreign key to STUD_NO in STUDENT relation. Figure: Let’s see the difference between Primary Key and Foreign Key: PRIMARY KEY A primary key is used to ensure data in the specific column is unique. It uniquely identifie...

Python Methods with Description.

Python includes following list methods  1) list.append(obj) = Appends object obj to list  2) list.count(obj) = Returns count of how many times obj occurs in list  3) list.extend(seq) = Appends the contents of seq to list 4) list.index(obj) = Returns the lowest index in list that obj appears 5) list.insert(index, obj) = Inserts object obj into list at offset index 6) list.pop(obj=list[-1]) = Removes and returns last object or obj from list 7) list.remove(obj) = Removes object obj from list 8) list.reverse() = Reverses objects of list in place 9) list.sort([func]) = Sorts objects of list, use compare func if given Function with Description 1) cmp(tuple1, tuple2) = Compares elements of both tuples. 2) len(tuple) = Gives the total length of the tuple. 3) max(tuple) = Returns item from the tuple with max value. 4) min(tuple) = Returns item from the tuple with min value. 5) tuple(seq) = Converts a list into tuple. Built-in Dictionary Functions & Methods Python includes ...