NUMBER PROGRAMS
Print n Natural Numbers
Goal: Print numbers from 1 to n.
Approach:
• Input: Read n.
• Loop from 1 to n.
• Print each number.
Sum of n Natural Numbers
Goal: Find sum 1 + 2 + ... + n.
Approach:
• Input: Read n.
• Initialize sum = 0.
• Loop: add each number to sum.
• Print sum.
Product of n Natural Numbers
Goal: Find product 1 × 2 × ... × n.
Approach:
• Input: Read n.
• Initialize product = 1.
• Loop: multiply product by each number.
• Print product.
Check Prime Number
Goal: Check if a number is prime.
Approach:
• Input: Read number.
• If ≤ 1 → not prime.
• Loop 2 to n/2: check if any divisor exists.
• If yes → not prime.
• Else → prime.
Check Perfect Number
Goal: Sum of factors equals the number.
Approach:
• Input: Read number.
• Loop 1 to n/2, sum factors.
• If sum equals number → perfect.
Check Strong Number
Goal: Sum of factorial of digits equals number.
Approach:
• Input: Read number.
• Loop: extract digit → factorial → sum.
• If sum equals number → strong.
Check Armstrong Number
Goal: Sum of digits to the power of digits count equals number.
Approach:
• Input: Read number.
• Count digits.
• Loop: extract digit → power → sum.
• If sum equals number → Armstrong.
Sum of All Digits
Goal: Add all digits.
Approach:
• Input: Read number.
• Loop: sum digits.
• Print sum.
Reverse Number
Goal: Reverse digits.
Approach:
• Input: Read number.
• Loop: extract digit → build reverse.
• Print reverse.
Check Palindrome Number
Goal: Reverse equals original.
Approach:
• Input: Read number.
• Reverse it.
• If reverse equals original → palindrome.
Check Xylem/Phloem Number
Goal: Xylem: sum of extreme digits equals mean digits sum.
Approach:
• Input: Read number.
• Extract first & last digits.
• Sum means digits.
• Compare sums.
Check Happy Number
Goal: Sum squares of digits until single digit; ends at 1 → Happy.
Approach:
• Input: Read number.
• Loop: sum squares → repeat.
• If reaches 1 → Happy.
Check Neon Number
Goal: Sum of digits of square equals number.
Approach:
• Input: Read number.
• Square it.
• Sum square digits.
• If equals original → Neon.
Swap Two Numbers (No Temp)
Goal: Swap without 3rd variable.
Approach:
• Input: Read a, b.
• Use: a = a + b, b = a - b, a = a - b.
• Print swapped.
Check Buzz Number
Goal: Number ends with 7 or divisible by 7.
Approach:
• Input: Read number.
• If last digit is 7 or n % 7 == 0 → Buzz.
Sum Digits Until Single Digit
Goal: Keep summing digits until single digit remains.
Approach:
• Input: Read number.
• Loop: sum digits, repeat if sum > 9.
• Output sum.
Check Leap Year
Goal: Year divisible by 4, not by 100, except if divisible by 400.
Approach:
• Input: Read year.
• Check conditions.
• Output leap or not.
Find Alternative Prime Numbers
Goal: Print every second prime in a range.
Approach:
• Input: range limit.
• Loop: find primes, count.
• If count is odd → print.
Check Twisted Prime
Goal: Reverse the number, check if both original & reverse are prime.
Approach:
• Input: Read number.
• Check if prime.
• Reverse → check if prime.
Check Sunny Number
Goal: Number + 1 is perfect square.
Approach:
• Input: Read number.
• Add 1, check if perfect square.
Check Automorphic Number
Goal: Number’s square ends with same number.
Approach:
• Input: Read number.
• Square it.
• If square ends with number → Automorphic.
Check Duck Number
Goal: Number contains zero (not at start).
Approach:
• Input: Read number.
• Loop: if any digit is 0 (skip leading) → Duck.
Check Bouncy Number
Goal: Number is neither increasing nor decreasing digits.
Approach:
• Input: Read number.
• Check if digits only increase or decrease → if not, Bouncy.
Check Mistry Number
Goal: Number contains 4 & 7 only.
Approach:
• Input: Read number.
• Loop: each digit must be 4 or 7.
Check Smith Number
Goal: Composite number where sum of digits = sum of prime factors' digits.
Approach:
• Input: Read number.
• Find prime factors, sum their digits.
• Sum original digits.
• Compare sums.
Decimal to Binary
Goal: Convert decimal to binary.
Approach:
• Input: Read decimal.
• Loop: divide by 2 → collect remainders.
• Reverse remainders.
Decimal to Octal
Goal: Convert decimal to octal.
Approach:
• Same as binary, but divide by 8.
Decimal to Hexadecimal
Goal: Convert decimal to hex.
Approach:
• Same logic, divide by 16.
• Digits >9 → A-F.
Binary to Octal
Goal: Convert binary to decimal → decimal to octal.
Approach:
• Convert binary to decimal.
• Decimal to octal.
Binary to Decimal
Goal: Convert binary to decimal.
Approach:
• Input: Read binary as string.
• Loop: multiply digit by 2^position.
• Sum.
Octal to Decimal
Goal: Convert octal to decimal.
Approach:
• Same as binary → multiply digit by 8^position.
Octal to Binary
Goal: Octal → decimal → binary.
Approach:
• Convert octal to decimal.
• Decimal to binary.
Binary to Hexadecimal
Goal: Binary → decimal → hex.
Approach:
• Convert binary to decimal.
• Decimal to hex.
Hexadecimal to Binary
Goal: Hex → decimal → binary.
Approach:
• Convert hex to decimal.
• Decimal to binary.