TechTorch

Location:HOME > Technology > content

Technology

How to Write a Program to Sum Prime Numbers from an Input Array

March 05, 2025Technology3825
How to Write a Program to Sum Prime Numbers from an Input Array In pro

How to Write a Program to Sum Prime Numbers from an Input Array

In programming, understanding how to manipulate arrays and perform calculations is a fundamental skill. In this article, we will walk through the process of creating a C program that takes an array of 10 numbers as input, identifies which of these numbers are prime, and then sums those prime numbers. If no prime numbers are found, the program will output a specific message.

Understanding Prime Numbers and Array Manipulation

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, 11 are all prime numbers. Array manipulation involves creating, accessing, and modifying elements of an array. An array is a data structure used to store a collection of elements, which can be of the same or different types, in a single variable.

Step-by-Step Guide to Writing the C Program

To solve the problem, we need to follow a series of steps. Here's how you can write the C program:

Step 1: Include Necessary Header Files

The first step is to include the necessary header files. For this program, we will need to include stdio.h which is the standard input and output header file in C.

include stdio.h

Step 2: Define the Main Function

The main function is the entry point of any C program. Here, we will define an integer array of 10 elements, a variable to store the sum of prime numbers, and a flag variable to track prime status.

int main() {    int n, i  0, sum  0, flag  0, j  0;    int arr[10];}

Step 3: Get Input from the User

The next step is to prompt the user to input 10 numbers and store them in the array. This can be done using the printf and scanf functions.

printf(Enter 10 integers:
);for (i  0; i  10; i  ) {    scanf(%d, arr[i]);}

Step 4: Check Each Number for Primality and Sum Prime Numbers

For each number in the array, we will use a nested loop to check if it is a prime number. If it is, we will add it to the sum.

for (i  0; i  10; i  ) {    flag  0;    for (j  2; j  arr[i] / 2; j  ) {        if (arr[i] % j  0) {            flag  1;            break;        }    }    if (flag  0) {        sum   arr[i];    }}

Step 5: Output the Result

Finally, we will use the printf function to display the sum of the prime numbers. If no prime numbers are found, we will display a specific message indicating that.

if (sum ! 0) {    printf(Sum of prime numbers in the array: %d
, sum);} else {    printf(No prime numbers found in the array.
);}

Conclusion

By following these steps, you can create a C program that takes an array of 10 numbers, identifies the prime numbers, and sums them. This program not only helps in understanding the basics of array manipulation and prime number checking but also demonstrates the importance of loops and conditional statements in programming.