3 Replies - 14330 Views - Last Post: 16 November 2006 - 05:26 PM Rate Topic: -----

#1 Artep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;

//Read and add numbers from user
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

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Artep  Icon User is offline

  • New D.I.C Head

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

Re: How do I get the sum of 3 numbers

Posted 16 November 2006 - 08:31 AM

View PostAmadeus, 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
Was This Post Helpful? 0
  • +
  • -

#4 GWatt  Icon User is offline

  • member icon

Reputation: 307
  • View blog
  • Posts: 3,104
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1