1. Introduction
This R program finds the largest of three elements. It demonstrates basic conditional logic in R to compare values and determine the biggest one.
2. Program Steps
1. Declare three elements.
2. Compare the elements to find the largest.
3. Print the largest element.
3. Code Program
# Step 1: Declare three elements
element1 <- 15
element2 <- 21
element3 <- 10
# Step 2: Compare the elements to find the largest
largest_element <- element1 # Assume element1 is the largest initially
if (element2 > largest_element) {
largest_element <- element2
}
if (element3 > largest_element) {
largest_element <- element3
}
# Step 3: Print the largest element
print("The largest element is:")
print(largest_element)
Output:
The largest element is: [21]
Explanation:
1. element1 <- 15, element2 <- 21, element3 <- 10: Initialize three elements with values 15, 21, and 10, respectively.
2. The if statements compare element2 and element3 with the current largest_element to determine the largest among them.
3. print("The largest element is:"), print(largest_element): Print the largest element found.
Comments
Post a Comment