Showing posts with label first. Show all posts
Count number of sunday on 1st of every month in C/C++ programing code
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();
}
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();
}
Lets Get Started with programming.
Writing your
first program in C/C++
According to me programming is not something to learn it
comes to you automatically by practicing. Writing program is more affectionate
in this respect then becoming bookish.
So here is your first program to start with try writing it
and understanding it.
1. // my first
program in C
2. #include <stdio.h>
3. void
main () {
4. printf("My
first program");
5. }
1. // my
first program in C++
2. #include <iostream>
3. using namespace std;
4. void
main () {
5. cout <<
"My first program";
6. }
OUTPUT PANEL
My first program
Explaining of program in C
Line
1: // my first program in C++
// indicates the start of comment all
the text written in the line next too this will be ignored as programming line
and itt will juxt be added in programming as it is. This is used for adding a
comment to tell the other programmer about any instruction or guiding him.It is
also used for the programmer to remember its own program. In this particular
case our comment is about the description of our program.
Line 2: #include
<stdio.h>
Lines that start with # are not the
regular programming lines these are the directives for pre-processor to add the
piece of code already made by different programmer which are most often used so
they are already added to the software so that time may be saved and all
programmer don’t have to write that same code every time. A programmer could
also make its own directives.
In our particular case we have added
<stdio.h> which is standard input
output header file. As we have to produce output on screen.
Line 3: void main () {
Every
program must have at least one function that is its main function. No program
could run without a main function you may add multiple function in a
program but the program will start from
its main function. Consider the construction of our main function in this
program void
main () {}
main
is the name of our function followed by parenthesis () because it is the
declaration of function.
In
parenthesis we may define any parameter for our function they are empty because
our program have no parameter. void is the return type of function return type
of any function must be defined it is the expression or value which the
function will return after its execution. We have used (void) which mean empty
our function will not return any value after its execution. The next thing is
{} anything written inside this will be the statement of our main program and
our function consist of these instruction.
It
is also called body of the function.
Line
4: printf("My first program");
This
line is statement. A statement is expression that produces some effect. In our
program this is the only line that produces some effect. printf() is command to
take out put in C lang. inside parenthesis () we write what to output. Anything written within ”” will be shown as it. printf is the function
statement that comes from the <stdio.h> that is why we have included that
file. If we want to take output in any other form other than standard output on
screen then we have to use the different directive or make one of our own.
;
is the most important part of the statement this is the indication of the end
of the statement to the compiler every single statement must be followed by
this semi colon otherwise there will be an error.
Line
5: }
Is
the end of our main function body.
Explanation in C++ for same program.
All
the things remains the same as in C. the changes are as follow.
·
Iostream is the standard input output directive
in C++.
·
Instead of printf(“”); structure
cout << “ ” ; syntax is used.
·
3rd line in C++ that is using
namespace std;
All the elements
of standard C++ library are stored in namespace std so most of source file will
contain this statement in start as we want to use C++ element cout is one of
them used in our program.
Try writing this
program to check your self. Practice make a man perfect. Best of luck. Hope our
blog will help you to reach to the destination of a good programmer.