**Sum of negative and positive numbers** is to add positive and negative number.

In this tutorial we are going to see one new program which is based on the array concept in c.

First we will see what is positive and what is negative number

**Positive number **

A number which is greater than 0 is the positive number(integer).

Numbers 0, 1, 2,……. are positive numbers.

**Negative number **

A number which is less than 0 is the negative number (integer).

Numbers -1, -2, -3 …. are negative numbers.

**Logic to find sum of positive and negative numbers**

First we take a numbers input from user in array. Next week check numbers from array one by one if it is positive number we add with positive number. If number is negative we add with negative number. At end print sum of positive and negative number.

**Concept you need to know from c programming**

1. Basic input and output in c

2. Array in c

3. Conditions and looping in c

4. Arithmetic operators in c

**Algorithm to find sum of positive and negative numbers**

1. Start

2. Declare variables

3. Take a numbers from user(positive and negative)

4. Read array from start to end

5. If number in array is positive then add to positive number

6. If number in array is negative then add to negative number

7. After array ends

8. Print sum of positive and negative number

9. End

**C program to find sum of positive and negative numbers**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include<stdio.h> main() { int i,size,pos=0,neg=0; float avg; printf("Enter the size of array\n"); scanf("%d",&size); int array[size]; printf("Enter numbers to find sum of positive and negative numbers\n"); for(i=0;i<size;i++) { scanf("%d",&array[i]); } for(i=0;i<size;i++) { if(array[i]>0) pos+=array[i]; if(array[i]<0) neg+=array[i]; } printf("The sum of positive inegers is %d\n",pos); printf("The sum of negative integers is %d\n",neg); } |

**Output :**

**Explanation :-**

**1. First we preprocessor directives in programs.**

**2. Write main function from where the execution of program begins.**

**3. Declare variable**

size => to store N number

i => temporary variable

pos => to add positive numbers

neg => to add negative numbers

array => array to store N numbers

**4. Take input size that is total numbers user want to enter**

printf(“Enter the size of array\n”);

scanf(“%d”,&size);

=> size = 5

**5. Now assign this size to array to take 5 input from user**

int array[size];

**6. Take a input number from user(positive and negative numbers )**

printf(“Enter numbers to find sum of positive and negative numbers\n”);

for(i=0;i<size;i++) => to take 5 input from user

scanf(“%d”,&array[i]) => to take input use scanf

=> 10, -20,-10,20,10

**7. Now add the numbers**

**8. Traverse the array from start to end that is size**

for(i=0;i<size;i++) => to Traverse start to 5

**At iteration 1 :**

i = 0, 0 < 5 this condition becomes true so for loop executed

if(array[i]>0) => 10 > 0 this condition becomes true if block executed

pos+=array[i] => pos = 0 + 10 = 10

if(array[i]<0) => 10 >0 this condition becomes false so if block will not executed

**At iteration 2 :**

i = 1, 1 < 5 this condition becomes true so for loop executed

if(array[i]>0) => -20 > 0 this condition becomes false and this if block skipped

if(array[i]<0) => -20 < 0 this condition becomes true and if block executed

neg+=array[i] => neg = 0+-20 = -20

**At iteration 3 :**

i = 2, 2 < 5 this condition becomes true so for loop executed

if(array[i]>0) => -10 > 0 this condition becomes false and this if block skipped

if(array[i]<0) => -10 < 0 this condition becomes true and if block executed

neg+=array[i] => neg = -20 +-10 = -30

**At iteration 4 :**

i = 3, 3 < 5 this condition becomes true so for loop executed

if(array[i]>0) => 20 > 0 this condition becomes true if block executed

pos+=array[i] => pos = 10 + 20 = 30

if(array[i]<0) => 20 >0 this condition becomes false so if block will not executed

**At iteration 5 :**

i = 4, 4 < 5 this condition becomes true so for loop executed

if(array[i]>0) => 10 > 0 this condition becomes true if block executed

pos+=array[i] => pos = 30 + 10 = 40

if(array[i]<0) => 10 >0 this condition becomes false so if block will not executed

**At iteration 6 :**

i = 5, 5 < 5 this condition becomes false and for loop execution stops

**9. Print the sum of positive and negative numbers**

printf(“The sum of positive integers is %d\n”,pos);

printf(“The sum of negative integers is %d\n”,neg);

=> The sum of positive integers is 40

=> The sum of negative integers is -30

## Leave a Reply