How to square a number with C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shop
    New Member
    • May 2010
    • 4

    How to square a number with C++

    what is the simplest or the plain code for squaring a number?

    i.e

    in a loop form

    for(i=1; i<20; i++) //shows squaring output of 1 up to 20
    int x; //
    int y; //
    1
    ..
    20 // in a loop form, how do i square??

    variables..
    codes....
    printf("??????" );
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Are you familiar with using arithmetic operators (add, subtract, multiply, divide) in an expression?
    Forget about programming for a moment, how would you manually square a number using a simple 4-function calculator?

    Comment

    • shop
      New Member
      • May 2010
      • 4

      #3
      i would square it by its self..

      Comment

      • shop
        New Member
        • May 2010
        • 4

        #4
        yes i understand but i want to know the coding.

        in times table it would be:

        Code:
        int main()
        {
        	//Times table
        	printf("\n");
        	
        	int i, j, k, l;
        	int result; 
        	int result2;
        	
        	printf("Times table to follow\n");
        	printf("\n");
        
        	for(i=0; i<12; i++)  //will go through it 12 times starting at 0
        	{
        		result = i*1;
        		result2 = i*2;
        		
        		printf("%d times 1 = %d	", i, result);
        		printf("%d times 2 = %d\n", i, result2);
        	}
        	
        /* Doing the same with a while loop*/
        	i=0;
        
        	while(i<=12)     // print times tables for 3 and 4
        	{
        		result = i*3;
        		result2 = i*4;
        
        		printf("%d times 3 = %d		", i, result);
        		printf("%d times 4 = %d\n",i, result2);
        		i++;
        	}
        
        return 0;
        
        }
        Last edited by Banfa; May 10 '10, 05:52 PM. Reason: Please use [code]...[/code] round code you post

        Comment

        • shop
          New Member
          • May 2010
          • 4

          #5
          So how do i do it for squaring?? atleast a clear hint.. please

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            When posting code please use [code]...[/code] tags round it to format it.


            I think the problem is simpler than you imagine in your last code listing you only need to change a single character on the result calculation lines to go from times table to square output.

            From post #3 I guess you know how to square a number all you have to do is write that formula into your code.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              In mathematical notation, exponentiation is typically represented by a superscript (for squaring, the superscript is "2"). C and C++ do not have an exponentiation operator so you need to find another way to describe the exponentiation function.

              Forget about programming for a moment, suppose you have a simple 4-function calculator. It has buttons for addition, subtraction, multiplication, and division; but no buttons for exponentiation or squaring. How would you compute "9 squared" on this calculator?

              C/C++ expressions support those same four functions; so use the same computation method in your expression statement that you used on the calculator.

              Comment

              • jkmyoung
                Recognized Expert Top Contributor
                • Mar 2006
                • 2057

                #8
                a squared = a X a.
                Put that into code.

                Comment

                Working...