# 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 John18 Answers

New AnswerF 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

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

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.

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

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 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

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 .

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

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