Thread: square of 2 numbers

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Question square of 2 numbers

    I want to add square of 2 numbers and get the average...........
    but dont know to get the square of a number in C
    please post the keyword to get square or cube.........thanks

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Code:
    File Edit Options Buffers Tools C Help                                                                                                                                                                            
    #include <stdio.h>
    #include <math.h>
    
    int main() {
      int a = 2;
      int b = 3;
      int square  = pow(a, 2) + pow(b, 2);
      int average = square / 2;
      printf("square == %d || average == %d\n", square, average);
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by raveen1001 View Post
    I want to add square of 2 numbers and get the average...........
    but dont know to get the square of a number in C
    please post the keyword to get square or cube.........thanks
    Sounds like a job for pow(x,y).

    Code:
    #include math.h
    #include stdio.h
    
    int main(void) {
      double x, y;
      x = 2.0;
      y = 3.0;
    
      printf("%lf raised to %lf is %lf", x, y, pow(x,y));
    
      return 0;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How does using pow really help? If they're just squaring, they don't need a function for it. Square a number in C the same way you do in math. Multiply it by itself.
    Code:
    foosquared = foo * foo;

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    He mentioned getting the cube of a number also. I inferred from that, and the the tone of his post, that he didn't want to just use number * number, for his answers.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    By this if he want to do x^n then he have to write

    Code:
    const int ans = x * x * x * x * x ............ n times of what ?????

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for( x = 0, end = start; x < pow; x++ )
        end *= start;
    You could just use a loop.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    wat is the use of pow anyway?..just newby

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    pow

    Grab a book or a man page. Or use a search engine. Sheesh.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    pow() is an API provided by <math.h>

    lets say u want to do 7 ^ 8 then u just simply call this API pow(7, 8)

    u have to decide which is better

    Code:
    int result = 1;
    for (unsigned int index = 0; index < 8; ++index) {
      result *= 7;
    }
    OR
    unsigned int result = pow(7, 8);
    Code:
    
    
    or on shell just say man pow

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RockyMarrone
    u have to decide which is better
    There is a third option: use a version of pow for integral exponents that computes x^n more efficiently than just multiplying x n times.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use a lookup table!


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by quzah
    Use a lookup table!
    That is probably not generally feasible though... but if we want to actually compute 7^8 at compile time, I'll probably either whip out my calculator if I cannot (be bothered to) do it mentally, or just bite and use
    Code:
    const int foo = 7 * 7 * 7 * 7 * 7 * 7 * 7 * 7;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    even i dont understand why i will go for lookup table as i m having the reach to math.h

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by laserlight View Post
    That is probably not generally feasible though...
    It wasn't really as a feasible suggestion, but since you were talking about integers, it was a possible solution. Possible, not feasible. Although really, if you wanted to, do it as a binary file, generate it, and use fseek to find your answer.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to reverse numbers with sizeof operator
    By JoelearningC in forum C Programming
    Replies: 13
    Last Post: 03-09-2008, 11:53 AM
  2. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM
  3. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  4. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM
  5. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 04:31 AM