1.
public class SumOfArray {
2. public static void main(String[] args) {
3. //Initialize array
4. int [] arr = new int [] {1, 2, 3, 4, 5};
5. int sum = 0;
6. //Loop through the array to calculate sum of elements
7. for (int i = 0; i < arr.length; i++) {
8. sum = sum + arr[i];
9. }
10. System.out.println("Sum of all the elements of an array: " + sum);
11. }
12. }
1. public class SumOfArray {
2. public static void main(String[] args) {
3. //Initialize array
4. int [] arr = new int [] {1, 2, 3, 4, 5};
5. int sum = 0;
6. //Loop through the array to calculate sum of elements
7. for (int i = 0; i < arr.length; i++) {
8. sum = sum + arr[i];
9. }
10. System.out.println("Sum of all the elements of an array: " + sum);
11. }
12. }
1. mport java.util.Scanner;
2. public class AutomorphicNumberExample2
3. {
4. public static void main(String args[])
5. {
6. Scanner in = new Scanner(System.in);
7. System.out.print("Enter a number to check: ");
8. //reading a number from the user
9. int num = in.nextInt();
10. int count=0;
11. //determines the square of the given number
12. int square = num*num;
13. //copying the variable num into temp
14. int temp = num;
15. //iterate over the variable num until the condition become false
16. while(temp>0)
17. {
18. count++;
19. //removes last digit of the variable num
20. temp=temp/10;
21. }
22. //determines the last digit of the variable square
23. int lastDigit = (int) (square%(Math.pow(10, count)));
24. //compare num with last digit of the variable square
25. if(num == lastDigit)
26. System.out.println(num+ " is an automorphic number.");
27. else
28. System.out.println(num+ " is not an automorphic number.");
29. }
30. }