TechArena Community Program to calulate the square of number

#1
10-12-2008
 Member Join Date: Nov 2008 Posts: 1,185
Program to calulate the square of number

I have made a C code that can replace the comma in a number by a decimal point. But my problem is that I also want to calculate the square of the number. Do you have any idea regarding the same?

Code:
```# include <stdio.h>
# include <stdlib.h>

int main (int argc, char *argv[])
{
char x = 0;

while (x != '\n')
{
scanf ( "%c", &x);
if (x == ',')
printf (".");
else
printf ( "%c", x);
}
return 0;
}```
#2
10-12-2008
 Member Join Date: May 2008 Posts: 271
Re: Program to calulate the square of number

I don't know much about C coding but I think you should use the atoi () function. It converts any ASCII variable to integer value.
#3
10-12-2008
 Member Join Date: Nov 2008 Posts: 1,185
Re: Program to calulate the square of number

Thank you XSI for your answer, and I changed the code a bit according to your opinion. But the function atoi () gives me part of the number (just the whole number) whereas I also want to retrieve the decimal.

Code:
```# include <stdio.h>
# include <stdlib.h>

int main ()
{
char t[10]={0};
int i;
scanf ( "% s", t);
for (i = 0; i <10; i + +)
{
if (t ==',')
t ='.';
}
printf ( "% s \ n", t);
printf ( "% d", atoi (t));
return 0;
}```
#4
10-12-2008
 Member Join Date: May 2008 Posts: 271
Re: Program to calulate the square of number

If you want both the whole number and the numbers after comma (i.e. decimal number) then use the function atof () and not atoi (). atof results in floating values.

 Tags: