[go: up one dir, main page]

0% found this document useful (0 votes)
3 views12 pages

Loops Cheat Sheet

The document outlines various programming problems and their solutions, categorized into levels such as basics, extra, and medium-level questions. Each problem is accompanied by a brief description and pseudocode for implementation. Key topics include natural numbers, factorials, Fibonacci series, and voting systems, among others.

Uploaded by

SHARMIN A S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Loops Cheat Sheet

The document outlines various programming problems and their solutions, categorized into levels such as basics, extra, and medium-level questions. Each problem is accompanied by a brief description and pseudocode for implementation. Key topics include natural numbers, factorials, Fibonacci series, and voting systems, among others.

Uploaded by

SHARMIN A S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Here’s a summary of the types of problems provided and their solutions along with pseudocode

where necessary:

---

Level 1 Questions (Basics of Loops and Patterns)

1. First N Natural Numbers and Their Sum


Solution: Use a for loop to print numbers and accumulate the sum.

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

4. Odd Numbers and Their Sum


Solution: Loop through numbers, check if odd, and sum them.
Pseudocode:

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

5. Patterns (Stars, Numbers, Pyramid)


Solution: Use nested loops for rows and columns.
Example for a right-angle triangle with *:

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

2. Sum of Digits of a Number


Solution: Extract digits using % and /.
Pseudocode:

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:

votes1 = 0, votes2 = 0, invalid = 0


while True
Input: vote
if vote == 0 break
if vote == 1 votes1++
else if vote == 2 votes2++
else invalid++
print results

---

Medium-Level Questions

1. Smallest Integer with Three 3's


Solution: Increment the number and check if it contains three '3's.
Pseudocode:

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"

3. Binary Conversion Without Arrays


Pseudocode:

Input: n
binary = ""
while n > 0
binary = (n % 2) + binary
n = n // 2
print "Binary =", binary

4. Secret Guess Game


Solution: Compare guessed digits against the secret number.
Pseudocode:

Input: secret, guess


correct_pos = 0, correct_digits = 0
for i = 0 to 3
if secret[i] == guess[i] correct_pos++
else if guess[i] in secret correct_digits++
print "Hint:", correct_pos, "-", correct_digits

---

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:

---

1. Print First N Natural Numbers and Their Sum

Approach:

Use a loop to print numbers from 1 to N.


Maintain a running sum of these numbers.

Pseudocode:

Input: n
sum = 0
for i = 1 to n
print i
sum = sum + i
print "Sum of first", n, "natural numbers =", sum

---

2. Calculate Cube of Numbers Up to N

Approach:

Use a loop to iterate from 1 to N.

For each number, calculate its cube as i * i * i.

Pseudocode:

Input: n
for i = 1 to n
cube = i * i * i
print "Number:", i, "Cube:", cube

---

3. Multiplication Table for a Given Number

Approach:

Use a loop to multiply the given number by integers from 1 to 10.

Print each result.

Pseudocode:
Input: num
for i = 1 to 10
result = num * i
print num, "x", i, "=", result

---

4. Odd Numbers and Their Sum

Approach:

Start from 1 and use a loop to find odd numbers.

Add each odd number to a running sum.

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:

Start with 0 and 1.

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.

Use a loop to multiply numbers from 1 to N.

Pseudocode:

Input: n
factorial = 1
for i = 1 to n
factorial = factorial * i
print "Factorial of", n, "=", factorial

---

7. Sum of Digits of a Number

Approach:

Extract digits using % 10 and reduce the number using / 10.

Add each extracted digit to the sum.

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

---

9. Check if a Number is Prime

Approach:

A number is prime if it has no divisors other than 1 and itself.

Use a loop to check divisors from 2 to √N.

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"

---

10. Decimal to Binary Conversion

Approach:

Use modulo % to get binary digits and reduce the number using division /.

Form the binary string.

Pseudocode:

Input: num
binary = ""
while num > 0
binary = (num % 2) + binary
num = num // 2
print "Binary =", binary

---

11. Secret Guess Game

Approach:

Compare the guessed number with the secret number.


Count digits that match in position and digits that exist in the secret but are in the wrong
position.

Pseudocode:

Input: secret, guess


correct_position = 0
wrong_position = 0
for i = 0 to 3
if secret[i] == guess[i]
correct_position = correct_position + 1
else if guess[i] in secret
wrong_position = wrong_position + 1
print "Hint:", correct_position, "-", wrong_position

---

12. Election Voting System

Approach:

Use counters to track votes for each candidate and invalid votes.

Continue until a vote of 0 is entered.

Pseudocode:

votes1 = 0, votes2 = 0, invalid_votes = 0


while True
Input: vote
if vote == 0
break
if vote == 1
votes1 = votes1 + 1
else if vote == 2
votes2 = votes2 + 1
else
invalid_votes = invalid_votes + 1
print "Candidate 1 votes =", votes1
print "Candidate 2 votes =", votes2
print "Invalid votes =", invalid_votes
---

13. Armstrong Number

Approach:

A number is an Armstrong number if the sum of the cubes of its digits equals the number itself.

Extract digits, cube them, and sum them.

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!

You might also like