Dremendo Tag Line

Input a number and check if it is a special two-digit number or not using a function in Java

Function - Question 6

In this question, we will see how to input a number and check if it is a special two-digit number or not in Java programming using a function. To know more about function click on the function lesson.

Q6) Write a program in Java to input a number and check if it is a special two-digit number or not using a function. The function should return 1 if the numbers is a special two-digit number else return 0.

A special two-digit number is such that when the sum of its digits is added to the product of its digits, the result is equal to the original two-digit number.

Example: 59 is a special two-digit number.
Sum of its digits 5+9 = 14
Product of its digit 5*9 = 45
Sum of sum and product of the digits 14+45 = 59

Program

import java.util.Scanner;

public class Q6
{
    public static int specialnumber(int num)
    {
        int s=0,p=1,t,d;
        t=num;

        if(t>=10 && t<=99)
        {
            while(t>0)
            {
                d=t%10;
                s=s+d;   // sum of digits
                p=p*d;   // product of digits
                t=t/10;
            }
            if(s+p==num)
            {
                return 1;
            }
        }
        return 0;
    }

    public static void main(String args[])
    {
        int n;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a number ");
        n=sc.nextInt();
        if(specialnumber(n)==1)
        {
            System.out.print("Special Two Digit Number");
        }
        else
        {
            System.out.print("Not Special Two Digit Number");
        }
    }
}

Output

Enter a number 59
Special Two Digit Number
video-poster