Write a C program to that allows the user to enter 'n' numbers and finds the number of non-negative numbers entered and the sum of all positive numbers entered using a for loop.
Input Format:
Input consists of n+1 integers. The first integer corresponds to n. The next n integers correspond to the numbers to be added. Consider 0 to be a positive number.
Output Format:
Refer Sample Input and Output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]
Sample Input and Output:
Enter the value of n
4
Enter the number
5
Enter the number
-2
Enter the number
-1
Enter the number
6
Number of positive numbers entered is 2 and the sum is 11
Code:
#include<stdio.h> int main() { int n,i; int a, count=0,sum=0; printf("Enter the value of n\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the number\n"); scanf("%d",&a); if(a>=0) { count++; sum=sum+a; } } printf("Number of positive numbers entered is %d and the sum is %d",count,sum); return 0; }
Sample Output:
ReplyDelete(the limit will set how many input nos.)
Enter the limit: 5
Input no. 1: 5
Input no. 2: -6
Input no. 3: -2
Input no. 4: 0
Input no. 5: 10
Sum of positive input no. is 15
Sum of negative input no. is -8
Can you do this for me? Thank you