Counting Sundays || for loop || C++
In this, we are going to see for loop, the program is based on for loop which basically, counts the number of Sundays in a 31 days Month.

The code given below can be used for TURBO C++ Compiler:-

    #include<iostream.h>
#include<conio.h>

void main()
{
    clrscr();
	int i, n, count = 0;
	cout << "Enter no of days in the month \n";
	cin >> n;
	if (n == 30 or n == 31 or n == 28 or n == 29)
	{
		for (i = 1; i <= n; i++)
		{
			if (i % 7 == 0)
			{
				count = count + 1;
			}
		}
		if (n == 28)
		{
			cout << "There are " << count << "Sundays" << endl;
		}
		else if (n == 29 or n == 30 or n == 31)
		{
			cout << "There are " << count << " Sundays with probability of 1 extra sunday" << endl;
		}
	}
	else
		cout << "There is no month with " << n << " days" << endl;

	getch();
}
    
    

The code given below can be used for g++/gcc Compiler:-

    #include<iostream>
using namespace std;

int main()
{
	int i, n, count = 0;
	cout << "Enter no of days in the month \n";
	cin >> n;
	if (n == 30 or n == 31 or n == 28 or n == 29)
	{
		for (i = 1; i <= n; i++)
		{
			if (i % 7 == 0)
			{
				count = count + 1;
			}
		}
		if (n == 28)
		{
			cout << "There are " << count << "Sundays" << endl;
		}
		else if (n == 29 or n == 30 or n == 31)
		{
			cout << "There are " << count << " Sundays with probability of 1 extra sunday" << endl;
		}
	}
	else
		cout << "There is no month with " << n << " days" << endl;

	return 0;
}
    
    

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post