-2

So far I've come up with this however it doesn't seem to output results consistently.

if (year/4*4==year)
{
cout<<"It is a leap year!";
}
else
{
cout<<"It is not a leap year!";
}

How can i solve this?

  • I don't know turbo-C/C++ but if standard math rules still hold won't that always be true? | lets say year is 1 if (1 / 4) * 4 equals 1 or if 0.25 * 4 equals 1 – Andrew Bone Apr 28 '16 at 14:17
  • leap year has some basic concept, 1--- it should be divisible by 4 or 400, but if its divisible by 100, then it must be divisible by 400......code according to this concept – piyushj Apr 28 '16 at 14:18
  • @AndrewBone is there a formula on how i can check if a number is divisible by 4? – aspiring programmer Apr 28 '16 at 14:21
  • @aspiringprogrammer as I say I'm just going by guess work but something like floor(year/4)*4==year might do it, I'm not sure if floor exists in C though. Worth a shot I guess. – Andrew Bone Apr 28 '16 at 14:27
  • @AndrewBone what is floor ? – aspiring programmer Apr 28 '16 at 14:30
  • @aspiringprogrammer round down to the closest integer so back to our example from before | floor(1 / 4)*4 | floor(0.25)*4 | 0*4 | 0 != 1| – Andrew Bone Apr 28 '16 at 14:33
  • 1
    @aspiringprogrammer `if ( year % 4 == 0 )` to check if year (an integer, I guess) is divisible by 4 – Bob__ Apr 28 '16 at 14:34
  • @AndrewBone in C, if both operands of a division are integer, an integer division is performed. So 1/4 gives 0. 1%4 gives the reminder: 1 – Bob__ Apr 28 '16 at 14:39
  • it still outputs incorrectly. – aspiring programmer Apr 28 '16 at 14:47
  • Can you please show us Your inputs and outputs? – Bob__ Apr 28 '16 at 14:50
  • if ( floor(year/4)*year==year) still gives the same output as if(year/4*4==year) like when i put 1991, it says that it is a leap year when it is not. – aspiring programmer Apr 28 '16 at 14:52
  • How exactly do you declare `year`? [Here](https://ideone.com/J0cjJ9) you can see that if year is an int your code should work, at least the divisible by 4 part, finding a leap year is [a bit more complicated](https://ideone.com/JUUo62). – Bob__ Apr 28 '16 at 16:00
  • yup i declared it as int still does not work. – aspiring programmer Apr 28 '16 at 16:08
  • @Bob__ i tried this https://ideone.com/J0cjJ9 and it works! now the problem is how do i modify this for loop ? so that the output will depend on the users input? – aspiring programmer Apr 28 '16 at 16:11
  • How do you do that in your code? That's probably where the error come from. Are you checking the user input? – Bob__ Apr 28 '16 at 16:14
  • @Bob__ int year; cin>>year; now what do i do with the for loop part? – aspiring programmer Apr 28 '16 at 16:16
  • like [here](https://ideone.com/wkHDNM)... – Bob__ Apr 28 '16 at 16:17
  • @Bob__ i see thank you so much! how about if the user just wants to input one particular year? – aspiring programmer Apr 28 '16 at 16:25
  • int year; cin>>year; instead of the loop... Does it works or not with your compiler? Have you tryed to compile the snippets of the links in your environment? – Bob__ Apr 28 '16 at 16:37
  • yes int year;cin year; does work in my compiler and everything you've sent has worked so far. My question is how to modify that for loop ? So when a user enter "1992" it'll just output "It is a leap year?". Thank you for all your help so far, i am truly grateful. – aspiring programmer Apr 28 '16 at 16:44

1 Answers1

0

If you want to check if some years entered by the user are leap years or not, you just have to write a loop.

In my code I also added an helper function (Turbo C++ has bool type, right? It's part of the standard...) with the exact formula (you can find that online). Note that the loop ends when the user inputs something that is not an integer (or EOF).

#include <iostream>

bool is_leap( int y )
{
    return  y % 400 == 0  ||  ( y % 100 != 0  &&  y % 4 == 0 );
}

int main()
{
    int year;

    while ( std::cin >> year )
    {
        if ( is_leap(year) )
        {
            std::cout << year << " is a leap year!\n";
        }
        else
        {
            std::cout << year << " is not a leap year.\n";
        }
    }

    return 0;
}

For example, you can try:

1857 is not a leap year.
1900 is not a leap year.
1980 is a leap year!
1991 is not a leap year.
2000 is a leap year!
2016 is a leap year!
Bob__
  • 12,361
  • 3
  • 28
  • 42