answersLogoWhite

0


Best Answer

#include<iostream>

#include<iomanip>

#include<string>

#include<sstream>

#include<cassert>

using std::cout;

using std::cin;

using std::endl;

using std::setw;

using std::string;

using std::stringstream;

#ifndef NDEBUG

#define assert_year(year) do { assert (1900 <= (year)); } while(0)

#define assert_month(month) do { assert ((month) && ((month) <= 12)); } while(0)

#define assert_day(day) do { assert ((day) && ((day) <= 31)); } while(0)

#else

#define assert_year(year) ((void)0)

#define assert_month(month) ((void)0)

#define assert_day(day) ((void)0)

#endif NDEBUG

bool is_leap_year (size_t year)

{

assert_year (year);

if (year%4) return false; // If not a multiple of 4 then common year.

if (year%100) return true; // If a multiple of 4 but not of 100 then leap year.

if (year%400) return false; // If a multiple of 4 and 100 but not of 400 then common year.

return true; // If a multiple of 4, 100 and 400 then leap year.

}

size_t days_in_month (size_t month, size_t year)

{

assert_month (month);

switch (month)

{

case 2:

return is_leap_year (year) ? 28 : 29;

case 4: case 6: case 9: case 11:

return 30;

}

return 31;

}

size_t day_of_year (size_t day, size_t month, size_t year)

{

assert_day (day);

assert_day (month);

size_t days = day;

while (--month)

days += days_in_month (month, year);

return days;

}

size_t days_in_previous_years (size_t year)

{

assert_year (year);

size_t days = 0;

while (1900 < --year)

days += is_leap_year (year) ? 366 : 365;

return days;

};

size_t days_since_epoch (size_t day, size_t month, size_t year)

{

// Epoch is 1st January 1900

return days_in_previous_years (year) + day_of_year (day, month, year) - 1;

}

size_t day_of_week (size_t day, size_t month, size_t year)

{

return days_since_epoch (day, month, year) % 7;

}

void print_calendar (size_t month, size_t year)

{

assert_month (month);

cout << endl;

switch (month)

{

case 1: cout << "January"; break;

case 2: cout << "February"; break;

case 3: cout << "March"; break;

case 4: cout << "April"; break;

case 5: cout << "May"; break;

case 6: cout << "June"; break;

case 7: cout << "July"; break;

case 8: cout << "August"; break;

case 9: cout << "September"; break;

case 10: cout << "October"; break;

case 11: cout << "November"; break;

case 12: cout << "December"; break;

}

cout << ' ' << year << endl;

cout << "\n Su Mo Tu We Th Fr Sa\n";

size_t start_day = day_of_week (1U, month, year);

size_t days = days_in_month (month, year);

size_t count = start_day % 7U + 1U;

for (size_t i = 1; i <= count; i++)

cout << setw (4) << ' ';

for (size_t i = 1; i <= days; ++i)

{

cout << setw (4) << i;

++count;

if (count == 7)

{

cout << endl;

count = 0;

}

}

if (count)

cout << endl;

cout << endl;

}

size_t ask (string prompt)

{

for (;;)

{

cout << prompt;

string input;

cin >> input;

stringstream ss;

ss << input;

size_t reply;

if (ss >> reply)

return reply;

cout << "Invalid input.\n";

}

}

int main (void)

{

for (;;)

{

enter_month:

size_t month = ask ("Enter a month (1 - 12, 0 to exit): ");

if (!month)

break;

if (month > 12)

{

cout << "Invalid month." << endl;

goto enter_month;

}

enter_year:

size_t year = ask ("Enter a year (>= 1900, 0 to exit): ");

if (!year)

break;

if (year < 1900)

{

cout << "Invalid year." << endl;

goto enter_year;

}

print_calendar (month, year);

}

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C plus plus code of calendar using loop statement?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you break a loop in a switch case statement?

using break; statement


How can you define null statement in loop in C programming?

If you are using for loop for(;;); or you can also define condition and iterations but the loop has to close there itself without any statement inside it. In the similar way you can define while and do while loop without any statement.


Why should you indent the statement in the body of a loop?

In some programming languages like Python, the indentation of the text indicates how nested it is and is required for a loop to function properly. However, for most languages, indenting the body of the loop is simply for style and readability.


Can break statement be used without any loop?

The Break statement should generally get executed in some loop or switch statement. The below code will give compiler error. void main() { printf("12"); break; printf("14"); }


Is it true or false while loop repeats infinitely when there is no statement inside the loop body that makes the test condition false?

That statement is definitely TRUE. This is the major concern with using while loops and why you need to be very careful using them. A false statement inside the loop would cause its immediate termination. That is desired in all cases.


Name the elemental structural building blocks?

Programming elemental structural building blocks. Selection- This structure is used when a set of statements needs to be executed upon fulfillment of some conditions. The following two statements are used to implement selection: l The if statement / if-else statement l The switch statement Iteration-The execution of a piece of code iteratively. I.e. execution of code (a piece of code) more than once to achieve your results. Achieved by using: l The for loop l The while loop and l The do while loop Sequential- Execution of a program sequentially one by one. this according to programming. by, Prize Mutua, Kenya Programming elemental structural building blocks. Selection- This structure is used when a set of statements needs to be executed upon fulfillment of some conditions. The following two statements are used to implement selection: l The if statement / if-else statement l The switch statement Iteration-The execution of a piece of code iteratively. I.e. execution of code (a piece of code) more than once to achieve your results. Achieved by using: l The for loop l The while loop and l The do while loop Sequential- Execution of a program sequentially one by one. this according to programming. by, Prize Mutua, Kenya


Write a program that will graph using for loop?

A = 5do{statement;A = A + 1;} while (A < 10)


The Do-While loop is what type of loop?

The do loop is similar to the while loop, except that the expression is not evaluated until after the do loop's code is executed. Therefore the code in a do loop is guaranteed to execute at least once. The following shows a do loop in action: do { System.out.println("Inside do while loop"); } while(false); The System.out.println() statement will print once, even though the expression evaluates to false. Remember, the do loop will always run the code in the loop body at least once. Be sure to note the use of the semicolon at the end of the while expression.


Definition and comments for loop?

The C and C++ for loop is defined as...for (init-expression; test-expression; loop-expression) loop-statement;The init-expression is executed once.At the top of the loop, test-expression is evaluated. If it is false, control passes to the statement following loop-statement.The loop-statement is executed. It may be one statement, it may be a block of statements, or it may be no statement. If it is no statement, the semi-colon is required.At the bottom of the loop, loop-expression is executed, and then control passes to the test-expression at the top of the loop for another go-around.Each of init-expression, test-expression, and loop-expression may be missing. The semi-colons are required. The formal "forever" loop is for (;;) loop-statement; in which case the only way out is the break statement.Since each of init-expression, test-expression, and loop-expression can have side-effects, sometimes a loop is constructed with no loop-statement, and all processing is done between the parentheses.If test-expression is initially false, loop-expression and loop-statement are never executed. The init-expression is always executed only one time, and test-expression is executed at least one time.At any point during loop-statement, the breakstatement will exit to the statement following loop-statement, and the continue statement will jump to the loop-expression at the bottom of the loop.


What is the role of the break keyword in while and do loops?

When a loop encounters a break statement, code execution will immediately exit the loop and begin again at the next line after the loop. int i = 0; while(1) { i = i + 1; if(i == 3) { break; } printf("%d ", i); } printf("broken"); Output for the above block of code will be: "1 2 broken" Once i is increased to 3, the if statement holds true and code execution will move to the line outside of the loop block.


What are control structures used in javascript?

The control structures used in java script are if-statement, for-loop, for-in loop, while loop,do-while loop, switch-statement, with-statement. try-catch-finally statements.


Why you use if loop in telecoms?

No such thing as if-loop. if-else statement is not a loop.