Question

//Hi, I'm working on a function of a larger program that counts the number of leap...

//Hi, I'm working on a function of a larger program that counts the number of leap years through a wide range of dates. I'm told that both mathematical equations are accurate, but I // am likely using flawed syntax having an if statement inside of a for loop. Thank you for your debugging help.

#include <stdio.h>

int main()
{ int i, y=2000, numleapyears=0;
for(i=1;i<y; i++)
{
//Alternate equation
//if (((y%4==0) && (y%100!==0)) || (y % 400 ==0))
if ((!(y%4) && (y%100) ) || !(y%400))
{numleapyears++;}

}
printf("%d", numleapyears);

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Change ((!(y%4) && (y%100)) || !(y%400)) to ((!(i%4) && (i%100)) || !(i%400)) . y value is 2000. So, "if statement" condition will always be checked against same value(i.e., 2000). That is the reason for wrong output.

Here is the working code:

I #include <stdio.h> 3 int main() 4-{ int 1, y-2000, numieapyears-0; //Alternate equation //if if (((1%4--0) II (İX100 ! #-e)) Il 8 (i ะ-e)) && (1%100)) % 400 ((1(1%4) && !(1%400)) 10 numleapyears++;) printf(%d, num!eapyears); 12 13 14

output:

Please give thumbs up, if you like it. Thanks.​

Add a comment
Know the answer?
Add Answer to:
//Hi, I'm working on a function of a larger program that counts the number of leap...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string usi...

    C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only). It can contain ONLY the following libraries: stdio, stdlib, math and string. I want to use the following function prototypes to try and implement things: /*Check whether a given number is prime or not*/ int checkPrime(int n) /*to find gcd*/ int gcd(int a, int h) void Encrypt(); void Decrypt(); int getPublicKeys(); int getPrivateKeys();...

  • i'm trouble getting my program to print this output and repeat until asked to quit the...

    i'm trouble getting my program to print this output and repeat until asked to quit the program Enter a telephone number using letterss (EXIT to quit): GET LOAN The corresponding telephone number is: 438-5626 Here's my code: #include <stdio.h> #include <string.h> char getNumber(char aC) { char c = ' '; switch (aC) { case 'A': case 'B': case 'C': c = '2'; break; case 'D': case 'E': case 'F': c = '3'; break; case 'G': case 'H': case 'I': c...

  • I'm having trouble getting a certain output with my program Here's my output: Please input a...

    I'm having trouble getting a certain output with my program Here's my output: Please input a value in Roman numeral or EXIT or quit: MM MM = 2000 Please input a value in Roman numeral or EXIT or quit: mxvi Illegal Characters. Please input a value in Roman numeral or EXIT or quit: MXVI MXVI = 969 Please input a value in Roman numeral or EXIT or quit: EXIT Illegal Characters. Here's my desired output: Please input a value in...

  • C Programming I have this cost estimation program, but I want to add to it more...

    C Programming I have this cost estimation program, but I want to add to it more functions to be more accurate and more useful. I want to add to the program an array and a loop that can help me with the materials that can be use to the building, as ceramic, wood and concrete and extra functions. ########## #include <stdio.h> int main(void) { float wid,len,yrs; int metersq = 2000; int floors,rooms,win,doors,restrooms,umet; char floortype[20]; char ti[20]; int woodfloor = 25;...

  • Hi! I'm working on building a GUI that makes cars race across the screen. So far...

    Hi! I'm working on building a GUI that makes cars race across the screen. So far my code produces three cars, and they each show up without error, but do not move across the screen. Can someone point me in the right direction? Thank you! CarComponent.java package p5; import java.awt.*; import java.util.*; import javax.swing.*; @SuppressWarnings("serial") public class CarComponent extends JComponent { private ArrayList<Car> cars; public CarComponent() { cars = new ArrayList<Car>(); } public void add(Car car) { cars.add(car); } public...

  • C++ Can someone please help with this code... even when the players number isnt a winning number the program always says the player wins: #include <stdio.h> #include <time.h> //main functi...

    C++ Can someone please help with this code... even when the players number isnt a winning number the program always says the player wins: #include <stdio.h> #include <time.h> //main function int main() {    //seeding a random number    srand(time(0));    //define the arrays for the wagered, winning amount, percent amount    double amounts[7] = { 1, 5, 10, 20, 50, 100, 1000 };    double payoff[7] = { 100, 500, 1000, 2000, 5000, 10000, 100000 };    double percent[7] = { 0.01, 0.05, 1, 2,...

  • I know I'm on the right path, but don't know where to continue from here. This is a C++ assignmen...

    I know I'm on the right path, but don't know where to continue from here. This is a C++ assignment Your job is to write that will display a calendar for any given month of a given year. The user will need to type the number of the month as an integer from 1 to 12 (1 is for January, etc.), and the year as a 4-digit integer. This assignment simply requires that your program make use of more than...

  • Hi any help is appreciated! I am using C++. The Julian Day Number (JDN) is a...

    Hi any help is appreciated! I am using C++. The Julian Day Number (JDN) is a sequential count of days since the beginning of the Julian Period. Day number zero corresponds to 1 January 4713 BCE. As a result of calendar reform in the 16th Century, we will only be computing day numbers since 15 October 1582 (which was the first day of the modern Gregorian calendar, having day number 2299161). 1 January 2001 was day number 2451911. To compute...

  • Programming language C The third picture is the code that is being refactored Student 1:10 PM...

    Programming language C The third picture is the code that is being refactored Student 1:10 PM 3296.- Function Specifications [15 points 5 bonus points Use appropriate loops to ensure that if the user enters on incorrect value (out-of-range) mare hece-for an input, that the user is given an accurate messoge and provided an opportunity te enter the value egain (fer every input) Use an appropriate loop to allow the user to execute the program over and over again up to...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT