473,386 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

is it a leap year in c++

8
here's what i have. using microsoft studio

Expand|Select|Wrap|Line Numbers
  1. #define _CRT_SECURE_NO_DEPRECATE
  2. #include <stdio.h>
  3.  
  4. int main( void )
  5. {
  6.     int year, b, c, e;  
  7.     b = 4;
  8.     c = 100;
  9.     e = 400;
  10.  
  11.     /* prompt the user for a year to check */
  12.     printf("Enter a year:  ");
  13.     scanf("%d", &year); 
  14.     /* put your code below here */
  15.  
  16.     if ((year%b) != 0)                                                                                                       /*dividing the year by 4 and checking    remainder*/
  17.  
  18. printf("This year is not a leap year\n");
  19.  
  20. if ((year%b) == 0)    /*dividing the year by 100 and checking remainder*/
  21.  
  22. if ((year%e) != 0)    /*dividing the year by 400 and checking remainder*/
  23.  
  24. printf("This year is not a leap year\n");
  25.  
  26. if ((year%b) == 0)    /*dividing the year by 4 and checking remainder*/
  27.  
  28. if ((year%e) == 0)    /*dividing the year by 400 and checking remainder*/
  29.  
  30. printf("This year is a leap year\n");
  31.  
  32. }
it works well for years that are not leap years but any year like 1996, 2004 or 2008 says it's not a leap year and I don't understand why my last three lines don't solve this problem.
Feb 1 '08 #1
5 3467
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. if ((year%b) == 0)    /*dividing the year by 100 and checking remainder*/
  2.  
  3. if ((year%e) != 0)    /*dividing the year by 400 and checking remainder*/
  4.  
  5. printf("This year is not a leap year\n");
  6.  
  7. if ((year%b) == 0)    /*dividing the year by 4 and checking remainder*/
  8.  
  9. if ((year%e) == 0)    /*dividing the year by 400 and checking remainder*/
  10.  
  11. printf("This year is a leap year\n");
These two sections have the same if statements, but different results. You also never use c.

In addition, if this is C++, why are you

1) using stdio.h?
2) using printf?
3) using scanf?

I think this is actually C.
Feb 1 '08 #2
here's what i have. using microsoft studio

Expand|Select|Wrap|Line Numbers
  1. #define _CRT_SECURE_NO_DEPRECATE
  2. #include <stdio.h>
  3.  
  4. int main( void )
  5. {
  6.     int year, b, c, e;  
  7.     b = 4;
  8.     c = 100;
  9.     e = 400;
  10.  
  11.     /* prompt the user for a year to check */
  12.     printf("Enter a year:  ");
  13.     scanf("%d", &year); 
  14.     /* put your code below here */
  15.  
  16.     if ((year%b) != 0)                                                                                                       /*dividing the year by 4 and checking    remainder*/
  17.  
  18. printf("This year is not a leap year\n");
  19.  
  20. if ((year%b) == 0)    /*dividing the year by 100 and checking remainder*/
  21.  
  22. if ((year%e) != 0)    /*dividing the year by 400 and checking remainder*/
  23.  
  24. printf("This year is not a leap year\n");
  25.  
  26. if ((year%b) == 0)    /*dividing the year by 4 and checking remainder*/
  27.  
  28. if ((year%e) == 0)    /*dividing the year by 400 and checking remainder*/
  29.  
  30. printf("This year is a leap year\n");
  31.  
  32. }
it works well for years that are not leap years but any year like 1996, 2004 or 2008 says it's not a leap year and I don't understand why my last three lines don't solve this problem.


where do you use "c=100", i could not find it.

algorithm is
if( ( year % 400 == 0) || (( year % 4 == 0 ) && ( year %100 != 0 )) ){
leap year
else
not a leap year.

Regards
Arul
Feb 1 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
Actually, I belive the algorithm is:

A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400.

Thus years such as 1996, 1992, 1988 and so on are leap years because they are divisible by 4 but not by 100. For century years, the 400 rule is important. Thus, century years 1900, 1800 and 1700 while all still divisible by 4 are also exactly divisible by 100. As they are not further divisible by 400, they are not leap years.
Feb 1 '08 #4
jimix
8
O.k. Thanks for your help. I understand the algorithm and tried an "else" statement.(it seems simpler this way) I get one error here and don't know what I am missing. This is c not c++ and my original post had some integer mistakes.


Expand|Select|Wrap|Line Numbers
  1. #define _CRT_SECURE_NO_DEPRECATE
  2. #include <stdio.h>
  3.  
  4. int main( void )
  5. {
  6.     int year, a, b, e;  
  7.     a = 4;
  8.     b = 100;
  9.     e = 400;
  10.  
  11.     /* prompt the user for a year to check */
  12.     printf("Enter a year:  ");
  13.     scanf("%d", &year); 
  14.  
  15.     if (year%e == 0) 
  16.  
  17.     if (year%b != 0) && (year%a == 0)
  18.  
  19.         printf("This is a leap year\n")
  20.     else
  21.         printf("This is not a leap year\n")
  22.  
  23.     }
Feb 2 '08 #5
jimix
8
nevermind I figured it out. (with your help). thanks.
Feb 2 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: deepak.rathore | last post by:
can someone give the regular expr. to validate leap yr like 02/29/2000,02/29/00 Thanks
2
by: owz | last post by:
Ok, I am making a program (java class file) 2 work out if a date entered is valid or invalid for the day, month year, and for leap years.. dd/mm/yyyy . I seem 2 have gotten it 2 validate the year...
8
by: rn216_ccc | last post by:
Hi there all, I found a web site,. http://www.onlineconversion.com/leapyear.htm, where it can give a list of leap years by within the given range by the user, or the user may enter a year and the...
7
by: karafire2003 | last post by:
Hey there...i was wondering if someone can help me with my program. I'm doing the leap year program thing. i thought i had it all figured out, but it ALWAYS says that it's not a leap year. here's my...
8
by: SONIQ | last post by:
You can determine whether a year is a leap year by testing if it is divisible by 4. However, years that are also divisible by 100 are not leap years, unless they are also divisible by 400, in which...
12
by: Brigitte Behrmann | last post by:
Can anyone assist with my code. I just cannot get this to work: I have to write a script that allows the user to enter a year & then determine whether it is a leap year or not. The request is for a...
37
by: mazwolfe | last post by:
I'm new here, so excuse me if my style is incorrect. Can anyone come up with a better method for this calculation? Code: int is_leap(int year) { switch (year % 19) { case 0: case 3: case 6:...
4
by: jrw133 | last post by:
Hello. So im a little bit stuck on one of my homework questions. heres the question: Write a C Shell Script that performs the following functions. Asks the user to input a year. The script...
4
by: hallsers | last post by:
Here is the problem i am having: - When a user logs in, on page load it will take the dob of all users in a stored friends list and compare them against the following conditions - To display any...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.