Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 921217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:50:19+00:00 2026-05-15T18:50:19+00:00

I have made a program which converts numbers entered into a string into an

  • 0

I have made a program which converts numbers entered into a string into an integer like atoi does, but its giving wrong output.

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
void main(void)
{
 static int sum;
 int i,x,y,z;
 char string[10];
 printf("Enter a string:\n");
 gets(string);
 x=strlen(string);
 for(i=0; ;i++)
 {
  if(string[i]=='\0')
  {
   break;
  }
  y=pow(10,i);
  z=string[x-i+1]*y;
  sum+=z;
 }
 printf("%d",sum);
 getch();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:50:20+00:00Added an answer on May 15, 2026 at 6:50 pm

    Ok. Here is a quick review of your code. Comments embedded.

    #include<stdio.h>
    

    Leave a space between #include and <stdio.h>.

    #include<conio.h>
    

    This is a non-standard Windows-only header that you don’t need. Don’t include this.

    #include<math.h>
    #include<string.h>
    

    Again use a space, when including your headers.

    void main(void)
    

    While this is legal, it is more common to find the signature int main(int argc, char* argv[]) as the signature for the main function. I would suggest that you use that signature.

     {
         static int sum;
    

    Why are you making this static? Are you planning to invoke main repeatedly and have the previous result for sum persist from one invocation of main to another? If not, then don’t make it static.

     int i,x,y,z;
     char string[10];
    

    Consider allocating more space for your string. Ten characters is quite small. Also consider creating a variable to represent the size of your string, rather than using a magic number, since you will likely have to reference the buffer size in multiple places.

    printf("Enter a string:\n");
    gets(string);
    

    No. Don’t do that!!! The function gets is a major security vulnerability!. It makes your program susceptible to buffer overflow attacks. Instead, use fgets, and specify the size of the buffer that you want to fill, so that it doesn’t overrun your buffer. You should never, ever use plain gets.

    x=strlen(string);
    

    Consider choosing a more descriptive name for x. Perhaps len. It is perfectly ok (and good) to create variables that have identifiers longer than a single letter.

    for(i=0; ;i++)
    {
      if(string[i]=='\0')
      {
         break;
      }
    

    Consider putting the termination condition in the for-loop; for(i = 0; string[i]!='\0'; i++).

      y=pow(10,i);
      z=string[x-i+1]*y;
    

    Hint: there is a smarter way to do this than using pow.

      sum+=z;
     }
     printf("%d",sum);
    

    Ok. The above is fine, although you might want to use “%d\n”.

     getch();
    

    You really shouldn’t be doing this on all systems. Instead, do:

    #ifdef _WIN32
        system("pause");
    #endif
    

    If possible, though, I would suggest you avoid that weird pausing behavior. Suppose your professor uses an automated script to validate the output of your program. Putting any sort of pause in the program (even on Windows), will break such a script. If you don’t want the terminal window to disappear while on Windows, you should invoke your program from the command prompt.

    }
    

    If you were to change the signature to something returning int as I suggested, then you would want to add the statement return 0; before the end of the function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 449k
  • Answers 449k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could use the RANK() or DENSE_RANK() features to get… May 15, 2026 at 8:07 pm
  • Editorial Team
    Editorial Team added an answer You can use recursive modules. Language manual uses precisely the… May 15, 2026 at 8:07 pm
  • Editorial Team
    Editorial Team added an answer I think this is exactly what you should do, loop… May 15, 2026 at 8:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.