{ }codelper
negative remainderP0016

Weekday after k days

Weekdays are numbered: 0 Sunday, 1 Monday, 2 Tuesday, 3 Wednesday, 4 Thursday, 5 Friday, 6 Saturday.
The first line gives today's weekday number d, and the second line gives an integer k.
Print the weekday number (0 to 6) k days from today.
A negative k means |k| days ago, and 0 means today.

Input
0 ≤ d ≤ 6, -1,000,000,000 ≤ k ≤ 1,000,000,000
Output
one weekday number from 0 to 6
Example 1 · stdin
1
10
stdout
4
Example 2 · stdin
0
-1
stdout
6
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

The core is (d + k) % 7, but the sign of the remainder differs by language.
Python's % takes the sign of the divisor (7), so -1 % 7 is 6 and the answer is right as is.
In C, C++, Java, C#, JS, TS and Craykon, % takes the sign of the dividend (the remainder of division truncated toward zero), so -1 % 7 is -1.
So add 7 and take the remainder again: ((d + k) % 7 + 7) % 7.
Java has Math.floorMod(d + k, 7) for exactly this.
Do not fix a negative result with an absolute value — the day before Sunday (0) would become Monday (1) instead of Saturday (6).
In Craykon write ((d + k) % 7 + 7) % 7.

Pythonmain.py · 4 lines
d = int(input())k = int(input())# Python의 %는 나누는 수(7)와 같은 부호 — 늘 0~6이 나온다print((d + k) % 7)
JavaScriptmain.js · 3 lines
const [d, k] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);// JS의 %는 나뉘는 수의 부호를 따라서 -1 % 7 === -1 — 7을 더해 한 번 더 나눈다console.log((((d + k) % 7) + 7) % 7);
TypeScriptmain.ts · 3 lines
import { readFileSync } from "node:fs";const [d, k]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);console.log((((d + k) % 7) + 7) % 7);
JavaMain.java · 11 lines
import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int d = sc.nextInt();        int k = sc.nextInt();        // floorMod는 결과가 나누는 수와 같은 부호(0~6). %는 -1 % 7 == -1        System.out.println(Math.floorMod(d + k, 7));    }}
C#Main.cs · 12 lines
using System; class Program{    static void Main()    {        int d = int.Parse(Console.ReadLine().Trim());        int k = int.Parse(Console.ReadLine().Trim());        // C#의 %는 나뉘는 수의 부호를 따른다 — 7을 더해 0~6으로        Console.WriteLine(((d + k) % 7 + 7) % 7);    }}
C++main.cpp · 7 lines
#include <iostream> int main() {    int d, k;    std::cin >> d >> k;    std::cout << ((d + k) % 7 + 7) % 7 << '\n';}
Cmain.c · 9 lines
#include <stdio.h> int main(void) {    int d, k;    scanf("%d %d", &d, &k);    /* C의 %는 0 쪽으로 버린 나눗셈의 나머지라 음수가 나올 수 있다 */    printf("%d\n", ((d + k) % 7 + 7) % 7);    return 0;}
Craykonmain.cray · 4 lines
d = Console.ReadLine().ToNumber();k = Console.ReadLine().ToNumber();// 크레이콘의 %도 JS처럼 -1 % 7 == -1 — 7을 더해 한 번 더 나눈다Console.WriteLine(((d + k) % 7 + 7) % 7);