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
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
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]?
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 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