Home > Net >  i want to test if different years is a leap year or not. I want to be able to type in several years
i want to test if different years is a leap year or not. I want to be able to type in several years

Time:10-02

leap_year<-function(years) {

if(years>=400 && years%%100==0 && years%%400==0                        
| years%%100!=0 && years%%4==0) {

print(TRUE) 

}  else  {

print(FALSE) }

}

CodePudding user response:

The lubrdiate library has the leap_year function, don't reinvent the wheel.

Try:

library(lubridate)    

leap_year<-function(years) {
print(all(leap_year(years)))

CodePudding user response:

you can easiyl check if a year is a leap year with year%4 if a year is dividable with 4 and int is the output its a leap year

i dont't know which language you use i would realize it like this:

if(year%4 == 0){
    print(year)
}else{
   print("Year is not a leap year)
}

to go through all inputs i would safe them in a array and then go through every element end execute the if this would look like this:

for(int i = 0; i < years.length; i  ){
    if(year%4 == 0){
        print(year)
    }else{
        print("Year is not a leap year)
    }
}

hope i could help

  •  Tags:  
  • r
  • Related