Problem statement:- Program to Find the sum of negative and positive numbers in an array.
Data requirement:-
Input Data:- arr
Output Data:-sum_pos, sum_neg, size
Output Data:-sum_pos, sum_neg, size
Additional Data:- i, j
Program in C
Here is the source code of the C Program to Find the sum of negative and positive numbers in an array.
Code:
#include<stdio.h>
int main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
double arr[size],sum_pos=0.0,sum_neg=0.0;
int i=0,j=0;
printf("Enter the Element of the array:\n");
while(i<size)
{
scanf("%lf",&arr[i]);
i++;
}
while(j<size)
{ if(arr[j]>0)
sum_pos+=arr[j];
else
sum_neg+=arr[j];
j++;
}
printf("sum of positive number : %0.2lf",sum_pos);
printf("\nsum of Negative number : %0.2lf",sum_neg);
}
int main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
double arr[size],sum_pos=0.0,sum_neg=0.0;
int i=0,j=0;
printf("Enter the Element of the array:\n");
while(i<size)
{
scanf("%lf",&arr[i]);
i++;
}
while(j<size)
{ if(arr[j]>0)
sum_pos+=arr[j];
else
sum_neg+=arr[j];
j++;
}
printf("sum of positive number : %0.2lf",sum_pos);
printf("\nsum of Negative number : %0.2lf",sum_neg);
}
Enter the size of the array:5
Enter the Element of the array:
-20
30
-55.555
-45
25.8
sum of positive number : 55.80
sum of Negative number : -120.56
Enter the Element of the array:
-20
30
-55.555
-45
25.8
sum of positive number : 55.80
sum of Negative number : -120.56
Program in C++
Here is the source code of the C++ Program to Find the sum of negative and positive numbers in an array.
Code:
#include<iostream>
using namespace std;
int main()
{
cout<<"Enter the size of the array:";
int size;
cin>>size;
double arr[size],sum_pos=0.0,sum_neg=0.0;
int i=0,j=0;
cout<<"Enter the Element of the array:\n";
while(i<size)
{
cin>>arr[i];
i++;
}
while(j<size)
{ if(arr[j]>0)
sum_pos+=arr[j];
else
sum_neg+=arr[j];
j++;
}
cout<<"sum of positive number : "<<sum_pos;
cout<<"\nsum of Negative number : "<<sum_neg;
}
using namespace std;
int main()
{
cout<<"Enter the size of the array:";
int size;
cin>>size;
double arr[size],sum_pos=0.0,sum_neg=0.0;
int i=0,j=0;
cout<<"Enter the Element of the array:\n";
while(i<size)
{
cin>>arr[i];
i++;
}
while(j<size)
{ if(arr[j]>0)
sum_pos+=arr[j];
else
sum_neg+=arr[j];
j++;
}
cout<<"sum of positive number : "<<sum_pos;
cout<<"\nsum of Negative number : "<<sum_neg;
}
Input/Output:
Enter the size of the array:4
Enter the Element of the array:
30
-70
88.5
-65.3
sum of positive number : 118.5
sum of Negative number : -135.3
Enter the Element of the array:
30
-70
88.5
-65.3
sum of positive number : 118.5
sum of Negative number : -135.3
Program in Java
Here is the source code of the Java Program to Find the sum of negative and positive numbers in an array.
Code:
import java.util.Scanner;
public class p5 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size;
size=sc.nextInt();
double arr[ ]=new double[size];
double sum_pos=0.0,sum_neg=0.0;
int i=0,j=0;
System.out.println("Enter the Element of the array:\n");
while(i<size)
{
arr[i]=sc.nextDouble();
i++;
}
while(j<size)
{ if(arr[j]>0)
sum_pos+=arr[j];
else
sum_neg+=arr[j];
j++;
}
System.out.println("sum of positive number : "+sum_pos);
System.out.print("\nsum of Negative number : "+sum_neg);
sc.close();
}
}
public class p5 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size;
size=sc.nextInt();
double arr[ ]=new double[size];
double sum_pos=0.0,sum_neg=0.0;
int i=0,j=0;
System.out.println("Enter the Element of the array:\n");
while(i<size)
{
arr[i]=sc.nextDouble();
i++;
}
while(j<size)
{ if(arr[j]>0)
sum_pos+=arr[j];
else
sum_neg+=arr[j];
j++;
}
System.out.println("sum of positive number : "+sum_pos);
System.out.print("\nsum of Negative number : "+sum_neg);
sc.close();
}
}
Input/Output:
Enter the size of the array:
5
Enter the Element of the array:
-30
78.5
-25
55.3
-47
sum of positive number : 133.8
sum of Negative number : -102.0
5
Enter the Element of the array:
-30
78.5
-25
55.3
-47
sum of positive number : 133.8
sum of Negative number : -102.0
Program in Python
Here is the source code of the Python Program to Find the sum of negative and positive numbers in an array.
Code:
arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = float(input())
arr.append(num)
sum_pos=0.0
sum_neg=0.0
for j in range(0,size):
if (arr[j] > 0):
sum_pos += arr[j]
else:
sum_neg += arr[j]
print("sum of positive number : ",sum_pos)
print("sum of Negative number : ",sum_neg)
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = float(input())
arr.append(num)
sum_pos=0.0
sum_neg=0.0
for j in range(0,size):
if (arr[j] > 0):
sum_pos += arr[j]
else:
sum_neg += arr[j]
print("sum of positive number : ",sum_pos)
print("sum of Negative number : ",sum_neg)
Input/Output:
Enter the size of the array: 5
Enter the Element of the array:
-30
78
54.6
25
-45.36
sum of positive number : 157.6
sum of Negative number : -75.36
Enter the Element of the array:
-30
78
54.6
25
-45.36
sum of positive number : 157.6
sum of Negative number : -75.36
0 Comments
Please do not Enter any spam link in the comment box