w3resource

R Programming Control Structure: Exercises, Practice, Solution

R Programming Control Structures [19 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

This category can cover exercises related to loops (for, while), conditional statements (if-else), and other control structures like switch-case.

1. Write a R program that creates a for loop to print numbers from 1 to 10.
Click me to see the sample solution

2. Write a R program that creates while loop to calculate the factorial of a given number.
Click me to see the sample solution

3. Write a R program function to find the sum of all even numbers between two given numbers using a for loop.
Click me to see the sample solution

4. Write a R program to print the Fibonacci series using a while loop.
Click me to see the sample solution

5. Write a R program function to check if a given number is prime using a while loop.
Click me to see the sample solution

6. Write a R program to print the multiplication table of a given number using a for loop.
Click me to see the sample solution

7. Write a R program function to calculate the nth term of the Fibonacci series using recursion.
Click me to see the sample solution

8. Write a R program to find the factorial of a given number using recursion.
Click me to see the sample solution

9. Write a R program function to compute the sum of cubes of numbers from 1 to n using a for loop.
Click me to see the sample solution

10. Write a R program to check if a given number is Armstrong number using a while loop.
Click me to see the sample solution

11. Write a R program function to find the GCD (Greatest Common Divisor) of two numbers using recursion.
Click me to see the sample solution

12. Write a R program to generate the Pascal's triangle up to a given number of rows using nested for loops.
Click me to see the sample solution

13. Write a R program function to calculate the factorial of a given number using both recursion and iteration (for loop).
Click me to see the sample solution

14. Write a R program to find the sum of all elements in an array using a for loop.
Click me to see the sample solution

15. Write a R program function to check if a given number is a perfect number using a while loop.
From Wikipedia -
In number theory, a perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For instance, 6 has divisors 1, 2 and 3 (excluding itself), and 1 + 2 + 3 = 6, so 6 is a perfect number. The next perfect number is 28, since 1 + 2 + 4 + 7 + 14 = 28.
Click me to see the sample solution

16. Write a R program to find the reverse of a given number using a while loop.
Click me to see the sample solution

17. Write a R program function to calculate the power of a number using recursion.
Click me to see the sample solution

18. Write a R program to print the ASCII value of all lowercase letters using a for loop.
Click me to see the sample solution

19. Write a R program function to check if a given number is a palindrome using recursion.
Click me to see the sample solution

R Programming Code Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.