Thread: Adding one to every digit of number

  1. #1
    Registered User culater's Avatar
    Join Date
    May 2015
    Posts
    8

    Adding one to every digit of number

    Hi there,
    I' m encountering the following warning:
    Warning W8004 sum1num.c 7: 'd' is assigned a value that is never used in function main.
    How can I remove it in this case and what should I do in case of any other code having this warning?? Also, is there any way to shorten this code and is my indentation right?
    Here's the
    Code:
    #include <stdio.h>
    #include <math.h>
    /*Adding one to every digit of number*/
    float main ()
    {
    int a,  c = 0, e = 0, n, p, x;
    float d = 0, b;
    printf("Enter a number\n");
    scanf("%d", &n);
    p = n;
    while (n != 0)
       {
       n = n/10;
       c++ ;
       }
    for (b = 0; b<c; b++)
       {
       d = exp(b*log(10));
       e = e + d;
       }
    x = p + e;
       printf("The new number is = %d\n", x);
    return 0;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    use
    Code:
    float d, b;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User culater's Avatar
    Join Date
    May 2015
    Posts
    8
    Thanks!
    a minor mistake

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can also get rid of d completely if you wanted and change this loop:
    Code:
    for (b = 0; b<c; b++)
    {
        d = exp(b*log(10));
        e = e + d;
    }
    To this:
    Code:
    for (b = 0; b<c; b++)
    {
        e += exp(b*log(10));
    }
    You also don't really need the variable x (or a for that matter), you can just print p+e in the printf directly.

    It also seems that b, given how it is used, should be an int, not a float.

    This needs to be changed:
    Code:
    float main ()
    You can also get rid of the need for the "number of digits" calculation for-loop (and therefore for the variable p as well) and use this instead to set the variable c.
    Code:
    c = floor(log10(n))+1;
    p wouldn't be needed anymore because you're not destroying the original n now and the last printf call can just be for n+e instead of p+e.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding a number digit by digit.
    By sdfx in forum C Programming
    Replies: 4
    Last Post: 05-10-2011, 05:54 PM
  2. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  3. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  4. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 03:28 AM
  5. outputs number digit by digit
    By Unregistered in forum C++ Programming
    Replies: 14
    Last Post: 05-17-2002, 04:43 PM