Question

In: Computer Science

A palindrome prime is a prime number that reads the same forwards or backwards. An example...

A palindrome prime is a prime number that reads the same forwards or backwards. An example of a
palindrome prime is 131. Write a method with the following signature for determining if a given
number is a palindrome prime.
public static boolean isPallyPrime(int nVal)

Note: For this assignment you are not allowed to use the built in Java class Array as part of your solution for any of these questions. Your Method signatures must
be the same as given here.

Solutions

Expert Solution

I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments(read them for better understanding).

Images of the Code:


Note: If the below code is missing indentation please refer code Images

Typed Code:

//importing required package
import java.util.Scanner;
public class Main
{
   public static void main(String[] args)
   {
   //Scanner is used to take inputs from the user
   Scanner scnr = new Scanner(System.in);
   System.out.print("Enter a Number: ");
   //Getting input from the user
       int nVal = scnr.nextInt();
       //calling function ==> if the method returns true then
       if(isPallyPrime(nVal))
       {
       //given number is palindrome prime
       System.out.print(nVal+" is a palindrome prime");
       }
       //else
       else
       {
       //given number is not a palindrome prime
       System.out.print(nVal+" is not a palindrome prime");
       }
   }
   //function called
   public static boolean isPallyPrime(int nVal)
   {
   //initializing required variables
   int val = nVal,count = 0,rev = 0, pal = 0;
   //for loop will iterate val times
   for(int i = 1; i <= val; i++)
   {
   //if val is divided by i and remainder is 0
   if(val%i == 0)
   {
   //increasing count
   count++;
   }
   }
   //while loop will iterate until val is greater than 0
   while(val > 0)
{
//rev is used to store last digit
rev = val % 10;
//adding rev value to it
pal = pal * 10 + rev;
//dividing val by 10
val = val/10;
}
//if nVal = pal and count = 2
if(nVal == pal && count == 2)
{
//return true
return true;
}
//else
else
{
//return false
return false;
}
   }
}
//code ended here

Output:


If You Have Any Doubts. Please Ask Using Comments.

Have A Great Day!


Related Solutions

2. Palindromes A palindrome is a word that reads the same forwards and backwards. For example,...
2. Palindromes A palindrome is a word that reads the same forwards and backwards. For example, \aibohphobia" (the irrational fear of palindromes) is a word that reads the same forwards and backwards. Write a function called isPalindrome() that accepts a string as a parameter and returns True if the string is a palindrome, False otherwise. Using the function you created, write a program Python home work Python
IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards....
IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty...
#Python: A palindrome is a sequence of characters that reads the same backwards as forwards. For...
#Python: A palindrome is a sequence of characters that reads the same backwards as forwards. For example, ‘Eve’, ‘madam’, and 20502, are palindromes. Write a function called testPalindrome() that asks the user to input a string and returns if that string is a palindrome with the output as follows, without red: >>> Please enter a string: eve Your string "eve" is a palindrome. >>> testPalindrome() Please enter a string: end Your string "end" is not a palindrome. >>> testPalindrome() Please...
python question A word is a palindrome if it the same read forwards and backwards. We...
python question A word is a palindrome if it the same read forwards and backwards. We will call a word a fuzzy palindrome if it is the same read forwards and backwards, except for possible differences in case. For example, both 'tattarrattat' and 'TaTtArRAttat' are fuzzy palindromes. Define a function is_fuzzy_palindrome that returns True if and only if its argument is a fuzzy palindrome. This method may be useful: S.lower() -> str : Return a copy of the string S...
making a python code for this: A palindrome is a sequence that reads the same backwards...
making a python code for this: A palindrome is a sequence that reads the same backwards as forwards. Numbers can also be palindromes if we consider their digits as a sequence, for example 12121 and 8228 are palindromes. We can find palindromes from an initial seed number using the reverse and add method: choose a number, reverse its digits and add it to the original. If the sum is not a palindrome (which means, it is not the same number...
(Palindrome number - A number is a palindrome if it reads the same from right to...
(Palindrome number - A number is a palindrome if it reads the same from right to left and from left to right, for example 676 is a palindrome number) Write a program that prompts the user to enter a three-digit integer number and determines whether it is a palindrome number or not In Java Please
In C++: This is the problem: [(1)] A palindrome is a string that reads the same...
In C++: This is the problem: [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q...
JAVA Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward...
JAVA Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward or backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a boolean method that users recursion to determine where a String argument is a palindrome. The method should return true if the argument reads the same forward and backward. Demonstrate the method in a program. Include the following...
1. A is called a palindrome if it reads the same from left and right. For...
1. A is called a palindrome if it reads the same from left and right. For instance, 13631 is a palindrome, while 435734 is not. A 6-digit number n is randomly chosen. Find the probability of the event that (a) n is a palindrome. (b) n is odd and a palindrome. (c) n is even and a palindrome.
A is called a palindrome if it reads the same from left and right. For instance,...
A is called a palindrome if it reads the same from left and right. For instance, 13631 is a palindrome, while 435734 is not. A 6-digit number n is randomly chosen. Find the probability of the event that (a) n is a palindrome. (b) n is odd and a palindrome. (c) n is even and a palindrome.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT