Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum all digits of a number and show the digits separately in Java

I've been using this site quite a lot but I've never really wrote anything. Today, I've stumbled upon a problem which solution I can't seem to find.

The problem is that, I have an int variable with an unknown number of digits. It is asked of me to sum all of those digits and then have it printed/shown_as_a_message with all those digit separated.

For example, the user enters the number "12345678" and in the end I need to have a message saying "The numbers 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36". I know how to separate and sum all of those numbers, but I don't know how to save each number and then have it shown, because not only do I not know the right amount of digits, but I don't know their values either.

Usually that wouldn't be hard to do, but here comes the tricky part for me, since we haven't learned things like arrays, strings, indexes, etc., we're not allowed to use them. So, I'm supposed to do it with the simplest type of code possible.

So far, I've got the following:

import java.util.Scanner;
public class Numbers {
    public static void main(String[] args) {
        int number, result;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number:");
        number = sc.nextInt();
        result = 0;
        while (number > 0){
            result = result + (number % 10);
            number = number / 10;
        }
        System.out.println("This is where the message would go.");
    }
}
like image 744
DoombringerBG Avatar asked Nov 17 '16 21:11

DoombringerBG


People also ask

How do you isolate a digit in Java?

You can convert a number into String and then you can use toCharArray() or split() method to separate the number into digits. String number = String. valueOf(someInt); char[] digits1 = number.


2 Answers

static final Scanner input = new Scanner(System.in);

public static void main(String[] args) {
    int number, result;
    System.out.println("Enter number:");
    number = input.nextInt();
    result = 0;
    System.out.print("The numbers: ");

    //Reverses the number
    int reversedNum = 0;
    while (number != 0) {
        reversedNum = reversedNum * 10 + number % 10;
        number = number / 10;
    }

    //Iterates over the number and prints it out
    while (reversedNum > 0) {
        System.out.print((reversedNum % 10));

        result = result + (reversedNum % 10);
        reversedNum = reversedNum / 10;

        if (reversedNum > 0) {
            System.out.print(" + ");
        }
    }
    System.out.println(" = " + result);
}

This works to print out all the numbers in the correct order without using arrays and strings.

Example:

Enter number:
12345
The numbers: 1 + 2 + 3 + 4 + 5 = 15
BUILD SUCCESSFUL (total time: 5 seconds)
like image 154
Luke Melaia Avatar answered Nov 04 '22 01:11

Luke Melaia


Perhaps something like this where you aren't using any sort of container. Simply concatenate to the string each of time you find a new number.

    int number, result;
    String finalOutput = "";
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number:");
    number = sc.nextInt();
    result = 0;

    while (number > 0){
        result = result + (number % 10);

        finalOutput += (number % 10);

        number = number / 10;
        if(number > 0)
        {
            finalOutput += " + ";
        }else{
            finalOutput += " = ";
        }
    }
    System.out.println("The numbers " + finalOutput + result);
like image 28
RAZ_Muh_Taz Avatar answered Nov 04 '22 01:11

RAZ_Muh_Taz