javaalgorithmloopsformulacontrol-structure

Display the sum of all raised odd and even number using loop and control structures


I need to create a java program that will accepts 10 integers and if the entered number is odd raise it to ^ 1 power and if the user input another odd number raised it again to the next power ^ 2, same with Even numbers however the power starts at 10 and will decrease in each even numbers inputed. After that, I have to display the sum of all Even and Odd numbers that was raised.

The hard thing is were not allowed to use arrays, and java.util.Math.

For example:

The program ask the user to input 10 integers:

User inputted ( integers 1-10 ):

1 ^ 1 = 1 (odd)

2 ^ 10 = 1024 (even)

3 ^ 2 = 9 (odd)

4 ^ 9 = 262 114 (even)

5 ^ 3 = 125 (odd)

6 ^ 8 = 1 679 616 (even)

7 ^ 4 = 2 401 (odd)

8 ^ 7 = 2 097 152 (even)

9 ^ 5 = 59 049 (odd)

10 ^ 6 = 1 000 000 (even)

Output: The sum of Even Powered

The sum of Odd Powered

Thanks Devon, I tried to run the program but i have logical error.

public class SumOfOddEvenMain {

public static void main(String[] args) {
    SumOfOddEven temp = new SumOfOddEven();
    System.out.println(temp.Run());
}

}

import java.util.Scanner;

public class SumOfOddEven {
private long sumOfOdds;
private long sumOfEvens;
private int countOfOdds;
private int countOfEvens;

public SumOfOddEven() {
    countOfOdds = 1;
    countOfEvens = 10;
    sumOfOdds = 0;
    sumOfEvens = 0;
}
public String Run() {
    Scanner in = new Scanner(System.in);

    for(int i = 0; i < 10; i++){
        System.out.println("Enter integer number " + i);
        int number = in.nextInt();

        if(number % 2 == 0) {
            for(int j = countOfEvens; j > 1; j--) {
                number *= number;
            }

            sumOfEvens += number;
            countOfEvens--;
        }
        else {
            for(int k = countOfOdds; k > 1; k--) {
                number *= number;
            }

            sumOfOdds += number;
            countOfOdds++;
        }
    }

    in.close();
    return "Sum of odds: " + sumOfOdds + "\nSum of evens: " + sumOfEvens;
}

}

Program Output:

Enter integer number 0
1
Enter integer number 1
2
Enter integer number 2
3
Enter integer number 3
4
Enter integer number 4
5
Enter integer number 5
6
Enter integer number 6
7
Enter integer number 7
8
Enter integer number 8
9
Enter integer number 9
10
Sum of odds: -495568963
Sum of evens: 0

Solution

  • You can do it as follows:

    import java.util.Scanner;
    
    public class SumOfOddEven {    
        public static void main(String[] args) {
            int sumOddInts = 0, sumEvenInts = 0, countOddInts = 1, countEvenInts = 10;
            Scanner in = new Scanner(System.in);
            for (int i = 1; i <= 10; i++) {
                System.out.println("Count: " + i + ", enter an integer (from 1 to 10): ");
                int number;
                do {
                    number = in.nextInt();
                    if (!(number >= 0 && number <= 10)) {
                        System.out.println("Try again, enter an integer (from 1 to 10): ");
                    } else {
                        if (number % 2 == 0) {
                            sumEvenInts += power(number, countEvenInts--);
                        } else {
                            sumOddInts += power(number, countOddInts++);
                        }
                    }
                } while (!(number >= 0 && number <= 10));
            }
            in.close();
            System.out.println("The sum of even powered: " + sumEvenInts);
            System.out.println("The sum of odd powered: " + sumOddInts);
        }
    
        static int power(int n, int power) {
            int num = 1;
            for (int i = 1; i <= power; i++) {
                num *= n;
            }
            return num;
        }
    }
    

    Note: The program given above restricts the numbers, being raised to the power as per the requirement, to 1 to 10 to avoid integer overflow

    A sample run:

    Count: 1, enter an integer (from 1 to 10): 
    12
    Try again, enter an integer (from 1 to 10): 
    3
    Count: 2, enter an integer (from 1 to 10): 
    5
    Count: 3, enter an integer (from 1 to 10): 
    6
    Count: 4, enter an integer (from 1 to 10): 
    7
    Count: 5, enter an integer (from 1 to 10): 
    8
    Count: 6, enter an integer (from 1 to 10): 
    2
    Count: 7, enter an integer (from 1 to 10): 
    1
    Count: 8, enter an integer (from 1 to 10): 
    9
    Count: 9, enter an integer (from 1 to 10): 
    10
    Count: 10, enter an integer (from 1 to 10): 
    4
    The sum of even powered: 204688256
    The sum of odd powered: 59421