Replace all 0 s with 5 in Java

0 votes

Please help me with writing a code to replace all 0's in a number from 5 in Java. Here is the test case:

Input:

1000430

Output:

1555435
Mar 6, 2019 in Java by Prachi
6,379 views

1 answer to this question.

+1 vote

Take a look at this, hope this will help.

int convertFive(int n)
{
    int count = 1,num=0,rem,res;
        while(n != 0) // checking whether number is not equals to zero
        {
            res=n/10; //breaking the last digit
            rem=n%10; // finding the last digit
            if(rem==0) //replacing it with 5
            {
            num=num+count*5; // building the new number
            }

            else num = num + rem*count; //if it is not 0, building the new number
            count = count*10; // increasing the place value for next digit.
            n=n/10;
        }
        return (num);
//Your code here
}

Hope this helps.

answered Mar 6, 2019 by Priyaj
• 58,020 points

Related Questions In Java

0 votes
1 answer

Replace String with another in java

String s = "HelloSuresh"; s = s.replace("Hello",""); System.out.pri ...READ MORE

answered Aug 21, 2018 in Java by Parth
• 4,640 points
885 views
0 votes
1 answer

Java replace : replace all charecters with space except the matched string like "test"

Hi, @jyra, You can go through this example:  String ...READ MORE

answered Oct 14, 2020 in Java by Gitika
• 65,770 points
803 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,394 views
0 votes
2 answers

Get all the permutations of a string in Java

You could use recursion to do this.  Try ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
2,275 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,347 views
+1 vote
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
1,210 views
+1 vote
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,680 points
3,772 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
3,623 views
+17 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
253,813 views
+29 votes
3 answers

How can we set java.library.path in Eclipse?

Go into the library settings for your ...READ MORE

answered Jul 3, 2018 in Java by sharth
• 3,370 points
22,468 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP