Question

In: Computer Science

Write this in c++ Leap year and Second in year program Whether a year is a...

Write this in c++

Leap year and Second in year program

  1. Whether a year is a leap year on not a leap year.
  2. The number of seconds in a year. (FYI, it will be a different amount for leap vs non-leap year.) You can calculate the seconds by whatever means you choose, whether with defined constants and calculations or just calculations. It is your choice.
  3. If the user inputs a year number greater than zero, the program returns how many seconds were or will be in that year.
  4. Put in descriptions for your request for input. If you do the bonus portion, also put that in for your request.
  5. Tell the user that an input of -99 ends the program. (+5)
    Put your prompt in a loop, asking the user each time what year to check. (+5)
  6. You should assume a day length of exactly 24 hours. Obviously, multiplying 365*24*3600 isn’t exactly a programming challenge, and it won’t even give the right answer for all years.
  7. You must take account of leap years, which means your years will not all have the same number of days. If you do not already know, here is the calculation for leap years:

1. If a year is divisible by 4 with no remainder, it is a leap year,
2. unless it is a centurial year (ending in 00).
3. Then it is not a leap year,
4. unless it is a year divisible by 400, in which case it is still a leap year.

For example:

1904 is a leap year—evenly divisible by 4.
1900 is not a leap year—it is evenly divisible by 4, and also evenly divisible by 100. However, it is not evenly divisible by 400.
2000 is a leap year— it is evenly divisible by 4, evenly divisible by 100, but also evenly divisible by 400.
Your calculation should ignore all other variations in the length of the calendar year, such as leap seconds or the change from the Julian to the Gregorian system.

Solutions

Expert Solution

import java.util.Scanner;

public class YearToSeconds {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       while (true) {
           System.out.println("Enter year (-99 to quit): ");
           int year = sc.nextInt();
           if(year==-99)
               break;
           long seconds = 0;
           if (isLeap(year))
               seconds = 365 * 24 * 3600;
           else
               seconds = 366 * 24 * 3600;
           System.out.println(year + " has " + seconds + " seconds");
       }
   }

   private static boolean isLeap(int year) {
       boolean leap = false;
       // if any year is divisable by 4 than there are many chances for leap year
       // except few
       if (year % 4 == 0) {
           // if it is divisable by 100 than it shoud also divisable by 400 like 2000 etc
           if (year % 100 == 0) {
               // year is divisible by 400, so the year is a leap year
               if (year % 400 == 0)
                   leap = true;
               else
                   leap = false;
           } else
               leap = true;
       } else
           leap = false;
       return leap;
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

Write a program in C++ to check whether a number is prime or not
Write a program in C++ to check whether a number is prime or not
Write a program in assembly language that outputs the leap years between a beginning and an...
Write a program in assembly language that outputs the leap years between a beginning and an ending year. 1. Initialize $a0 and $a1 with the beginning and ending years. 2. Print out the leap years between 1970 and 2030. OUTPUT: From 1970 to 2030: 1972 … 2028
C Programming Question: Q) Write a C - program to check whether a given graph is...
C Programming Question: Q) Write a C - program to check whether a given graph is Bipartite using Breadth-first search (adjacency list) Do take your time but please do post full code and also please do it in C.
Write a Python code that will ask a user to input a year and return "Leap...
Write a Python code that will ask a user to input a year and return "Leap Year" if the year is a leap year, or "No Leap Year" if it is not a leap year. The program then asks for another year and performs the same task in the form of a loop, until the user inputs the then integer "-1." At that point, the program will exit the loop.
Write a C++ program to check whether a number is prime or not. A prime number...
Write a C++ program to check whether a number is prime or not. A prime number is a positive integer that has exactly two positive integer factors, 1 and itself. Eg: 2, 3, 5, 7, 11, 13, 17, ... Sample Output: Input a number to check prime or not: 13 The entered number is a prime number. Input a number to check prime or not: 28 The entered number is not a prime number. Enhance the program to list all...
Design and Write a program that asks for a 4-digit year and determines whether that year...
Design and Write a program that asks for a 4-digit year and determines whether that year is a leap year or not. This should work for any year from 1700 to 2022. Anything not completely obvious to a newbie must use # notes on lines to explain. Must be modularized and repeatable. Should have an invocation of the main routine at the end of the program. Must have Flowgorithm and Python code.
Write a C program that would constantly watch all processes (say once every second), and if...
Write a C program that would constantly watch all processes (say once every second), and if it encountered a notepad, it would kill it immediately.
Write a C# program that prints a calendar for a given year. Call this program calendar....
Write a C# program that prints a calendar for a given year. Call this program calendar. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:            0 Sunday                     1 Monday                   2 Tuesday                   3 Wednesday       4 Thursday                 5 Friday                      6 Saturday Your program should...
IN C++ Write a program to gauge the rate of inflation for the past year. The...
IN C++ Write a program to gauge the rate of inflation for the past year. The program asks for the price of an item (such as a hot dog or a one carat diamond) both one year ago and today. It estimates the inflation rate as the difference in price divided by the year ago price. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the rate of...
JAVA Take in a user input. if user input is "Leap Year" your program should run...
JAVA Take in a user input. if user input is "Leap Year" your program should run exercise 1 if user input is "word count" your program should run exercise 2 Both exercises should run in separate methods Exercise 1: write a java method to check whether a year(integer) entered by the user is a leap year or not. Exercise 2: Write a java method to count all words in a string.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT