# Day of the Month

Page 1 of 1

## 3 Replies - 600 Views - Last Post: 03 August 2009 - 08:10 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=117947&amp;s=bbb17c9ff76f9cd34f410c2903df29fb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 am_alpha

Reputation: 0
• Posts: 3
• Joined: 29-July 09

# Day of the Month

Posted 01 August 2009 - 03:50 AM

A c program that if a user input a number from 1-30 it will tell what day of he month it is. Assuming that 1 is Wednesday.. I will greatly appreciate all your response.. {totally a beginner} so this is my code..huhu..

```
#include<stdio.h>

int main (void)
{
int day
printf("Enter a number and i will tell what day of the month:");
scanf("%d", &day);

if day<31
printf("Number is invalid!");

else if day=1 && day=8 && day=15 && day=22 && day=29
printf("The day is wednesday!");

else if day=2 && day=9 && day=16 && day=23 && day=30
printf("The day is thursday!");

else if day=3 && day=10 && day=17 && day=24 && day=31
printf("The day is friday!");

else if day=4 && day=11 && day=18 && day=25
printf("The day is saturday!");

else if day=5 && day=12 && day=19 && day=26
printf("The day is sunday!");

else if day=6 && day=13 && day=20 && day=27
printf("The day is monday!");

else if day=7 && day=14 && day=21 && day=28
printf("The day is tuesday!");

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Day of the Month

### #2 Wippies

Reputation: 1
• Posts: 12
• Joined: 01-August 09

## Re: Day of the Month

Posted 01 August 2009 - 04:53 AM

By searching you would find several topics about same subject .
Here is some .

### #3 FoodGood

Reputation: 5
• Posts: 97
• Joined: 08-July 09

## Re: Day of the Month

Posted 01 August 2009 - 05:08 AM

Well the validation is wrong..
All the numbers in your "if..else" are smaller than 31
You should check if the "day > 30" or "day < 1" --> Invalid day
If you know "switch" block, it is easier to use it in you case..

I have mathematical approach (DO NOT READ IF YOU DON'T KNOW MATHEMATICAL SERIES)
You can see that every day is a linear series: d=7 and a0=1(Wednesday), 2(Thursday) etc..
IF we simplify the An=A0 + (n-1)*d -----> A0 = An - 7*n + 7

An = the day you get from the user
n = the place of the day in the match series, which is "day/7"
now you have the A0 = 1...7, and you can check only these numbers

This post has been edited by FoodGood: 01 August 2009 - 05:11 AM

### #4 am_alpha

Reputation: 0
• Posts: 3
• Joined: 29-July 09

## Re: Day of the Month

Posted 03 August 2009 - 08:10 PM

FoodGood, on 1 Aug, 2009 - 04:08 AM, said:

Well the validation is wrong..
All the numbers in your "if..else" are smaller than 31
You should check if the "day > 30" or "day < 1" --> Invalid day
If you know "switch" block, it is easier to use it in you case..

I have mathematical approach (DO NOT READ IF YOU DON'T KNOW MATHEMATICAL SERIES)
You can see that every day is a linear series: d=7 and a0=1(Wednesday), 2(Thursday) etc..
IF we simplify the An=A0 + (n-1)*d -----> A0 = An - 7*n + 7

An = the day you get from the user
n = the place of the day in the match series, which is "day/7"
now you have the A0 = 1...7, and you can check only these numbers

and this has been what i have got..
[code]
#include<stdio.h>

int main (void)
{
int day
printf("Enter a number from 1-30");
scanf("%d", &day)

if day<=0
printf("Number is invalid.");

else if day>0
rem=day%10
else if rem=0
printf("Tuesday.");
else if rem=1
printf("Wednesday.");
else if rem=2
printf("Thursday.");
else if rem=3
printf("Friday.");
else if rem=4
printf("Saturday.");
else if rem=5
printf("Sunday.");
else if rem=6
printf("Monday.");
return 0;
}

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }