```
#include <stdio.h>
int main()
{
double a,b,c;
a=10.0;
b=2.0;
c=100.0;
printf("The sum of %g and %g divided by %g equals %g\n",a,b,c,(a+b)/c);
return 0;
}
```

14,770,588 members

See more:

Hi,I'm learning c and it's been interesting, I'm at the lowest of beginners if there's anything like that, without wasting much time this is my question.

I'm trying to print,the sum of a and b divided by c equals=? Given a,b and c are 10,2 and 100 respectively,I've tried but it's not giving me the right answer. I would appreciate if anyone could show me how it's done. Thanks

**What I have tried:**

I'm trying to print,the sum of a and b divided by c equals=? Given a,b and c are 10,2 and 100 respectively,I've tried but it's not giving me the right answer. I would appreciate if anyone could show me how it's done. Thanks

#include <stdio.h> int main(){ int a,b,c; a=10; b=2; c=100; printf("The sum of %d and %d divided by %d equals %d\n",a,b,c,(a+b)%10); }

Try

```
#include <stdio.h>
int main()
{
double a,b,c;
a=10.0;
b=2.0;
c=100.0;
printf("The sum of %g and %g divided by %g equals %g\n",a,b,c,(a+b)/c);
return 0;
}
```

Comments

My 5 too.

I think the .0 are not needed since variables are floats.

I think the .0 are not needed since variables are floats.

Thank you.

Indeed they are not needed.

Indeed they are not needed.

Thanks,it worked but I think I'll just have to go back to the textbook I'm reading because I have no idea what %g is or does or the double you used or why mine didn't give me the correct answer,what did I do wrong?

Don't bother about '%g'. It is just a format specifier used to get 'nice output' from floating point numbers.

The important point is the usage of doubles (floating point numbers) instead of integers. See, for instance:

https://www.calebcurry.com/c-programming-tutorial-21-int-float-and-double-data-types/

The important point is the usage of doubles (floating point numbers) instead of integers. See, for instance:

https://www.calebcurry.com/c-programming-tutorial-21-int-float-and-double-data-types/

Actually, I always use .0 too, even when it's not needed. The reason is that sometimes the compiler might interpret the code differently based on the type of, e. g., a function parameter. Then it's important to specify the right type.

It also helps you detect inaccurate types: if you accidentally declare a variable as int (or similar), the compiler will point it out once you try to initialize or modify it with a .0 literal.

It also helps you detect inaccurate types: if you accidentally declare a variable as int (or similar), the compiler will point it out once you try to initialize or modify it with a .0 literal.

5ed!

Thank you!

As a beginner, you have to read documentation (or at least do some search for anything new to you).

Where have you seen that**%** is the operator for division ?

Beware result of division of integers is an integer.

You will need to search on how to get floating point result on a division of integers.

Where have you seen that

Beware result of division of integers is an integer.

You will need to search on how to get floating point result on a division of integers.

v2

Comments

My 5.

Thank you

5ed!

Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900