3 Replies - 554 Views - Last Post: 03 August 2009 - 08:10 PM Rate Topic: -----

#1 am_alpha  Icon User is offline

  • New D.I.C Head

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

Day of the Month

Post icon  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  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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 .
Was This Post Helpful? 1
  • +
  • -

#3 FoodGood  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#4 am_alpha  Icon User is offline

  • New D.I.C Head

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

Re: Day of the Month

Posted 03 August 2009 - 08:10 PM

View PostFoodGood, 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



i have been thoroughly reading your reply..
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;
}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1