Important Notice:

Simple Programs Using Loops

Simple Programs Using Loops

24 views 4 min read
Simple Programs Using Loops :-

Python में loops का उपयोग किसी statement या statements के समूह को बार-बार execute (दोहराकर निष्पादित) करने के लिए किया जाता है। for और while loops की सहायता से कई प्रकार के simple programs आसानी से बनाए जा सकते हैं।

English

In Python, loops are used to execute a statement or a group of statements repeatedly. Many simple programs can be easily created using for and while loops.

1. Print Numbers from 1 to 10 :-

1 से 10 तक numbers print करने के लिए for loop का उपयोग किया जा सकता है।

English

A for loop can be used to print numbers from 1 to 10.

Example:

for i in range(1, 11):
    print(i)

Output:

1
2
3
4
5
6
7
8
9
10
 
2. Print Even Numbers from 1 to 20 :-

% operator की सहायता से even numbers को check किया जा सकता है।

English

The % operator can be used to check and print even numbers.

Example:

for i in range(1, 21):
    if i % 2 == 0:
        print(i)

Output:

2
4
6
8
10
12
14
16
18
20
 
3. Print Odd Numbers from 1 to 20 :-

यदि number को 2 से divide करने पर remainder 1 आता है, तो वह odd number होता है।

English

If a number gives a remainder of 1 when divided by 2, it is an odd number.

Example:

for i in range(1, 21):
    if i % 2 != 0:
        print(i)

Output:

1
3
5
7
9
11
13
15
17
19
 
4. Find the Sum of First 10 Natural Numbers :-

Loop की सहायता से numbers का sum निकाला जा सकता है।

English

A loop can be used to calculate the sum of numbers.

Example:

sum = 0

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

print("Sum =", sum)

Output:

Sum = 55

Because:

1 + 2 + 3 + ... + 10 = 55
 
5. Multiplication Table :-

किसी number की multiplication table print करने के लिए for loop का उपयोग किया जा सकता है।

English

A for loop can be used to print the multiplication table of a number.

Example:

number = int(input("Enter a number: "))

for i in range(1, 11):
    print(number, "×", i, "=", number * i)

Example Output:

Enter a number: 5
5 × 1 = 5
5 × 2 = 10
5 × 3 = 15
5 × 4 = 20
5 × 5 = 25
5 × 6 = 30
5 × 7 = 35
5 × 8 = 40
5 × 9 = 45
5 × 10 = 50
 
6. Find Factorial of a Number :-

किसी number का factorial निकालने के लिए loop का उपयोग किया जा सकता है।

English

A loop can be used to find the factorial of a number.

Example:

number = int(input("Enter a number: "))
factorial = 1

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

print("Factorial =", factorial)

Example Output:

Enter a number: 5
Factorial = 120

Because:

5! = 5 × 4 × 3 × 2 × 1
   = 120
 
7. Reverse Counting :-

Loop में negative step का उपयोग करके reverse counting की जा सकती है।

English

Reverse counting can be performed by using a negative step in a loop.

Example:

for i in range(10, 0, -1):
    print(i)

Output:

10
9
8
7
6
5
4
3
2
1
 
8. Count Characters in a String :-

Loop का उपयोग string के characters को count करने के लिए भी किया जा सकता है।

English

A loop can also be used to count the characters in a string.

Example:

name = "Python"
count = 0

for ch in name:
    count += 1

print("Total characters =", count)

Output:

Total characters = 6
 
9. Print Each Element of a List :-

for loop की सहायता से list के प्रत्येक element को print किया जा सकता है।

English

A for loop can be used to print each element of a list.

Example:

numbers = [10, 20, 30, 40, 50]

for num in numbers:
    print(num)

Output:

10
20
30
40
50
 
10. Find the Largest Number in a List :-

Loop की सहायता से list में सबसे बड़ी value खोजी जा सकती है।

English

A loop can be used to find the largest value in a list.

Example:

numbers = [10, 25, 7, 40, 15]

largest = numbers[0]

for num in numbers:
    if num > largest:
        largest = num

print("Largest =", largest)

Output:

Largest = 40
 
11. Check Prime Number :-

Loop का उपयोग यह check करने के लिए किया जा सकता है कि कोई number Prime Number है या नहीं।

English

A loop can be used to check whether a number is a Prime Number.

Example:

number = int(input("Enter a number: "))
count = 0

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

if count == 2:
    print("Prime Number")
else:
    print("Not a Prime Number")

Example Output:

Enter a number: 7
Prime Number

Because:

7 → divisible by 1 and 7 only
 
12. Fibonacci Series :-

Loop की सहायता से Fibonacci Series generate की जा सकती है।

English

A loop can be used to generate the Fibonacci Series.

Example:

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

a = 0
b = 1

for i in range(n):
    print(a, end=" ")
    a, b = b, a + b

Example Output:

Enter number of terms: 7
0 1 1 2 3 5 8

Related Notes