Write a R program to multiply two vectors of integers type and length 3

1. Introduction

This R program demonstrates how to multiply two integer vectors of length 3. Each element of the first vector is multiplied with the corresponding element in the second vector.

2. Program Steps

1. Create two integer vectors of length 3.

2. Multiply the corresponding elements of these vectors.

3. Display the result of the multiplication.

3. Code Program

# Step 1: Create two integer vectors
vector1 <- c(1, 2, 3)  # First vector
vector2 <- c(4, 5, 6)  # Second vector

# Step 2: Multiply the vectors element-wise
result <- vector1 * vector2

# Step 3: Display the result
print("Result of multiplying the two vectors:")
print(result)

Output:

Result of multiplying the two vectors:
[4 10 18]

Explanation:

1. vector1 <- c(1, 2, 3): Defines the first integer vector with elements 1, 2, and 3.

2. vector2 <- c(4, 5, 6): Defines the second integer vector with elements 4, 5, and 6.

3. result <- vector1 * vector2: Multiplies the two vectors element-wise. The multiplication is done between corresponding elements: 1*4, 2*5, and 3*6.

4. print("Result of multiplying the two vectors:"): Prints a message indicating the result of the multiplication.

5. print(result): Displays the resulting vector after multiplication, which contains the products of corresponding elements.


Comments