(C++) CPP Programs - 12

Friday, December 14, 2007

12. Write a C program to read N integers (0, +ve and –ve) into an array A and to find

a) Sum of negative numbers.

b) Sum of positive numbers.

c) Average of all numbers.

Output the various results computed with proper headings.

/* Addition and Subtraction of array elements*/

#include

#include

#include

void main()

{

int a[44],psum=0,nsum=0,n,i;

float avg;

clrscr();

printf("Enter the no. of elements\n\a");

scanf("%d",&n);

printf("Enter the elements one by one\n\a");

for(i=0;i

scanf("%d",&a[i]);

for(i=0;i

{

if(a[i]>0)

psum+=a[i];

else if(a[i]<0)

nsum+=a[i];

}

avg=(fabs)(psum+nsum)/n;

printf("Sum of positive numbers=%d\n\a",psum);

printf("Sum of negative numbers=%d\n\a",nsum);

printf("Average of all numbers=%f\n\a",avg);

getch();

}

0 comments:

Post a Comment

Chitika

About This Blog

Followers

Blog Archive

  © Blogger template The Professional Template II by Ourblogtemplates.com 2009

Back to TOP