math problem! Please help

hi everyone!

how can i convert a result as example below in one formula? and i need the result show up on a 4digit 7 segment display.

1 * 0.95 = 95 instead of 0.95

2 * 0.95 = 19 instead of 1.9

99 * 0.95 = 9405 instead of 94.05

Thank you

Are you sure you don't want 95, 190, and 9405? That would make more sense.

1 * 95 = 95
2 * 95 = 190
99 * 95 = 9405

hi john

it is clever!

but i don't want the result last digit 0.

any way to exlude it in the result.

i could put a if statement to determine is there any 0 in last digit. but how can i do it?

i got it.

first use modular

number % 2

if result is 1 equal to singular

if result is 0 equal to plural

if the number is singular just multiple by 95 like this one below

1*95 = 95

on the other side if the number is plural the result will be

2 * 95 = 180
12 * 95 =1140

A stupid question ! please tell me !

7% 2 = 1
95 % 2 = 1

are they right?
Thank you

are they right?

Yes, but you could have written a sketch to confirm that in less time than it took to post the question.

Looks like the expression you want is:

int result = (X & 1) ? X95 : (X95)/10;

This will produce the results:

0 -> 0
1 -> 95
2 -> 19
3 -> 285
4 -> 38
5 -> 475
...
97 -> 9215
98 -> 931
99 -> 9405

Thanks John for the hint. I did it yesterday.
My code is longer but it works. but i would like to know how the way you do it.
would you please explain your expression? it is quiet confuse for me.

int number = random (1,99);
if (number % 10 == 0){
goto random_number; <----- I try to not using goto(); function but i am not clever enough to not using it.
}
if (number % 2 == 0 ){
answer = number * 95 / 10;
}
if ( number % 2 == 1){
answer = number * 95;
}

PaulS:

are they right?

Yes, but you could have written a sketch to confirm that in less time than it took to post the question.

ok. Paul! no more stupid question next time. thanks for remind me that solving problem by myself will make me clever than asking.

tonify:

int number = random (1,99);

if (number % 10 == 0){
     goto random_number;  <----- I try to not using goto(); function but i am not clever enough to not using it.
   }
   if (number % 2 == 0 ){
     answer = number * 95 / 10;
   }
   if ( number % 2 == 1){
     answer = number * 95;
   }

To get rid of the goto, try:

int number;
    do {number = random (1,99);} while (number % 10 == 0) ;  // oops... had != before
    if (number % 2 == 0 ){
      answer = (number * 95) / 10;
    }
    if ( number % 2 == 1){
      answer = number * 95; 
    }

The formula "int result = (X & 1) ? X95 : (X95)/10;" is just a shorthand way of saying:

int result;
if (X & 1)  // This is the same as: (X % 2) != 0
   result = X * 95;
else
   result = (X * 95) / 10;

thanks john !