WAP to accept elements in a SDA calculate sum
of only single digit nos. and sum of only double
digit nos. of the array separately
Answers
DETAILED EXPLANATION
import java.util.Scanner;
class Program {
public static void main(String[] args) {
int n = 10; // This is equal to the number of elements in your array.
int sumOfSingleDigitNum = 0;
int sumOfDoubleDigitNum = 0;
// Create an integer array with 'n' number of elements.
int array[] = new int[n];
// Create scanner object
Scanner sc = new Scanner(System.in);
// Run a loop and ask for input
for (int i = 0 ; i < n ; i++) {
System.out.print("Enter a number: ");
// Store the number in the array.
array[i] = sc.nextInt();
}
// Run a loop to iterate over each element of the array
for (int i = 0 ; i < n ; i++) {
// Check if the number is single digit
if ( (array[i] > 0) && (array[i] < 10) ) {
// Add that to the sum of single digits.
sumOfSingleDigitNum += array[i];
}
// Else check if the number is double digit
else if ( (array[i] > 10) && (array[i] < 100) ) {
sumOfDoubleDigitNum += array[i];
}
}
// Print the sum of all single and double digit numbers.
System.out.println("Sum of all single digit numbers : " + sumOfSingleDigitNum);
System.out.println("Sum of all double digit numbers : " + sumOfDoubleDigitNum);
}
}
/*
OUTPUT :-
=====================================
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: 5
Enter a number: 11
Enter a number: 12
Enter a number: 13
Enter a number: 14
Enter a number: 15
Sum of all single digit numbers : 15
Sum of all double digit numbers : 65
=====================================
*/