How do you add odd numbers up in Java?

  • Comp Sci
  • Thread starter KillaKem
  • Start date
  • Tags
    Java Numbers
In summary, The program described is attempting to add all odd numbers in a given number, but is not functioning correctly. The individual seeking assistance is unsure of why the program is not working and suggests printing more values or using a debugger to troubleshoot. Additionally, there is a discrepancy in the desired output, as the example given adds up to 9 instead of 8.
  • #1
KillaKem
23
0
I want to add all odd numbers in a number n(ie if n=123456, then i am lookin' for 1+3+5=8)
I have written a small program and it won't work i don't know why!How do i solve my prob?

public class Odd
{
public static void main(String[] args)
{
int n = 3; // just set n to any number

int x = 1;
int num_digits = 0;

while (n/x != 0)
{
x *= 10;
num_digits++;
}

x = 0;

int num;
int sum = 0;

while ( x < num_digits );
{
num = n%10;
n = n/10;

if ( num%2 != 0)
{
sum = sum + num;
}
x++;
}


System.out.printf("%d%n", sum);

}
}
 
Physics news on Phys.org
  • #2
KillaKem said:
I have written a small program and it won't work i don't know why!
So fix that! Try printing a lot more things, to see if they are giving the results you expect or not. Or use a debugger to step through and monitor what value things have.
 
  • #3
KillaKem said:
I want to add all odd numbers in a number n(ie if n=123456, then i am lookin' for 1+3+5=8)
1 + 3 + 5 = 9, not 8.
 

Related to How do you add odd numbers up in Java?

1. How do I add odd numbers up in Java using a loop?

In order to add odd numbers up in Java using a loop, you can use a for loop with a counter variable starting at 1 and incrementing by 2 each time. Within the loop, you can add the current value of the counter variable to a separate variable that will keep track of the sum. The loop should run until the counter variable reaches the desired number of odd numbers to add.

2. Can I add odd numbers up in Java without using a loop?

Yes, you can add odd numbers up in Java without using a loop. One way to do this is by using the formula for the sum of an arithmetic sequence, which is (n/2)(2a + (n-1)d), where n is the number of terms, a is the first term, and d is the common difference. In this case, the first term would be 1 and the common difference would be 2, since we are adding odd numbers.

3. How can I add odd numbers up in Java without using any math formulas?

If you do not want to use any math formulas, you can also add odd numbers up in Java by using a recursive function. This function would take in the starting number and the desired number of odd numbers to add. The function would add the starting number to the result, and then call itself with the next odd number as the starting number until the desired number of odd numbers is reached.

4. Is there a built-in function in Java for adding odd numbers?

No, there is not a built-in function in Java specifically for adding odd numbers. However, there is a built-in function for finding the sum of an array of numbers, which can be used to add odd numbers by passing in an array of only odd numbers.

5. Can I add odd numbers up in Java with negative numbers?

Yes, you can add odd numbers up in Java with negative numbers. As long as you follow the same principles of adding odd numbers, the result will be the same regardless of the sign of the numbers. For example, -3 + -1 + 1 + 3 would still result in 0.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
3
Replies
80
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top