Replace all 0’s with 1 in a given integer using Java
Replace all 0’s with 1 using Java
In this article we will create a program to replace all 0’s with 1 using java. For this purpose we will ask the user to enter a positive number and check each digit one by one that it is equal to 0 or not. If the digit is equal to 0 then replace the digit by 1, and else no change.
Question can come like Way 1
Write a code to change all zero's as one's (0s as 1s) in a given number? ex: 120014 needs to be printed as 121114
Question can come like Way 2
implement a c program to replace all 0's with 1 in a given integer as an input, all the 0's in the number has to be replaced with 1.
Implementation
- We will convert the integer into string.
- Then we will convert it into list and then we will traverse through the list.
- Wherever we find a ‘0’ we will replace with ‘1’.
Algorithm
- Take Input in num and initialize a variable num with with value 0
- If num is equals to zero then update the value of num2 to 1
- Iterate using a while loop until num is greater then 0
- For each iteration initialize a variable rem and store unit digit of num
- If rem equals to 0 then set rem to 1
- Set num to num divide by 10 & num2 equals to num2*10+rem
- Reverse and print num2
Java code
Run
//Java program to replace all 0's with 1 in a given integer : import java.util.Scanner; public class Main { public static void main(String[] args) { //scanner class declaration Scanner sc = new Scanner(System.in); //input from the user System.out.print("Enter the number : "); int number = sc.nextInt(); //convert the number to string and then calculate its length String str = Integer.toString(number); int len = str.length(); String str1 = ""; //use the logic to replace all 0's with 1 in a given integer for(int i = 0 ; i < len ; i++) { if(str.charAt(i) == '0') str1 = str1 + '1'; else str1 = str1 + str.charAt(i); } System.out.println("Converted number is: "+str1); //closing scanner class(not compulsory, but good practice) sc.close(); } }
Output
Enter the number : 706120678 Converted number : 716121678
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
private static int replace(int num1){
if(num1==0) return 0;
int x=(num1%10);
if(x==0) x=1;
return (replace(num1/10)*10)+x;
}
public static void main(String[] args) {
int num1=120014;
System.out.println(replace(num1));
}
public class Main
{
public static void main(String[] args) {
int num=706120678,rem,k=1;
int replacenum=0;
while(num!=0){
rem=num%10;
if(rem==0){
rem=1;
}
replacenum+=rem*k;
num=num/10;
k=k*10;
}
System.out.println(replacenum);
}
}
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String num=n+””;
int c=num.length();
String res=””;
for(int i=0;i<c;i++){
if(num.charAt(i)=='0'){
res+=1+"";
}
else{
res+=num.charAt(i);
}
}
System.out.println(res);
}
}
public class Main
{
public static void main(String[] args) {
int num = 706120678;
num = Integer.valueOf(String.valueOf(num).replace(“0″,”1”));
//num = Integer.parseInt(String.valueOf(num).replace(“0″,”1”));
System.out.print(num);
}
}
public class Main
{
public static void main(String[] args) {
int num = 706120678;
System.out.print(String.valueOf(num).replace(“0″,”1”));
}
}
import java.util.*;
@FunctionalInterface
interface ReplaceNumber
{
public abstract void m1();
}
class Replace implements ReplaceNumber
{
@Override
public void m1()
{
Scanner s=new Scanner(System.in);
System.out.print(“enter a number : “);
int a=s.nextInt();
String str=Integer.toString(a);
System.out.print(“enter delete number :”);
int c=s.nextInt();
System.out.print(“enter replace number : “);
int add=s.nextInt();
String g=Integer.toString(add);
String d=Integer.toString(c);
String str1=””;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)==d.charAt(0))
str1+=g.charAt(0);
else str1+=str.charAt(i);
}
System.out.println("convert number is : "+str1);
}
}
class Hello
{
public static void main(String[] args)
{
ReplaceNumber r=new Replace();
r.m1();
}
}
import java.util.*;
class main{
static int replace(int n){
int temp = 0;
int res = 0;
int j=0;
for(int i=10;n>0;n/=10){
temp = n % i;
if(temp==0){
res += 1*Math.pow(10,j);
}else{
res += temp*Math.pow(10,j);
}
j++;
}
return res;
}
public static void main(String[] args) {
int n = 706120678;
System.out.println(replace(n));
}
}
import java.util.*;
class Main{
public static void main(String[] args){
System.out.println(“Enter A No.”);
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
String str = Integer.toString(number);
str = str.replace(“0”, “1”);
System.out.println(str);
}
}
public class RelpaceAll0With1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int n = s.nextInt();int rem =0;int ans =0;int countvalue=1;int rem1 =0;
while(n!=0) {
rem = n%10;
if(rem ==0) {
rem1 = 1;
}
else {
rem1 = rem;
}
ans = ans + (rem1*countvalue);
countvalue = countvalue*10;
n = n/10;
}
System.out.println(ans);
}
}
import java.util.*;public class Main
{
public static void main(String[] args)
{
String str=””;
String str2=””;
System.out.println(“enter”);
Scanner s= new Scanner(System.in);
str=s.nextLine();
for(int i=0; i<str.length();i++){
if(str.charAt(i)=='0'){
str2=str2+'1';
}
else{
str2=str2+str.charAt(i);
}
}
System.out.println(str2);
}
}
import java.util.*;
public class Main
{
static int replace(int number)
{
// Base case for recursion termination
if (number == 0)
return 0;
// Extract the last digit and change it if needed
int digit = number % 10;
if (digit == 0)
digit = 1;
// Convert remaining digits and append the last digit
return replace(number/10) * 10 + digit;
}
static int Convert(int number)
{
if (number == 0)
return 1;
else
return replace(number);
}
public static void main(String[] args)
{
int number;
Scanner sc = new Scanner(System.in);
System.out.print(“Enter the number : “);
number = sc.nextInt();
System.out.print(“Number after replacement : “);
System.out.print(replace(number));
}
}
/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press “Run” button to execute it.
*******************************************************************************/
class A{
int b=0;
void replace(int arr[],int s,int num){
int i,rem=0;
while(num!=0){
rem=num%10;
for(i=b;i<=b;i++){
arr[i]=rem;
}
num=num/10;
b=b+1;
}
for( i=0;i<s;i++){
if(arr[i]==0){
arr[i]=1;
}
}
for(i=0;i<s;i++){
System.out.println(arr[i]);
}
}
}
class Main{
public static void main(String args[]){
int num=12030405;
int c=8;
int arr[]=new int[8];
A obj=new A();
obj.replace(arr,c,num);
}
}