Loops Cheat Sheet
Loops Cheat Sheet
where necessary:
---
Pseudocode:
Input: n
sum = 0
for i = 1 to n
print i
sum += i
print "Sum =", sum
2. Cube of Numbers up to N
Solution: Use a loop to calculate i^3 for i from 1 to n.
Pseudocode:
Input: n
for i = 1 to n
print "Number:", i, "Cube:", i^3
3. Multiplication Table
Solution: Iterate from 1 to 10, multiplying the given number.
Pseudocode:
Input: num
for i = 1 to 10
print num, "x", i, "=", num * i
Input: n
sum = 0
count = 0
i=1
while count < n
if i % 2 != 0
print i
sum += i
count += 1
i += 1
print "Sum =", sum
Input: rows
for i = 1 to rows
print '*' i times
6. Fibonacci Series
Solution: Use a loop to calculate Fibonacci terms.
Pseudocode:
Input: n
a = 0, b = 1
print a, b
for i = 3 to n
c=a+b
print c
a = b, b = c
---
Extra Questions
1. Factorial Calculation
Solution: Multiply numbers from 1 to n.
Pseudocode:
Input: n
factorial = 1
for i = 1 to n
factorial *= i
print "Factorial =", factorial
Input: num
sum = 0
while num > 0
sum += num % 10
num = num // 10
print "Sum =", sum
3. Reverse a Number
Pseudocode:
Input: num
reverse = 0
while num > 0
digit = num % 10
reverse = reverse * 10 + digit
num = num // 10
print "Reversed =", reverse
4. Voting System
Pseudocode:
---
Medium-Level Questions
Input: n
while True
n++
count = count occurrences of '3' in n
if count >= 3
print n
break
2. Non-Hypotenuse Number
Solution: Check if the number cannot be expressed as the sum of two squares.
Pseudocode:
Input: n
is_hypotenuse = False
for a = 1 to sqrt(n)
for b = a+1 to sqrt(n)
if a^2 + b^2 == n^2
is_hypotenuse = True
print "YES" if not is_hypotenuse else "NO"
Input: n
binary = ""
while n > 0
binary = (n % 2) + binary
n = n // 2
print "Binary =", binary
---
This summary includes the main problem types and solutions. Let me know which you'd like
further details on!
Below is a detailed approach and pseudocode for all the important methods mentioned across
the questions:
---
Approach:
Pseudocode:
Input: n
sum = 0
for i = 1 to n
print i
sum = sum + i
print "Sum of first", n, "natural numbers =", sum
---
Approach:
Pseudocode:
Input: n
for i = 1 to n
cube = i * i * i
print "Number:", i, "Cube:", cube
---
Approach:
Pseudocode:
Input: num
for i = 1 to 10
result = num * i
print num, "x", i, "=", result
---
Approach:
Pseudocode:
Input: n
sum = 0
count = 0
i=1
while count < n
if i % 2 != 0
print i
sum = sum + i
count = count + 1
i=i+1
print "Sum of first", n, "odd numbers =", sum
---
5. Fibonacci Series
Approach:
Use a loop to calculate the next Fibonacci number as the sum of the previous two.
Pseudocode:
Input: n
a = 0, b = 1
print a, b
for i = 3 to n
c=a+b
print c
a=b
b=c
---
6. Factorial of a Number
Approach:
Initialize factorial as 1.
Pseudocode:
Input: n
factorial = 1
for i = 1 to n
factorial = factorial * i
print "Factorial of", n, "=", factorial
---
Approach:
Pseudocode:
Input: num
sum = 0
while num > 0
digit = num % 10
sum = sum + digit
num = num // 10
print "Sum of digits =", sum
---
8. Reverse a Number
Approach:
Extract digits from the number one by one and form the reverse using multiplication and
addition.
Pseudocode:
Input: num
reverse = 0
while num > 0
digit = num % 10
reverse = reverse * 10 + digit
num = num // 10
print "Reversed number =", reverse
---
Approach:
Pseudocode:
Input: num
is_prime = True
if num <= 1
is_prime = False
else
for i = 2 to sqrt(num)
if num % i == 0
is_prime = False
break
if is_prime
print num, "is a prime number"
else
print num, "is not a prime number"
---
Approach:
Use modulo % to get binary digits and reduce the number using division /.
Pseudocode:
Input: num
binary = ""
while num > 0
binary = (num % 2) + binary
num = num // 2
print "Binary =", binary
---
Approach:
Pseudocode:
---
Approach:
Use counters to track votes for each candidate and invalid votes.
Pseudocode:
Approach:
A number is an Armstrong number if the sum of the cubes of its digits equals the number itself.
Pseudocode:
Input: num
sum = 0
temp = num
while temp > 0
digit = temp % 10
sum = sum + (digit^3)
temp = temp // 10
if sum == num
print num, "is an Armstrong number"
else
print num, "is not an Armstrong number"
---
These methods cover most of the essential programming constructs like loops, conditions, and
mathematical operations. Let me know if you'd like any specific method explained further or
expanded!