R Program to find sequence,sum and mean of given numbers


November 15, 2021, Learn eTutorial
852

How to find sequence, sum, and mean? 

For creating an R program to find the 'sequence', 'sum', and 'mean' of the given numbers, we are using built-in functions seq(), mean(), and sum(). The user input is passed to these functions from the main program. seq() is an inbuilt function that will create or generate the sequence using the user input. sum() function calculates and returns the added value of the elements we passed to the function. mean() will calculate the mean of the values by using the sum and number of values by dividing the sum by the number of values.

seq(from, to)
sum(…, na.rm = FALSE)
mean(x)
 
 

How the sequence, sum, and mean of numbers are calculated in R Program

In this R program, we directly give the values to built-in functions that are 'seq()', 'mean()', and 'sum()'. These are the built-in functions in R that can do the processing and return the result to the main program. Finally, we print the return values as results. 

ALGORITHM

STEP 1: Use the R functions and pass the values into it

STEP 2: call seq() with the lower and upper limit as the arguments

STEP 3: call mean() mentioning the limit of numbers

STEP 4: call sum() with two values which are the upper and lower limits.

STEP 5: print the return value from each function

R Source Code

                                          print("Sequence of numbers from 20 to 40:")
print(seq(20,40))
print("Mean of numbers from 20 to 60:")
print(mean(20:60))
print("Sum of numbers from 51 to 91:")
print(sum(51:91))

                                      

OUTPUT

[1] "Sequence of numbers from 20 to 40:"
[1] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
[1] "Mean of numbers from 20 to 60:"
[1] 40
[1] "Sum of numbers from 51 to 91:"
[1] 2911