You asked:
give me 20 pesudocodes for infosys interview process provide logic for each answer. only give loops
and also give step wise explanation by applying sample input. declare a,b,i
set a=20,b=40
for i=0 i<=b i++
print i
end for
this is a sample question for us. make sure all loops qns are in this format and give me the
pseudocodes. 10 for loops, 10 while loops, 10 do while loops
Gemini Replied:
You stopped this response
You asked:
give me 30 pesudocodes for infosys interview process provide logic for each answer. only give loops
and also give step wise explanation by applying sample input. declare a,b,i
set a=20,b=40
for i=0 i<=b i++
print i
end for
this is a sample question for us. make sure all loops qns are in this format and give me the
pseudocodes. 10 for loops, 10 while loops, 10 do while loops
Gemini Replied:
Here are 30 pseudocode loop examples, formatted as requested, with logic and step-by-step
explanations.
10 'For' Loop Pseudocodes
Exported via AI Exporter 1 / 26 页
1. Print Numbers from 'a' to 'b'
Pseudocode:
declare a, b, i
set a=5, b=10
for i=a i<=b i++
print i
end for
Logic: This loop iterates from a starting value a up to and including an ending value b ,
printing the value of i in each iteration.
Step-wise Explanation (Sample Input: a=5, b=10):
Step 1: i is set to a (5). Is 5 <= 10 ? Yes. Print 5. i becomes 6.
Step 2: Is 6 <= 10 ? Yes. Print 6. i becomes 7.
Step 3: Is 7 <= 10 ? Yes. Print 7. i becomes 8.
Step 4: Is 8 <= 10 ? Yes. Print 8. i becomes 9.
Step 5: Is 9 <= 10 ? Yes. Print 9. i becomes 10.
Step 6: Is 10 <= 10 ? Yes. Print 10. i becomes 11.
Step 7: Is 11 <= 10 ? No. Loop terminates.
Output: 5 6 7 8 9 10
2. Sum of First 'N' Natural Numbers
Pseudocode:
declare N, i, sum
set N=5, sum=0
for i=1 i<=N i++
sum = sum + i
end for
print sum
Logic: This loop iterates from 1 to N . In each iteration, it adds the current value of i to the
sum variable. Finally, it prints the total sum.
Step-wise Explanation (Sample Input: N=5):
Init: sum = 0 .
Step 1: i=1 . Is 1 <= 5 ? Yes. sum = 0 + 1 = 1 . i becomes 2.
Exported via AI Exporter 2 / 26 页
Step 2: i=2 . Is 2 <= 5 ? Yes. sum = 1 + 2 = 3 . i becomes 3.
Step 3: i=3 . Is 3 <= 5 ? Yes. sum = 3 + 3 = 6 . i becomes 4.
Step 4: i=4 . Is 4 <= 5 ? Yes. sum = 6 + 4 = 10 . i becomes 5.
Step 5: i=5 . Is 5 <= 5 ? Yes. sum = 10 + 5 = 15 . i becomes 6.
Step 6: i=6 . Is 6 <= 5 ? No. Loop terminates.
Output: 15
3. Factorial of a Number 'N'
Pseudocode:
declare N, i, factorial
set N=4, factorial=1
for i=1 i<=N i++
factorial = factorial * i
end for
print factorial
Logic: This loop calculates the factorial of N by multiplying the factorial variable by each
number from 1 up to N .
Step-wise Explanation (Sample Input: N=4):
Init: factorial = 1 .
Step 1: i=1 . Is 1 <= 4 ? Yes. factorial = 1 * 1 = 1 . i becomes 2.
Step 2: i=2 . Is 2 <= 4 ? Yes. factorial = 1 * 2 = 2 . i becomes 3.
Step 3: i=3 . Is 3 <= 4 ? Yes. factorial = 2 * 3 = 6 . i becomes 4.
Step 4: i=4 . Is 4 <= 4 ? Yes. factorial = 6 * 4 = 24 . i becomes 5.
Step 5: i=5 . Is 5 <= 4 ? No. Loop terminates.
Output: 24
4. Print Even Numbers up to 'N'
Pseudocode:
Exported via AI Exporter 3 / 26 页
declare N, i
set N=10
for i=2 i<=N i=i+2
print i
end for
Logic: This loop starts at 2 and increments i by 2 in each step, ensuring that only even
numbers are printed. It stops when i exceeds N .
Step-wise Explanation (Sample Input: N=10):
Step 1: i=2 . Is 2 <= 10 ? Yes. Print 2. i becomes 4.
Step 2: i=4 . Is 4 <= 10 ? Yes. Print 4. i becomes 6.
Step 3: i=6 . Is 6 <= 10 ? Yes. Print 6. i becomes 8.
Step 4: i=8 . Is 8 <= 10 ? Yes. Print 8. i becomes 10.
Step 5: i=10 . Is 10 <= 10 ? Yes. Print 10. i becomes 12.
Step 6: i=12 . Is 12 <= 10 ? No. Loop terminates.
Output: 2 4 6 8 10
5. Print Numbers in Reverse from 'a' to 'b'
Pseudocode:
declare a, b, i
set a=10, b=5
for i=a i>=b i--
print i
end for
Logic: This loop starts at a higher value a and decrements ( i-- ) until it reaches a lower value
b .
Step-wise Explanation (Sample Input: a=10, b=5):
Step 1: i=10 . Is 10 >= 5 ? Yes. Print 10. i becomes 9.
Step 2: i=9 . Is 9 >= 5 ? Yes. Print 9. i becomes 8.
Step 3: i=8 . Is 8 >= 5 ? Yes. Print 8. i becomes 7.
Step 4: i=7 . Is 7 >= 5 ? Yes. Print 7. i becomes 6.
Step 5: i=6 . Is 6 >= 5 ? Yes. Print 6. i becomes 5.
Exported via AI Exporter 4 / 26 页
Step 6: i=5 . Is 5 >= 5 ? Yes. Print 5. i becomes 4.
Step 7: i=4 . Is 4 >= 5 ? No. Loop terminates.
Output: 10 9 8 7 6 5
6. Multiplication Table of 'N'
Pseudocode:
declare N, i, result
set N=5
for i=1 i<=10 i++
result = N * i
print N, "*", i, "=", result
end for
Logic: This loop iterates 10 times (from 1 to 10). In each iteration, it calculates the product of N
and the current iterator i and prints the result.
Step-wise Explanation (Sample Input: N=5):
Step 1: i=1 . result = 5 * 1 = 5 . Print "5 * 1 = 5". i becomes 2.
Step 2: i=2 . result = 5 * 2 = 10 . Print "5 * 2 = 10". i becomes 3.
... (Loop continues) ...
Step 10: i=10 . result = 5 * 10 = 50 . Print "5 * 10 = 50". i becomes 11.
Step 11: i=11 . Is 11 <= 10 ? No. Loop terminates.
Output: 5 * 1 = 5 5 * 2 = 10 ... 5 * 10 = 50
7. Calculate Power (a^b)
Pseudocode:
declare a, b, i, result
set a=3, b=4, result=1
for i=1 i<=b i++
result = result * a
end for
print result
Logic: This loop calculates a raised to the power of b . It multiplies result by a for b
times.
Step-wise Explanation (Sample Input: a=3, b=4):
Exported via AI Exporter 5 / 26 页
Init: result = 1 .
Step 1: i=1 . Is 1 <= 4 ? Yes. result = 1 * 3 = 3 . i becomes 2.
Step 2: i=2 . Is 2 <= 4 ? Yes. result = 3 * 3 = 9 . i becomes 3.
Step 3: i=3 . Is 3 <= 4 ? Yes. result = 9 * 3 = 27 . i becomes 4.
Step 4: i=4 . Is 4 <= 4 ? Yes. result = 27 * 3 = 81 . i becomes 5.
Step 5: i=5 . Is 5 <= 4 ? No. Loop terminates.
Output: 81
8. Check if a Number 'N' is Prime
Pseudocode:
declare N, i, isPrime
set N=7, isPrime=1 (1 means true)
for i=2 i < N i++
if (N % i == 0)
isPrime = 0 (0 means false)
break (exit the loop)
end if
end for
if (isPrime == 1)
print "Prime"
else
print "Not Prime"
end if
Logic: This loop checks for factors of N from 2 up to N-1 . If it finds any number i that divides
N perfectly (remainder is 0), it sets isPrime to false (0) and breaks the loop.
Step-wise Explanation (Sample Input: N=7):
Init: isPrime = 1 .
Step 1: i=2 . Is 2 < 7 ? Yes. Is 7 % 2 == 0 ? No. i becomes 3.
Step 2: i=3 . Is 3 < 7 ? Yes. Is 7 % 3 == 0 ? No. i becomes 4.
Step 3: i=4 . Is 4 < 7 ? Yes. Is 7 % 4 == 0 ? No. i becomes 5.
Step 4: i=5 . Is 5 < 7 ? Yes. Is 7 % 5 == 0 ? No. i becomes 6.
Step 5: i=6 . Is 6 < 7 ? Yes. Is 7 % 6 == 0 ? No. i becomes 7.
Step 6: i=7 . Is 7 < 7 ? No. Loop terminates.
Final Check: Is isPrime == 1 ? Yes.
Exported via AI Exporter 6 / 26 页
Output: Prime
9. First 'N' Fibonacci Numbers
Pseudocode:
declare N, i, a, b, nextTerm
set N=7, a=0, b=1
print a
print b
for i=3 i<=N i++
nextTerm = a + b
print nextTerm
a = b
b = nextTerm
end for
Logic: This loop generates the Fibonacci sequence. It starts by printing the first two terms (0 and
1). Then, it loops from 3 to N , each time calculating the nextTerm by summing the previous
two ( a and b ), printing it, and then updating a and b for the next iteration.
Step-wise Explanation (Sample Input: N=7):
Init: Print 0. Print 1. a=0 , b=1 .
Step 1: i=3 . Is 3 <= 7 ? Yes.
nextTerm = 0 + 1 = 1 .
Print 1.
a = 1 , b = 1 . i becomes 4.
Step 2: i=4 . Is 4 <= 7 ? Yes.
nextTerm = 1 + 1 = 2 .
Print 2.
a = 1 , b = 2 . i becomes 5.
Step 3: i=5 . Is 5 <= 7 ? Yes.
nextTerm = 1 + 2 = 3 .
Print 3.
a = 2 , b = 3 . i becomes 6.
Step 4: i=6 . Is 6 <= 7 ? Yes.
Exported via AI Exporter 7 / 26 页
nextTerm = 2 + 3 = 5 .
Print 5.
a = 3 , b = 5 . i becomes 7.
Step 5: i=7 . Is 7 <= 7 ? Yes.
nextTerm = 3 + 5 = 8 .
Print 8.
a = 5 , b = 8 . i becomes 8.
Step 6: i=8 . Is 8 <= 7 ? No. Loop terminates.
Output: 0 1 1 2 3 5 8
10. Print a Simple Pattern
Pseudocode:
declare N, i, j
set N=4
for i=1 i<=N i++
for j=1 j<=i j++
print "*" (on the same line)
end for
print (new line)
end for
Logic: This uses nested loops. The outer loop ( i ) controls the number of rows. The inner loop (
j ) controls the number of stars to print in each row. The inner loop runs i times.
Step-wise Explanation (Sample Input: N=4):
Outer i=1: Is 1 <= 4 ? Yes.
Inner j=1: Is 1 <= 1 ? Yes. Print "*". j becomes 2.
Inner j=2: Is 2 <= 1 ? No. Inner loop ends.
Print (new line). i becomes 2.
Outer i=2: Is 2 <= 4 ? Yes.
Inner j=1: Is 1 <= 2 ? Yes. Print "*". j becomes 2.
Inner j=2: Is 2 <= 2 ? Yes. Print "*". j becomes 3.
Inner j=3: Is 3 <= 2 ? No. Inner loop ends.
Exported via AI Exporter 8 / 26 页
Print (new line). i becomes 3.
Outer i=3: (Prints 3 stars) ... i becomes 4.
Outer i=4: (Prints 4 stars) ... i becomes 5.
Outer i=5: Is 5 <= 4 ? No. Loop terminates.
Output: * **
10 'While' Loop Pseudocodes
11. Sum of Digits of a Number
Pseudocode:
declare num, digit, sum
set num=123, sum=0
while (num > 0)
digit = num % 10
sum = sum + digit
num = num / 10 (integer division)
end while
print sum
Logic: This loop repeatedly extracts the last digit of num using the modulo operator ( % 10 ). It
adds this digit to sum and then removes the last digit from num using integer division ( / 10 ).
The loop continues as long as num is greater than 0.
Step-wise Explanation (Sample Input: num=123):
Init: sum = 0 .
Iteration 1: Is 123 > 0 ? Yes.
digit = 123 % 10 = 3 .
sum = 0 + 3 = 3 .
num = 123 / 10 = 12 .
Iteration 2: Is 12 > 0 ? Yes.
digit = 12 % 10 = 2 .
sum = 3 + 2 = 5 .
Exported via AI Exporter 9 / 26 页
num = 12 / 10 = 1 .
Iteration 3: Is 1 > 0 ? Yes.
digit = 1 % 10 = 1 .
sum = 5 + 1 = 6 .
num = 1 / 10 = 0 .
Iteration 4: Is 0 > 0 ? No. Loop terminates.
Output: 6
12. Reverse a Number
Pseudocode:
declare num, digit, reverse
set num=123, reverse=0
while (num > 0)
digit = num % 10
reverse = (reverse * 10) + digit
num = num / 10 (integer division)
end while
print reverse
Logic: This loop builds the reversed number. It extracts the last digit of num and appends it to
the reverse variable by first multiplying reverse by 10.
Step-wise Explanation (Sample Input: num=123):
Init: reverse = 0 .
Iteration 1: Is 123 > 0 ? Yes.
digit = 123 % 10 = 3 .
reverse = (0 * 10) + 3 = 3 .
num = 123 / 10 = 12 .
Iteration 2: Is 12 > 0 ? Yes.
digit = 12 % 10 = 2 .
reverse = (3 * 10) + 2 = 32 .
num = 12 / 10 = 1 .
Iteration 3: Is 1 > 0 ? Yes.
Exported via AI Exporter 10 / 26 页
digit = 1 % 10 = 1 .
reverse = (32 * 10) + 1 = 321 .
num = 1 / 10 = 0 .
Iteration 4: Is 0 > 0 ? No. Loop terminates.
Output: 321
13. Count the Number of Digits
Pseudocode:
declare num, count
set num=789, count=0
while (num > 0)
num = num / 10 (integer division)
count = count + 1
end while
print count
Logic: This loop repeatedly divides the number by 10, removing the last digit. For every
successful division (where num was > 0), it increments the count .
Step-wise Explanation (Sample Input: num=789):
Init: count = 0 .
Iteration 1: Is 789 > 0 ? Yes.
num = 789 / 10 = 78 .
count = 0 + 1 = 1 .
Iteration 2: Is 78 > 0 ? Yes.
num = 78 / 10 = 7 .
count = 1 + 1 = 2 .
Iteration 3: Is 7 > 0 ? Yes.
num = 7 / 10 = 0 .
count = 2 + 1 = 3 .
Iteration 4: Is 0 > 0 ? No. Loop terminates.
Output: 3
Exported via AI Exporter 11 / 26 页
14. Check for Palindrome Number
Pseudocode:
declare num, digit, reverse, original
set num=121, reverse=0
set original = num
while (num > 0)
digit = num % 10
reverse = (reverse * 10) + digit
num = num / 10
end while
if (original == reverse)
print "Palindrome"
else
print "Not Palindrome"
end if
Logic: This code first reverses a number (see #12) while storing the original value. It then
compares the original number with the reverse . If they are equal, it's a palindrome.
Step-wise Explanation (Sample Input: num=121):
Init: original = 121 , reverse = 0 .
Iteration 1: num=121 . digit=1 . reverse=1 . num=12 .
Iteration 2: num=12 . digit=2 . reverse=12 . num=1 .
Iteration 3: num=1 . digit=1 . reverse=121 . num=0 .
Iteration 4: num=0 . Loop terminates.
Final Check: Is original (121) == reverse (121)? Yes.
Output: Palindrome
15. GCD of Two Numbers (Euclidean Algorithm)
Pseudocode:
declare a, b, temp
set a=48, b=18
while (b > 0)
temp = b
b = a % b
a = temp
end while
print a
Exported via AI Exporter 12 / 26 页
Logic: This loop implements the Euclidean algorithm. It repeatedly replaces a with b and b
with the remainder of a / b , until b becomes 0. The GCD is the value of a at the end.
Step-wise Explanation (Sample Input: a=48, b=18):
Iteration 1: Is 18 > 0 ? Yes.
temp = 18 .
b = 48 % 18 = 12 .
a = 18 .
Iteration 2: Is 12 > 0 ? Yes.
temp = 12 .
b = 18 % 12 = 6 .
a = 12 .
Iteration 3: Is 6 > 0 ? Yes.
temp = 6 .
b = 12 % 6 = 0 .
a = 6 .
Iteration 4: Is 0 > 0 ? No. Loop terminates.
Output: 6
16. Simple Countdown
Pseudocode:
declare i
set i=5
while (i > 0)
print i
i = i - 1
end while
print "Go!"
Logic: A simple countdown loop that prints the value of i and then decrements it, stopping
when i is no longer greater than 0.
Step-wise Explanation (Sample Input: i=5):
Iteration 1: Is 5 > 0 ? Yes. Print 5. i becomes 4.
Exported via AI Exporter 13 / 26 页
Iteration 2: Is 4 > 0 ? Yes. Print 4. i becomes 3.
Iteration 3: Is 3 > 0 ? Yes. Print 3. i becomes 2.
Iteration 4: Is 2 > 0 ? Yes. Print 2. i becomes 1.
Iteration 5: Is 1 > 0 ? Yes. Print 1. i becomes 0.
Iteration 6: Is 0 > 0 ? No. Loop terminates.
Final: Print "Go!".
Output: 5 4 3 2 1 Go!
17. Find First Multiple of 7 after 'a'
Pseudocode:
declare a
set a=10
while (a % 7 != 0)
a = a + 1
end while
print a
Logic: This loop keeps incrementing a by 1 as long as it is not perfectly divisible by 7. When the
loop stops, a holds the first number (starting from the initial a ) that is a multiple of 7.
Step-wise Explanation (Sample Input: a=10):
Iteration 1: Is 10 % 7 != 0 ? (3 != 0). Yes. a becomes 11.
Iteration 2: Is 11 % 7 != 0 ? (4 != 0). Yes. a becomes 12.
Iteration 3: Is 12 % 7 != 0 ? (5 != 0). Yes. a becomes 13.
Iteration 4: Is 13 % 7 != 0 ? (6 != 0). Yes. a becomes 14.
Iteration 5: Is 14 % 7 != 0 ? (0 != 0). No. Loop terminates.
Output: 14
18. Keep Doubling 'a' until it is > 'b'
Pseudocode:
Exported via AI Exporter 14 / 26 页
declare a, b
set a=3, b=50
while (a <= b)
print a
a = a * 2
end while
Logic: This loop prints the current value of a and then doubles it. It continues as long as a is
less than or equal to b .
Step-wise Explanation (Sample Input: a=3, b=50):
Iteration 1: Is 3 <= 50 ? Yes. Print 3. a = 3 * 2 = 6 .
Iteration 2: Is 6 <= 50 ? Yes. Print 6. a = 6 * 2 = 12 .
Iteration 3: Is 12 <= 50 ? Yes. Print 12. a = 12 * 2 = 24 .
Iteration 4: Is 24 <= 50 ? Yes. Print 24. a = 24 * 2 = 48 .
Iteration 5: Is 48 <= 50 ? Yes. Print 48. a = 48 * 2 = 96 .
Iteration 6: Is 96 <= 50 ? No. Loop terminates.
Output: 3 6 12 24 48
19. Armstrong Number Check (e.g., 153)
Pseudocode:
declare num, digit, sum, original
set num=153, sum=0
set original = num
while (num > 0)
digit = num % 10
sum = sum + (digit * digit * digit)
num = num / 10
end while
if (original == sum)
print "Armstrong"
else
print "Not Armstrong"
end if
Logic: An Armstrong number is a number that equals the sum of its digits, each raised to the
power of the number of digits (here, 3). This loop extracts each digit, cubes it, and adds it to sum
. Finally, it compares sum to the original number.
Step-wise Explanation (Sample Input: num=153):
Init: original = 153 , sum = 0 .
Exported via AI Exporter 15 / 26 页
Iteration 1: num=153 .
digit = 3 .
sum = 0 + (3*3*3) = 27 .
num = 15 .
Iteration 2: num=15 .
digit = 5 .
sum = 27 + (5*5*5) = 27 + 125 = 152 .
num = 1 .
Iteration 3: num=1 .
digit = 1 .
sum = 152 + (1*1*1) = 152 + 1 = 153 .
num = 0 .
Iteration 4: num=0 . Loop terminates.
Final Check: Is original (153) == sum (153)? Yes.
Output: Armstrong
20. Sum numbers until sum > 50
Pseudocode:
declare sum, i
set sum=0, i=1
while (sum <= 50)
sum = sum + i
print "adding", i, "sum is", sum
i = i + 1
end while
Logic: This loop keeps adding consecutive numbers (1, 2, 3...) to sum until the sum becomes
greater than 50.
Step-wise Explanation (Sample Input: sum=0, i=1):
Iteration 1: Is 0 <= 50 ? Yes. sum = 0 + 1 = 1 . Print "adding 1 sum is 1". i becomes 2.
Iteration 2: Is 1 <= 50 ? Yes. sum = 1 + 2 = 3 . Print "adding 2 sum is 3". i becomes 3.
Exported via AI Exporter 16 / 26 页
... (Loop continues) ...
Iteration 9: Is 36 <= 50 ? Yes. sum = 36 + 9 = 45 . Print "adding 9 sum is 45". i
becomes 10.
Iteration 10: Is 45 <= 50 ? Yes. sum = 45 + 10 = 55 . Print "adding 10 sum is 55". i
becomes 11.
Iteration 11: Is 55 <= 50 ? No. Loop terminates.
Output: (Prints all steps, ending with "adding 10 sum is 55")
10 'Do-While' Loop Pseudocodes
21. Simple Counter (Guaranteed to run once)
Pseudocode:
declare i
set i=1
do
print i
i = i + 1
while (i <= 5)
end do
Logic: This loop first executes the body (prints i and increments it) and then checks the
condition. It will continue to run as long as i is less than or equal to 5.
Step-wise Explanation (Sample Input: i=1):
Iteration 1:
(Body) Print 1. i becomes 2.
(Check) Is 2 <= 5 ? Yes.
Iteration 2:
(Body) Print 2. i becomes 3.
(Check) Is 3 <= 5 ? Yes.
Iteration 3:
(Body) Print 3. i becomes 4.
(Check) Is 4 <= 5 ? Yes.
Exported via AI Exporter 17 / 26 页
Iteration 4:
(Body) Print 4. i becomes 5.
(Check) Is 5 <= 5 ? Yes.
Iteration 5:
(Body) Print 5. i becomes 6.
(Check) Is 6 <= 5 ? No. Loop terminates.
Output: 1 2 3 4 5
22. Loop that Runs Only Once
Pseudocode:
declare a
set a=10
do
print a
a = a + 1
while (a < 5)
end do
Logic: The do-while loop always executes its body at least once. Here, it prints 10. Then, it
checks the condition. Since 11 (the new a ) is not less than 5, the loop terminates.
Step-wise Explanation (Sample Input: a=10):
Iteration 1:
(Body) Print 10. a becomes 11.
(Check) Is 11 < 5 ? No. Loop terminates.
Output: 10
23. Keep Adding to Sum until it Exceeds 20
Pseudocode:
declare sum, a
set sum=0, a=5
do
sum = sum + a
print sum
while (sum <= 20)
end do
Exported via AI Exporter 18 / 26 页
Logic: This loop will repeatedly add 5 to sum and print it. The loop continues as long as sum is
20 or less.
Step-wise Explanation (Sample Input: sum=0, a=5):
Iteration 1:
(Body) sum = 0 + 5 = 5 . Print 5.
(Check) Is 5 <= 20 ? Yes.
Iteration 2:
(Body) sum = 5 + 5 = 10 . Print 10.
(Check) Is 10 <= 20 ? Yes.
Iteration 3:
(Body) sum = 10 + 5 = 15 . Print 15.
(Check) Is 15 <= 20 ? Yes.
Iteration 4:
(Body) sum = 15 + 5 = 20 . Print 20.
(Check) Is 20 <= 20 ? Yes.
Iteration 5:
(Body) sum = 20 + 5 = 25 . Print 25.
(Check) Is 25 <= 20 ? No. Loop terminates.
Output: 5 10 15 20 25
24. Simulated Password Entry (Asks at least once)
Pseudocode:
declare input, password
set password=123, input=0 (simulating user input)
do
print "Enter password:"
set input = 456 (simulate wrong input first)
if (input != password)
print "Wrong!"
end if
while (input != password)
print "Correct!"
Exported via AI Exporter 19 / 26 页
Logic: This simulates a password prompt that must run at least once. We'll simulate the user
entering the wrong password, then the right one. (Note: In a real program, the set input would
be a read command).
Step-wise Explanation (Simulated):
Iteration 1:
(Body) Print "Enter password:". input is set to 456. Is 456 != 123 ? Yes. Print
"Wrong!".
(Check) Is 456 != 123 ? Yes.
Iteration 2:
(Body) Print "Enter password:". We will simulate input is now set to 123. Is
123 != 123 ? No.
(Check) Is 123 != 123 ? No. Loop terminates.
Final: Print "Correct!".
Output: Enter password: Wrong! Enter password: Correct!
25. Keep Dividing 'N' by 2
Pseudocode:
declare N
set N=40
do
N = N / 2 (integer division)
print N
while (N > 1)
end do
Logic: This loop repeatedly divides N by 2 and prints the result. It continues as long as N is
greater than 1.
Step-wise Explanation (Sample Input: N=40):
Iteration 1:
(Body) N = 40 / 2 = 20 . Print 20.
(Check) Is 20 > 1 ? Yes.
Iteration 2:
(Body) N = 20 / 2 = 10 . Print 10.
Exported via AI Exporter 20 / 26 页
(Check) Is 10 > 1 ? Yes.
Iteration 3:
(Body) N = 10 / 2 = 5 . Print 5.
(Check) Is 5 > 1 ? Yes.
Iteration 4:
(Body) N = 5 / 2 = 2 . Print 2.
(Check) Is 2 > 1 ? Yes.
Iteration 5:
(Body) N = 2 / 2 = 1 . Print 1.
(Check) Is 1 > 1 ? No. Loop terminates.
Output: 20 10 5 2 1
26. Factorial (Do-While version)
Pseudocode:
declare N, i, factorial
set N=4, factorial=1, i=1
do
factorial = factorial * i
i = i + 1
while (i <= N)
end do
print factorial
Logic: A do-while version of factorial. It multiplies factorial by i and then increments i ,
checking the condition afterward. (Note: This correctly handles N=0, as it runs once,
fact=1*1=1 , i=2 , 2<=0 is false. Wait, no. It fails N=0. Let's adjust.)
Pseudocode (Corrected for N=0):
declare N, factorial
set N=4, factorial=1
if (N > 0)
do
factorial = factorial * N
N = N - 1
while (N > 0)
end do
end if
print factorial
Exported via AI Exporter 21 / 26 页
Logic: This version multiplies down from N . The do-while ensures it runs at least once if N >
0. If N=0, the loop is skipped and factorial=1 is printed.
Step-wise Explanation (Sample Input: N=4):
Init: Is 4 > 0 ? Yes.
Iteration 1:
(Body) factorial = 1 * 4 = 4 . N becomes 3.
(Check) Is 3 > 0 ? Yes.
Iteration 2:
(Body) factorial = 4 * 3 = 12 . N becomes 2.
(Check) Is 2 > 0 ? Yes.
Iteration 3:
(Body) factorial = 12 * 2 = 24 . N becomes 1.
(Check) Is 1 > 0 ? Yes.
Iteration 4:
(Body) factorial = 24 * 1 = 24 . N becomes 0.
(Check) Is 0 > 0 ? No. Loop terminates.
Final: Print 24.
Output: 24
27. Generate Sequence (1, 4, 7, 10...)
Pseudocode:
declare i
set i=1
do
print i
i = i + 3
while (i < 15)
end do
Logic: This loop prints i and then adds 3 to it. It continues as long as i is less than 15.
Step-wise Explanation (Sample Input: i=1):
Exported via AI Exporter 22 / 26 页
Iteration 1:
(Body) Print 1. i becomes 4.
(Check) Is 4 < 15 ? Yes.
Iteration 2:
(Body) Print 4. i becomes 7.
(Check) Is 7 < 15 ? Yes.
Iteration 3:
(Body) Print 7. i becomes 10.
(Check) Is 10 < 15 ? Yes.
Iteration 4:
(Body) Print 10. i becomes 13.
(Check) Is 13 < 15 ? Yes.
Iteration 5:
(Body) Print 13. i becomes 16.
(Check) Is 16 < 15 ? No. Loop terminates.
Output: 1 4 7 10 13
28. Find first number > 'a' divisible by 3 and 5
Pseudocode:
declare a
set a=20
do
a = a + 1
while ( (a % 3 != 0) OR (a % 5 != 0) )
end do
print a
Logic: This loop starts incrementing a by 1. It continues to loop as long as a is not divisible by
3 OR not divisible by 5. It stops only when a is divisible by both.
Step-wise Explanation (Sample Input: a=20):
Iteration 1: (Body) a becomes 21. (Check) Is (21%3 != 0) (False) OR (21%5 != 0)
(True)? Yes (False OR True is True).
Exported via AI Exporter 23 / 26 页
Iteration 2: (Body) a becomes 22. (Check) Is (22%3 != 0) (True) OR (22%5 != 0)
(True)? Yes.
... (Loop continues for 23, 24, 25, 26, 27, 28, 29) ...
Iteration 10: (Body) a becomes 30. (Check) Is (30%3 != 0) (False) OR (30%5 != 0)
(False)? No (False OR False is False).
Final: Loop terminates. Print 30.
Output: 30
29. Simple Menu Simulation
Pseudocode:
declare choice
set choice = 1 (must be non-3 to start)
do
print "1. Start"
print "2. Stop"
print "3. Exit"
set choice = 3 (simulating user input for 'Exit')
while (choice != 3)
end do
print "Exiting..."
Logic: A do-while is perfect for menus because you must show the menu at least once. The
loop continues until the user's choice is 3.
Step-wise Explanation (Simulated):
Iteration 1:
(Body) Prints "1. Start", "2. Stop", "3. Exit".
We simulate user input by setting choice = 3 .
(Check) Is 3 != 3 ? No. Loop terminates.
Final: Print "Exiting...".
Output:
1. Start
2. Stop
3. Exit Exiting...
30. Print Even Numbers Backwards from 'a'
Exported via AI Exporter 24 / 26 页
Pseudocode:
declare a
set a=11
// Ensure 'a' is even first
if (a % 2 != 0)
a = a - 1
end if
do
print a
a = a - 2
while (a >= 0)
end do
Logic: This loop first ensures a is an even number. Then, it enters a do-while loop, printing
the even number and decrementing by 2. It continues as long as a is 0 or positive.
Step-wise Explanation (Sample Input: a=11):
Init: Is 11 % 2 != 0 ? Yes. a becomes 10.
Iteration 1:
(Body) Print 10. a becomes 8.
(Check) Is 8 >= 0 ? Yes.
Iteration 2:
(Body) Print 8. a becomes 6.
(Check) Is 6 >= 0 ? Yes.
Iteration 3:
(Body) Print 6. a becomes 4.
(Check) Is 4 >= 0 ? Yes.
Iteration 4:
(Body) Print 4. a becomes 2.
(Check) Is 2 >= 0 ? Yes.
Iteration 5:
(Body) Print 2. a becomes 0.
(Check) Is 0 >= 0 ? Yes.
Exported via AI Exporter 25 / 26 页
Iteration 6:
(Body) Print 0. a becomes -2.
(Check) Is -2 >= 0 ? No. Loop terminates.
Output: 10 8 6 4 2 0
Exported via AI Exporter 26 / 26 页