reset password
Author Message
Anonymous
Posts: 166
Posted 22:50 Nov 09, 2013 |

Is there a way to do this under all the same class? 

/*

The sum of all even numbers between 2 and 100 (inclusive).

The sum of all squares between 1 and 100 (inclusive)

All powers of 2 from 20 up to 220

The sum of all odd numbers between a and b (inclusive), where a and b are inputs

The sum of all odd digits of an input. (For example, if the input is 32677, the sum would be 3 + 7 + 7 = 17.)

*/

public class  E6_1

{

    public static void main(String[] args)

    {

    int sum=0;

        for(int i=1;i<=100;i++)

        {

            if(i%2==0)

            {

                System.out.println(i);

                sum+=i;

            }

        }

    System.out.println(sum);

    }

}

I did the first one, but I don't know if I can continue in this or have to open a new class. 

I don't want to open multiple classes and have to upload every single one individually but if theres no other way, then I'll settle 

Last edited by Anonymous at 22:50 Nov 09, 2013.
Anonymous
Posts: 166
Posted 12:27 Nov 11, 2013 |

Why don't you just put multiple for-statements in the public class?

for(parameters)

{

}

[next loop]

for(parameters)

{

}

 

etc, etc...

 

You can put multiple loops in one class.

Last edited by Anonymous at 12:29 Nov 11, 2013.