Computer Science, asked by gs160160, 3 months ago

Q1. ​Write a program to store 10 four digit numbers in an array. (Check that they should be at least 4 digit long otherwise do not enter it into the array) . Display only those numbers which are prime palindrome.(i.e. Which are prime as well as palindrome.)
Can you plsss solve it fast

Answers

Answered by anindyaadhikari13
7

Required Answer:-

Question:

  • Write a program to store 10 four digit numbers in an array. Check that they should be at least 4 digit long otherwise do not enter into the array. Display only those numbers which are prime as well as palindromes.

Approach:

This is a good question. Read this post to get your answer.

Here comes the program.

package com.student;

import java.util.*;

public class Number

{

public static void main(String[] args)

{

Scanner sc=new Scanner(System.in);

int a[]=new int[10];

System.out.println("Enter 10numbers in the array: ");

for(int i=0;i<10;)

{

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

int n=sc.nextInt();

if(NumberOfDigits(n)==4)

{

a[i]=n;

i++;

}

else

System.out.println("Number of digits is not 4. Enter again..");

}

for(int i=0;i<10;i++)

{

if(isPrime(a[i]) && isPalindrome(a[i]))

{

System.out.println(a[i]+" is prime as well as palindrome at location "+i);

}

sc.close();

}

}

static boolean isPrime(int n)

{

int c=0;

for(int i=1;i<=n;i++)

{

if(n%i==0)

c++;

}

return (c==2);

}

static boolean isPalindrome(int n)

{

int m=n, s=0;

while(m!=0)

{

s=s*10+m%10;

m/=10;

}

return (s==n);

}

static int NumberOfDigits(int n)

{

int c=0;

for(;n!=0;n/=10)

c++;

return c;

}

}

How it's solved?

First, we have to import Scanner class. Scanner class allows us to take input from the user. It is present in utility package of Java. The I have created a class, main() method and other three methods to check prime, check palindrome and to count the number of digits. Now, let's come to the main. method. In the main method, I have created objects of Scanner class. Then, I have asked the user to enter 10 4digits number in the array. The nexInt() method of scanner class is used to input an integer. Now, using methods, I have checked if the each number in the array is prime as well as palindrome or not. If it's true, then the number is displayed on the screen.

Variable Description:

For main() method.

  1. a[] (integer array):- Used to store the numbers.
  2. n (int):- Used to enter the numbers for the array. If number of digits in n is greater than 3 then in is assigned in array.
  3. i (int):- Loop variable (for accessing array elements.

For isPrime() method.

  1. n (int):- To store Entered number.
  2. c (int):- To store the number of factors of n.

For isPalindrome() method.

  1. n (int):- To store Entered number.
  2. m (int):- To store value of n.
  3. s (int):- To store the reversed value of n.

For NumberOfDigits() method.

  1. n (int):- To store Entered number.
  2. c (int):- To store number of digits of n.

Output is attached.

Attachments:
Similar questions