Quote Originally Posted by Tomwa View Post
Your primary issues were:
1) Failed to initialize your variables, when you create a variable memory is allocated for that variable by the operating system, there is however no guarantee that the value in that memory is 0 (Thus why you have to initialize your variables to zero) this ensures you don't get crazy numbers from your arithmetic.

2) Indentation, remember that you indent when you enter a new code block and when you have to much to fit on one line. You also dropped a space between return and 0.

3) Arithmetic, you had some botched arithmetic, you didn't need to use the modulus operator at all (thats the % sign).

4) Botched printf (I use linux man to lookup most function definitions:
printf(3): formatted output conversion - Linux man page

5) Unnecessary scanf, after entering all of your monetary values you accidentally read new values in to sum and r.

Hopefully I've been concise in my explanations, if you need any further clarification I'll be glad to assist.

Best of luck!

Here's the fixed code:
Code:
#include <stdio.h>

int main(void)
{
    /* Remember to initialize variables to zero
     * as you cannot depend on having zero-ed memory */
    int p = 0,n = 0,d = 0,q = 0;
    double sum = 0;
    /* You didn't need the additional variables,
     * everything you wanted to do can be done with the variables 
     * you already made */

    printf("enter number of pennies:");
    scanf("%d",&p);
    
    printf("enter number of nickels:");
    scanf("%d",&n);
    /* Multiply number of nickels by 5,
     * Note: n*=5 is the same thing as n = n * 5 */
    n *= 5;

    printf("enter number of dimes:");
    scanf("%d",&d);
    /* Multiply number of dimes by 10,
     * Note: d*=10 is the same thing as d = d * 5 */
    d *= 10;

    printf("enter number of quarters:");
    scanf("%d",&q);
    // Do the same thing for quarters
    q *= 25;

    /* Because money has decimal points we need more precision
     * This means we need to cast our integers to doubles (or floats)
     * to ensure we don't lose the decimal points 
     * (An integer divided by an integer is an integer) */                                       
    sum=(double)(n+d+q+p)/100;

    // Print the value of sum;
    // Note: .2f is a format specifiers it tells printf you print
    // 2 decimal places.
    printf("you have %.2f dollars",sum);

    return 0;
}
Thanks a lot for very useful info I am very novice on programming and I thought "float" represent only decimal values between 0 and 1. And thats why I needed to calculate cents and dollars separately and get them together at the end. I created kind of complex formula to make the "sum" an integer. I know I should have read more before programming this.