In the previous article, we have discussed about Java Program to Find the Difference Between the Sum of Positive Numbers and Negative Numbers
In this article we will see how we can find the product between the sum of positive numbers and negative numbers by using Java programming language.
Java Program to Find the Product Between the Sum of Positive Numbers and Negative Numbers
We have to find difference between the sum of positive numbers and negative numbers.
- Positive numbers are greater than 0.
- Negative numbers are less than 0 and in front of negative number there is a minus ‘
-
‘ sign.
For example:
We have numbers such as 12, -2, 6, 8, -36, 56, -10, 20
Sum of positive numbers = 12 + 6 + 8 + 56 + 20 = 102
Sum of negative numbers = -2 + (-36) + (-10) = -48
Product between Sum of positive numbers and Sum of negative numbers = 102 * (-48) = -4896
Let’s see different ways to find sum of all even and odd digits of a number.
Method-1: Java Program to Find Product of Sum of Odd Digits and Even Digits of a Given Number By Using Static Input Value
Approach:
- Declare an integer array ‘
number
‘ and initialize the value. - Declare two integer variables
posSum
andnegSum
and initialize both to 0. - Using a for loop check each number of array.
- If number is positive then add it to
posSum
- else add it to
negSum
- Now find the product between
posSum
andnegSum
by multiplying both and print the result.
Program:
public class Main { public static void main(String args[]) { //an integr array 'number' declared and initialized int number[] = {10, 2, -43, 34, -7}; //declaring two variables posSum and negSum //and initializing both to 0 int posSum=0; int negSum=0; //for loop to iterate array for(int i=0;i<number.length;i++) { if(number[i] >= 0) { posSum=posSum+number[i]; } else { negSum=negSum+number[i]; } } System.out.println("Sum of positive numbers: "+ posSum); System.out.println("Sum of negative numbers: "+ negSum); //find product between posSum and negSum and print result System.out.println("Product between Sum of odd digits and Sum of even digits are: "+ (posSum*negSum)); } }
Output: Sum of positive numbers: 46 Sum of negative numbers: -50 Product between Sum of odd digits and Sum of even digits are: -2300
Method-2: Java Program to Find Product of Sum of Odd Digits and Even Digits of a Given Number By Using User Input Value
Approach:
- Declare an integer array ‘
number
‘ and take the array size as input. - Take array elements as input from user.
- Declare two integer variables
posSum
andnegSum
and initialize both to 0. - Using a for loop check each number of the array.
- If number is positive then add it to
posSum
- else add it to
negSum
- Now find the product between
posSum
andnegSum
by multiplying both and print the result.
Program:
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.print("Enter the number of elements in the array: "); int num = sc.nextInt(); int number[] = new int[num]; System.out.print("Enter the elements: "); for (int i = 0; i < num; i++) { number[i] = sc.nextInt(); } //declaring two variables posSum and negSum //and initializing both to 0 int posSum=0; int negSum=0; //for loop to iterate array for(int i=0;i<number.length;i++) { if(number[i] >= 0) { posSum=posSum+number[i]; } else { negSum=negSum+number[i]; } } System.out.println("Sum of positive numbers: "+ posSum); System.out.println("Sum of negative numbers: "+ negSum); //find product between posSum and negSum and print result System.out.println("Product between Sum of odd digits and Sum of even digits are: "+ (posSum*negSum)); } }
Output: Enter the number of elements in the array: 5 Enter the elements: 2 3 -4 6 -5 Sum of positive numbers: 11 Sum of negative numbers: -9 Product between Sum of odd digits and Sum of even digits are: -99
Method-3: Java Program to Find Product of Sum of Odd Digits and Even Digits of a Given Number By Using User Defined Method
Approach:
- Declare an integer array ‘
number
‘ and take the array size as input. - Take array elements as input from user.
- Call a user defined method
findProduct()
by passing the array as parameter. - Inside method, declare two integer variables
posSum
andnegSum
and initialize both to 0. - Using a for loop check each number of the array.
- If number is positive then add it to
posSum
- else add it to
negSum
- Now find the product between
posSum
andnegSum
by multiplying both and print the result.
Program:
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.print("Enter the number of elements in the array: "); int num = sc.nextInt(); int number[] = new int[num]; System.out.print("Enter the elements: "); for (int i = 0; i < num; i++) { number[i] = sc.nextInt(); } //calling user defined method findProduct() findProduct(number); } public static void findProduct(int[] number) { //declaring two variables posSum and negSum //and initializing both to 0 int posSum=0; int negSum=0; //for loop to iterate array for(int i=0;i<number.length;i++) { if(number[i] >= 0) { posSum=posSum+number[i]; } else { negSum=negSum+number[i]; } } System.out.println("Sum of positive numbers: "+ posSum); System.out.println("Sum of negative numbers: "+ negSum); //find product between posSum and negSum and print result System.out.println("Product between Sum of odd digits and Sum of even digits are: "+ (posSum*negSum)); } }
Output: Enter the number of elements in the array: 6 Enter the elements: 2 3 4 -2 -3 -4 Sum of positive numbers: 9 Sum of negative numbers: -9 Product between Sum of odd digits and Sum of even digits are: -81
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Related Java Programs: