HRS - Ask. Learn. Share Knowledge. Logo

In Computers and Technology / High School | 2025-07-08

The following program uses function FindSum to calculate the sum of positive numbers in a list of n numbers entered by a user. #include int FindSum(int n); int main() { int n, sum = 0; printf("How many numbers? "); scanf("%d", &n); sum = FindSum(n); printf("\nThe sum is %d\n", sum); } The examples of input and output of the program are shown below. Input: How many numbers? 6 3 7 -5 4 10 3 Output: The sum is 24 Write the code segment for function FindSum() to complete the above program.

Asked by averk1041

Answer (1)

To complete the program, you need to define the FindSum function that calculates the sum of positive numbers that a user inputs. Here is how you can do it:
#include <stdio.h>
int FindSum(int n) { int i, number, sum = 0; // Loop through each number the user inputs for(i = 0; i < n; i++) { printf("Enter number %d: ", i+1); scanf("%d", &number); // Add to sum if the number is positive if(number > 0) { sum += number; } } return sum; }
How it Works

Prompt for Numbers: The function FindSum takes the integer n as an argument, indicating how many numbers the user will enter.

Input Loop: We loop n times, each time prompting the user to enter a number.

Check for Positivity: Inside the loop, after reading each number, we check if it is positive.

Calculate Sum: If a number is positive, we add it to the sum. If it's not positive, we ignore it.

Return the Sum: After all numbers are processed, the function returns the calculated sum of all positive numbers entered by the user.


This code will work to read the given sequence of numbers input by the user and will compute the sum of only the positive ones, demonstrating basic skills in using conditionals and loops in C programming.

Answered by SophiaElizab | 2025-07-21