PropellerAds
Posted by : Unknown Wednesday, 14 January 2015

Problem: Write a code to calculte number of sunday on first of every month  from 1st Jan 1901 to 31st Dec 2000 wherease the at 1st Jan 1900 the day is Monday.

The following code demonstrate the working of program. in this program there is use of nested loops 1st for total years thats is 1900 - 2000 inside this each year have 12 months, so inside year loop their is loop from 1 - 12 which is for months.  inside this we have if structure which decides which month is this if that months have 31 days it assign z = 31 if that month have 30 days it assign z = 30; if it is February it checks whether it is a leap year or not and then assign respective value to z.

now inside this loop we have a counter day which is controlled through if  to have value from 1-7 where 1 represent Monday , 2 Tuesday and so on in this manner 7 represent Sunday.

now we have a variable total Sunday which is initially 0 in final if structure there are 3 condition
1. day ==7 that is the day should be Sunday
2. z == 1 that is the date of month should be 1st
3. the year should be greater than 1900 as we have to count from 1901.

if these 3 condition satisfies then the counter for counting that is total Sunday increased by 1.

and after all loops successfully executed there is a statement to  print the value in console .


Hope u have understood it very well for any question or ambiguity plz comment below.

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

using namespace std;

void main(){

int x[3] = {7, 1, 1900};//first sunday
int y[3];
int z;

int day = 0;
int total_sunday = 0;

for (int i = 1900; i <= 2000; i++){
for (int j = 1;  j <= 12;  j++){
if (j == 1 || j == 3 || j == 5 || j == 7 || j == 8 || j == 12){
z = 31;
}
else if (j == 4 || j == 6 || j == 9 || j == 10 || j == 11) {
z = 30;
}
else if (j==2 && i%4 == 0)
{
z = 29;
}
else{
z = 28;
}

for (int k = 1; k <= z; k++){
y[0] = k;
y[1] = j;
y[2] = i;

day++;

if (day > 7){
day = 1;
}

if (day == 7 && y[0] == 1 && y[2] > 1900){
total_sunday++;
}
}
}
}

cout << "Total sunday on 1st of month during 1 jan 1901 to 31 dec 2000 are: " << total_sunday;

_getch();

}

Leave a Reply

Subscribe to Posts | Subscribe to Comments

PropellerAds
PropellerAds

Popular Post

Powered by Blogger.

- Copyright © BSCS -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -