Posts

Arrays missing element program

Image
package Practice; public class ArrayMissingElement { public static void main(String[] args) { int arraySum = 0, totalSum = 0, element; int a[] = { 1, 2, 3, 4, 5, 8, 9, 6 }; int max = a[0], min = a[0]; for (int i = 0; i < a.length; i++) { if (a[i] < min) { min = a[i]; } else if (a[i] > max) { max = a[i]; } } System.out.println("max value=" + max); System.out.println("min value=" + min); for (int i = 0; i < a.length; i++) { arraySum = arraySum + a[i]; } System.out.println("array sum=" + arraySum); for (int i = min; i <= max; i++) { totalSum = totalSum + i;// i is initialize to min } System.out.println("total max to min sum=" + totalSum); element = totalSum - arraySum; System.out.println("Missing Element is=" + element); } } Output:

Program to check Amicable Number in java

Image
/* Amicable numbers  are two different numbers so related that the  sum of the proper divisors  of each is equal to the other number. (A proper divisor of a number is a positive factor of that number other than the number itself. */ import java.util.*; public class AmicableNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter value for first number"); int a = sc.nextInt(); System.out.println("Enter value for second number: "); int b = sc.nextInt(); int sum1 = 0, sum2 = 0; System.out.println("Factors of " + a + " are="); for (int i = 1; i < a; i++) { if (a % i == 0) { System.out.print(i + " "); sum1 = sum1 + i; } } System.out.println("  sum of factor a==" + sum1); System.out.println(); System.out.println("Factors of " + b + " are="); for (int i = 1; i < b; i++) { if (b % i == ...