Program to Sum Positive and Negative Elements in Array
- Write a program to Sum Positive and Negative Elements in Array in C
- Write a program to Sum Positive and Negative Elements in Array in C++
- Write a program to Sum Positive and Negative Elements in Array in Python
- Write a program to Sum Positive and Negative Elements in Array in PHP
- Write a program to Sum Positive and Negative Elements in Array in Java
- Write a program to Sum Positive and Negative Elements in Array in Java Script
- Write a program to Sum Positive and Negative Elements in Array in C#
Explanation:
The following reasoning can be used to determine the sum of the array’s positive and negative elements:
Logic:
- Initialize Variables:
- positive_sum = 0
- negative_sum = 0
- Iterate Through the Array:
- For each element:
- If the element is greater than 0, add it to positive_sum = 0.
- If the element is less than 0, add it to negative_sum = 0.
- For each element:
- Output:
- After processing all elements, return or print the values of positive_sum = 0 and negative_sum = 0.
Program to Sum Positive and Negative Elements in Array
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> int main() { int arr[] = {10, -5, 3, -20, 8, -1}; // Example array int size = sizeof(arr) / sizeof(arr[0]); int positive_sum = 0, negative_sum = 0; for (int i = 0; i < size; i++) { if (arr[i] > 0) { positive_sum += arr[i]; } else if (arr[i] < 0) { negative_sum += arr[i]; } } printf("Sum of Positive Elements: %d\n", positive_sum); printf("Sum of Negative Elements: %d\n", negative_sum); return 0; }
#include <iostream> using namespace std; int main() { int arr[] = {10, -5, 3, -20, 8, -1}; // Example array int size = sizeof(arr) / sizeof(arr[0]); int positive_sum = 0, negative_sum = 0; for (int i = 0; i < size; i++) { if (arr[i] > 0) { positive_sum += arr[i]; } else if (arr[i] < 0) { negative_sum += arr[i]; } } cout << "Sum of Positive Elements: " << positive_sum << endl; cout << "Sum of Negative Elements: " << negative_sum << endl; return 0; }
arr = [10, -5, 3, -20, 8, -1] # Example array positive_sum = sum(x for x in arr if x > 0) negative_sum = sum(x for x in arr if x < 0) print(f"Sum of Positive Elements: {positive_sum}") print(f"Sum of Negative Elements: {negative_sum}")
<?php $arr = array(10, -5, 3, -20, 8, -1); // Example array $positive_sum = 0; $negative_sum = 0; foreach ($arr as $num) { if ($num > 0) { $positive_sum += $num; } elseif ($num < 0) { $negative_sum += $num; } } echo "Sum of Positive Elements: $positive_sum\n"; echo "Sum of Negative Elements: $negative_sum\n"; ?>
public class Main { public static void main(String[] args) { int[] arr = {10, -5, 3, -20, 8, -1}; // Example array int positiveSum = 0, negativeSum = 0; for (int num : arr) { if (num > 0) { positiveSum += num; } else if (num < 0) { negativeSum += num; } } System.out.println("Sum of Positive Elements: " + positiveSum); System.out.println("Sum of Negative Elements: " + negativeSum); } }
const arr = [10, -5, 3, -20, 8, -1]; // Example array let positiveSum = 0, negativeSum = 0; arr.forEach(num => { if (num > 0) { positiveSum += num; } else if (num < 0) { negativeSum += num; } }); console.log(`Sum of Positive Elements: ${positiveSum}`); console.log(`Sum of Negative Elements: ${negativeSum}`);
using System; class Program { static void Main() { int[] arr = {10, -5, 3, -20, 8, -1}; // Example array int positiveSum = 0, negativeSum = 0; foreach (int num in arr) { if (num > 0) { positiveSum += num; } else if (num < 0) { negativeSum += num; } } Console.WriteLine($"Sum of Positive Elements: {positiveSum}"); Console.WriteLine($"Sum of Negative Elements: {negativeSum}"); } }