Thread: Trying to find an original value of a percentage down to another value

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    93

    Trying to find an original value of a percentage down to another value

    I know the title doesn't make a whole lot of sense and I'm going to be terrible at trying to explain it.

    I'm trying to write a program that finds the original value from a percentage down and the last known value.

    Example:
    Bitcoin was at $4420 which was down 4.9% from last month. I'm wanting my program to calculate what that number was.

    The way it's currently written, I'm aware that percents must be entered like "0.05" for 5%.

    Code:
    //
    //  main.c
    //  valuefinder
    //
    //  Created by Joshua Ernzen on 10/3/17.
    //  Copyright  2017 Joshua Ernzen. All rights reserved.
    //
    // This program is suppose to find the starting value from a percent down.
    // Example: $4,420 is 4.9% down from last month. Find the original value
    
    #include <stdio.h>
    #include <stdint.h>
    
    int main() {
        double incnumber, usernumber, numberholder, finalnumber, percent;
        int8_t loop;
        loop = 1;
        
        printf("Please enter the value: ");
        scanf("%lf", &usernumber);
        printf("Please enter the percentage: ");
        scanf("%lf", &percent);
        
        incnumber = usernumber;
        
        while(loop == 1) {
            numberholder = incnumber * percent;
            finalnumber = numberholder + incnumber;
            
            if(finalnumber - numberholder != usernumber) {
                incnumber = incnumber + 0.01;
                printf("%lf\n", incnumber);
            }
            if(finalnumber - numberholder == usernumber)
                loop = 0;
        }
        
        printf("%lf is %lf of %lf\n", usernumber, percent, finalnumber);
        
        return 0;
    }
    The way it currently works seems to ignore the "if" statements.

    What it actually does is, let's say I enter 95 and down 5%. That would mean that 100 was the original value. The program just adds 4.75 to 95 which means it just added the value of 5% of 95 to 95.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,093
    95 / (1.0 - 0.05) = 100
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Why have you started a new thread for the same topic?
    If you had responded to my last post on that thread, I would have continued to help you find the solution.

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    I believe I made the first one and couldn't find it, so thinking I must not of created the first one I created a second one :/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a percentage
    By Alecroy in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2010, 07:24 PM
  2. Original MMORPG
    By FiftyNine50 in forum Projects and Job Recruitment
    Replies: 13
    Last Post: 08-17-2007, 12:54 PM
  3. Original sin()
    By SMurf in forum C Programming
    Replies: 6
    Last Post: 02-22-2005, 02:48 AM
  4. Percentage
    By skyglin in forum C Programming
    Replies: 7
    Last Post: 06-23-2003, 05:18 PM
  5. you need to come original!
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-24-2001, 09:47 PM

Tags for this Thread