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 program segment and answer the questions given below:
int x[][] = {{2,4,5,6}, {5,7,8,1}, {34, 1, 10, 9}};
(a) What is the position of 34?
(b) What is the result of x[2][3] + x[1][2]?
A single dimensional array contains 37 elements. Which of the following represents the index of its second-to-last element.
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)
What is the output of the Java code given below?
String color[] = {"Blue", "Red", "Violet"}; System.out.println(color[2].length());
- 6
- 5
- 3
- 2