π Day 25 of My Automation Journey β Prime Numbers, Reverse Logic & Loops π
Today, instead of just writing programs, I focused on understanding the βWHYβ behind each logic. Letβs break everything down step by step π πΉ 1. Divisors Logic β Step-by-Step Understanding π» Pro...

Source: DEV Community
Today, instead of just writing programs, I focused on understanding the βWHYβ behind each logic. Letβs break everything down step by step π πΉ 1. Divisors Logic β Step-by-Step Understanding π» Program: int user = 15; int i = 0; while (i <= user) { if (i % user == 0) System.out.println(i); i++; } π§ Logic Explained: i % user == 0 means: π βIs i divisible by user?β Loop runs from 0 β 15 Letβs trace: i value i % 15 Condition 0 0 β
Print 1β14 Not 0 β Skip 15 0 β
Print π€ Output: 0 15 β οΈ Important Insight: π This program is actually finding multiples of 15 within range, NOT divisors. β
Correct Divisor Logic should be: if (user % i == 0) πΉ 2. Prime Number Logic β Deep Explanation π» Program: int user = 3; boolean status = true; int i = 2; while(i < user) { if(user % i == 0) { status = false; break; } i++; } if (status==true) System.out.println("Prime"); else System.out.println("Not a prime"); π§ What is a Prime Number? A number is prime if: It has exactly 2 factors β 1 and itself οΏ½