Write a R program to find sum of elements of vector

1. Introduction

This R program calculates the sum of all elements in a vector. The vector can contain any number of numeric elements, and the program will return their total sum.

2. Program Steps

1. Create a numeric vector.

2. Calculate the sum of the elements in the vector.

3. Print the sum.

3. Code Program

# Step 1: Create a numeric vector
numeric_vector <- c(5, 10, 15, 20, 25)

# Step 2: Calculate the sum of the elements
sum_of_elements <- sum(numeric_vector)

# Step 3: Print the sum
print("Sum of elements in the vector:")
print(sum_of_elements)

Output:

Sum of elements in the vector:
[75]

Explanation:

1. c(5, 10, 15, 20, 25): Creates a numeric vector with the elements 5, 10, 15, 20, and 25.

2. sum(numeric_vector): Calculates the sum of the elements in the vector.

3. print("Sum of elements in the vector:"), print(sum_of_elements): Prints the total sum of the elements in the vector.


Comments