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.
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
Related Questions
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]?
Define a class to accept values into an integer array of order 4 x 4 and check whether it is a DIAGONAL array or not. An array is DIAGONAL if the sum of the left diagonal elements equals the sum of the right diagonal elements. Print the appropriate message.
Example:
3 4 2 5
2 5 2 3
5 3 2 7
1 3 7 1Sum of the left diagonal element = 3 + 5 + 2 + 1 = 11
Sum of the right diagonal element = 5 + 2 + 3 + 1 = 11
Name the method of search depicted in the below picture:
Define a class pin code and store the given pin codes in a single dimensional array. Sort these pin codes in ascending order using the Selection Sort technique only. Display the sorted array.
110061, 110001, 110029, 110023, 110055, 110006, 110019, 110033