0% found this document useful (0 votes)
4 views3 pages

Number PGM

The document contains Java code examples for three classes that check specific number properties: Duck number, EvenPal number, and Superspy number. A Duck number contains a zero, an EvenPal number is a palindrome with an even digit sum, and a Superspy number has a digit sum equal to the number of digits. Each class includes user input handling and logic to determine the respective number type.

Uploaded by

prachir741
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Number PGM

The document contains Java code examples for three classes that check specific number properties: Duck number, EvenPal number, and Superspy number. A Duck number contains a zero, an EvenPal number is a palindrome with an even digit sum, and a Superspy number has a digit sum equal to the number of digits. Each class includes user input handling and logic to determine the respective number type.

Uploaded by

prachir741
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Question 1

Define a class to accept a 3 digit number and check whether it is a duck number or not. [15]
Note: A number is a duck number if it has zero in it
Example1:
Input: 2083
Output: Invalid
Example 2:
Input: 103
Output: Duck number
import java.util.*;
class Duck_number
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the number");
int n=sc.nextInt();

if(n<100||n>999)
{
System.out.println("Invalid input");
System.exit(0);
}

int rem=0,temp=0,n1=n;
while(n>0)
{
rem=n%10;
if(rem==0)
{
temp=1;
break;
}
n=n/10;
}
if(temp==1)
System.out.println(n1+" is a duck number");
else
System.out.println(n1+" is not a duck number");
}
}
Define a class to accept a number from user and check if it is an EvenPal number or not. (The number is said
to be EvenPal number when number is palindrome number (a number is palindrome if it is equal to its reverse)
and sum of its digits is an even number.) Example: 121 – is a palindrome number
Sum of the digits – 1+2+1 = 4 which is an even number

import java.util.*;

public class EvenPal


{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");


int n = sc.nextInt();
int rem=0, sum = 0,rev=0,n1=n;

while (n> 0)
{
rem = n%10;
rev = rev*10 +rem;
sum = sum+rem;
n= n/10;
}

if (n1 == rev && sum % 2 == 0)


System.out.println(n1 + " is an EvenPal number.");
else
System.out.println(n1 + " is not an EvenPal number.");

}
}
Define a class to accept a number and check whether it is a superspy number or not . A super spy number is a number where sum of
digit is equal to number of digit
Example:
N=1021
Sum of digit=1+0+2+1=4
Number of digit=4
Output: 1021 is a superspy number
import java.util.*;

public class superspy


{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");


int n = sc.nextInt();
int sum = 0,rem=0, count = 0,n1=n;
while(n>0)
{
rem=n%10;
sum=sum+rem;
count++;
n=n/10;
}
if(sum == count)
System.out.println(n1 + " is an superspy number.");
else
System.out.println(n1 + " is not an superspy number.");

}
}

You might also like