Write a java program to enter 10 elements in an array. Find the sum of all array elements which are divisible by 7. Also the elements along with the sum.
Answers
Answered by
0
Answer:
import java.util.Scanner;
public class ArrSum {
private int[] arr;
public void input() {
Scanner sc = new Scanner(System.in);
arr = new int[10];
System.out.println("Enter 10 no.s");
for (int i = 0; i < 10;i++) {
arr[i] = sc.nextInt();
}
}
public void display() {
int sum = 0;
System.out.println("\nElements divisible by 7 are");
for (int i: arr) {
if (i % 7 == 0) {
System.out.println(i);
sum += i;
}
}
System.out.println("\nSum = "+ sum);
}
public static void main(String[] args) {
ArrSum as = new ArrSum();
as.input();
as.display();
}
}
Similar questions
English,
1 month ago
Chemistry,
1 month ago
English,
3 months ago
India Languages,
3 months ago
Psychology,
10 months ago
English,
10 months ago
Math,
10 months ago