Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 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; 
}
Reply With Quote
  #2  
Old 10-12-2008
XSI XSI is offline
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.
Reply With Quote
  #3  
Old 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; 
}
Reply With Quote
  #4  
Old 10-12-2008
XSI XSI is offline
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Program to calulate the square of number"
Thread Thread Starter Forum Replies Last Post
What is the program to print table of given number? Roxy_jacob Software Development 5 04-09-2011 10:40 PM
Shell program to reverse a number Bottlenecked Software Development 5 12-01-2010 02:20 PM
How can I find square root using c program? ScarFace 01 Software Development 5 06-01-2010 02:16 PM
C sharp program for swapping number Sonam Goenka Software Development 5 31-12-2009 02:55 PM
What is the Program to find-out factorial of given number? Sheenas Software Development 5 27-11-2009 01:42 PM


All times are GMT +5.5. The time now is 07:02 AM.