write a program in java to accept a number and display the new number after removing all the zeros
Answers
Answered by
7
class demo
{
public static void main(String args[])
{
int num;
String s,ss;
Scanner sc=new Scanner();
System.out.println(“Enter a no.”);
num=sc.nextInt();
s=String.valueOf(num); //convert the integer into string
ss=s.replaceAll(“0”,””); //replacing all 0 with nothing
num=Integer.parseInt(ss); //string to integer back
System.out.println(num);
}
}
Answered by
3
Answer:
//Write a program to accept a no. & display the new number after removing all zeros.
class Zero
{
public static void main(int n)
{
int rem=0,p,x,z=0,r=0,rev=0;
while(n>0)
{
rem=n%10;
if(rem==0)
{
x=rem;
}
else
{
p=rem;
rev=rev*10+p;
}
n=n/10;
}
while(rev>0)
{
z=rev%10;
r=r*10+z;
rev=rev/10;
}
System.out.println(“The new no.=”+r);
}
}
Explanation: Hope u understand it
Similar questions