Results 1 to 6 of 6

Thread: Square/cube root

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    1

    Square/cube root

    How do I allow a user to input a number and then make them select the number 1 to cube or number 2 to square it and produce the result? I don't want to use the math function but rather something like this to square it:
    result = var * 2;
    System.out.println(result);
    Any help in starting would be appreciated

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Square/cube root

    Hi tracy22... Welcome to the forums...

    This is an untested code for finding the squares and cubes:
    java Code:
    1. public class myJavaProgram {
    2.   public static void main(String[] args) {
    3.    
    4.     //read the number from the user and store it in variables. Here, I have hardcoded the values
    5.     int num1 = 2;
    6.     int num2 = 100;
    7.    
    8.     //calculating the Sqaure and Cube
    9.     int ResultSquare=num1*num1;
    10.     int ResultCube=num2*num2*num2;
    11.    
    12.     System.out.println("Square :" + ResultSquare);
    13.     System.out.println("Cube :" + ResultSquare);
    14.   }
    15. }

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Square/cube root

    Ah! You are asking for square root and cube roots ? I think, I read the question wrongly.

    Then try these links:
    1. http://www.homeschoolmath.net/teachi...-why-works.php
    2. http://www.homeschoolmath.net/teachi...-algorithm.php

    ...

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  4. #4
    Lively Member OT²O's Avatar
    Join Date
    Jan 2010
    Location
    Washington State
    Posts
    96

    Re: Square/cube root

    Here you go mate.
    Code:
    Math.sqrt()
    and
    Code:
    Math.cbrt()
    ex.
    Code:
    public class apples {
    	public static void main(String args[]){
    		System.out.println(Math.sqrt(100));
    		
    	}
    }
    Which would print out 10.0
    On the off chance that I helped you (Rate This Post){
    Also If your problem was resolved;
    }

  5. #5
    Lively Member OT²O's Avatar
    Join Date
    Jan 2010
    Location
    Washington State
    Posts
    96

    Re: Square/cube root

    Just realized you were not asking for the root. lol
    On the off chance that I helped you (Rate This Post){
    Also If your problem was resolved;
    }

  6. #6
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: Square/cube root

    Since you were explicitly not asking for the Math functions ...
    I guess you might be looking for an operator, in which case I have to disappoint you. Java has no power operator as is available in other languages, such as in C#.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width