Dremendo Tag Line

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

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 C++ programming using a function. To know more about function click on the function lesson.

Q6) Write a program in C++ 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

#include <iostream>
#include <conio.h>

using namespace std;

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;
}

int main()
{
    int n;
    cout<<"Enter a number ";
    cin>>n;
    if(specialnumber(n)==1)
    {
        cout<<"Special Two Digit Number";
    }
    else
    {
        cout<<"Not Special Two Digit Number";
    }
    return 0;
}

Output

Enter a number 59
Special Two Digit Number
video-poster