# Thread: Simple Math Program

1. ## Simple Math Program

I haven't programmed in C in a while, and I decided to write a simple program for a friend of mine. It's a numerology program that takes your name, converts each letter into a number in the alphabet (A = 1, B = 2, Z = 26, etc.). I wrote a function that takes a number and splits the digits up, adding them together to form a single digit, and return that digit, only when I run it, it only returns 9, no matter what number I put in (23, 543, 7, etc.) Can you help me? I don't know what I did wrong.

The code below is a program to test the crunch function:
Code:
```#include<stdio.h>

int crunch(int num);

int main()
{
int c, d;

printf("Enter a number: ");
scanf("%d", c);

d = crunch(c);

printf("%d", d);
return 0;
}

int crunch(int num)
{
int sum = 0;
int tmp;
while(num != 0)
{
tmp = num % 10;
sum += tmp;
num = num / 10;
}
if(sum >= 10)
return crunch(sum);
return sum;
}```

2. > scanf("%d", c);
If your compiler isn't complaining, turn up the warning level until it does.

This should be
scanf("%d", &c);

Recommended compiler options for gcc are
-W -Wall -ansi -pedantic -O2

3. > scanf("%d", c);

Try:
scanf("%d", &c);

Now let me disappear before Dave_Sinkula tells me I'm wrong ...

4. I can't believe I forgot about that. Thank you!

Popular pages Recent additions