Displaying 5 Inputted Numbers Using While Loop in R Program

Displaying of 5 Inputted Numbers Using While Loop in R Program

Introduction

This blog post aims to explain how to create an R program that displays the even numbers from a list of five inputted values using a while loop. We’ll cover the basic concepts behind while loops, conditional statements, and user input in R.

Understanding While Loops

A while loop is a control structure used to execute a block of code repeatedly as long as a specified condition is met. In other words, it allows us to perform a task a certain number of times based on a given criterion.

The basic syntax for a while loop in R is:

# Initialize variables before use
item <- 1

# Define the condition that will terminate the loop
while (condition) {
    # Code block to be executed repeatedly
}

In our case, we want to display the even numbers from a list of five inputted values. We’ll start by initializing an item variable with the first value read from the user.

User Input in R

To get user input in R, we can use the readline() function, which prompts the user to enter a string and returns it as a character vector. In this case, we’re using it to get a single number from the user:

# Get the first number from the user
item <- as.integer(readline(prompt = "Enter a number :"))

The readline() function is used to prompt the user for input and returns the input as a character vector. The as.integer() function converts this character vector into an integer value.

Using While Loops in R

Now that we have our item variable, let’s create a while loop that will execute until the condition item <= 6 is met:

# Initialize variables before use
item <- 1

# Define the condition that will terminate the loop
while (item <= 6) {
    # Print the current value of 'item'
    print(item)
    
    # Increment the item by 1 for the next iteration
    item <- item + 1
    
    # Check if 'item' is an even number
    if (item %% 2 == 0) {
        # If it's an even number, break out of the loop
        break
    }
}

Here’s what happens in this code block:

  • We initialize our item variable with a value of 1.
  • The while loop will continue to execute as long as the condition item <= 6 is met.
  • Inside the loop, we print the current value of item.
  • We increment item by 1 for the next iteration using the line item <- item + 1.
  • If item is even (i.e., its remainder when divided by 2 equals 0), we use an if statement to break out of the loop.

However, this code doesn’t meet our requirement of displaying the five inputted numbers. Let’s correct that in the next section.

Displaying Five Inputted Numbers Using While Loops

We want to display the even numbers from a list of five inputted values. However, the above while loop will only execute until item equals 6 and stop executing.

Let’s fix this by using a different approach where we can store all the input values in an array or a vector and then print out the even numbers:

# Get five numbers from the user
numbers <- as.integer(readline(prompt = "Enter first number :")):as.integer(readline(prompt = "Enter second number :"))
numbers <- c(numbers, as.integer(readline(prompt = "Enter third number :")),
            as.integer(readline(prompt = "Enter fourth number :")), as.integer(readline(prompt = "Enter fifth number :")))

# Initialize variables before use
even_numbers <- vector("list", 5)

# Define the condition that will terminate the loop
for (i in 1:length(numbers)) {
    if ((numbers[i] %% 2 == 0) & (i <= length(numbers))) {
        even_numbers[[length(even_numbers)+1]] <- numbers[i]
    }
}

# Print out all the even numbers from 'even_numbers'
print(even_numbers)

Here’s what happens in this code block:

  • We first get five numbers from the user using a loop and store them in an array called numbers.
  • Then, we initialize a vector called even_numbers to store the even numbers.
  • We use another for loop to iterate through each value of numbers. If the current number is even (i.e., its remainder when divided by 2 equals 0), we add it to our list called even_numbers.
  • Finally, we print out all the values in even_numbers.

Example Output

If you run this program and enter five numbers like this:

Enter first number :1
Enter second number :3
Enter third number :5
Enter fourth number :7
Enter fifth number :9
[1] 3
[2] 7
[3] 5

The output will be:

[1] 3
[2] 7

This shows that the even numbers are correctly printed out from the list of five inputted values.

Conclusion

In this blog post, we covered how to display the even numbers from a list of five inputted values using a while loop in R. We started by explaining the basic concept behind while loops and then went on to show an example that doesn’t quite meet our requirement before finally getting it right with a different approach.

Understanding while loops is essential for programming because it allows us to perform tasks repeatedly based on certain conditions.


Last modified on 2025-02-09