Revision- 7
Steps
Start by initializing the result variable with the value 1, representing the base case.
Use a loop structure, such as a for loop, to iterate N times. Set up the loop to run from 1 to N inclusive.
Within each iteration, multiply the current value of result by 2, updating the result variable.
Continue loop execution until the loop reaches the Nth iteration.
After completing the loop, the value of result will hold the Nth power of 2.
Steps
Start by obtaining the two numbers, A and B, for which you want to find the GCD.
Compare the values of A and B to identify the smaller number. This will be crucial in determining the range of the iteration.
Use a loop structure, such as a for loop, to iterate from 1 to the smaller number inclusive.
Within each iteration, check if the current number divides both A and B without leaving a remainder. Use the modulus operator (%) to perform this check.
If the current number divides both A and B without leaving a remainder, update a variable, let's call it gcd, to store the largest divisor found so far.
Continue the loop until it reaches the end of the iteration range.
After completing the loop, the value of gcd will hold the GCD of the original two numbers.
Steps
Start by obtaining the number N for which you want to determine if it is prime.
Set up a loop structure, such as a for loop, to iterate from 2 to N - 1.
Within each iteration, check if the current number divides N without leaving a remainder. Use the modulus operator (%) to perform this check.
If a divisor is found, the number is not prime. Exit the loop and conclude that the number is not prime. If no divisor is found after the entire iteration, the number is prime.
Output the result indicating whether the number is prime or not.
Finding Prime Numbers from 1 to N Using Iterations
Steps
Input the value of N, which represents the upper limit of the range.
Iterate through each number from 2 to N:
For each number, check if it is divisible by any number from 2 to the square root of the number (inclusive). If there is a divisor greater than square root of number then there will also be a divisor less than the square root of the number. So, iterating till square root of number will be sufficient for validating if a number is prime or not
If it is divisible by any number other than 1 and itself, it is not a prime number. Move to the next number.
If it is not divisible by any number other than 1 and itself, it is a prime number. Output the number as one of the prime numbers in the range.