Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Nested For Loop Problem

  1. #1
    Junior Member
    Join Date
    May 2022
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Nested For Loop Problem

    Hello I am new in nested looping and I need help with my nested for loop assignment, Here's the instruction and what I have done so far:

    Create a Java program that accepts integer input from the user. Loop the input until to the last value. Generate a power value of each value from the range of input. For odd numbers, the power value is increasing range from 1 to the last odd number. For the even numbers, the power value is decreasing starts from the total of even numbers until 1.
    Example:

    The value input from the user is 6.

    The program displays the range with corresponding power and total.

    1^1=1---------------- ODD (1x1=1)------------Power of 1 - for odd is increasing
    2^3=8---------------- EVEN (2x2x2=8)---------Power of 3 - for even is decreasing
    3^2=9---------------- ODD (3x3=9)------------Power of 2 - for odd is increasing
    4^2=16--------------- EVEN (4x4=16)----------Power of 2 - for even is decreasing
    5^3=125------------- ODD (5x5x5=125)-------Power of 3 - for odd is increasing
    6^1=6---------------- EVEN (6x1=6)------------Power of 1 - for even is decreasing

    The sum of ODD increasing power is: 135 ( 1+9+125 = 135) - sum all the odd results.
    The sum of EVEN decreasing power is: 30 (8+16+6=30) - sum all the even results.

     int loopRange = scanner.nextInt();
                 int sumOdd = 0, sumEven = 0;
     
                     for(int x = 1; x <= loopRange; x++){
                            if (x % 2 == 1){
                                int base = x;
                                int ansOdd = 1;
                                int y;
     
                                for(y = 1; y <= 3 ; y++){
                                    ansOdd *= base;
                                    sumOdd += ansOdd;
                            }
                            System.out.println(base + "^" + y + "=" + ansOdd);
     
                            }
                            else if  (x % 2 == 0){
                                int base = x;
                                int ansEven = 1;
                                int y;
     
                                for(y = 3; y <= 1; y--){
                                    ansEven *= base;
                                    sumEven += ansEven;
                            }
                            System.out.println(base + "^" + y + "=" + ansEven); 
     
                        }
                    }
                        System.out.println("The sum of ODD increasing power is: " + sumOdd);
                        System.out.println("The sum of EVEN decreasing power is: " + sumEven);
            }   
    }

    The output is this:

     

    Please enter loop range: 10
    1^4=1
    2^3=1
    3^4=27
    4^3=1
    5^4=125
    6^3=1
    7^4=343
    8^3=1
    9^4=729
    10^3=1
    The sum of ODD increasing power is: 1415
    The sum of EVEN decreasing power is: 0




    But I want the output to be like this:

     

    Please enter loop range: 10
    1^1=1
    2^5=32
    3^2=9
    4^4=256
    5^3=125
    6^3=216
    7^4=2401
    8^2=64
    9^5=59049
    10^1=10
    The sum of ODD increasing power is: 61581
    The sum of EVEN decreasing power is: 578


    Last edited by Maureen22; May 3rd, 2022 at 07:17 AM.

  2. #2
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: Nested For Loop Problem

    package me.andreaita44;
     
    import java.util.Scanner;
     
    /**
     *
     * @author andrea.campostrini
     */
    public class Test {
     
        public static void main(String[] args) {
     
            /*------------------------------------------*/
            Scanner scanner = new Scanner(System.in);
            int loopRange = 0, sumOdd = 0, sumEven = 0, n1 = 1, n2 = 2, x1 = 1;
            /*------------------------------------------*/
     
     
            //if loopRange is less than 0 the loop doesn't work.
            do {
                System.out.print("Please enter loop range: ");
                loopRange = scanner.nextInt();
                if(loopRange < 0) {
                    System.out.println("\nError: Invalid loop range.\n\tPlease enter a number equal or grater than 0.\n");
                }
            } while(loopRange < 0);
     
     
     
            for(int x2 = loopRange/2 ; n1 <= loopRange ; x2--, x1++) {
                //odd
                int ans1 = (int) Math.pow(n1, x1);
                System.out.println(n1 + "^" + x1 + "=" + ans1);
                sumOdd += ans1;
                n1 += 2;
     
     
                //even
                if(n2 <= loopRange) {
                    int ans2 = (int) Math.pow(n2, x2);
                    System.out.println(n2 + "^" + x2 + "=" + ans2);
                    sumEven += ans2;
                    n2 += 2;
                }
            }
     
     
            System.out.println("The sum of ODD increasing power is: " + sumOdd);
            System.out.println("The sum of EVEN decreasing power is: " + sumEven);
        }
    }
    Last edited by andreaita44; May 3rd, 2022 at 12:43 PM.

  3. The Following User Says Thank You to andreaita44 For This Useful Post:

    Maureen22 (May 4th, 2022)

  4. #3
    Junior Member
    Join Date
    May 2022
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Nested For Loop Problem

    Thank you for correcting my work sir/miss, but I forgot to mention that we are not allowed to use Math.pow in this problem.

Similar Threads

  1. Funky triple nested loop problem. (Due tonight)(Confusing)
    By goodtanner in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 10th, 2014, 08:00 PM
  2. Please Help: Need Help with my Nested Loop
    By hiroprotagonist in forum Loops & Control Statements
    Replies: 1
    Last Post: November 24th, 2012, 10:43 AM
  3. Help me out with this nested for loop plz..
    By samadniz in forum Loops & Control Statements
    Replies: 3
    Last Post: September 3rd, 2012, 10:00 AM
  4. Having some nested loop problem
    By joel1314 in forum Loops & Control Statements
    Replies: 13
    Last Post: June 3rd, 2012, 05:04 AM
  5. a problem with a code... (for loop and an "if" nested..)
    By kobi1988 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 13th, 2011, 12:59 PM

Tags for this Thread