473,382 Members | 1,204 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to square a number with C++

4
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("??????");
May 10 '10 #1
7 22240
donbock
2,426 Expert 2GB
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?
May 10 '10 #2
shop
4
i would square it by its self..
May 10 '10 #3
shop
4
yes i understand but i want to know the coding.

in times table it would be:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     //Times table
  4.     printf("\n");
  5.  
  6.     int i, j, k, l;
  7.     int result; 
  8.     int result2;
  9.  
  10.     printf("Times table to follow\n");
  11.     printf("\n");
  12.  
  13.     for(i=0; i<12; i++)  //will go through it 12 times starting at 0
  14.     {
  15.         result = i*1;
  16.         result2 = i*2;
  17.  
  18.         printf("%d times 1 = %d    ", i, result);
  19.         printf("%d times 2 = %d\n", i, result2);
  20.     }
  21.  
  22. /* Doing the same with a while loop*/
  23.     i=0;
  24.  
  25.     while(i<=12)     // print times tables for 3 and 4
  26.     {
  27.         result = i*3;
  28.         result2 = i*4;
  29.  
  30.         printf("%d times 3 = %d        ", i, result);
  31.         printf("%d times 4 = %d\n",i, result2);
  32.         i++;
  33.     }
  34.  
  35. return 0;
  36.  
  37. }
May 10 '10 #4
shop
4
So how do i do it for squaring?? atleast a clear hint.. please
May 10 '10 #5
Banfa
9,065 Expert Mod 8TB
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.
May 10 '10 #6
donbock
2,426 Expert 2GB
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.
May 10 '10 #7
jkmyoung
2,057 Expert 2GB
a squared = a X a.
Put that into code.
May 11 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Elaine Jackson | last post by:
Is there a function that takes a number with binary numeral a1...an to the number with binary numeral b1...bn, where each bi is 1 if ai is 0, and vice versa? (For example, the function's value at...
15
by: Steven T. Hatton | last post by:
Did I mess something along the way, or is there no function in Standard C++ to raise an (unsigned) int to a power of (unsigned) int returning (unsigned) int? I never gave this a second thought...
21
by: Daniel Schüle | last post by:
Hello NG, I am wondering if there were proposals or previous disscussions in this NG considering using 'while' in comprehension lists # pseudo code i=2 lst= of course this could be easily...
3
by: aking | last post by:
Dear Python people, im a newbie to python and here...so hello! Im trying to iterate through values in a dictionary so i can find the closest value and then extract the key for that...
12
by: MC felon | last post by:
sorry for that! anyway.. how do i make a function that takes 2 numbers as arguments (1 as the number and the 2nd as the exponent) and raises that number to the power (stored as exponent) i...
35
by: fermineutron | last post by:
For a while now i have been "playing" with a little C program to compute the factorial of large numbers. Currently it takes aboy 1 second per 1000 multiplications, that is 25000P1000 will take...
18
by: atv | last post by:
at least to me it is. I can't figure out for the life what it is i'm doing wrong here. i have a function called assign_coordinate. usually, i pass a malloced pointer to it, then individual...
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
0
by: Dipanwita | last post by:
I have written a RSA encryption/decryption function in c using the formula a = b^c %n. For solving the equation I have used Squaring and multiplying method for modulo exponentiation . These...
6
by: Blue sky | last post by:
Hi ,I think the follow program is right in logical But why the compiler output :"square:declared identifier" #include<stdio.h> #include<math.h> int main() { double x1;
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.