Computer Applications
Write a java program to accept an integer. From it create a new integer by removing all zeros from it. For example, for the integer 20180 the new integer after removing all zeros from it will be 218. Print the original as well as the new integer.
Java
Java Iterative Stmts
32 Likes
Answer
import java.util.Scanner;
public class Kboat
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int num = in.nextInt();
int idx = 0;
int newNum = 0;
int n = num;
while (n != 0) {
int d = n % 10;
if (d != 0) {
newNum += (int)(d * Math.pow(10, idx));
idx++;
}
n /= 10;
}
System.out.println("Original number = " + num);
System.out.println("New number = " + newNum);
}
}
Output

Answered By
11 Likes
Related Questions
Give the output of the following program segment. How many times is the loop executed?
for(x=10; x>20;x++) System.out.println(x); System.out.println(x*2);
Convert the following for loop segment to an exit-controlled loop.
for (int x = 1, y = 2; x < 11; x += 2, y += 2) { System.out.println(x + "\t" + y); }
To execute a loop 10 times, which of the following is correct?
- for (int i=11;i<=30;i+=2)
- for (int i=11;i<=30;i+=3)
- for (int i=11;i<20;i++)
- for (int i=11;i<=21;i++)
How many times will the following loop execute? Write the output of the code:
int a = 5; while (a > 0) { System.out.println(a-- + 2); if (a % 3 == 0) break; }