While Loop to Calculate the Square of numbers in R
Last Updated :
06 Sep, 2023
In R Programming Language, a while loop is used to repeatedly execute a block of code as long as a certain condition remains true. We can utilize a while loop to calculate the squares of numbers by taking an input, squaring it, and displaying the result. This concept is beneficial when you want to perform an operation multiple times based on a specific condition.
Example:
calculating squares of no ‘s from 1 to n
input no – 5
output – 1 4 9 16 25
Concepts related to the topic:
- while loop: A control structure that allows a block of code to be executed repeatedly as long as a certain condition is true.
- Mathematical operation: Calculating the square of a number involves multiplying the number by itself.
- Take input: Get the number from the user for which the square needs to be calculated.
- Calculate the square: Multiply the input number by itself to obtain the square.
- Display the result: Output the calculated square.
Example 1: Calculate the square of numbers using a while loop
R
calculateSquare <- function (num) {
i <- 1
while (i <= num) {
square <- i * i
cat (i, "squared is" , square, "\n" )
i <- i + 1
}
}
input <- 10
calculateSquare (input)
|
Output:
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
6 squared is 36
7 squared is 49
8 squared is 64
9 squared is 81
10 squared is 100
- Input: The function
calculateSquare
takes a single argument num
, which represents the number for which you want to calculate squares.
- Counter Initialization: Inside the function, you initialize a counter variable
i
with the value 1. This counter will be used to iterate through numbers from 1 up to num
.
- While Loop: The
while
loop runs as long as the counter i
is less than or equal to the input number num
.
- Calculating Square: Inside the loop:
square <- i * i
: Calculate the square of the current value of i
.
- Printing Output: After calculating the square, the code uses the
cat
function to print a message indicating the current value of i
and its square.
- Increment Counter:
i <- i + 1
increments the value of i
by 1 in each iteration, ensuring that the loop progresses to the next number.
- Loop Continuation: The loop continues to calculate and print squares for values of
i
starting from 1 up to the input number num
.
Example 2: Calculate the squares of numbers from 1 to 10 and store the results in a vector
R
calculateSquaresVector <- function (num) {
i <- 1
squares <- c ()
while (i <= num) {
square <- i * i
squares <- c (squares, square)
i <- i + 1
}
return (squares)
}
input <- 10
output <- calculateSquaresVector (input)
print (output)
|
Output:
[1] 1 4 9 16 25 36 49 64 81 100
Example 3: Calculate the square of a number until the square exceeds a certain limit
R
calculateSquareWithLimit <- function (num, limit) {
square <- num * num
while (square <= limit) {
cat (num, "squared is" , square, "\n" )
num <- num + 1
square <- num * num
}
}
input <- 1
limit <- 50
calculateSquareWithLimit (input, limit)
|
Output:
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
6 squared is 36
7 squared is 49
calculateSquareWithLimit function(num, limit).
- This line defines the function named
calculateSquareWithLimit
that takes two parameters: num
and limit
.
- Initializes a variable named
square
with the square of the input num
.
- This initiates a
while
loop that continues as long as the value of square
is less than or equal to the specified limit
.
- Prints a message using the
cat()
function that includes the current value of num
, its square (square
), and a newline.
num <- num + 1
:
- Increments the value of
num
by 1 in each iteration of the loop.
square <- num * num
:
- Recalculates the square of the updated
num
value.
Share your thoughts in the comments
Please Login to comment...