# How do I get the sum of 3 numbers

Page 1 of 1

## 3 Replies - 14692 Views - Last Post: 16 November 2006 - 05:26 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=20947&amp;s=485ccde8d5e0738d84f5a0432f3af561&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Artep

Reputation: 0
• Posts: 6
• Joined: 07-November 06

# How do I get the sum of 3 numbers

Posted 16 November 2006 - 08:22 AM

This is my code for adding three numbers, my problem is that UI cant get the three digits to stand together like 123 = 6 instead my program writes 1 2 3. I guess that the computer think 123 is one number and 1 2 3 is three numbers but what can I do the write one number like 123 as a three digit number and sum it? Please help explaine, Artep

import java.util.Scanner;

/*
* Example: Calculate the sum of three numbers
*/

public class tal
{
public static void main (String[] args)
{

// Define and initialize variables
Scanner input = new Scanner (System.in);
int sum = 0;

System.out.print("Write three digits : ");
sum = input.nextInt();
sum += input.nextInt();
sum += input.nextInt();

//Display sum
System.out.printf("Sum is: %d",sum);

Is This A Good Question/Topic? 0

## Replies To: How do I get the sum of 3 numbers

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: How do I get the sum of 3 numbers

Posted 16 November 2006 - 08:24 AM

I'd suggest taking the number as a string, then in a loop, convert each element to a number and add them.

### #3 Artep

Reputation: 0
• Posts: 6
• Joined: 07-November 06

## Re: How do I get the sum of 3 numbers

Posted 16 November 2006 - 08:31 AM

Amadeus, on 16 Nov, 2006 - 08:24 AM, said:

I'd suggest taking the number as a string, then in a loop, convert each element to a number and add them.

Thanks for quick aswear, but I dont know how to do that. Could you please give me an example. /Artep

### #4 GWatt

Reputation: 307
• Posts: 3,105
• Joined: 01-December 05

## Re: How do I get the sum of 3 numbers

Posted 16 November 2006 - 05:26 PM

I copied your code, and changed nothing (except to add ending braces), and the code worked fine.