Sum of All Even Numbers Up to N
Print the sum of all even numbers up to n.
BeginnerPhase 2 - Level 1: Basic LoopingExample 8 of 14
sum-of-all-even-numbers-up-to-n.py
1# Take n as input2n = int(input("Enter n: "))34# Calculate sum of even numbers5sum_even = 06for i in range(2, n + 1, 2):7 sum_even += i89print(f"Sum of even numbers up to {n}: {sum_even}")
Output
Enter n: 10 Sum of even numbers up to 10: 30
What's going on
Sum only even numbers using step 2.
Key Concepts:
Start from 2 (first even number)
Step by 2 to get only even 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