#Conversion. Help

42 messages · Page 1 of 1 (latest)

violet harnessBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

flat dagger
#

What did I do wrong here?

brittle ocean
#

when you did the division of cents / 100

#

cents did not change

#

you can use /= to divide and set a value

#

cents /= 100

flat dagger
flat dagger
#

i know u just did a compound arithmetic

brittle ocean
#

if you want to read more^

flat dagger
brittle ocean
#

yeah

flat dagger
# brittle ocean yeah

#include <stdio.h>

int main(void)
{
int cents = 0;

printf("Number of cents\? ");
scanf("%d", &cents);

cents /= 100;

printf("\n%d dollars and %d cents.", cents);

return 0;

}

#

what should i do now?

brittle ocean
#

no keep the thing before

#

just change the division into a division assignment

flat dagger
brittle ocean
#

the code you had in the beginning

flat dagger
#

so no compound arithmetics in my code then?

brittle ocean
#
#include <stdio.h>

int main(void)
{
    int cents = 0;

    printf("Number of cents\? ");
    scanf("%d", &cents);

    int total_dollars = cents /= 100;
    int total_cents = cents;

    printf("\n%d dollars and %d cents.", total_dollars, total_cents);

    return 0;
}
flat dagger
#

ah, i see

flat dagger
brittle ocean
#

oh wait I just thought of a better way

flat dagger
#

i'm struggling to find a formula for it on google and separate the final 2 digits from dollars

brittle ocean
#

and a way that works lol

#

have you heard of modulo operator

#

also known as remainder operator

#

%

#

you can get total dollar and pretend I didn't say anything

#

and use remainder to find the cents

flat dagger
#

%% prints a single % sign, so gotta avoid that

#

value will most likely be truncated with int, but not with float type

brittle ocean
#

hint: 126 % 100 = 26

#

I got class now but you should get it

flat dagger
flat dagger
#

Anyone still able to help?

final marsh
#

If so, you will need three variables.

The first variable will hold the total amount in cents, which you input from the user. A good name for this might be total_cents.

The second variable will the calculated dollars. A good name for this might be calculated_dollars. You can get this value by dividing total_cents by 100 and assigning the result to calculated_dollars.

The third variable will be the calculated cents. A good name for this might be calculated_cents. You can get this value by subtracting calculated_dollars * 100 from total_cents.

@flat dagger Does that make sense (not cents)? 😁

flat dagger
flat dagger