Back to articles
πŸš€ Day 25 of My Automation Journey – Prime Numbers, Reverse Logic & Loops πŸ”

πŸš€ Day 25 of My Automation Journey – Prime Numbers, Reverse Logic & Loops πŸ”

via Dev.tobala d kaveri

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 e

Continue reading on Dev.to

Opens in a new tab

Read Full Article
0 views

Related Articles