Thread: Leap year program using if statements

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Well, keep rearranging it until it works.

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    
    
    int main()
    {
        int year;
    	std::cout << "Enter a year and I will determine if it's a leap year:  " ;
    	std::cin >> year ;
    	
    	int leap;
    	int leap2;
    	int leap3;
    	leap = year % 4 ;
    	leap2 = year % 100 ;
    	leap3 = year % 400 ;
    
    	if (leap2!=0 && leap == 0 || leap3==0)
    	{
    				std::cout << "It is a leap year.";
    				std::cin.get();
    				std::cin.get();
    			   }
    			else 
    			{
    				std::cout << "It is not a leap year.";
    				std::cin.get();
    				std::cin.get();
    			}
    
      return 0;
    }
    Last edited by Oldman47; 12-04-2009 at 07:53 PM.

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    Bingo. That was going to be what I tried next. I had four different way written down on a piece of paper and I was working through them.

    Thanks for the help.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    My final code:

    Code:
     
    #include <iostream>
    
    int year;
    int leap;
    int leap2;
    int leap3;
    
    
    int main()
    {
    	std::cout << "Enter a year and I will determine if it's a leap year:  " ;
    	std::cin >> year ;
    	
    	leap = year % 4 ;
    	leap2 = year % 100 ;
    	leap3 = year % 400 ;
    
      if(leap == 0 || leap3 == 0 && leap2 != 0 )
      {
    				std::cout << "It is a leap year." << '\n' ;
    				
    			 }
    		else 
    			{
    				std::cout << "It is not a leap year." << '\n' ;
      }
       	
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. n00b needs help with small program
    By rightbrainer in forum C Programming
    Replies: 3
    Last Post: 04-15-2009, 05:52 PM
  2. Replies: 0
    Last Post: 04-08-2009, 04:23 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. problem with if statements (leap year)
    By radiantarchon28 in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2007, 02:40 PM
  5. a program to create months in a year newbie question
    By robasc in forum C Programming
    Replies: 2
    Last Post: 03-05-2005, 05:41 PM