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 difference between the sum of positive numbers and negative numbers by using Java programming language.

Java Program to Find the Difference 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

Difference between Sum of positive numbers and Sum of negative numbers = 102 – (-48) = 102 + 48 = 150

Let’s see different ways to find product between 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 and negSum 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 difference between posSum and negSum and print the result.

Program:

public class Main 
{
   public static void main(String args[])
   {
      //an integer 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 difference between posSum and negSum and print result
      System.out.println("Difference between Sum of odd digits and Sum of even digits are: "+ (posSum-negSum));
      
   }
}
Output:

Sum of positive numbers: 46
Sum of negative numbers: -50
Difference between Sum of odd digits and Sum of even digits are: 96

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 and negSum 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 difference between posSum and negSum 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 difference between posSum and negSum and print result
      System.out.println("Difference 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: 10 10 10 10 10
Sum of positive numbers: 50
Sum of negative numbers: 0
Difference between Sum of odd digits and Sum of even digits are: 50

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 findDifference() by passing the array as parameter.
  • Inside method, declare two integer variables posSum and negSum 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 difference between posSum and negSum 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 findDifference()
        findDifference(number);
   }
   
   public static void findDifference(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 difference between posSum and negSum and print result
      System.out.println("Difference 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: 10 10 -10 -10 10
Sum of positive numbers: 30
Sum of negative numbers: -20
Difference between Sum of odd digits and Sum of even digits are: 50

Want to excel in java coding? Practice with these Java Programs examples with output and write any
kind of easy or difficult programs in the java language.

Leave a Comment