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

Thread: Java program to find n power of any number

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java program to find n power of any number

    Hey guys...I'm trying to make a java program that gets 2 numbers and the 2nd number is the exponent of the first. Example: the first number is 3 and the second number is 2. The value returned will be 3 to the 2 power, or 9. Please make use of if/else statements, while loops, for loops, arrays, and system.out.printlns. Thanks sooooooo much u guys. If u can't use the things I stated it's okay.


  2. #2
    Junior Member
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pleaseeeee help with this programmm!!!!

    also when you write the code, please add comments to it to describe the process of each line

  3. #3
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: Pleaseeeee help with this programmm!!!!

    wait for my code
    Last edited by Truffy; June 10th, 2009 at 09:35 PM.

  4. #4
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: Pleaseeeee help with this programmm!!!!

    Hi Eldiablo,

    Welcome to the forums.

    Heres the sample code. Hope it gives you a hint and idea.

     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
     
    public class ElDiablo {
     
        private static BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
     
        public  static void main(String[] args)throws IOException
     
         {
     
        String number1,exp; 
        double num1, n,num2,  result = 0;
     
        System.out.print("Enter a number : ");
        number1=br.readLine();
        num1=Double.parseDouble(number1);
     
        System.out.print("Enter the second number : ");
        exp=br.readLine();
        num2=Double.parseDouble(exp);
        n= Double.parseDouble(exp);
        //you can add or play with this part. :) create your own logic in here so that you will not use the math   //function
    //    for(double i=0; i<=num2; i++){
    //        result = num1 * num1;
    //    }
     
    System.out.println("Using the Math fucntion" +Math.pow(num1, n));
    //    System.out.println("Not using the Math function : " +result);
     
     
          }
     
    }

  5. The Following 2 Users Say Thank You to Truffy For This Useful Post:

    Eldiablo (June 10th, 2009), JavaPF (January 9th, 2010)

  6. #5
    Junior Member
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pleaseeeee help with this programmm!!!!

    Hey thanks a lot man. You saved my life.

  7. #6
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Thumbs up Re: Pleaseeeee help with this programmm!!!!

    Your welcome. Im a lady anyway. please mark as solved if you are satisfy with the answer.

  8. #7
    Junior Member
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pleaseeeee help with this programmm!!!!

    Oh wow lol I'm so very sorry. Thanks again

  9. #8
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: Pleaseeeee help with this programmm!!!!

    No problem with that.

  10. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Pleaseeeee help with this programmm!!!!

    Hello Eldiablo,

    Welcome to the Java Programming Forums.

    Good work Truffy.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  11. #10
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Smile Re: Pleaseeeee help with this programmm!!!!

    Quote Originally Posted by JavaPF View Post
    Hello Eldiablo,

    Welcome to the Java Programming Forums.

    Good work Truffy.
    Thank you.

    Cheers,
    Truffy