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


}

thanx in advance

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.

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!

This question has already been answered. Start a new discussion instead.