i got an exercise in which i've to write a code that does:
when you enter the day of the
month (f.e. 21th) and the day (f.e. Sunday) and the month
(f.e. august) will display if in the given month there is a
13th Friday.

so i made a prog. that counts the days, it works, but even by my beginner standards the prog is too dumb and long, i made an if for every day of the week /i posted there only the if for sunday & monday, but it's all the same for the other days :( /

my question is, if there is a way to do it a bit more elegantly and shortly?

``````//calendar

#include <stdio.h>
#define s scanf
#define p printf

void main ()
{
int dw,dm,m;

p("plz enter dw, dm, m\n");

s("%d%d%d",&dw,&dm,&m);

//for Sundays

if (dw==1)
{

while (!(dm>=1&&dm<=7))
dm=dm-7;

if(dm==1)
p("in this month there is a 13th friday\n");
else
p("in this month there is no 13th friday\n");
}
//for mondays

if (dw==2)
{

while (!(dm>=1&&dm<=7))
dm=dm-7;

if(dm==2)
p("in this month there is a 13th friday\n");
else
p("in this month there is no 13th friday\n");
}
//and so on for the 7 days

}``````

## All 4 Replies

Well I'd like to tell two things :

1> I really didn't get how is your algorithm going to test if there is the thirteenth friday in that month or not.
Your program doesn't have any reference for the year and without that there is no way to check the total number of days from the years start and the computations you have done is something totally out of the way.

2>Here I am not correcting your algorithm but shortening it as you had asked.You can device a far better algorithm so try ;)

``````//calendar
#include<stdio.h>
#define s scanf
#define p printf
int main ()
{
int dw,dm,m;
p("plz enter dw, dm, m\n");
s("%d%d%d",&dw,&dm,&m);
//for Alldays taken together

while (!(dm>=1&&dm<=7)) dm=dm-7;

if(dm==dw) p("in this month there is a 13th friday\n");
else p("in this month there is no 13th friday\n");

return 0;
}``````

You are doing nothing but the above code.And to be frank above code does nothing ;). And ya never use void main,use int main get used to the standards.

#define s scanf
#define p printf
And definitely don't do this either.

Your programs will end up as a meaningless stream of single letters and punctuation to everyone else from the start, and it will be the same for you the moment you stop working on the program.

what salem said.

and, anyhow, if you really want to do this correctly, you will use the standard C library <time.h> ... you will make use of the time_t type, and the structure tm.

Well I'd like to tell two things :

1> I really didn't get how is your algorithm going to test if there is the thirteenth friday in that month or not.
Your program doesn't have any reference for the year and without that there is no way to check the total number of days from the years start and the computations you have done is something totally out of the way.

it's cause 13th Fridays only occur if a month begins with 1th Sunday.

2>Here I am not correcting your algorithm but shortening it as you had asked.You can device a far better algorithm so try ;)

whoa, thanx, much better than mine

... And ya never use void main,use int main get used to the standards.

okay, i didn't know there is a standard to use only int main.
our teacher always used void main

and thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.