Write A Program in Java To Input A Number and Check Whether It Is A Fascinating Number or Not
Write A Program in Java To Input A Number and Check Whether It Is A Fascinating Number or Not
import java.util.Scanner;
Page 1||
Output
if (isFascinating)
else
System.out.println(num + " is not a Fascinating Number");
}
}
if (n <= 0) {
System.out.println(n + " is not a Smith Number.");
return;
}
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.
Example: 3,5,7,11
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;
}
}
}
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;
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;
}
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.
Soltion.
import java.util.Scanner;
while(copyNum != 0) {
int digit = copyNum % 10;
copyNum /= 10;
revNum = revNum * 10 + digit;
}
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.