Click here to Skip to main content
14,696,499 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
#include <stdio.h>
 
int main()
{
  int year;
 
  printf("Enter a year to check if it is a leap year\n");
  scanf("%d", &year);
 
  if ( year%400 = 0)
    printf("%d is a leap year.\n", year);
  else if ( year%100 = 0)
    printf("%d is not a leap year.\n", year);
  else if ( year%4 = 0 )
    printf("%d is a leap year.\n", year);
  else
    printf("%d is not a leap year.\n", year);  
 
  return 0;
}


What I have tried:

I am not sure what is the wrong in this code?
Posted
Updated 2-Jun-20 6:36am
v3
Comments
phil.o 2-Jun-20 9:10am
   
Please use the green "Improve Question" button and qualify the "What I have tried" part with what you actually tried. No one will do your homework for you.
phil.o 2-Jun-20 11:05am
   
Problem is, the code that you are showing is not relative in any way to your original question.
Rick York 2-Jun-20 11:49am
   
You would be better off having a function to call and pass it a year and it returns a boolean value of leap year or not.

The equal-comparison operator in C-language family is ==, not =.
if (year % 400 == 0) // etc. for other comparisons
   
v2
Comments
Patrice T 2-Jun-20 10:39am
   
+5 strait to the point.
phil.o 2-Jun-20 11:05am
   
Thanks :)
You have two bugs in your code which you must figure out. The first is the missing "=" for the comparison. A typical newbie bugs - but this should also tell you the compiler. Learn to read it.

The second is more complicated because of some edge cases to be read in the wikipedia - leap year calculation.
   
The point of homework is to do some work.

It's not the solution that's important. It is learning to get to the solution. You may need to learn things you have not yet experience, or didn't quite understand from a lecture or book. Doing them makes a big difference. Learning from the errors along the way is really a big deal and a very good thing.

As for your post - all you've done is give us your homework question - not shown us the least bit of work on your part. Told us where you are stuck.

Being a programmer, at least if you want to be any good, means digging into problems until you can solve them. Sometimes very hard work. Be sure you really want to "Think for a Living".
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900