0% found this document useful (0 votes)
97 views

Write A Program in Java To Input A Number and Check Whether It Is A Fascinating Number or Not

Uploaded by

fake Fake
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Write A Program in Java To Input A Number and Check Whether It Is A Fascinating Number or Not

Uploaded by

fake Fake
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Program 1.

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.

Let's understand the concept of Fascinating Number through the following


example:
Consider the number 192
192 x 1 = 192
192 x 2 = 384
192 x 3 = 576
Concatenating the results: 192 384 576
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.

import java.util.Scanner;

public class KboatFascinatingNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number to check: ");
int num = in.nextInt();

if (num < 100) {


System.out.println(num + " is not a Fascinating Number");
return;
}

int num2 = num * 2;


int num3 = num * 3;

boolean isFascinating = true;


String str = "" + num + num2 + num3;
for (char i = '1'; i <= '9'; i++) {
int idx1 = str.indexOf(i);
int idx2 = str.lastIndexOf(i);
if (idx1 == -1 || idx1 != idx2) {
isFascinating = false;
break;

Page 1||
Output
if (isFascinating)

System.out.println(num + " is a Fascinating Number");

else
System.out.println(num + " is not a Fascinating Number");
}
}

Algorithm for Checking a Fascinating Number


1. **Input Handling**:
Read the number from the user input.
If the number is less than 100, print that it is not a fascinating number
and exit.
2. **Generate Multiples**:
Calculate the second multiple of the number (num * 2).
Calculate the third multiple of the number (num * 3).
3. **Concatenate Strings**:
Convert the number, its second multiple, and its third multiple to
strings.
Concatenate these strings together.
4. **Check for Fascination**:
Initialize a boolean flag `isFascinating` to true.
Loop through characters '1' to '9'.
For each character, check if it appears exactly once in the concatenated
string.
If any character is missing or appears more than once, set
`isFascinating` to false and break the loop.
5. **Output Result**:
If `isFascinating` is true, print that the number is a fascinating
number.Otherwise, print that the number is not a fascinating
number.This algorithm outlines the step-by-step approach taken by the
Java program to determine if a given number is a fascinating number.
Program 2.
A Smith number is a composite number, whose sum of the digits is equal to the
sum of its prime factors. For example:
4, 22, 27, 58, 85, 94, 121 ………. are Smith numbers.

Write a program in Java to enter a number and check whether it is a Smith


number or not.

Sample Input: 666


Sum of the digits: 6 + 6 + 6 = 18
Prime factors are: 2, 3, 3, 37
Sum of the digits of the prime factors: 2 + 3 + 3 + (3 + 7) = 18
Thus, 666 is a Smith Number.
import java.util.Scanner;

public class KboatSmithNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int n = in.nextInt();

if (n <= 0) {
System.out.println(n + " is not a Smith Number.");
return;
}

boolean isComposite = false;


for (int i = 2; i < n; i++) {
if (n % i == 0) {
isComposite = true;
break;
}
}

if (isComposite && n != 1) {
int sumDigits = 0;
int t = n;
while (t != 0) {
int d = t % 10;
sumDigits += d;
t /= 10;
}

int sumPrimeDigits = 0;
t = n;
for(int i = 2; i < t; i++) {
Output
while(t % i == 0) {

t /= i;
int temp = i;
while (temp != 0) {
int d = temp % 10;
sumPrimeDigits += d;
temp /= 10;
}
}
}

if(t > 2) {
while (t != 0) {
int d = t % 10;
sumPrimeDigits += d;
t /= 10;
}
}

if (sumPrimeDigits == sumDigits)
System.out.println(n + " is a Smith Number.");
else
System.out.println(n + " is not a Smith Number.");
}
else {
System.out.println(n + " is not a Smith Number.");
}
}
}
Algorithm for Checking a Smith Number

1. **Input Handling**:
- Read the number `n` from the user input.
- If `n` is less than or equal to 0, print that it is not a Smith Number and exit.
2. **Check for Composite Number**:
- Initialize a boolean flag `isComposite` to false.
- Loop from 2 to `n-1` to check if `n` is divisible by any number. If it is, set `isComposite` to true and
break the loop.
- If `isComposite` is false or `n` is 1, print that it is not a Smith Number and exit.
3. **Calculate Digit Sum of `n`**:
- Initialize `sumDigits` to 0.
- While `n` is not 0:
- Add the last digit of `n` to `sumDigits`.
- Remove the last digit from `n`.
4. **Calculate Digit Sum of Prime Factors of `n`**:
- Initialize `sumPrimeDigits` to 0.
- For each integer `i` from 2 to the original value of `n`:
- While `n` is divisible by `i`:
- Divide `n` by `i`.
- For each digit in `i`, add it to `sumPrimeDigits`.
- If `n` is greater than 2 after the loop, add the digits of `n` to `sumPrimeDigits`.
5. **Compare Sums**:
- If `sumPrimeDigits` equals `sumDigits`, print that `n` is a Smith Number.
- Otherwise, print that `n` is not a Smith Number.This algorithm outlines the step-by-step approach
taken by the Java program to determine if a given number is a Smith Number.
Program 3.
Write a menu driven program to accept a number from the user and check
whether it is a Prime number or an Automorphic number.

(a) Prime number: (A number is said to be prime, if it is only divisible by 1 and


itself)

Example: 3,5,7,11

(b) Automorphic number: (Automorphic number is the number which is


contained in the last digit(s) of its square.)

Example: 25 is an Automorphic number as its square is 625 and 25 is present as


the last two digits.
SOULTION
import java.util.Scanner;

public class KboatPrimeAutomorphic


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Prime number");
System.out.println("2. Automorphic number");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
System.out.print("Enter number: ");
int num = in.nextInt();

switch (choice) {
case 1:
int c = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
c++;
}
}
if (c == 2)
System.out.println(num + " is Prime");
else
System.out.println(num + " is not Prime");
break;

case 2:
int numCopy = num;
int sq = num * num;
Output
int d = 0;

/*
* Count the number of
* digits in num
*/
while(num > 0) {
d++;
num /= 10;
}

/*
* Extract the last d digits
* from square of num
*/
int ld = (int)(sq % Math.pow(10, d));

if (ld == numCopy)
System.out.println(numCopy + " is automorphic");
else
System.out.println(numCopy + " is not automorphic");
break;

default:
System.out.println("Incorrect Choice");
break;
}
}
}

### Algorithm for Checking Prime and Automorphic Numbers

1. **Input Handling**:
- Display options for the user to choose:
1. Prime number
2. Automorphic number
- Read the user's choice.
- Read the number `num` from the user input.
2. **Choice Handling**:
- **Case 1**: Check if `num` is a prime number.
- **Case 2**: Check if `num` is an automorphic number.
3. **Prime Number Check**:
- Initialize a counter `c` to 0.
- Loop from 1 to `num`:
- If `num` is divisible by the current loop index, increment `c`.
- If `c` is exactly 2, print that `num` is prime.
- Otherwise, print that `num` is not prime.
4. **Automorphic Number Check**:
- Copy the value of `num` to `numCopy`.
- Calculate the square of `num` and store it in `sq`.
- Initialize a counter `d` to 0.
- Count the number of digits in `num` by looping until `num` is 0, incrementing `d` and dividing `num`
by 10 each time.
- Extract the last `d` digits from `sq` by calculating `sq % 10^d`.
- If the extracted digits are equal to `numCopy`, print that `numCopy` is automorphic.
- Otherwise, print that `numCopy` is not automorphic.
5. **Default Case**:
- If the user input is not 1 or 2, print "Incorrect Choice".

This algorithm outlines the step-by-step approach taken by the Java program to
determine if a given number is either a prime number or an automorphic number
based on the user's choice.
Output
Program 4.
A 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'.
Solution.
import java.util.Scanner;

public class KboatTwistedPrime


{
public void twistedPrimeCheck() {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int num = in.nextInt();

if (num == 1) {
System.out.println(num + " is not a twisted prime number");
}
else {
boolean isPrime = true;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}

if (isPrime) {

int t = num;
int revNum = 0;

while (t != 0) {
int digit = t % 10;
t /= 10;
revNum = revNum * 10 + digit;
}

for (int i = 2; i <= revNum / 2; i++) {


if (revNum % i == 0) {
isPrime = false;
break;
}
}
}

if (isPrime)
System.out.println(num + " is a twisted prime number");
else
System.out.println(num + " is not a twisted prime number");
}

}
}
Here's the algorithm for checking if a number is a Twisted Prime Number:

1. **Input**:
- Prompt the user to enter a number.
2. **Initial Check**:
- If the number is 1, it is not a twisted prime number. Print a message and exit.
3. **Check if the Number is Prime**:
- Initialize a boolean flag `isPrime` to `true`.
- Loop through the numbers from 2 to `num / 2`.
- If the number is divisible by any of these numbers, set `isPrime` to `false` and break the loop.
- If `isPrime` remains `true`, proceed to the next step. Otherwise, print that the number is not a
twisted prime number and exit.
4. **Reverse the Number**:
- Initialize `revNum` to 0.
- While the number is not 0:
- Extract the last digit of the number.
- Append the digit to `revNum`.
- Remove the last digit from the number.
5. **Check if the Reversed Number is Prime**:
- If the number is still prime (i.e., `isPrime` is `true`):
- Loop through the numbers from 2 to `revNum / 2`.
- If the reversed number is divisible by any of these numbers, set `isPrime` to `false` and break the
loop.- If `isPrime` remains `true`, print that the number is a twisted prime number. Otherwise, print that
the number is not a twisted prime number.

This algorithm ensures that both the original number and its reversed version are prime to determine if
it is a twisted prime number.
Output
Program 5.
Write a program to input a number and display the new number after reversing
the digits of the original number. The program also displays the absolute
difference between the original number and the reversed number.

Sample Input: 194


Sample Output: 491
Absolute Difference= 297

Soltion.

import java.util.Scanner;

public class KboatDigitReverse


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


System.out.print("Enter Number: ");
int orgNum = in.nextInt();

int copyNum = orgNum;


int revNum = 0;

while(copyNum != 0) {
int digit = copyNum % 10;
copyNum /= 10;
revNum = revNum * 10 + digit;
}

int diff = revNum - orgNum;


System.out.println("Reversed Number = " + revNum);
System.out.println("Absolute Difference = " + Math.abs(diff));
}
}

Sure, here's the algorithm for the `KboatDigitReverse` program that reverses a given integer and
calculates the absolute difference between the original and the reversed number:

1. **Initialize Scanner:**
- Create a `Scanner` object to read input from the user.
2. **Read Input:**
- Prompt the user to enter a number.
.
Store the input number in a variable `orgNum`.
3. **Initialize Variables:**
- Create a copy of the original number and store it in `copyNum`.
- Initialize `revNum` to 0, which will hold the reversed number.
4. **Reverse the Number:**
- While `copyNum` is not zero:
- Extract the last digit of `copyNum` using the modulus operator (`copyNum % 10`) and store it in
`digit`.
- Remove the last digit from `copyNum` by dividing it by 10 (`copyNum /= 10`).
- Update `revNum` by multiplying it by 10 and adding `digit` to it.
5. **Calculate the Difference:**
- Calculate the difference between the reversed number (`revNum`) and the original number
(`orgNum`) and store it in `diff`.
6. **Print Results:**
- Print the reversed number.
- Print the absolute value of the difference using `Math.abs(diff)`.

This algorithm effectively captures the process described in your Java program for reversing a number
and finding the absolute difference between the original and the reversed number
Output
Program 6.

You might also like