Prime Number | Do/While Loop | Lecture #81 #numbersystem #armstrong #coding #logical #developer
π Purpose of This Program This program checks whether a given number is a prime number using a do-while loop. π§© Variable Explanation num β Stores the number entered by the user. rem β Declared but not used in the program. temp = 1 β Used as a flag variable. 1 means the number is assumed to be prime. 0 means the number is not prime. c = 2 β Used to check divisibility starting from 2. π₯ Step-by-Step Working 1οΈβ£ User Input The program asks the user to enter a number. That number is stored in the variable num. 2οΈβ£ Do-While Loop Starts The loop checks whether the number is divisible by values starting from 2 up to num/2. Why up to num/2? Because a number cannot have a factor greater than half of itself (except the number itself). 3οΈβ£ Inside the Loop It checks: π If the number is divisible by c If yes: It means the number has another factor. So it is not prime. temp becomes 0. The loop stops using break. If not: It increases c by 1. The loop continues. 4οΈβ£ Loop Condition The loop continues while: c is less than or equal to num/2 5οΈβ£ Final Check After the loop ends: If temp is still 1 β No number divided it β It is Prime If temp is 0 β It was divisible β It is Not Prime β Important Observation (Small Mistake in Logic) If the user enters: 0 or 1 The program will incorrectly say it is Prime, because the loop will not run properly. But actually: 0 is NOT prime 1 is NOT prime So a condition should be added to handle numbers less than or equal to 1. π― Summary β The program uses a do-while loop β It checks divisibility from 2 to num/2 β Uses a flag variable to decide prime or not β Works correctly for numbers greater than 1 β Small improvement needed for 0 and 1 #coding #hindi #maths #numbers #logical #cprogramming #developer #study #concept #iteration #controlstatements
Download
1 formatsVideo Formats
Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.