05A-2014 – CSKC
Sign In

Search





Q&A #7425 siteicon  

Question:

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 twodigit number.

Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of its digits = 5 x 9 = 45
Sum of the sum of digits and product of digits= 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value is equal to the number input, output the message “Special 2-digit number” otherwise, output the message “Not a Special 2-digit number”.

Answer:

We create a Scanner object and accept the input number using nextInt() method. The input is stored in the variable ‘number’. It is given
in the question that the input will be a two digit number. We need to extract the two digits of the number into two variables ‘leftDigit’ and
‘rightDigit’. The left digit can be extracted by dividing the number by 10. (Note that ‘number’ is an int and the divisor 10 is an int. So, the
result will be an int and not a float i.e. we obtain only the quotient as the result). The right digit can be obtained by calculating the
remainder when ‘number’ is divided by 10.
Let us take an example input – number = 59.
Then, leftDigit = number / 10 = 59 / 10 = 5
rightDigit = number % 10 = 59 % 10 = 9
Once, we have the two numbers, calculating the sum and product of the digits is easy.

 

int sumOfDigits = rightDigit + leftDigit;
 int productOfDigits = rightDigit * leftDigit;

Next, we add the two variables above and store it in a new variable.

int sumOfProductAndSum = sumOfDigits + productOfDigits;

Finally, we use if else decision statements to check if the sum of product of digits and sum of digits is same as the input number and print
an appropriate message.

import java.util.Scanner;
 public class SpecialNumber {
 public static void main(String[] args) {
 Scanner scanner = new Scanner(System.in);
 System.out.print("Enter number: ");
 int number = scanner.nextInt();
 int rightDigit = number % 10;
 int leftDigit = number / 10;
 int sumOfDigits = rightDigit + leftDigit;
 int productOfDigits = rightDigit * leftDigit;
 int sumOfProductAndSum = sumOfDigits + productOfDigits;
 if (sumOfProductAndSum == number) {
 System.out.println("Special 2-digit number");
 } else {
 System.out.println("Not a Special 2-digit number");
 }
 }
 }


Share

C. Sir| Created: 18-Apr-2019 | Updated: 18-Apr-2019|ICSE2014









×
ssignment Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning Display Using IO Manipulation User Defined Data Types Enumerated Types Preprocessor Directives And Macros Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constructor Concepts Default Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Mult-Level Inheritance Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Stacks Queues Introductory Sessions Beginning to Program Tokens Keywords Identifiers Literals Data Types Variables and Constants Operators (Arithmetic-Assignment-Unary-Relational-Logical) Expressions Statements and Formula Based Calculations Class Object Creation and Access Wrapper Classes Constructor Basics Conversion and Checking Functions Math Functions Function building and calling Simple If and If-Else Selection Switch Based Selections Basic Loop Types Array Basics Array Search Array Sorting Character Handling String Basics String Class Functions Types of Errors Try Catch Introductory Sessions Beginning to Program Introductory Sessions Beginning to Program Introductory Sessions Beginning to Program Data Types Variables and Constants Introductory Sessions Beginning to Program Date and Time Handling Regular Expressions Command Line Arguments Concurrent Execution Making Executable File Python Introduction (Features Python Introduction (Features, Installation, Modes) Handling Unicode Internet Interactions Imaging and Compression Special Data File Handling Python Basics Cryptography Blockchain and Cryptocurrency Generative AI keywords, identifiers, variables Beginning To Program Tokens, Keywords, Identifiers Variables and Expressions Data Types Operators Concept Learning Examples Operators Solved Practical Examples elational nary)