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 8775833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:54:11+00:00 2026-06-13T18:54:11+00:00

I tried making a C program to convert a user entered decimal number to

  • 0

I tried making a C program to convert a user entered decimal number to octal. I wrote the C code with my own logic without any research of how other users try to do it. It works fine for the number 601 and some other numbers but for most numbers it returns the octal equivalent with the last digit being 1 less than it should be. For 75 it returns 112 instead of 113. I realize using printf with %o gets the job done but it’s kind of defeating the purpose of learning to program. Here’s my code:

#include <stdio.h>
#include <math.h>

/* converting decimal to octal */

int main() 
{
    int n,x,y,p,s;
    printf("Enter a decimal number ");
    scanf("%d",&x);
    s=0;p=0;

    while (x!=0)
    {
         y=x%8;
         s=s+y*pow(10,p);
         x=(x-y)/8;
         p=p+1;
    }  

  printf("the octal equivalent is: %d\n",s);
  getch();
  return 0;
}
  • 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-06-13T18:54:13+00:00Added an answer on June 13, 2026 at 6:54 pm

    That’s an interesting way to do it. I’ll give an up-vote on the question just for originality.

    Fix the problem by adding a fudge factor:

    s = s + y*pow(10,p) + 0.1;
    

    The problem is floating point accuracy. One of those pow calculations is returning something like 99.99999999999992 instead of 100. Converted back to int loses the 0.999… fraction and gets 99 instead of 100. Or 999 instead of 1000. It’s not in the one’s place because a zero exponent is a special case that gets 1.0 exactly.

    By the way, since you can only get 9 digits in a 32-bit int, so susmit’s suggestion of multiplying out the power of 10 in int form is quite reasonable. Except in special cases, pow will compute pow(x,y) as exp(y*log(x)). exp() and log() tend to be expensive computations.

    Even more by the way, I’m unable to reproduce that problem, even by forcing exp and log computations. I get values of (10 + 1.8e-15), (100 + 4.3e-14) and (1000 + 6.8e-13), all rounding correctly because the error as all positive. This is on a x86 and I believe I compiled this calculator with VC++2008. What compiler are you using?

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

Sidebar

Related Questions

I am trying to convert some pre-existing html/JavaScript files to Flex. I tried making
I just installed Ubuntu and tried making the famed Hello World program to make
I have tried making (my first) a C# program: using System; using System.Collections.Generic; using
I tried making a program that flips a coin(shows image of heads first and
I am making a Java program. I have a BigInteger number and I need
I'm making a program which is getting inputs from the user, while each input
I have tried making a program to sort numbers of an array. I have
So I'm practicing pointers to functions, and tried out making this simple program, here's
I tried making the title as descriptive as possible... What I was wondering was;
I tried making a wrapper class that encapsulates an object, a string (for naming

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.