This is a program in C that is not working for me. It is supposed to take a value i and also a multiple j and round i up to the nearest multiple of j.
My program keeps returning crazy numbers though. For example if I input 256 for i and 7 for j i should get 259 (263 - 4). Instead I get 33827176 or other crazy numbers. Any ideas?
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <stdio.h>
int main(void)
{
int i, j, Next_multiple;
printf("This program rounds an integer to the next largest integer of a given multiple\n");
printf("Please enter the integer to be rounded.\n");
scanf("%d", &i);
printf("Please enter the integer that will be the multiple\n");
scanf("%d", &j);
Next_multiple = i + j- i%j;
printf("The number is %d\n", &Next_multiple);
return 0;
}
Your problem is probably with line 11. scanf takes references, but printf takes values, because it doesn't need to modify the variable. Line 11 should look like this: printf("The number is %d.\n", Next_multiple);
In case you were wondering about the crazy large numbers, you can think of it as printing, rather than the value of the number, the storage address for the number.