Hi. I'm new here. Anyway, I have an assignment of writing programs from outputs provided.
Here's an output given in the assignment that I think is wrongly done. I'm saying this because I tried many, many times just to get the exact output, but only to no avail.
So, I just wanna make sure.

Here's the output given:

Output: 24 roses cost 19.95 per 12

#include <stdio.h>
main()
{
printf("%d roses cost %2f per %d\n", 24, 19.95, 12);
return 0;
}

But now, the problem here is that when I write this program, I do not get exactly 19.95 for the output. But instead, I got a 19.950000....so, WTH??? How did the -0000 came from???

## All 5 Replies

Reread your text on `printf()` format specifiers. Specifically the %f specifier.

Use

``printf("%d roses cost %2.2f per %d\n", 24, 19.95, 12);``
commented: Thnaks, dude, for making my post worthless -4

Use

``printf("%d roses cost %2.2f per %d\n", 24, 19.95, 12);``

Okay, Thanks a lot Dude!

But just one quick question: How will I know when to use 2f or 2.2f??

Say for example I want it to appear as 64.21000 on the program.

But when I use 2.2f, I got a 64.21 instead.

WTH? Again.

Is there a list/table of numeric conversion characters I can refer to easily? Because I'm not sure how to use C++ Syntax.

Suppose you have something "%x.yf", x,y are two integers.
x=number of digits you want to display before '.'
y=number of digits you want to display after '.'

The default value of y is 6 that why you were getting 64.21000.

printf("%2.3f",11.1111); will print 11.111

Suppose you have something "%x.yf", x,y are two integers.
x=number of digits you want to display before '.'
y=number of digits you want to display after '.'

The default value of y is 6 that why you were getting 64.21000.

printf("%2.3f",11.1111); will print 11.111

Hi!

I have another assignment regarding writing a program based on a given output.

Here's the Output given:

Output:

Day:
Month:
Year:

My birth date is on 31 August 1980.

So, here's my answer( Although it is wrong; with about 6 ERRORS in it):

#include <stdio.h>
main()
{
char date of birth[15];
scanf("%s", date of birth);
char Day[15]
printf("Day:");
scanf("%s", Day);
char Month[15]
printf("Month:");
scanf("%s", Month);
char Year[15]
printf("Year:");
scanf("%s", Year);
printf("\n\nMy birth date is on %s\n", date of birth);
return 0;
}

So, this is SO NOT CORRECT, right???

Can anyone tell me where did I go wrong in writing this program code? Coz I got 6 ERRORS in it!!!

And it's due tomorrow too! BUMMER.