can anybody guys tell me why we are using amount=remaining in 13th line? and why we are using print statement in the outside ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

can anybody guys tell me why we are using amount=remaining in 13th line? and why we are using print statement in the outside ?

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); int remaining=amount; //your code goes here for(int i=0;i<3;i++){ int payment=((amount)/10); remaining=amount-payment; amount=remaining; } System.out.println(remaining); } }

15th Aug 2022, 8:16 AM
MATHIVANAN T A
MATHIVANAN T A - avatar
2 Answers
+ 4
MATHIVANAN T A The variable - amount is being re-assigned the value of remaining each iteration of the loop. The print statement is outside the loop so you only get a visual result once the loop has finished.
15th Aug 2022, 9:33 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
You can eliminate the use of remaining variable. Directly use amount, instead. amount = amount - payment; //even replace payment with by amount/10.
15th Aug 2022, 11:41 AM
Jayakrishna 🇮🇳