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)): void main() { int a,b,c,d; printf ("This is a calculator that let you enter four integer numbers and use them to work: (a*b)/(c+d) \n\n"); printf ("Enter number a: "); scanf ("%d",&a); printf ("\n\n(%d *b)/(c+d) \n\n",a); printf ("Enter number b: "); scanf ("%d",&b); printf ("\n\n(%d * %d)/(c+d) \n\n",a,b); printf("Enter number c: "); scanf ("%d",&c); printf("\n\n(%d * %d)/(%d +d)\n\n",a,b,c); printf("Enter number d: "); scanf ("%d",&d); printf("\n\n(%d * %d)/(%d + %d)\n\n",a,b,c,d); printf("The answer of (%d * %d)/(%d + %d) is %f",a,b,c,d, (a*b)/(c+d)); }
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)): printf("The answer of (%d * %d)/(%d + %d) is %f",a,b,c,d, ((float)a*b)/(c+d));
%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.
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)
%3.5f will print 005.00000 etc., for the rest of the parameters see here http://www.cplusplus.com/reference/clibrary/cstdio/printf/ I dont think it is easily possible to cut the trailing zeroes.
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)): #include <stdio.h> void main() { float a,b,c,d,e; 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"); printf ("Enter number a: "); scanf ("%f",&a); printf ("\n\n(%f *b)/(c+d) \n\n",a); printf ("Enter number b: "); scanf ("%f",&b); printf ("\n\n(%f * %f)/(c+d) \n\n",a,b); printf("Enter number c: "); scanf ("%f",&c); printf("\n\n(%f * %f)/(%f +d)\n\n",a,b,c); printf("Enter number d: "); scanf ("%f",&d); printf("\n\n(%f * %f)/(%f + %f)\n\n",a,b,c,d); printf("Work out (%f * %f)/(%f + %f) =\n\n",a,b,c,d); scanf("%f",&e); if (e = (a*b)/(c+d)) {printf ("\nCorrect answer!\n\n");} if (e != (a*b)/(c+d)) {printf("The correct answer is %f",(a*b)/(c+d));} }
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)): if( abs(a-b) < 0.001 )printf("equal"); 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.
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.
%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.