Reputation: 597
I just want to ask if how do you determine
Enter a number: 14
Then the date to day is Sunday.
the above demonstration is from 1-7=monday-sunday then if the value is 8 then its monday again...but how could I determine it?using loops and modulo?...
I appreciate any comments for helping me...
any language are ok, I just want to know the algorithm with it...
Upvotes: 0
Views: 144
Reputation: 962
import java.util.Scanner;
public class TestMain {
private static final String[] DAYS = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thusday", "Friday", "Saturday" };
public static void main(final String[] args) {
System.out.print("Enter a number: ");
final Scanner scanner = new Scanner(System.in);
final int n = scanner.nextInt();
System.out.print("Then the date to day is " + DAYS[n % 7]);
}
}
Upvotes: 1
Reputation: 294
datArr= new Array ('Sun', 'Mon', 'Tue',Wen', 'Thu, 'Fri', 'Sat');
day =datArr[(daynum%7)];
If Sunday gets treated as day zero there is no need to subtract 1 after the modulo. Of course most of the solutions will fail with zero dive if an actual value of zero is encountered.
Upvotes: 1
Reputation: 8986
string day;
int inputNumber;
if(inputNumber%7 == 0)
day = "monday"; // or sunday if you prefer :p
else if(inputNumber%7 == 1)
day = tuesday
...
...
...
Upvotes: 1
Reputation: 5157
Get modulo 7 of the int value thats passed in and make a switch statement for it.
switch:
case 1:
Sunday
case 2:
Monday
// and so on
Upvotes: 1
Reputation: 101456
In C++, int day = (i-1) % 7;
will give you a zero-based offset. Here's sample code:
#include <iostream>
#include<string>
using namespace std;
int main()
{
static const std::string days [] =
{
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
static const size_t num_days = sizeof(days)/sizeof(days[0]);
for( int i = 1; i <= 31; ++i )
{
int day = (i-1) % 7;
cout << day << " = " << days[day] << "\n";
}
}
Upvotes: 1
Reputation: 49919
You can just use module
Module got to 0 so it would be something like
<?php
$value = 13;
$daynames = array("mon", "tues", "wed", "thur", "fri", "sat", "sun");
// The + 1 is because % is 0, and you want 1-7
echo (($value % 7) + 1) . "day of the week";
echo "the day is " . $daynames[($value % 7)];
?>
Example: http://codepad.org/wbGuMI06
Upvotes: 2
Reputation: 490058
Taking the number modulo 7 would seem like the obvious possibility.
Upvotes: 1
Reputation: 82559
You can use a switch statement and modulo arithmetic to do this.
The first step to learning computer science is to learn that numbers don't usually start at 1. They start at 0. Getting this idea down will help you tremendously in the future!
// accept numbers 0-6. If 7 were to get put in, it would end up being 0 since 7 % 7 = 0.
switch(num % 7) {
case 0: return "Sunday";
case 1: return "Monday";
case 2: return "Tuesday";
/// rest here...
default: throw new IllegalArgumentException();
}
Upvotes: 1
Reputation: 12586
If you have a switch/case or if/else for the seven days you can use modulo 7 to get the input into the desired range of 7 days.
Upvotes: 1