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 2 of 2

Thread: How do I calculate power using for loop?

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

    Default How do I calculate power using for loop?

    Hello I need some help.

    How do I use for loop in order to calculate the power of a number?

    In here, I used Math.pow and I need to change it and use for loop instead.

    import java.util.Scanner;
     
    public class LoopActivity
    {
        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);
        }
    }

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

    Default Re: How do I calculate power using for loop?

    int ans2 = n2^x2;
    if you want to use for loop:
        public static double power(int base, int exponent) {
            double b = base;
            if(exponent <= 1) {
                for(int e = exponent; e < 1; e++) {
                    b /= base;
                }
            } else {
                for(int e = 1; e < exponent; e++) {
                    b *= base;
                }
            }
            return b;
        }
    it works only with integer exponent
    Last edited by andreaita44; May 4th, 2022 at 11:09 AM.

Similar Threads

  1. Using a for loop to calculate Pi
    By jtearwicker in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 12th, 2019, 01:09 PM
  2. Replies: 0
    Last Post: May 4th, 2013, 01:21 AM
  3. Game Loop, calculate the Frames Per Second.
    By Dirk94 in forum Threads
    Replies: 2
    Last Post: June 20th, 2012, 07:35 AM
  4. the java power
    By haygaurav in forum Java Theory & Questions
    Replies: 4
    Last Post: June 15th, 2011, 08:36 AM
  5. [SOLVED] Using for loop to calculate sine function (factorial issue)
    By Actinistia in forum Loops & Control Statements
    Replies: 2
    Last Post: March 11th, 2011, 03:38 PM