how to replace a number with another number in java code example

how to replace a number with another number in java

3

3 Examples

0
// CPP program to replace a digit with other
// in a given number.
#include <bits/stdc++.h>
using namespace std;
 
int replaceDigit(int x, int d1, int d2)
{
    int result = 0, multiply = 1;
    while (x / 10 > 0) {
        // Take remainder of number starting from the unit
        // place digit
        int remainder = x % 10;
        // check whether it is equal to the digit to be
        // replaced.if yes then replace
        if (remainder == d1)
            result = result + d2 * multiply;
        else // else remain as such
            result = result + remainder * multiply;
        // Update and move forward from unit place to
        // hundred place and so on.
        multiply *= 10;
        x = x / 10; // update the value
    }
    // check whether it is equal to the digit to be
    // replaced.if yes then replace
    if (x == d1)
        result = result + d2 * multiply;
    else // else remain as such
        result = result + x * multiply;
    return result;
}
 
// Driver code
int main()
{
    int x = 645, d1 = 6, d2 = 5;
    cout << replaceDigit(x, d1, d2) << endl;
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta

0
import java.util.*;

class JAVAhw10 {
    private static final int[] table = {3,9,4,1,7,6,8,2,0,5};

    public static void main(String[] args) {

        int a, b, c, d, e;
        a = (int)((Math.random()*100) % (9) + 0);
        b = (int)((Math.random()*100) % (9) + 0);
        c = (int)((Math.random()*100) % (9) + 0);
        d = (int)((Math.random()*100) % (9) + 0);
        e = (int)((Math.random()*100) % (9) + 0);

        int DigitIn[] = new int[5];
        int table[] = new int[10];

        //filling in the arrays
        DigitIn[0]=a;
        DigitIn[1]=b;
        DigitIn[2]=c;
        DigitIn[3]=d;
        DigitIn[4]=e;

        System.out.println("This program ecnrypts 5 random numbers");
        System.out.println(Arrays.toString(DigitIn));

        int[] output = encrypt(DigitIn);
        System.out.println(Arrays.toString(output));

    }

    public static int[] encrypt(int[] input){
       int[] output = new int[input.length];
       for (int i=0; i<input.length; i++){
           output[i] = table[input[i]];
       }
       return output;
    }
}

0
#include <stdio.h>
int digitreplace(int n, int digit, int replace) {
   int res=0, d=1;
   int rem;
   while(n) {
      //finding the remainder from the back
      rem = n%10;
      //Checking whether the remainder equal to the
      //digit we want to replace. If yes then replace.
      if(rem == digit)
         res = res + replace * d;
      //Else dont replace just store the same in res.
      else
         res = res + rem * d;
         d *= 10;
         n /= 10;
   }
   printf("%d\n", res);
      return 0;
}
//main function
int main(int argc, char const *argv[]) {
   int n = 983;
   int digit = 9;
   int replace = 7;
   digitreplace(n, digit, replace);
   return 0;
}

Other Code Examples