(HackerRank) Write a Modular C Programming code to solve Stop at a point, Compute the sum of all positive values and print the sum and the number of values added.
Given a set of n integers containing both positive and negative values,
Compute the sum of all positive values and print the sum and the number of values added.
The program should terminate when the sum exceeds 999.
Input Format
A number n specify total numbers.
Next input n different numbers.
Constraints
n is positive number.
numbers can be positive or negative.
program should terminate when the sum exceeds 999.
Output Format
Sum of numbers.
Count of how many numbers were added.
Sample Input 0
5
-10
-20
4
5
6
Sample Output 0
Sum is 15.
Numbers added 3.
Sample Input 1
5
123
454
678
Sample Output 1
Sum is 1255.
Numbers added 3.
Sample Input 2
-9
Sample Output 2
Invalid input.
Refer : C Programming HackerRank all solutions for Loops | Arrays | strings
CODE:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> void read(int a[30],int n) { int i; for(i=0;i<n;i++) { scanf("%d",&a[i]); } } void sum(int a[30],int n) { int i,sum=0,count=0; for(i=0;i<n;i++) { if(a[i]>0) { sum=sum+a[i]; count++; } if(sum>999) { break; } } printf("Sum is %d.\n",sum); printf("Numbers added %d.\n",count); } int main() { int a[30],n; scanf("%d",&n); if(n>0) { read(a,n); sum(a,n); } else printf("Invalid input.\n"); return 0; }
OUTPUT
Congratulations, you passed the sample test case. Click the Submit Code button to run your code against all the test cases. Input (stdin) 5 -10 -20 4 5 6 Your Output (stdout) Sum is 15. Numbers added 3. Expected Output Sum is 15. Numbers added 3. Interview
Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, MISC, Solved model question papers & Hacker Rank all solutions on the below page:
Top 100+ C Programming codes – KLE Technological University