Write a program to take a number as input then display the digits in the following format:
For example:
Input : 2315
Output : 5+1+3+2=11
Using following method prototype:
String generate(int n)
import java.util.*;
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
System.out.print("Enter the number: ");
int number = keyBoard.nextInt();
System.out.println(generate(number));
keyBoard.close();
}
static String generate(int n) {
String ouput = "";
int sum = 0;
int digit;
while (n > 0) {
digit = n % 10;
if (n > 2) {
ouput += digit + " + ";
} else {
ouput += digit;
}
sum = sum + digit;
n = n / 10;
}
ouput += " = " + sum;
return ouput;
}
}
Comments
Leave a comment