Answer to Question #289953 in Java | JSP | JSF for Genus

Question #289953

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)


1
Expert's answer
2022-01-23T09:40:08-0500


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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS