Results 1 to 4 of 4

Thread: Program to calulate the square of number

  1. #1
    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. #2
    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. #3
    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. #4
    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.

Similar Threads

  1. What is the program to print table of given number?
    By Roxy_jacob in forum Software Development
    Replies: 5
    Last Post: 04-09-2011, 10:40 PM
  2. Shell program to reverse a number
    By Bottlenecked in forum Software Development
    Replies: 5
    Last Post: 12-01-2010, 02:20 PM
  3. How can I find square root using c program?
    By ScarFace 01 in forum Software Development
    Replies: 5
    Last Post: 06-01-2010, 02:16 PM
  4. C sharp program for swapping number
    By Sonam Goenka in forum Software Development
    Replies: 5
    Last Post: 31-12-2009, 02:55 PM
  5. What is the Program to find-out factorial of given number?
    By Sheenas in forum Software Development
    Replies: 5
    Last Post: 27-11-2009, 01:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,708,738.50725 seconds with 17 queries