All pastes #189021 Raw Edit

Anonymous

public text v1 · immutable
·published 2006-10-02 18:52 UTC
rendered paste body
/*
        Name: Sean Griffin
        Date: October 2nd, 2006
        Description:  Write code that will sum the place value digits of a number
                      provide by the user.

                      The user will be prompted to provide a number as well as the
                      number of digits this number contains. These two numbers
                      will then be passed into the place_value_add() function.

                      Inside this function the numbers will be used in a logical
                      statement that will strip each indivudal value and add them
                      together.  Once the final sum is determine(by digits) it
                      will be returned.
*/

#include <iostream.h>
#include <cmath>   //allows access to pow()

int place_value_add(int input, int digits); //adds individual place values

void main(void)
{
        int input;
        int digits;

        std::cout << "This program will add the individual place values of a number that you provide" << endl;
        std::cout << "Please provide a number: ";
        std::cin >> input;
        std::cout << "Please provide the number of digits in your number: ";
        std::cin >> digits;
        std::cout << "The place value sum of your number is: " <<place_value_add(input, digits);

        char c1;
        cin >> c1;


}

int place_value_add(int input, int digits)
{
        int total=0;
        const double BASE=10.0;

        //sumation sequence
        for(int i=1; i<=digits; i++)
        {
                int x = int(std::pow(BASE,i));
                int y = int(std::pow(BASE,i-1));

                total+=(input%x-input%y)/y;
        }

        return total;
}

/* Sample output

This program will add the individual place values of a number that you provide
Please provide a number: 4352
Please provide the number of digits in your number: 4
The place value sum of your number is: 14

/*