Fascinating Numbers: Some Numbers of 3 Digits or More Exhibit A Very Interesting Property. The
Fascinating Numbers: Some Numbers of 3 Digits or More Exhibit A Very Interesting Property. The
Write a Program in Java to input a number and check whether it is a Fascinating Number or not..
Fascinating Numbers : Some numbers of 3 digits or more exhibit a very interesting property. The
property is such that, when the number is multiplied by 2 and 3, and both these products are
concatenated with the original number, all digits from 1 to 9 are present exactly once, regardless of
the number of zeroes.
Lets understand the concept of Fascinating Number through the following example:
Consider
the
number
192,
192
192
192
384
192 x 3 = 576
Concatenating the results : 192384576
It could be observed that 192384576 consists of all digits from 1 to 9 exactly once. Hence, it could
be concluded that 192 is a Fascinating Number.
Some examples of fascinating Numbers are : 192, 219, 273, 327, 1902, 1920, 2019 etc.
Programming Code:
import java.util.*;
class FascinatingNumber
{
boolean isUnique(String q)
{
int A[] = {0,0,0,0,0,0,0,0,0,0}; //to store frequency of every digit from '0'
int i, flag = 0;
char ch;
for(i=0; i<q.length(); i++)
{
ch = q.charAt(i);
A[ch-48]++;
/* increasing A[5] if ch='5' as '5'-48 = 53-48=5
* (ASCII values of '0' to '9' are 48 to 57) */
}
for(i=1; i<10; i++)
{
//checking if every digit from '1' to '9' are present exactly once or not
if(A[i]!=1)
{
flag = 1; //flag is set to 1 if frequency is not 1
break;
}
}
if(flag == 1)
return false;
else
return true;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
FascinatingNumber ob = new FascinatingNumber();
System.out.print("Enter a number : ");
int n = sc.nextInt();
String p = Integer.toString(n); //converting the number to String
if(p.length()<3)
System.out.println("Number should be of atleast 3 digits.");
else
{
Question:
Write
Program
in
Java
to
input
number
and
check
whether
it
is
a Pronic
Pronic Number : A pronic number, oblong number, rectangular number or heteromecic number, is a
number which is the product of two consecutive integers, that is, n (n + 1).
The
first
few
pronic
numbers
are:
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462 etc.
Programming Code:
import java.util.*;
class PronicNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = sc.nextInt();
int flag = 0;
for(int i=1; i<=n; i++)
{
if(i*(i+1) == n)
{
flag = 1;
break;
}
}
if(flag == 1)
System.out.println(n+" is a Pronic Number.");
else
System.out.println(n+" is not a Pronic Number.");
}
Question:
Write a Program in Java to input a number and check whether it is a Harshad Number or Niven
Number or not..
Harshad Number : In recreational mathematics, a Harshad number (or Niven number), is an integer
(in base 10) that is divisible by the sum of its digits.
Lets understand the concept of Harshad Number through the following example:
The number 18 is a Harshad number in base 10, because the sum of the digits 1 and 8 is 9
(1 + 8 = 9), and 18 is divisible by 9 (since 18 % 9 = 0)
The number 1729 is a Harshad number in base 10, because the sum of the digits 1 ,7, 2 and
9 is 19 (1 + 7 + 2 + 9 = 19), and 1729 is divisible by 19 (1729 = 19 * 91)
The number 19 is not a Harshad number in base 10, because the sum of the digits 1 and 9 is
10 (1 + 9 = 10), and 19 is not divisible by 10 (since 19 % 10 = 9)
The
first
few
Harshad
numbers
in
base
10
are:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50, 54, 60, 63, 70, 72, 80,
81, 84, 90, 100, 102, 108, 110, 111, 112, 114, 117, 120, 126, 132, 133, 135, 140, 144, 150, 152, 153,
156, 162, 171, 180, 190, 192, 195, 198, 200 etc.
Programming Code:
import java.util.*;
class HarshadNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
if(n%sum == 0)
System.out.println(n+" is a Harshad Number.");
else
System.out.println(n+" is not a Harshad Number.");
}
}
Question:
Write a Program in Java to input a number and check whether it is a Disarium Number or not.
Note: A number will be called DISARIUM if sum of its digits powered with their respective position is
equal to the original number.
For example 135 is a DISARIUM
(Workings 11+32+53 = 135, some other DISARIUM are 89, 175, 518 etc)
Programming Code:
import java.io.*;
class Disarium
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
System.out.print("Enter a number : ");
int n = Integer.parseInt(br.readLine());
int copy = n, d = 0, sum = 0;
int len = s.length(); //finding the length of the number i.e. no.of digits
while(copy>0)
{
d = copy % 10; //extracting the last digit
sum = sum + (int)Math.pow(d,len);
len--;
copy = copy / 10;
}
if(sum == n)
System.out.println(n+" is a Disarium Number.");
else
System.out.println(n+" is not a Disarium Number.");
}
}
Question:
A Smith number is a composite number, the sum of whose digits is the sum of the digits of its
prime factors obtained as a result of prime factorization (excluding 1). The first few such numbers
are 4, 22, 27, 58, 85, 94, 121 ..
Examples:
1. 666
Prime factors are 2, 3, 3, and 37
Sum of the digits are (6+6+6) = 18
Sum of the digits of the factors (2+3+3+(3+7)) = 18
2. 4937775
Prime factors are 3, 5, 5, 65837
Sum of the digits are (4+9+3+7+7+7+5) = 42
Sum of the digits of the factors (3+5+5+(6+5+8+3+7)) = 42
Write a program to input a number and display whether the number is a Smith number or not.
Sample data:
Input
94
Output
SMITH Number
Input
102
Output
Input
666
Output
SMITH Number
Input
999
Output
Programming Code:
import java.io.*;
class Smith
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//function for finding sum of digits
int sumDig(int n)
{
int s=0;
while(n>0)
{
s=s+n%10;
n=n/10;
}
return s;
}
if(a==b)
System.out.print("It is a Smith Number");
else
System.out.print("It is Not a Smith Number");
}
}
Question:
Given two positive numbers M and N, such that M is between 100 and 10000 and N is less than 100.
Find the smallest integer that is greater than M and whose digits add up to N. For example, if M =
100 and N = 11, then the smallest integer greater than 100 whose digits add up to 11 is 119.
Write a program to accept the numbers M and N from the user and print the smallest required
number whose sum of all its digits is equal to N. Also, print the total number of digits present in the
required number. The program should check for the validity of the inputs and display an appropriate
message for an invalid input.
Test your program with the sample data and some random data:
Example 1
INPUT :
M = 100
N = 11
OUTPUT :
The required number = 119
Total number of digits = 3
Example 2
INPUT :
M = 1500
N = 25
OUTPUT :
The required number = 1699
Total number of digits = 4
import java.util.*;
class Q1_ISC2015
{
int sumDig(long n) // Function to find sum of digits of a number
{
int sum = 0, d;
while(n>0)
{
d = (int)(n%10);
sum = sum + d;
n = n/10;
}
return sum;
}
String s = Long.toString(n);
int len = s.length();
return len;
}
Question:
Write a Program in Java to print all the Twin Prime numbers within a given range.
Note: Twin Prime numbers are a pair of numbers which are both prime and their difference is 2.
Example: Twin
Prime
numbers
in
the
range
Programming Code:
import java.io.*;
class TwinPrimeRange
{
boolean isPrime(int n) //funton for checking prime
{
int count=0;
for(int i=1; i<=n; i++)
{
if(n%i == 0)
count++;
}
to
100
are
if(count == 2)
return true;
else
return false;
}
if(p>q)
System.out.println("Invalid Range !");
else
{
}
}
}
}
Question:
A special two-digit number is such that when the sum of the digits is added to the product of its
digits, the result is equal to the original two-digit number.
Example:
Consider the number 59.Sum of digits = 5+9=14
Product of its digits = 5 x 9 = 45
Sum of the digits and product of digits = 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If
the value is equal to the number input, output the message special-two digit number otherwise,
output the message Not a special two-digit number.
Programming Code:
import java.io.*;
class Special_Q5_ICSE2014
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
System.out.print("Enter a 2 digit number : ");
int n = Integer.parseInt(br.readLine());
if((sum + pro) == n)
{
Question:
Write a Program in Java to input a number and check whether it is a Unique Number or not.
Note: A Unique number is a positive integer (without leading zeros) with no duplicate digits. For
example 7, 135, 214 are all unique numbers whereas 33, 3121, 300 are not.
Solution:
import java.io.*;
class UniqueNumber
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any number : ");
int n=Integer.parseInt(br.readLine());
}
}
}
if(flag==0)
System.out.println("**** The Number is a Unique Number ****");
else
System.out.println("**** The Number is Not a Unique Number ****");
}
}
Question:
An Emirp number is a number which is prime backwards and forwards. Example: 13 and 31 are both
prime
numbers.
Thus,
13
is
an
Emirp
number.
Design a class Emirp to check if a given number is Emirp number or not. Some of the members of
the class are given below:
Class name : Emirp
Data
members/instance
n
rev
:
:
variables:
stores
stores
the
the
reverse
number
of
the
number
functions:
nn)
: to
assign
nn,
rev
and
int isprime(int x) : check if the number is prime using the recursive technique and return 1 if prime
otherwise
return
void isEmirp() : reverse the given number and check if both the original number and the reverse
number are prime, by invoking the function isprime(int) and display the result with an appropriate
message.
Specify the class Emirp giving details of the constructor(int), int isprime(int) and void isEmirp().
Define the main() function to create an object and call the methods to check for Emirp number.
Programming Code:
import java.util.*;
class Emirp
{
int n, rev, f;
Emirp(int nn) //parameterised constructor
{
n=nn;
rev=0;
f=2;
}
/* Note: The function isPrime(...) will check whether 'n' is prime or not
* It will not check whether 'x' is prime or not.
* The variable 'x' is just a counter to increment the recursion process
*/
int isprime(int x)
{
if(x<=n)
{
if(n%x!=0)
{
isprime(x+1);
}
}
if(x==n)
return 1;
else
return 0;
}
void isEmirp()
{
int copy=n, d;
while(copy>0) // code for reversing a number
{
d=copy%10;
rev=rev*10+d;
copy=copy/10;
}
int a=isprime(f); //checking whether the Original number 'n' is Prime or not
f=2; //resetting the value of f for checking whether the reverse number is Pr
int b=isprime(f); //checking whether the Reverse number is Prime or not
if(a==1 && b==1) //If both Original and Reverse are Prime, then it is an Emir
System.out.println("It is an Emirp Number");
else
System.out.println("It is Not an Emirp Number");
Question:
An ISBN (International Standard Book Number) is a ten digit code which uniquely identifies a book.
The first nine digits represent the Group, Publisher and Title of the book and the last digit is used to
check whether ISBN is correct or not.
Each of the first nine digits of the code can take a value between 0 and 9. Sometimes it is necessary
to make the last digit equal to ten; this is done by writing the last digit of the code as X.
To verify an ISBN, calculate 10 times the first digit, plus 9 times the second digit, plus 8 times the
third and so on until we add 1 time the last digit. If the final number leaves no remainder when
divided by 11, the code is a valid ISBN.
For Example:
1. 0201103311 = 10*0 + 9*2 + 8*0 + 7*1 + 6*1 + 5*0 + 4*3 + 3*3 + 2*1 + 1*1 = 55
Since 55 leaves no remainder when divided by 11, hence it is a valid ISBN.
2. 007462542X = 10*0 + 9*0 + 8*7 + 7*4 + 6*6 + 5*2 + 4*5 + 3*4 + 2*2 + 1*10 = 176
Since 176 leaves no remainder when divided by 11, hence it is a valid ISBN.
Design a program to accept a ten digit code from the user. For an invalid input, display an
appropriate message. Verify the code for its validity in the format specified below:
import java.io.*;
class ISBN_ISC2013
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a 10 digit code : ");
String s=br.readLine();
int len=s.length();
if(len!=10)
System.out.println("Output : Invalid Input");
else
{
char ch;
int dig=0, sum=0, k=10;
for(int i=0; i<len; i++)
{
ch=s.charAt(i);
if(ch=='X')
dig=10;
else
dig=ch-48;
sum=sum+dig*k;
k--;
}
String ch;
int dig=0, sum=0, k=10;
for(int i=0; i<len; i++)
{
ch=Character.toString(s.charAt(i));
if(ch.equalsIgnoreCase("X"))
dig=10;
else
dig=Integer.parseInt(ch);
sum=sum+dig*k;
k--;
*/
}
}
}
Question:
A happy number is a number in which the eventual sum of the square of the digits of the number is
equal to 1.
Example:
):
getnum(int
constructor
nn) :
to
assign
to
the
parameter
assign
value
0
to
the
to
number
n
n
nn
int sum_sq_digits(int x) : returns the sum of the square of the digits of the number x, using
the recursive
technique
void ishappy( ) : checks if the given number is a happy number by calling the function
sum_sq_digits(int) and displays an appropriate message
Specify
the
class
Happy
giving
details
of
the constructor(
),
void
getnum(int),
int
sum_sq_digits(int) and void ishappy( ). Also define a main( )function to create an object and call
the methods to check for happy number.
Solution:
import java.io.*;
class Happy
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n;
Happy()
{
n=0;
}
int sum_sq_digits(int x)
{
if(x==0)
return 0;
else
{
int d=x%10;
return (d*d+ sum_sq_digits(x/10));
}
}
void ishappy()
{
int a=sum_sq_digits(n);
while(a>9)
{
a=sum_sq_digits(a);
}
if(a==1)
System.out.print(n+" is a Happy Number");
else
System.out.print(n+" is Not a Happy Number");
}
Question:
Write a Program in Java to input a number and check whether it is a Kaprekar number or not.
Note: A positive whole number n that has d number of digits is squared and split into two pieces, a
right-hand piece that has d digits and a left-hand piece that has remaining d or d-1 digits.
If the sum of the two pieces is equal to the number, then n is a Kaprekar number. The first few
Kaprekar numbers are: 9, 45, 297 ..
Example 1: 9
92 = 81, right-hand piece of 81 = 1 and left hand piece of 81 = 8
Sum = 1 + 8 = 9, i.e. equal to the number. Hence, 9 is a Kaprekar number.
Solution:
import java.io.*;
class Kaprekar
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a Number : ");
int n = Integer.parseInt(br.readLine()); //Inputting the number
if(sq<=9)
s = "0"+s; //Adding a zero in the beginning if the square is of single digit
int l = s.length(); //finding the length (i.e. no. of digits in the square).
int mid = l/2; //finding the middle point
//if sum of left and right numbers is equal to the original number then it is a K
if(x+y == n)
System.out.println(n+" is a Kaprekar Number");
else
System.out.println(n+" is Not a Kaprekar Number");
}
}