37.prajakta Zure (R Ass - No1)
37.prajakta Zure (R Ass - No1)
37.prajakta Zure (R Ass - No1)
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!
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!
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:-
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"
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:-
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"
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:-