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
2 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
3 Likes
Related Questions
Which of the following is a valid way to declare and initialize an integer array to store the ages of 50 students?
Consider the following two-dimensional array and answer the questions given below:
int x[ ][ ] = {{4,3,2}, {7,8,2}, {8, 3,10}, {1, 2, 9}};
(a) What is the order of the array?
(b) What is the value of x[0][0]+x[2][2]?
The statement used to find the total number of Strings present in the string array String s[] is:
- s.length
- s.length()
- length(s)
- len(s)
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)