37.prajakta Zure (R Ass - No1)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Lab Assignment No.

1] Accept a number from user and display whether it is odd or even.


Code :-
i <-readline(prompt="Enter any number: ")
x <-as.integer(i)
if ((x%%2 )==0){
print(paste("The no ",i,"is Even"))
}else{
print(paste("The no ",i,"is Odd"))
}
Output:-
Enter any number: 45
[1] "The no 45 is Odd"

2] Accept radius from user and display its area and perimeter.
Code :-
radius <- as.numeric(readline("Enter the radius of the circle: "))
area <- pi * radius^2
perimeter <- 2 * pi * radius
cat("The area of the circle is", area, "\n")
cat("The perimeter of the circle is", perimeter, "\n")
Output:-
Enter the radius of the circle: 15
The area of the circle is 706.8583
The perimeter of the circle is 94.24778

3] Accept weight, height and gender from user and display his/her BMI
with a proper message.
Code :-
weight <- as.numeric(readline("Enter your weight in kilograms: "))
height <- as.numeric(readline("Enter your height in meters: "))
gender <- readline("Enter your gender (Male/Female): ")
bmi <- weight / (height^2)
if (gender == "Male") {
cat("Hey buddy! Your BMI is", bmi, ". Keep up the good work!")
} else if (gender == "Female") {
cat("Hey friend! Your BMI is", bmi, ". You're doing great!")
} else {
cat("Oops! I'm sorry, I couldn't understand your gender. Please try
again.")
}
Output:-
Enter your weight in kilograms: 55
Enter your height in meters: 1.5
Enter your gender (Male/Female): Female
Hey friend! Your BMI is 24.44444 . You're doing great!

4] Accept any number from user and display its factorial.


Code :-
x<-as.numeric(readline("Enter no to find factorial: "))
fact<-1
for(i in 2:x)
{
fact<-fact*i
}
print(paste(x,"!=",fact))
Output:-
Enter no to find factorial: 4
[1] "4 != 24"

5] Accept any number from user and display whether it is positive, negative
or zero.
Code :-
number <- as.numeric(readline("Enter a number: "))
if (number > 0) {
cat("The number", number, "is positive!")
} else if (number < 0) {
cat("The number", number, "is negative!")
} else {
cat("The number is zero!")
}
Output:-
Enter a number: 45
The number 45 is positive!

6] Accept any number from user and display it is prime or not.


Code :-
x<-as.numeric(readline("Enter no to check prime or not: "))
for(i in 3:x-1)
{
if(x%%i==0){
print(paste(x," is not prime"))
break
}
}
if(i==x-1){
print(paste(x," is prime"))
}

Output:-
Enter no to check prime or not: 56
[1] "56 is not prime"

7] Accept any number from user and display its reverse number.
Code :-

Output:-

8] Display Fibonacci series up to given number of terms.


Code :-
n <-10
fib <-c(0,1)
for(i in 3:n){
fib[i] <-fib[i-1]+fib[i-2]
}
print(fib)
Output:-
0 1 1 2 3 5 8 13 21 34

9] Accept any number and display it is palindrome or not.


Code :-

Output:-

10] Accept a digit and display its spelling (e.g. Input: 8 Output: Eight).
Code :-
n<-as.numeric(readline("Enter any digit: "))
if(n==0){
print("zero")
}else if (n==1){
print("one")
}else if (n==2){
print("two")
}else if (n==3){
print("three")
}else if (n==4){
print("four")
}else if (n==5){
print("five")
}else if (n==6){
print("six")
}else if (n==7){
print("seven")
}else if (n==8){
print("eight")
}else if (n==9){
print("nine")
}else {
print("Invalid input")
}
Output:-
Enter any digit: 3
"three"

11] Display 1 to 10.


Code :-
for (i in 1:10){
print(i)
}
Output:-

12] Display 10 to 1.
Code :-
for (i in 10:1){
print(i)
}
Output:-
13] Display only even numbers between 1 to 30.
Code :-
for (i in 1:30){
if(i%%2==0){
print(i)
}
}
Output:-

14] Display table of given number.


Code :-
n<-as.numeric(readline("Enter number to print table values: "))
for(i in 1:10){
print(i*n)
}
Output:-
15] Display 1 to 10 table values.
Code :-
for(i in 1:10){
line<-"\n"
for(j in 1:10){
line<-paste(line,"\t",i*j)
}
cat(line)
}
Output:-

16] Accept principle amount, interest rate and number of years from user
and calculate simple interest.

18] Accept year from user and check whether it is leap year or not.
Code :-
year<-as.numeric(readline(prompt="Enter a year:"))
if((year%%4)==0){
paste(year,"is a Leap Year")
}else{
paste(year,"is not Leap Year")
}
Output:-
Enter a year:2027
"2027 is not Leap Year"

19] Display following pattern for given number of lines.


Code A] :-
for (i in 1:4){
line<-"\n"
for(j in 1:i){
line<-paste(line,"* ")
}
cat("\n",line)
}
Output:-

Code B] :-
for (i in 4:1){
line<-"\n"
for(j in 1:i){
line<-paste(line,"* ")
}
cat("\n",line)
}
Output:-
Code D] :-
for (i in 1:4){
line<-"\n"
for(j in 1:i){
line<-paste(line,j)
}
cat(line)
}
Output:-

You might also like