Compound Assignment
Completed
Read an integer n. Starting from it, apply += 5, *= 3, -= 2, /= 4, %= 7 and print the value after each step on one line.
Input
One integer.
Output
Five numbers on one line.
Example
Input
10
Output
15 45 43 10 3
Editor
Java
Hints
x += 5 means x = x + 5.
You can print with print(n + " ") instead of a StringBuilder.
Solution explanation
Compound operators combine an operation with assignment. Note that % of a negative number is negative in Java.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
StringBuilder out = new StringBuilder();
n += 5;
out.append(n).append(' ');
n *= 3;
out.append(n).append(' ');
n -= 2;
out.append(n).append(' ');
n /= 4;
out.append(n).append(' ');
n %= 7;
out.append(n);
System.out.println(out);
}
}