For loop Programs 15th Class

UpComing SoftWare Training Demos

๐Ÿš€ *Playwright With AI Testing Demo In Telugu*
๐Ÿ—“๏ธ *Date:* 28 Oct2026, 08:00 PM IST
๐Ÿ“ *Register Now!:*
https://www.vlrt.in/3r
๐Ÿ‘ฅ *Join WA Community:*
https://www.vlrt.in/3w
๐Ÿ’ก *Course Content:*
https://www.vlrt.in/3c
โ–ถ๏ธ *Demo Videos:*
https://www.vlrt.in/3v

๐Ÿš€ *Gen AI Engineer (Production Focused)*
๐Ÿ—“๏ธ *Date:* 29th Oct 2026, 06:00PM IST
๐Ÿ“ *Register Now!:*
https://www.vlrt.in/gr
๐Ÿ‘ฅ *Join WA Community:*
https://www.vlrt.in/gw
๐Ÿ’ก*Course Content:*
https://www.vlrt.in/ai
โ–ถ๏ธ *Demo Videos:*
https://www.vlrt.in/gv

๐Ÿš€ *Vulnerability Management Training Demo*
๐Ÿ—“๏ธ *Date:* 28th Oct 2026, 10:00 AM IST
๐Ÿ“*Register Now!:*
https://www.vlrt.in/vr
๐Ÿ‘ฅ *Join Community:*
https://www.vlrt.in/cw
๐Ÿ’ก *Course Content:*
https://www.vlrt.in/vc
โ–ถ๏ธ *Demo Videos:*
https://www.vlrt.in/Vm

ย Sum of the firstย nย natural numbersย 

n = int(input("Enter a positive integer: "))
sum = 0

for i in range(1, n + 1):
    sum += i

print(f"The sum of the first {n} numbers is: {sum}")

Explanation:

  1. Input: The user is prompted to enter a positive integerย n.
  2. Initialization: A variableย sumย is initialized toย 0ย to store the cumulative sum.
  3. For Loop:
    • The loop runs fromย 1ย toย nย (inclusive) usingย range(1, n + 1).
    • In each iteration, the current numberย iย is added toย sum.
  4. Output: The final sum is printed.

Example:

If the user inputs 5, the program calculates:

text

1 + 2 + 3 + 4 + 5 = 15

and prints:

text

The sum of the first 5 numbers is: 15

Factorial using for loop

python

n = int(input("Enter a positive integer: "))
factorial = 1

for i in range(1, n + 1):
    factorial *= i

print(f"The factorial of {n} is: {factorial}")

Explanation:

  1. Input: The user enters a positive integerย n.
  2. Initialization:ย factorialย is initialized toย 1ย (sinceย 0! = 1ย andย 1! = 1).
  3. For Loop:
    • The loop runs fromย 1ย toย nย (inclusive) usingย range(1, n + 1).
    • In each iteration,ย factorialย is multiplied byย i.
  4. Output: The final factorial value is printed.

Fibonacci Series in Python using for Loop

The Fibonacci series is a sequence where each number is the sum of the two preceding ones, starting from 0 and 1.

Example:
0, 1, 1, 2, 3, 5, 8, 13, 21, ...


Approach:

  1. Initializeย the first two terms (a=0,ย b=1).
  2. Loopย fromย 2ย toย nย (since the first two terms are already known).
  3. Updateย values in each iteration:
    • c = a + bย (next term)
    • Shiftย aย andย bย to move forward (a = b,ย b = c).

Python Code:

python

n = int(input("Enter the number of terms: "))

a, b = 0, 1  # First two terms

if n <= 0:
    print("Please enter a positive integer.")
elif n == 1:
    print(f"Fibonacci sequence up to {n} term:")
    print(a)
else:
    print(f"Fibonacci sequence up to {n} terms:")
    print(a, end=", ")
    print(b, end=", ")
    
    for i in range(2, n):  # Loop from 2 to n-1 (since first 2 terms are printed)
        c = a + b
        print(c, end=", ")
        a, b = b, c  # Update a and b for next iteration

Explanation:

  1. Input:ย nย (number of terms).
  2. Check for invalid inputsย (n โ‰ค 0).
  3. Print first two termsย (0, 1) ifย n โ‰ฅ 2.
  4. Loop fromย 2ย toย n-1ย (since first two terms are already printed).
  5. Compute next term (c = a + b)ย and print it.
  6. Updateย aย andย bย (aย takesย b‘s value,ย bย takesย c‘s value).

Finding Factors of a Number in Python

Factors of a number n are integers that divide n exactly without leaving a remainder.

Example:
Factors of 12 โ†’ 1, 2, 3, 4, 6, 12


Approach:

  1. Loop fromย 1ย toย nย (inclusive).
  2. Check divisibilityย (n % i == 0).
  3. Store/printย the factors.

Usingย forย Loop (Brute Force)

python

n = int(input("Enter a positive integer: "))
#factors = []

for i in range(1, n + 1):
if n % i == 0:
print(i)

#print(f"Factors of {n}: {factors}")

Output:

plaintext

Enter a positive integer: 12
Factors of 12: [1, 2, 3, 4, 6, 12]

Check if a Number is Prime in Python usingย forย Loop

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Examples:

  • 5ย โ†’ Prime (divisors:ย 1, 5)
  • 6ย โ†’ Not Prime (divisors:ย 1, 2, 3, 6)

Approach:

Otherwiseย โ†’ Prime.

Check ifย n โ‰ค 1ย โ†’ Not prime.

Check divisibility fromย 2ย toย โˆšnย (optimization to reduce checks).

If any divisor foundย โ†’ Not prime.

n = int(input('Enter a Number'))

count = 0

for i in range(1, n+1):
if n % i == 0:
count += 1

if count == 2:
print('Its a Prime')
else:
print('Its Not a Prime')

Print First n Prime Numbers in Python (Using for Loop)

Approach

  1. Start checking numbers fromย 2ย (the first prime number).
  2. For each number, check if it’s prime (by testing divisibility up to its square root).
  3. If prime, add it to the list and increment the count.
  4. Stop when we’ve foundย nย primes.

Solution Code

python

n = int(input("Enter how many primes you want: "))
primes = []
num = 2  # Start checking from the first prime number

while len(primes) < n:
    is_prime = True
    # Check divisibility up to โˆšnum (optimization)
    for i in range(2, int(num ** 0.5) + 1):
        if num % i == 0:
            is_prime = False
            break
    if is_prime:
        primes.append(num)
    num += 1

print(f"First {n} prime numbers: {primes}")

Explanation

  1. Input:ย nย (number of primes to generate).
  2. Initialize:
    • primes = []ย (stores prime numbers).
    • num = 2ย (start checking from 2, the smallest prime).
  3. While loop:ย Runs until we collectย nย primes.
  4. Prime Check:
    • Assumeย numย is prime (is_prime = True).
    • Check divisibility fromย 2ย toย โˆšnumย (optimization).
    • If divisible, markย is_prime = Falseย and break.
  5. If prime:ย Append toย primes.
  6. Incrementย num:ย Check the next number.
  7. Output:ย Print the list of primes.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *