# C problem

Discussion in 'Programmer's Corner' started by Dritech, Oct 19, 2012.

1. ### Dritech Thread Starter Well-Known Member

Sep 21, 2011
756
5
Hi,

I have the following program where i want the final result to show decimal numbers. As far as i know, %f is used to do so, but the answer is showing as 0.00000. Can someone help me sort out this problem please??

Code ( (Unknown Language)):
1.
2. void main()
3.
4.     {
5.         int a,b,c,d;
6.
7.         printf ("This is a calculator that let you enter four integer numbers and use them to work: (a*b)/(c+d) \n\n");
8.
9.         printf ("Enter number a: ");
10.         scanf ("%d",&a);
11.         printf ("\n\n(%d *b)/(c+d) \n\n",a);
12.
13.         printf ("Enter number b: ");
14.         scanf ("%d",&b);
15.         printf ("\n\n(%d * %d)/(c+d) \n\n",a,b);
16.
17.         printf("Enter number c: ");
18.         scanf ("%d",&c);
19.         printf("\n\n(%d * %d)/(%d +d)\n\n",a,b,c);
20.
21.         printf("Enter number d: ");
22.         scanf ("%d",&d);
23.         printf("\n\n(%d * %d)/(%d + %d)\n\n",a,b,c,d);
24.
25.
26.         printf("The answer of (%d * %d)/(%d + %d) is %f",a,b,c,d, (a*b)/(c+d));
27.
28.
29.     }
30.

2. ### kubeek AAC Fanatic!

Sep 20, 2005
4,688
806
When you add or divide two ints, you get back and int. Int cannot be printed with %f and the result will be truncated to integer numbers of course.
To get correct result you should convert all into floats, but since C automaticly converts the other if one is a float you should be ok with
Code ( (Unknown Language)):
1.
2.         printf("The answer of (%d * %d)/(%d + %d) is %f",a,b,c,d, ((float)a*b)/(c+d));
3.

Dritech likes this.
3. ### Papabravo Expert

Feb 24, 2006
10,340
1,850
%f is the format specifier for a floating point result. In order to compute such a result there must be an explicit conversion of the integer result where the remainder is thrown away to a floating point value.

Dritech likes this.
4. ### Dritech Thread Starter Well-Known Member

Sep 21, 2011
756
5
Thanks for your replies. Is there a way to remove the zeros if the number entered in an integer ?? (eg: instead of 5.000000 it displays 5, or instead of 5.50000 it displays 5.5 only)

Sep 20, 2005
4,688
806
6. ### Dritech Thread Starter Well-Known Member

Sep 21, 2011
756
5
Ok, thanks.

Now i am trying to use the if condition, but for some reason it is only showing the first condition no matter if the answer is correct or not. Does anyone know why? Below is the coding:

Code ( (Unknown Language)):
1. #include <stdio.h>
2.
3. void main()
4.
5.     {
6.         float a,b,c,d,e;
7.
8.         printf ("This is a calculator that let you enter four integer or decimal numbers and use them to work: (a*b)/(c+d) \n\n");
9.
10.         printf ("Enter number a: ");
11.         scanf ("%f",&a);
12.         printf ("\n\n(%f *b)/(c+d) \n\n",a);
13.
14.         printf ("Enter number b: ");
15.         scanf ("%f",&b);
16.         printf ("\n\n(%f * %f)/(c+d) \n\n",a,b);
17.
18.         printf("Enter number c: ");
19.         scanf ("%f",&c);
20.         printf("\n\n(%f * %f)/(%f +d)\n\n",a,b,c);
21.
22.         printf("Enter number d: ");
23.         scanf ("%f",&d);
24.         printf("\n\n(%f * %f)/(%f + %f)\n\n",a,b,c,d);
25.
26.         printf("Work out (%f * %f)/(%f + %f) =\n\n",a,b,c,d);
27.         scanf("%f",&e);
28.
29.         if (e = (a*b)/(c+d))
31.         if (e != (a*b)/(c+d))
32.         {printf("The correct answer is %f",(a*b)/(c+d));}
33.
34.     }

7. ### kubeek AAC Fanatic!

Sep 20, 2005
4,688
806
I am not sure what compiler you´re using, i think normally you should get a warning with the first if statement. That is because a=b is an assignement, but a==b tests for equality.

If you have if(a=b)...; then the program uses the value of a to evaluate the condition. And because 0 is treated as false and anything else is true, the first statement in your program will be executed. Only when b is 0 it wont execute.

Third thing, because float numbers have limited number of decimal places, you should test for equality in this way:
Code ( (Unknown Language)):
1. if( abs(a-b) < 0.001 )printf("equal");
2. else printf("not equal")
Abs() returns absolute value of the difference, and you need to #include <math.h> at the beginning of your code. Without this you will never get "equal" for say comparing 1/3 to 0.333 etc.

Last edited: Oct 19, 2012
Dritech likes this.

Sep 21, 2011
756
5

9. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,436
1,625
Minor nit picking point: Actually, first a is assigned the value of b, and *that* value is the basis of the comparison. Otherwise all is correct.

10. ### WBahn Moderator

Mar 31, 2012
18,085
4,917
%3.5f tells it to use a minimum width of 3 with 5 digits after the decimal point. So it will print 5.00000 and have a width of 7.

If you use %0.0f, it will print it as an integer (but with a trailing decimal point, I think).

There's no easy way to remove trailing zeros because the very concept is ill defined. For instance, let's say the result of a computation has a tiny bit of round-off error and comes out to be 5.500000000001 or 4.4999999999. How many "trailing zeros" are there?

If you write a function the examines the value and returns the number of digits to use after the decimal point. You can then construct a format string and pass it to printf() as the first argument.