1. ## Basic Sum Calculator

Hey everybody, I started programming C today, and here I am posting my first doubt:

Code:
```#include <stdio.h>
#include <conio.h>

int main(void)
{
int  num1;
int   num2;
int  sum;

scanf("%d", &num1);

scanf("%d", &num2);

sum = num1+num2;

printf("The sum is %d", sum);

getch();
}```
This works fine as a sum calculator, but only with INT values (I'm a programmer for a long time, but I started C today, used to program Java and Python, so I know quite a lot).

Whenever I try to use FLOAT values, it goes crazy, try it on your computer please.

ScoutDavid

2. Just declare your variables as floats, example float sum.

here's a link that I still use:

C variable types and declarations - Wikipedia, the free encyclopedia

3. Code:
```#include <stdio.h>
#include <conio.h>

int main(void)
{
float  num1;
float   num2;
float  sum;

scanf("%d", &num1);

scanf("%d", &num2);

sum = num1+num2;

printf("The sum is %d", sum);

getch();
}```
Thanks for the link very useful, but I followed your tip and still crazy, it says the result is 0 even before I type the second number...

4. You need to use %f in scanf if you are trying to get a floating point number. Same goes for printf. %d is to get an int (scanf), or print an int (printf).

5. Originally Posted by ScoutDavid
Code:
```#include <stdio.h>
#include <conio.h>

int main(void)
{
float  num1;
float   num2;
float  sum;

scanf("%d", &num1);

scanf("%d", &num2);

sum = num1+num2;

printf("The sum is %d", sum);

getch();
}```
Thanks for the link very useful, but I followed your tip and still crazy, it says the result is 0 even before I type the second number...
I did NOT want to make all your corrections, otherwise you wont learn. You
also have to change the scanf() format. You still have it scanning for type int,

This question is answered on the FAQ here, shall I search for it, or do you want
to give it a try?

Edit:

Nevermind, you were spoon fed the answer.

6. Code:
```#include <stdio.h>

int main(void)
{
float  num1;
float  num2;
float  sum;

scanf("%f", &num1);

scanf("%f", &num2);

sum = num1+num2;

printf("The sum is %f", sum);

getch();
}```
This might help. U need to change the input and output to float variables also.

7. How Do I? check out this FAQ:

Cprogramming.com FAQ

8. I've gotten there:

"You need to use %f in scanf if you are trying to get a floating point number."

Thank you very much!

9. Originally Posted by ScoutDavid
I've gotten there:

"You need to use %f in scanf if you are trying to get a floating point number."

Thank you very much!
And while you're in the FAQ, check out the rest of it! There's a lot of answers
there, so many, you'll learn the answers before you get a question.

(The How Do I.....)

10. #include <stdio.h>
#include <conio.h>

int main(void)
{
float num1;
float num2;
float sum;

scanf("%f", &num1);

scanf("%f", &num2);

sum = num1+num2;

printf("The sum is %f", sum);

getch();
}

3.4 + 52 = 55.400002?!!

What the heck?

11. Originally Posted by ScoutDavid

What the heck?

Hmmm... maybe I should have directed you to this link first:

How To Ask Questions The Smart Way

12. A float is only accurate to a few decimal places, but you can use:
Code:
`printf("The sum is %.2f", sum);`
to force it to show to 2 decimal places.

13. Originally Posted by Jamdog
A float is only accurate to a few decimal places, but you can use:
Code:
`printf("The sum is %.2f", sum);`
to force it to show to 2 decimal places.

Thank you very much!

14. Welcome to the forum, David!

Now repeat after me:

"On my honor, I will do my best to do my duty, and always post my code with code tags around it."

The forum will make your code miserable to study, if you don't use code tags (highlight your code and click on the # icon in the advanced editing window of the forum).