Menu
Coddy logo textTech

Triple Division

Lesson 24 of 25 in Coddy's Coding Problems: Volume 2 course.

Given a three-digit number, your task is to change only one digit from the number, so that the new number is divisible by 3, and it is the largest 3-digit number you can get from changing only one digit from the initial number

challenge icon

Challenge

Hard

Write a program that inputs a natural three-digit number from standard input. Modify just one digit to produce the largest possible three-digit number divisible by 3, while changing only a single digit from the original number

Input
123

Output
723

Input
999

Output
996

Explanation
In this first example, 423 is also divisible by 3, but it's not the largest number we can get. In the second example, 999 is the largest possible three-digit number, but we need to modify one digit so we change the least significant digit and we get the number 996

Try it yourself

#include <stdio.h>

int main() {
    // Write code here
    return 0;
}

All lessons in Coding Problems: Volume 2