Modulus Operator??

Hi Im new to working with Java, im having trouble with trying to multiply all the odd numbers between 1 and 100. I think i have it right to do even numbers but am i supposed to use the modulus operator for odd numbers? Heres what ive been trying to work on. Thanks, Shannon.


EVEN

import javax.swing.JOptionPane;
public class Sum{
public static void main(String args[])
{
int sum = 0;

for (int number = 1; number <= 100; number +=2)
sum *= number;

JOptionPane.showMessageDialog(null, "The sum is " + sum, "Multiply even Integers from 1 to 100", JOptionPane.INFORMATION_MESSAGE );

System.exit (0) ; //terminate the application

}

}



ODD


import javax.swing.JOptionPane;
public class Sum2{
public static void main(String args[])
{

int[] numberArray = new int[100];
int sum = 0;



for (int number=1; number<100; number++){
if ((numberArray[0] % 2 ) > 0)

sum *= number;
JOptionPane.showMessageDialog(null, "The sum is " + sum, "Multiply Odd Integers from 1 to 100", JOptionPane.INFORMATION_MESSAGE );
}

{
System.exit (0) ; //terminate the application

}
}

}

Comments

  • : int sum = 0;

    0 * anything = 0

    Try starting from 1


    The modulus op is a good way to tell if a number is odd or even. It gives the remainder of a division.
    [code]
    public static boolean isEven(int n) {
    return (n % 2) == 0;
    }
    [/code]

    ---------------------------------
    [size=1](Its just my sig)
    HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion