Like Tree1Likes
  • 1 Post By artistunknown

code snippet about leap year

This is a discussion on code snippet about leap year within the C# Programming forums, part of the General Programming Boards category; I have this code snippet for a leap year if you input a year like 1800 that is not a ...

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

    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
    150
    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
    itsme86 likes this.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

    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,175
    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, 03: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, 05: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, 09:00 AM
  5. Help with leap year program.
    By IxTanGxI in forum C Programming
    Replies: 8
    Last Post: 02-20-2006, 08:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21