Click here to Skip to main content
14,770,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

#include <stdio.h>
int main(){
int a,b,c;
printf("The sum of %d and %d divided by %d equals %d\n",a,b,c,(a+b)%10);
Updated 31-Mar-20 8:44am

#include <stdio.h>
int main()
  double a,b,c;
  printf("The sum of %g and %g divided by %g equals %g\n",a,b,c,(a+b)/c);
  return 0;
Patrice T 31-Mar-20 13:58pm
My 5 too.
I think the .0 are not needed since variables are floats.
CPallini 31-Mar-20 14:01pm
Thank you.
Indeed they are not needed.
Member 14788459 31-Mar-20 18:08pm
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?
CPallini 1-Apr-20 2:15am
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:
Stefan_Lang 1-Apr-20 2:43am
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.
Maciej Los 1-Apr-20 1:52am
CPallini 1-Apr-20 2:08am
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.
CPallini 31-Mar-20 13:39pm
My 5.
Patrice T 31-Mar-20 13:40pm
Thank you
Maciej Los 1-Apr-20 1:52am
Patrice T 1-Apr-20 1:57am
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