|
-
October 1st, 2006, 07:05 PM
#1
A few things...
Ok so just a few code tidbits needed - a function to add all the odd integers from 1 to n, a function that adds all the integers up from 1 to a number equalling that number such as 1 + 2 + 3 + 4 if they enter 10, and a function that adds the digits of a negative number such as 3 if the number is -12 or 5 if the number is -23.
Thanks please
-
October 2nd, 2006, 01:43 AM
#2
Re: A few things...
Hi,
Have you tried to do this by yourself yet? If so please post your code and say what you are having a problem with and then we'll be able to help you.
Byron Tymvios
Please use [ CODE ] and [/ CODE ] tags when posting code! See THIS on how to use code tags.
-
October 2nd, 2006, 06:18 PM
#3
Re: A few things...
They aren't all that hard to do.
Took me 30 minutes this morning to cobble some things together...
-
October 2nd, 2006, 08:39 PM
#4
Re: A few things...
I got the first two but for the next question, which is really just finding the sums of a NONnegative number, I jotted down the stuff below then got too confused:
public class test{
int i = 0;
public static int lube(int n){
while (i < n){
i = (n / 100) + (n / 10 % 10) + (n % 10);}
return i;}}
Is it something like that?
-
October 2nd, 2006, 09:04 PM
#5
Re: A few things...
Can you please elaborate on your last assignment?
-petter
-
October 2nd, 2006, 09:11 PM
#6
Re: A few things...
 Originally Posted by Frankmms
I got the first two but for the next question, which is really just finding the sums of a NONnegative number, I jotted down the stuff below then got too confused:
public class test{
int i = 0;
public static int lube(int n){
while (i < n){
i = (n / 100) + (n / 10 % 10) + (n % 10);}
return i;}}
Is it something like that?
Have a look at this:
Code:
int addDigits(int num)
{
int sum = 0;
while(num > 0)
{
sum += (num % 10); // gets the last digit and adds it to the sum
num = num / 10; //relies on integer division to move the number down.
}
return sum;
}
-
October 3rd, 2006, 04:34 AM
#7
Re: A few things...
You could also convert the number to a String, strip of the minus sign, convert all the characters back to integers and then add them.
Code:
public int addDigits(int num){
String s = String.valueOf(num);
String[] chars = s.split("");
int sum = 0;
for(int i = 0; i < chars.length; i++){
try{
sum += Integer.parseInt(chars[i]);
}catch(NumberFormatException nfe){
//Catch block will catch the
//Exception that gets thrown when
//trying to convert the minus sign
//to an int and will ignore it
}
}
return sum;
}
Byron Tymvios
Please use [ CODE ] and [/ CODE ] tags when posting code! See THIS on how to use code tags.
-
October 3rd, 2006, 08:43 PM
#8
Re: A few things...
Taking the sum and dividing it by 10 each time the loop runs then modding to get the last number looks like the easiest way to do it Thanks all.
-
October 3rd, 2006, 09:14 PM
#9
Re: A few things...
Haha back again...One day this thread will be hundreds of pages long :O
Ok so I'm supposed to create a triangle out of ***s, given a number of rows, so if you put in 3, it makes the triangle 3 rows tall, 4 is 4 rows tall, etc, looking like:
3 spaces *
2 spaces ***
1 space *****
0space*******
I have no idea how to go about even starting this
Last edited by Frankmms; October 4th, 2006 at 04:23 PM.
-
October 4th, 2006, 04:09 AM
#10
Re: A few things...
A new problem should start a new thread...
Think about how you would do it if you had to use a typewriter and paper (i.e. line by line). Then translate those steps into code.
Give me a fish and I eat for a day. Teach me to fish and I eat for a lifetime...
Chinese proverb
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
October 4th, 2006, 03:35 PM
#11
Re: A few things...
Ok so I'm thinking
public int makeTriangle(int rows){
for (i=1; i > rows; i++){
System.out.println("Okay now I get lost");}}
-
October 4th, 2006, 03:40 PM
#12
Re: A few things...
 Originally Posted by Frankmms
Ok so just a few code tidbits needed - a function to add all the odd integers from 1 to n, a function that adds all the integers up from 1 to a number equalling that number such as 1 + 2 + 3 + 4 if they enter 10, and a function that adds the digits of a negative number such as 3 if the number is -12 or 5 if the number is -23.
Thanks please 
Well, to become a programmer you need to know basic math. The one builds on the other, stupid.
-
October 4th, 2006, 03:42 PM
#13
Re: A few things...
It's not math I have trouble with, it's converting math into Java, stupid.
-
October 4th, 2006, 03:46 PM
#14
Re: A few things...
 Originally Posted by Frankmms
It's not math I have trouble with, it's converting math into Java, stupid.
So what's the problem then, smartie?
-
October 4th, 2006, 04:21 PM
#15
Re: A few things...
It's not math I have trouble with, it's converting math into Java, stupid.
I also don't get the adding spaces in front of the ***s to make it a centered triangle, maybe another loop, but that's the problems I'm having.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|