7 Replies - 6404 Views - Last Post: 07 September 2011 - 06:06 AM Rate Topic: -----

#1 hatredforyou  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-September 11

leap year java using while and do-while loop

Posted 07 September 2011 - 04:23 AM

hi guys can u help me with my program about the leap year using whule and do while loops?

it must display the leap year from 1900 to 2100. A year is a leap year if it divisible by 4.But if the year is divisible by 100 then it is not a leap year, unless divisible by 400.
Is This A Good Question/Topic? 0
  • +

Replies To: leap year java using while and do-while loop

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: leap year java using while and do-while loop

Posted 07 September 2011 - 04:47 AM

And what have you tried, what you want to loop to. Looks like you will loop from 1900 to 2100, then for each number find if it is divisible by those numbers or not, so if leap display it, else skip it.
Look at this thread
Was This Post Helpful? 1
  • +
  • -

#3 hatredforyou  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-September 11

Re: leap year java using while and do-while loop

Posted 07 September 2011 - 04:52 AM

View Postsmohd, on 07 September 2011 - 04:47 AM, said:

And what have you tried, what you want to loop to. Looks like you will loop from 1900 to 2100, then for each number find if it is divisible by those numbers or not, so if leap display it, else skip it.
Look at this thread


will what i want in the program is that it must show the syntax of while and do-while loop.

sorry about this because this was our homework..out teacher did not even bother to teach us about it=/

so was wondering how to do it?
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: leap year java using while and do-while loop

Posted 07 September 2011 - 05:13 AM

You will have a while loop in it that will continue to loop while year is less than 2100, then in it you will have the code to check if a year is leap or not, example:
 int year = 1900;
while(year <= 2100)
//now here check if it is leap or not
// try putting the conditions pbl show in the link above
// then if it is display as a leap
// else skip it and get to the next year
year++;
}//end while


Try to try doing it and tell us where you get stack so we can continue helping. We like to see your effort rather than giving the full running code for you. Try it and you can figure out :
Was This Post Helpful? 2
  • +
  • -

#5 hatredforyou  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-September 11

Re: leap year java using while and do-while loop

Posted 07 September 2011 - 05:32 AM

View Postsmohd, on 07 September 2011 - 05:13 AM, said:

You will have a while loop in it that will continue to loop while year is less than 2100, then in it you will have the code to check if a year is leap or not, example:
 int year = 1900;
while(year <= 2100)
//now here check if it is leap or not
// try putting the conditions pbl show in the link above
// then if it is display as a leap
// else skip it and get to the next year
year++;
}//end while


Try to try doing it and tell us where you get stack so we can continue helping. We like to see your effort rather than giving the full running code for you. Try it and you can figure out :


i tried what u said..and its running..

it just loops back to 1900.
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: leap year java using while and do-while loop

Posted 07 September 2011 - 05:36 AM

Can we see what you have tried? Post your code in code tags like :code: and tell us what problem/error is happening... Also what do you you mean by loops back to 1900?
Was This Post Helpful? 1
  • +
  • -

#7 hatredforyou  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-September 11

Re: leap year java using while and do-while loop

Posted 07 September 2011 - 05:43 AM

View Postsmohd, on 07 September 2011 - 05:36 AM, said:

Can we see what you have tried? Post your code in code tags like :code: and tell us what problem/error is happening... Also what do you you mean by loops back to 1900?


lemme finish it up..i think i got it..

just 1 more question..

on the part where u said "//here check if it is leap or not"

how do u check it?i mean what do u do?the rest i think i understand what to do..
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: leap year java using while and do-while loop

Posted 07 September 2011 - 06:06 AM

You check if the year is divisible by 4 using modulus(%), if yes then check if it divisible by 100, if yes then it must be divisible by 400 also as what you said. Like:
 if (year % 4 == 0){
  if(year % 400 == 0)
   //is leap
  else
   if(year %100 == 0)
     //no leap
}
else
  // not leap 
like so
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1