Question

Write a program in C that reads a month and a day in that month and...

Write a program in C that reads a month and a day in that month and computes how many days it is since
December 26th of previous year. For example, if input is 1 10 (January 10th), the number of days since
December 26th is 15. The program computes the number of days for a nonleap year (February has always
28 days). The input is two integers, one between 1 and 12 (inclusive) and one between 1 and 31 (inclusive)
and the output will be a single line “X days”, where X is the number of days.

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

Hello, here is the answer to this question.

EXPLANATION:

First we will declare few variables & initialize them.
We will take a variable fixed and initialize it with 26 as we have to calculate value from 26th december.
We will take a variable monthend and initialize it with 31 as there are 31 days in a month according to the question.
We will initialize the result variable with 0 value, which will later hold the solution.

Coming on to the algorithm:

We will make 2 cases:
1) If the month is January or Feb:
    We will calculate by this formula : result = (monthend * (month-1)) + (monthend - fixed) + day;

   monthend - fixed will give the value of the remaining days of the december month
   monthend * month-1 will give the days in the new year.

2) If the month is any other than January or February:
    We will calculate using this formula : result = (monthend * (month-1)) + (monthend - fixed) + day - 3;
    We are subtractiing by 3 because Feb month has 3 days less than the other months.

CODE:


#include <stdio.h>

int main()
{
    // VARIABLE DECLARATION
    int month,day;
    int fixed = 26;
    int monthend = 31;
    int result = 0;
  

    printf("Please Enter the Month and Day : ");
    scanf("%d %d",&month,&day);
    // if the month is Jan or Feb
    if( month == 1 || month == 2 ) {
        result = (monthend * (month-1)) + (monthend - fixed) + day;
    }
    // Any month except jan and feb.
    if( month > 2) {
        result = (monthend * (month-1)) + (monthend - fixed) + day - 3;
    }
  

    printf(" There are : %d Days!",result);

    return 0;
}


OutPut:

Add a comment
Know the answer?
Add Answer to:
Write a program in C that reads a month and a day in that month and...
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
  • using C programming Date Write a program that reads a month and a day in that...

    using C programming Date Write a program that reads a month and a day in that month and prints the date in format "month day". The input is two integers, one between 1 and 12 (inclusive) and one between 1 and 31 (inclusive) and the output is a single line in format "month day". For example, for input 2 10 the program will print "February 10th". The day will always have the ordinal indicator, e.g. 1st, 2nd, 3rd, 4th. Example...

  • This is a Java program Write a program that reads an unspecified number of integers, determines...

    This is a Java program Write a program that reads an unspecified number of integers, determines home many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number to 2 decimal places. System.out.println(countPositive); System.out.println(countNegative); System.out.println(total); System.out.printf("%.2f",total * 1.0 / count); Assume inputs are always integer [-912985158, 912985158] and ends with 0. Input 1 2 -1 3...

  • C programming! Write a program that reads integers until 0 and prints the sum of values...

    C programming! Write a program that reads integers until 0 and prints the sum of values on odd positions minus the sum of values on even positions. Let ?1, ?2, … , ??, 0 be the input sequence. Then the program prints the value of ?1 − ?2 + ?3 − ?4 + ⋯ ??. The input is a sequence of integers that always contains at least 0 and the output will be a single number. For example, for input...

  • Write a program that reads a string from keyboard and tests whether it contains a valid...

    Write a program that reads a string from keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format mm/dd/yyyy. A valid month value mm must be from 1 to 12 (January is 1). The day value dd must be 1 from to a value that is appropriate...

  • C Program Question 1: Write a program that reads a date from the keyboard and tests...

    C Program Question 1: Write a program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • Question 1: Write a program in C that reads a date from the keyboard and tests...

    Question 1: Write a program in C that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • C++ please Question 4: 125 points] Write and test a program that reads two positive integers...

    C++ please Question 4: 125 points] Write and test a program that reads two positive integers nl and n2 with n2 > nl. The program then calculates the sum of the prime numbers, using is prime () function developed above, between nl and n2 (inclusive) A Sample input Enter values for nl and n2 (nl<n2): 3 9 Output: Thé Sum of prime numbers between 3 and 9 (inclusive) is 15

  •    Lab/HW 3   Write a program that reads in the following data, all entered on one...

       Lab/HW 3   Write a program that reads in the following data, all entered on one line with at least one space separating them: a) an integer representing a month b) an integer representing the day of the month c) an integer representing a year 1. Check that the month is between 1 and 12. If it’s not print an error message. 2. Day cannot be 0 or less nor can it be more than 31 for all months. Print...

  • 1. Write a program that reads a sequence of numbers from the keyboard, and displays the...

    1. Write a program that reads a sequence of numbers from the keyboard, and displays the smallest number. The sequence has at least one number and is terminated by -999(-999 will not appear in the sequence other than as the end marker). 2. Write a program which requests an integer input n from the keyboard and computes the sum of square of k for all k from 1 to n(inclusive). use java

  • in python Write a program that reads the date of birth from the user as 3...

    in python Write a program that reads the date of birth from the user as 3 integer values (day, month, year) and then computes and prints their exact age in days. You need to take into account the leap You may not use any built-in functions related to date and time! Example: Someone born on 01/01/2000 is 365 days * 18 years + 5 days (since the years 2000, 2004, 2008, 2012, and 2016 are all leap years) + 352...

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