Hi guys, Im new here and i want a help to my java program. I want to 0,5,6,7, will have a value of 1 and the others are 2. | Sololearn: Learn to code for FREE!

+2

Hi guys, Im new here and i want a help to my java program. I want to 0,5,6,7, will have a value of 1 and the others are 2.

while(number != 0) { int remainder = number % 10; if(remainder == 0 || remainder == 6 || remainder == 7) remainder = 1; else { remainder = 2; } total += remainder; number = number/10; } return total;

9/15/2021 10:16:54 AM

F John

18 Answers

New Answer

+1

F John I think so that's because when it reaches to the 0 of 012 the condition of while loop becomes true and it doesn't add 1. Use 102 for it

+2

I've already suggested you to take input as a string. If you are restricted to take numeric input, then you can convert the number to string before you proceed with the sum. https://code.sololearn.com/c5wyojG9w0hV/?ref=app

+1

So basically you want to sum all the digits in given number, with each digit incremented by 1. Is that it? BTW, seeing you take int input for <num>, did you know that a zero prefix in integer is read in as octal number (base 8)? taking your example, 012 (octal) is 10 (decimal). You might get through this if you take input as string.

+1

Thank you . 😊 Yes but only numbers 0,5,6,7 have a value of 1 then the other numbers will have a value of 2. Then add the all digits . I dont how to start your request can you give some example or expand my program? Plsss

+1

https://code.sololearn.com/cMRJQtZfzL5P/?ref=app

+1

See it will even work without using getters and setters too

+1

Ipang THANK YOU FOR YOUR COOPERATION THIS WHAT I WANT. THANK YOU VERY MUCH 😊😍

+1

F John Welcome. Please don't call me sir

0

Can you please send the full program?

0

public class Buzzer { private int total; private int num; public Buzzer(int num) { this.num = num; } public void setNum(int num) { this.num = num; } public int getNum() { return this.num; } public int Method(){ while(num != 0) { int remainder = num % 10; if(remainder == 0 || remainder == 5 || remainder == 6 || remainder == 7) { remainder = 1; } else { remainder = 2; } total += remainder; num = num/10; } return total; } } //This is my whole program. I hope you can help me. For school purposes. // the error here in my program is when i put the zero in the first place . Like 012 the correct output is 5 because 0=1 , 1= 2 and 2 = 2 then add them all. Its 5 but if i run it the result is 4.

0

public class Buzzer { private int total; private int num; public Buzzer(int num) { this.num = num; } public void setNum(int num) { this.num = num; } public int getNum() { return this.num; } public int Method(){ while(num != 0) { int remainder = num % 10; if(remainder == 0 || remainder == 5 || remainder == 6 || remainder == 7) { remainder = 1; } else { remainder = 2; } total += remainder; num = num/10; } return total; } } //This is my whole program. I hope you can help me. For school purposes. // the error here in my program is when i put the zero in the first place . Like 012 the correct output is 5 because 0=1 , 1= 2 and 2 = 2 then add them all. Its 5 but if i run it the result is 4. public class Program { public static void main(String[] args) { Buzzer sc= new Bu

0

Uhmm. Thank you for your effort sir . But i tried this. The only problem is when i put zero in the first place it code not read the value of 0 which is 1 .

0

Umm. The output is different for 012 and 12. Are you sure that in every case 0 should remain in the first?

0

Now you check the code all your doubts will be cleared

0

Thank you for your cooperation sir but the output of 012 is 5 .

0

Ok now you check

0

F John Please check my code too

0

Atul [Inactive] WOW its also good what a nice code 😊😊 THANK YOU VERY MUCH FOR HELPING ME SIR. 😊 you give idea 😊