Computer Applications
A number is said to be Duck if the digit zero is (0) present in it. Write a program to accept a number and check whether the number is Duck or not. The program displays the message accordingly. (The number must not begin with zero)
Sample Input: 5063
Sample Output: It is a Duck number.
Sample Input: 7453
Sample Output: It is not a Duck number.
Java
Java Iterative Stmts
105 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");
}
}
Variable Description Table
Program Explanation
Output
Answered By
36 Likes
Related Questions
A computerised bus charges fare from each of its passengers based on the distance travelled as per the tariff given below:
Distance (in km) Charges First 5 km ₹80 Next 10 km ₹10/km More than 15 km ₹8/km As the passenger enters the bus, the computer prompts 'Enter distance you intend to travel'. On entering the distance, it prints his ticket and the control goes back for the next passenger. At the end of journey, the computer prints the following:
- the number of passenger travelled
- total fare received
Write a program to perform the above task.
[Hint: Perform the task based on user controlled loop]Write a program to input a number and display it into its Binary equivalent.
Sample Input: (21)10
Sample Output: (10101)2A prime number is said to be 'Twisted Prime', if the new number obtained after reversing the digits is also a prime number. Write a program to accept a number and check whether the number is 'Twisted Prime' or not.
Sample Input: 167
Sample Output: 761
167 is a 'Twisted Prime'.Write a program to input a number and check whether it is a prime number or not. If it is not a prime number then display the next number that is prime.
Sample Input: 14
Sample Output: 17