Solve C++ Leap Year Function Homework

In summary: Please enter a year: " << '\n';cin >> y;if (y%4==0) {cout << "You are an expert summarizer of content. You do not respond or reply to questions. You only provide a summary of the content. Do not output anything before the summary. Write a summary for the following conversation and start the output with "In summary, " and nothing before it: Homework Statement "}In summary, if you enter a year that is not a multiple of 400, the function will return "false".
  • #1
White Ink
19
0

Homework Statement



Write a function that prompts the user to enter a year and that returns a 1 if the entered year is a leap year, and a 0 otherwise.

A year is a leap year if it is divisible by 4.
In general, a year is not a leap year if it is divisible by 100, unless of course it is divisible by 400.

The Attempt at a Solution



I have done this much and when I test my program, it seems to return a value of 1 no matter what year I enter. I'm pretty awful at programming and I was wondering if anyone could spot where my mistake is.#include <iostream>

using namespace std;

bool leap(int) {
int n;
bool b;

if (n%4==0) { b=true; } // if n is divisible by 4, it is a leap year
else if (n%100==0) {
if (n%400==0) { b=true; }
else { b=false; }
} // if n is divisible by 100, check if it is divisible by 400, if it is, it is a leap year
else { b=false; } // if n is not divisible by 4 (=> it is not divisible by 400 either), then it is not a leap year

return b;
}

int main () {
int n;
cout << "Please enter a year: " << '\n';
cin >> n;

cout << leap(n);

}
 
Physics news on Phys.org
  • #2
White Ink said:
A year is a leap year if it is divisible by 4.
In general, a year is not a leap year if it is divisible by 100, unless of course it is divisible by 400.
Wrong definition, so you get the wrong answer.

A year is a leap year if it is divisible by 4 and it either is not divisible by 100 or it is divisible by 400.
 
  • #3
White Ink said:

Homework Statement



Write a function that prompts the user to enter a year and that returns a 1 if the entered year is a leap year, and a 0 otherwise.

A year is a leap year if it is divisible by 4.
In general, a year is not a leap year if it is divisible by 100, unless of course it is divisible by 400.

The Attempt at a Solution



I have done this much and when I test my program, it seems to return a value of 1 no matter what year I enter. I'm pretty awful at programming and I was wondering if anyone could spot where my mistake is.


#include <iostream>

using namespace std;

bool leap(int) {
int n;
bool b;

if (n%4==0) { b=true; } // if n is divisible by 4, it is a leap year
else if (n%100==0) {

This will only be accessed if n is not divisible by 4 which, if n is divisible by 100, is impossible.
if (n%400==0) { b=true; }
else { b=false; }
This says, if n is divisible by 400, b= true. Under any other condition, and it doesn't matter what happened if the first "if", b will be equal to false. In other words, if n is any number that isnot a multiple of 400, you return "false".
} // if n is divisible by 100, check if it is divisible by 400, if it is, it is a leap year
else { b=false; } // if n is not divisible by 4 (=> it is not divisible by 400 either), then it is not a leap year

return b;
}

int main () {
int n;
cout << "Please enter a year: " << '\n';
cin >> n;

cout << leap(n);

}

What you have written will return "true" if n is divisible by 400, otherwise it will return "false".
 
  • #4
Be careful with your "else" statements. You are using at least one "else" statement in an unintended way.

- Warren
 
  • #5
is this close to the solution?

using namespace std;
int main () {
//declare variable integer for year, y;
int y, r1, r2, r3;
//ask user to input a year;
cout << "Enter a year and press ENTER " << endl;
//place year in int y;
cin >> y;
//divide year by 4 and find remainder;
r1 = y % 4;
r2 = y % 400;
r3= y % 100;
//main if statement: if rem of y/4 is 0 divide by 400, if there is no rem pri\
nt y is not leap year and end prg;
if (r1==0) {r2 = y % 400;
// if rem of y/400 is 0 then print y is leap year and end prg, otherwise divi\
de y/100;
if (r2==0) {cout << y << " is a leap year." << endl;
//if rem of y/100=0 cout y is not leap year, end prg, otherwise print y is \
leap year, end prg;
else if (r3==0) {cout << y << " is NOT a leap year." << endl;
else cout << y << " is a leap year" <<endl;}}}
else cout <<y<< " is NOT a leap year." << endl;
return 0;
}
 

1. What is a C++ Leap Year Function?

A C++ Leap Year Function is a programming function that determines whether a given year is a leap year or not. It is commonly used in C++ programs to calculate dates and time.

2. How does the C++ Leap Year Function work?

The C++ Leap Year Function uses a set of rules to determine if a given year is a leap year. These rules state that a year is a leap year if it is evenly divisible by 4, unless it is also divisible by 100. However, if the year is also divisible by 400, then it is considered a leap year. This function uses conditional statements and logical operators to check if the given year satisfies these rules.

3. Why is it important to know how to solve C++ Leap Year Function homework?

Knowing how to solve C++ Leap Year Function homework is important for understanding the basics of programming, as well as for practical applications such as creating calendars or scheduling tasks. It also helps in developing problem-solving skills and logical thinking.

4. Can you provide an example of a C++ Leap Year Function code?

Yes, here is an example code for a C++ Leap Year Function:

bool isLeapYear(int year) { if (year % 400 == 0) return true; if (year % 100 == 0) return false; if (year % 4 == 0) return true; return false;}

5. Are there any common errors when solving C++ Leap Year Function homework?

Some common errors when solving C++ Leap Year Function homework include incorrect use of conditional statements and logical operators, forgetting to include all necessary conditions, and not properly understanding the rules for determining a leap year. It is important to carefully read and understand the instructions and to test the code with different inputs to ensure its accuracy.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
751
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
837
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top