Sum of All Odd Numbers Up to N
Print the sum of all odd numbers up to n.
BeginnerPhase 2 - Level 1: Basic LoopingExample 9 of 14
sum-of-all-odd-numbers-up-to-n.py
1# Take n as input2n = int(input("Enter n: "))34# Calculate sum of odd numbers5sum_odd = 06for i in range(1, n + 1, 2):7 sum_odd += i89print(f"Sum of odd numbers up to {n}: {sum_odd}")
Output
Enter n: 10 Sum of odd numbers up to 10: 25
What's going on
Sum only odd numbers using step 2.
Key Concepts:
Start from 1 (first odd number)
Step by 2 to get only odd numbers
Accumulate sum
🔗Related Content
- 📖
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- 📝
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- 💻
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum