user2989955
user2989955

Reputation:

How to calculate exponent with loop?

I am currently working on a program that calculates the power of certain numbers. The number limit is 1 to 9. My code is posted below. I have the following issues:

  1. Every time I run the program it doesn't print the correct answer.

  2. I want to modify the code so the application calculates X to power of Y, where X and Y are allowed to be integers in the range 1 to 9 (including 9). If the user enters an invalid value the program should ask the user for input again. When a user is done with entering the values for base and exponents, the program will print the result.

Conditions of this task is that I must use loops to calculate the result by doing several multiplications; I am not allowed to use any available method or API that calculates the result for me. Please help me come up with the solution.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exponent;
//import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int b,e;

    System.out.println("Enter the base");
    b = Integer.parseInt(br.readLine());
    System.out.println("Enter the power");
    e = Integer.parseInt(br.readLine());
    int t = 1;
    for(int i = 1;i <= e; i++);
    {
        t=t*b;
    }
    System.out.println(t);

    }
    // TODO code application logic here
}

Upvotes: 0

Views: 21859

Answers (4)

user2984602
user2984602

Reputation: 89

For the first part, it is just happening because you placed a semicolon after loop's declaration, which java just loops to that semicolon and nothing more. By removing semicolon the loop should work. However, for the second part, you can just add inputcheck method, as shown in my code below.

import java.io.*;
public class abc {
public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int b, e;
    System.out.println("Enter the base");
    b = check(Integer.parseInt(br.readLine()));
    System.out.println("Enter the power");
    e = check(Integer.parseInt(br.readLine()));
    int t = 1;
    for (int i = 1; i <= e; i++); {
        t = t * b;
    }
    System.out.println(t);
}

private static int check(int x) {
    while (x < 1 || x > 10)
        x = Integer.parseInt(br.readLine());
    return x;
}

Upvotes: 0

Matthew S.
Matthew S.

Reputation: 721

For the first part, its is a easy fix. You just added a semicolon where there shouldn't be in the for loop.

for(int i = 1;i <= e; i++); {

for(int i = 1;i <= e; i++){ //There should be no semicolon here

For the second part, you can do it with two very easy do-while loops.

//Replace this
System.out.println("Enter the base");
b = Integer.parseInt(br.readLine());
System.out.println("Enter the power");
e = Integer.parseInt(br.readLine());

//with
do{
    System.out.println("Enter the base");
    b = Integer.parseInt(br.readLine()); 
}while(b > 9 || b < 1);

do{
    System.out.println("Enter the power");
    e = Integer.parseInt(br.readLine()); 
}while(e > 9 || e < 1);

So the do-while loops, will first ask for the base or the power (Depending where in the code the program is running), then it will set the int to the value. If the value is greater than 9, ie: 10 or above, the program will reask for the base or power (Like I said, depended which loo is running), and then it will set the int again. It will do this, until the value is under 10. Like you want.

Here is an example of the output:

Enter the base
56
Enter the base
-4
Enter the base
4
Enter the power
67
Enter the power
10
Enter the power
-8
Enter the power
7
4 to the 7th power is 16384

If the code snippets are confusing, here is the entire compilable, working class:

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class StackOverflowAnswers{
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int b,e;

        do{ //Asks for the base
            System.out.println("Enter the base");
            b = Integer.parseInt(br.readLine()); 
        }while(b > 9 || b < 1); //If the base is not valid, it goes back to the "do" statement, which asks for the base, again.

        do{ //Asks for the power
            System.out.println("Enter the power");
            e = Integer.parseInt(br.readLine()); 
        }while(e > 9 || e < 1); //If the power is not valid, it goes back to the "do" statement, which asks for the power, again.

        int t = 1;

        for(int i = 1;i <= e; i++){ //No semicolon here
            t=t*b;
        }
        System.out.println(b + " to the " + e + "th power is " + t); //Just added some words and the base and the power for easier debugging and understanding.
    } 
}

Hope this helps.

Upvotes: 0

imulsion
imulsion

Reputation: 9040

For a start, there should be no semi colon after the for loop:

for(int i=1;i<=e; i++ )
        {
            t=t*b;
        }

A simple input test could be something along the lines of:

public boolean testInput(int e)
{
if(e>9||e<1)//where e is the inputted number
{
return false
}
else 
{
return true;
}

}

Then use it like this:

  boolean valid = false;
  while(valid!=true)
  {
  e = Integer.parseInt(br.readLine());
  if(testInput(e)==false)
  {
  System.out.println("Please enter a number between 1 and 9")
  continue;
  }
  else
  {
  valid = true;
  }
  }

Upvotes: 6

Mengjun
Mengjun

Reputation: 3197

Remove semi colon from for-loop

From

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

to

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

Upvotes: 0

Related Questions