Computer Applications
Write a Java program to store n numbers in an one dimensional array. Pass this array to a function number(int a[]). Display only those numbers whose sum of digit is prime.
Java
Java Arrays
3 Likes
Answer
import java.util.Scanner;
public class KboatSDAPrime
{
public void number(int a[]) {
System.out.println("Numbers whose sum of digit is prime:");
for (int i = 0; i < a.length; i++) {
int x = a[i];
int s = 0;
while (x != 0) {
int d = x % 10;
s += d;
x /= 10;
}
int c = 0;
for (int j = 1; j <= s; j++) {
if (s % j == 0) {
c++;
}
}
if (c == 2) {
System.out.println(a[i]);
}
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = in.nextInt();
int arr[] = new int[n];
System.out.println("Enter array elements: ");
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
KboatSDAPrime obj = new KboatSDAPrime();
obj.number(arr);
}
}Output
![BlueJ output of Write a Java program to store n numbers in an one dimensional array. Pass this array to a function number(int a[]). Display only those numbers whose sum of digit is prime. BlueJ output of Write a Java program to store n numbers in an one dimensional array. Pass this array to a function number(int a[]). Display only those numbers whose sum of digit is prime.](https://cdn1.knowledgeboat.com/img/java-programs/KboatSDASumDigitPrime-p1.jpg)
Answered By
1 Like
Related Questions
A single dimensional array has 50 elements, which of the following is the correct statement to initialize the last element to 100.
- x[51]=100
- x[48]=100
- x[49]=100
- x[50]=100
Name the below structure:

- One dimensional array
- Two Dimensional array with 4 rows and 5 columns
- Three dimensional array
- Two Dimensional array with 5 rows and 4 columns
Define a class to accept values into 4x4 array and find and display the sum of each row.
Example:
A[][]={{1,2,3,4},{5,6,7,8},{1,3,5,7},{2,5,3,1}}
Output:
sum of row 1 = 10 (1+2+3+4) sum of row 2 = 26 (5+6+7+8) sum of row 3 = 16 (1+3+5+7) sum of row 4 = 11 (2+5+3+1)