Dremendo Tag Line

Input a number and check if it is a duck number or not using a function in Java

Function - Question 5

In this question, we will see how to input a number and check if it is a duck number or not in Java programming using a function. To know more about function click on the function lesson.

Q5) Write a program in Java to input a number and check if it is a duck number or not using a function. The function should return 1 if the numbers is a duck number else return 0.

A duck number is a number which has zero present in it.

Example: 3210, 7056, 35070, etc.

Program

import java.util.Scanner;

public class Q5
{
    public static int ducknumber(int num)
    {
        int d;
        while(num>0)
        {
            d=num%10;
            if(d==0)
            {
                return 1;
            }
            num=num/10;
        }
        return 0;
    }

    public static void main(String args[])
    {
        int n;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a number ");
        n=sc.nextInt();
        if(ducknumber(n)==1)
        {
            System.out.print("Duck Number");
        }
        else
        {
            System.out.print("Not Duck Number");
        }
    }
}

Output

Enter a number 73062
Duck Number
video-poster