Computer Applications
Create a program in Java to find out if a number entered by the user is a Duck Number.
A Duck Number is a number which has zeroes present in it, but there should be no zero present in the beginning of the number. For example, 6710, 8066, 5660303 are all duck numbers whereas 05257, 080009 are not.
Java
Java Conditional Stmts
12 Likes
Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class KboatDuckNumber
{
public void duckNumberCheck() throws IOException {
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(reader);
System.out.print("Enter number: ");
boolean isDuck = false;
boolean firstZero = false;
int c = 0, d;
/*
* 10 is the ASCII code of newline
* We will read from inputstream
* one character at a time till we
* encounter a newline i.e. enter
*/
while((d = in.read()) != 10) {
char ch = (char)d;
if (c == 0 && ch == '0' )
firstZero = true;
if (!firstZero && ch == '0')
isDuck = true;
c++;
}
if (isDuck)
System.out.println("Duck Number");
else
System.out.println("Not a Duck Number");
}
}
Output
Answered By
7 Likes
Related Questions
Write a program in Java to compute the perimeter and area of a triangle, with its three sides given as a, b, and c using the following formulas:
Write a menu driven program to accept a number from the user and check whether it is a Buzz number or an Automorphic number.
i. Automorphic number is a number, whose square's last digit(s) are equal to that number. For example, 25 is an automorphic number, as its square is 625 and 25 is present as the last two digits.
ii. Buzz number is a number, that ends with 7 or is divisible by 7.The electricity board charges the bill according to the number of units consumed and the rate as given below:
Units Consumed Rate Per Unit First 100 units 80 Paisa per unit Next 200 units Rs. 1 per unit Above 300 units Rs. 2.50 per unit Write a program in Java to accept the total units consumed by a customer and calculate the bill. Assume that a meter rent of Rs. 500 is charged from the customer.
Mayur Transport Company charges for parcels as per the following tariff:
Weight Charges Upto 10 Kg. Rs. 30 per Kg. For the next 20 Kg. Rs. 20 per Kg. Above 30 Kg. Rs. 15 per Kg. Write a program in Java to calculate the charge for a parcel, taking the weight of the parcel as an input.