Thread: code snippet about leap year

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    208

    code snippet about leap year

    I have this code snippet for a leap year if you input a year like 1800 that is not a leap year but if you input a year 2000 it is a leap year how would I code that I feel pretty humble right now because I should be able to figure it out here's the code snippet I working with


    Code:
                if (year % 4 == 0 && year % 100 != 0 && year % 400 == 0)
                    Console.Write("\nThere are 28 days in this month!");
                else
                    Console.Write("\nThere are 29 days in this month!");
    I tried using || or but that doesn't work either

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Sorry guys for bothering you with this stupid problem I figured it out myself on the internet its just year % 4 == 0 && year % 100 != 0 || year % 400 == 0
    I think that solves it

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    I thought I had solved the problem but it is turning out to be harder than i thought
    if someone knows of a simple way to code this problem I'd like to see it

    inputs are just month and year
    output how many days are in that month for that given year

    there can be either 29 days 28 days 30 days or 31 days and you have to account for leap year
    I'll show you some of the code I have but it doesn't do the job just so you guys will have something to work with


    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Chapter8Problem8
    {
        class Program
        {
            static void Main()
            {
                int month = 0;
                int year = 0;
    
                Console.Write("\nEnter month: ", month);
                month = int.Parse(Console.ReadLine());
    
                Console.Write("\nEnter year: ", year);
                year = int.Parse(Console.ReadLine());
    
    
                if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                    Console.Write("\nThere are 31 days in this month!");
                else
                    Console.Write("\nThere are 28 days in this month!");
    
    
    
                if (month == 4 || month == 6 || month == 9 || month == 11 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                    Console.Write("\nThere are 30 days in this month!");
                else
                    Console.Write("");
    
    
             
    
                Console.ReadLine();
                
           }
    
    
    
                
    
         
       }
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    leap year test should only apply to february...
    Code:
    if(month == 2)
    {
       if(leap year) 
          print 29 days;
       else
          print 28 days;
    }
    else if(30 days month)
       print 30 days
    else
       print 31 days
    something like that
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    208

    Thanks for the help

    Thanks again Vart you guys are bad ass I made a slight change to your code just slightly I think it works I haven't tested ever possible state but, I think it works here's what you gave me

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Chapter8Problem8
    {
        class Program
        {
            static void Main()
            {
                int month = 0;
                int year = 0;
    
                Console.Write("\nEnter month: ", month);
                month = int.Parse(Console.ReadLine());
    
                Console.Write("\nEnter year: ", year);
                year = int.Parse(Console.ReadLine());
    
    
                if (month == 2)
                {
                    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                        Console.Write("\nThere are 29 days in this month!");
                    else
                        Console.Write("\nThere are 28 days in this month!");
                }
    
    
                if (month != 2)
                {
                    if (month == 4 || month == 6 || month == 9 || month == 11)
                        Console.Write("\nThere are 30 days in this month!");
                    else
                        Console.Write("There are 31 days in this month!");
                }
    
    
             
    
                Console.ReadLine();
                
           }
       }
    }

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I would personally just use DateTime.IsLeapYear().

    Code:
    int days = month == 2 ? (DateTime.IsLeapYear(year) ? 29 : 28) : ((new[] { 4, 6, 9, 11 }).Contains(month) ? 30 : 31);
    Console.WriteLine("There are {0} days in this month!", days);
    Or even better, just use DateTime.DaysInMonth():

    Code:
    Console.WriteLine("There are {0} days in this month!", DateTime.DaysInMonth(year, month));
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is leap year function
    By SirPrattlepod in forum C Programming
    Replies: 9
    Last Post: 08-15-2013, 02:49 AM
  2. leap year function
    By cameuth in forum C Programming
    Replies: 5
    Last Post: 11-26-2011, 01:05 AM
  3. Leap Year Problems
    By jc99 in forum C Programming
    Replies: 13
    Last Post: 06-21-2009, 04:02 AM
  4. Program showing if a year its leap year or not.
    By Cyberman86 in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2008, 08:00 AM
  5. Help with leap year program.
    By IxTanGxI in forum C Programming
    Replies: 8
    Last Post: 02-20-2006, 08:49 PM