• 0

Java code help


Question

Assignment: "Write a program SumOfDigits that reads an integer

greater than 0 and smaller than 1000. Add all the digits in the integer.

For example, if an integer is 932, the sum of all its digits is 14.

Hint: Take use of division (/) and remainder (%) operators."

/* Simple Java Program To Find the Sum of digits of number */

class SumDigit
{
public static void main(String args[])
{
	int sum, i,a,d;
	a = Integer.parseInt(args[0]);
		sum = 0;
		for(i=1;i< =10;i++)
			{d = a%10;
			a = a/10;
			sum=sum + d;} 
				System.out.println("Sum of Digit :"+sum);
}
}

Obviously using "For" in the code is incorrect, but this is one of the problems of my first assignment, I need help figuring out how to correct it.

Link to comment
Share on other sites

14 answers to this question

Recommended Posts

  • 0

Why is there a loop that goes to 10? The max value is 1000

The issue is that 0%10 = 10

int x = Integer.parseInt(args[0]);

int i;

int a;

int total;

for(i = 0; i < 3; i++)

{

a = x%10;

x = x/10;

total += a;

}

Link to comment
Share on other sites

  • 0

When using:

/* Simple Java Program To Find the Sum of digits of number */

class SumDigit
{
public static void main(String args[])
{
	int x = Integer.parseInt(args[0]);
	int i;
	int a;
	int total;
		for(i = 0; i &lt; 3; i++)
		{
			a = x%10;
			x = x/10;
			total += a;
}

I received the following error:

SumDigit.java:16: reached end of file while parsing
}
 ^
1 error

Link to comment
Share on other sites

  • 0

You're missing one (or more) of the closing brackets "}", you only have one closing bracket but three opening ones.

Also, a bit of advice (ignore if you want :p) but if you're just starting programming I would highly recommend you solve simple problems like this by yourself, even if you have to think about it for quite a long time.

Edited by James123
Link to comment
Share on other sites

  • 0

From what I can tell, you are missing 2x }, one for your For Loop, and another for your class.

/* Simple Java Program To Find the Sum of digits of number */

class SumDigit
{
}
public static void main(String args[])
{
	int x = Integer.parseInt(args[0]);
	int i;
	int a;
	int total;
		for(i = 0; i &lt; 3; i++)
		{
			a = x%10;
			x = x/10;
			total += a;
		 }
}

I might have missed other stuff too since I didn't run your code :)

Link to comment
Share on other sites

  • 0

there you go ..

{
  public static void main(String args[])
  {
	  char[] input = args[0].toCharArray();	  
	  int sum = 0;	  
	  for(int i=0;i&lt;input.length;i++)
	  {	   
	   sum=sum + Character.getNumericValue(input[i]);	   
	  }
	  System.out.println("Sum of Digit: "+sum);
  }
}

Link to comment
Share on other sites

  • 0

Start by figuring it out on paper with complete english sentences. Given a number, how should we proceed, step by step, to obtain each of its digits by using modulus and division, and add the result together.

Once you have figured out the algorithm, the Java translation should be evident. Of course, you have to write correct Java (not miss any curly brace or semicolon). Look at what Telemachus posted, it's your code but with the missing curly brackets added, so it should behave as you intended.

Link to comment
Share on other sites

  • 0
Start by figuring it out on paper with complete english sentences. Given a number, how should we proceed, step by step, to obtain each of its digits by using modulus and division, and add the result together.

Once you have figured out the algorithm, the Java translation should be evident. Of course, you have to write correct Java (not miss any curly brace or semicolon). Look at what Telemachus posted, it's your code but with the missing curly brackets added, so it should behave as you intended.

+1

Yes you should really do this by yourself. Take out a sheet of paper, and do it by hand.

Eventually you'll face much harder problems to solve, by then if you never learned this simple fundamental exercise your out of luck and may be pressured to switch majors. (My professor flat out told this guy to switch majors when he asked a "what is a pointer?" junior year of college). But don't worry, beginning courses will hopefully engrave this into you.

Link to comment
Share on other sites

  • 0

This code here, works without errors, requires input, but does not total up the sum of the numbers entered: HELP! =P

import java.util.Scanner;
public class SumOfDigits
{
	public static void main(String[]args)
	{
				Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		int sum = 0;
		while(x/1000 &gt; 0)
		{
			sum += x%10;
			x /= 10;	
		}			
		System.out.println(sum+x);
	}
}

Link to comment
Share on other sites

  • 0

Hi Alladaskill,

Like some people mentioned, when you get a problem like this and you don't immediately know the answer, I find it helps to try a few examples on paper and brainstorm. You already have a clue that you must use / and % operators.

Since we're using a base-10 (deciminal) system, using the divide or modulus operator on the number 10 will give you interesting results.

Take their example: Assume the user inputs 932.

932 % 10 = 2

932 / 10 = 93

Look what happened here, using % 10 you can find out what the last digit of the inputted number and using / 10 you are left with the remaining numbers.

Lets ask ourselves, what do we want to do here? We want to find the sum of adding up each digit in the inputted number right? So this implies we need a way of isolated each digit -- we just found a way of isolating the last number in 932 (2) with % 10! How could we isolate the 9 or the 3? This is where you have to think about loops. Programming is about getting the computer to do the monkey work, like repeating tasks.

What if the inputted number became 93, then we could use % 10 again to isolate the 3 right? And then if the inputted number became 9, using % 10 would isolate the 9. So if we change the inputted number and keep using % 10, we could isolate all three digits and add them up. In other words if we get the computer to loop a few operations we should be able to isolate each digit and add it to the sum.

I'll write it out in pseudo code...

sum = 0;
input = 0;
read input;

while (input &gt; 0)
  sum = sum + input % 10;
  input = input / 10;

print sum;

So the while loop here will go until the input is no longer greater than 0, this should essentially repeat for a maximum of 3 times if we divide by 10 each time through the loop based on an maximum value of 999 for the input. Go through the exercise of trying 932 with this pseudo code, what will be the value of sum and input after each pass through the loop.

I hope that helps. I think the whole point of this homework I think is to make you appreciate how to use loops and the / and % operators to come up with an algorithm to do some computations efficiently. There is more ways of solving this problem for sure, but they use other operations or they are probably less efficient.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.