im dying here..on a simple code
the question says
1. Write a program called SumDigits that obtains an integer from the user and then sums the digits. The program should use the remainder and integer division operators.
i understand the question but i dont get how to do it...could anyone help me? please?
Re: im dying here..on a simple code
Code java:
int x = 15; //number to sum
int sum = 0;
while(x > 0)
{
sum += x%10;
x /= 10;
}
System.out.println(sum);
As for the user input, you should be able to do it yourself.
All I did was a quick google search and I found the answer. Source http://www.dreamincode.net/code/snippet2727.htm
Re: im dying here..on a simple code
Ok, well we help step you through it, but not do it for you. So, I will provide you with the questions you need to ask yourself. Tell us what you know about each of these questions and provide code snippets and code examples for us to work with and help you better understand. Here are the questions (some are very basic, but are important to both creating the program and for us to gauge how much you already know):
1) How do I set up a program?
2) How do I prompt the user?
3) How do I receive and process a response from the user?
4) How do I sum integers?
5) How do I get a remainder?
6) How do I divide integers?
7) How do I return the results to the user?
Answer as many of these 7 questions as you can, in as much detail as you can. We can then help you with the scraps that you need to pick up.
Re: im dying here..on a simple code
Quote:
Originally Posted by
Brt93yoda
Code java:
int x = 15; //number to sum
int sum = 0;
while(x > 0)
{
sum += x%10;
x /= 10;
}
System.out.println(sum);
As for the user input, you should be able to do it yourself.
All I did was a quick google search and I found the answer. Source
http://www.dreamincode.net/code/snippet2727.htm
thanks alot...i really really appreciate it <3