An open API service indexing awesome lists of open source software.

https://github.com/imdarshangk/loops-python

It is used to execute a statement or Group of statement for multiple time.
https://github.com/imdarshangk/loops-python

forloops whileloops

Last synced: 7 months ago
JSON representation

It is used to execute a statement or Group of statement for multiple time.

Awesome Lists containing this project

README

          

# Loops Python

## Overview
This repository is dedicated to understanding and practicing **loops** in Python. It includes programs and exercises demonstrating the usage of `for` and `while` loops. The repository is suitable for beginners looking to grasp Python's iterative constructs effectively.

## Features
- Comprehensive examples of `for` and `while` loops.
- Practical exercises covering common use cases of loops.
- Clear and well-documented Python code for learning and reference.

## Contents
- **For Loops**: Iterating over sequences, ranges, and collections.
- **While Loops**: Looping with conditions and control mechanisms.

## Loops
```
Syntax:
while(condition):
#statement
```

## Forloop remove unit digit
Program to remove unit digit in a number

Example 1:
```
Enter a number: 4596
Number after removing unit digit= 459
```

## Forloop find unit digit
Program to find unit digit in a number.

units digit of a number is the digit in the one's place of the number. It is the rightmost digit of the number.

Example 1:
```
Enter a number: 545
Unit digit in a number 5
```

## Whileloop count number
while loop count numbers
```
condition : Run loop till count is less than 10

1
2
3
4
5
6
7
8
9
```

## Sum of unit digit
forloop

Program to find sum of unit digit in a given number.
```
Program Explanation:
1. User must first enter the value and store it in a variable.
2. The for loop is used and the last digit of the number is obtained by using the modulus operator.
3. The digit is added to another variable each time the loop is executed.
4. This loop terminates when the value of the number is 0.
5. The total sum of the number is then printed.
```
```
Example 1:

Enter a number: 1235
Sum= 11

Example 2:

Enter a number: 2598
Sum= 24

Example 3:

Enter a number: 9875325
Sum= 39
```

## Sum of digits
while loop
```
Program Explanation:
1} User must first enter the value and store it in a variable.
2} The for loop is used and the last digit of the number is obtained by using the modulus operator.
3} The digit is added to another variable each time the loop is executed.
4} This loop terminates when the value of the number is 0.
5} The total sum of the number is then printed.

Example 1:

Enter a number >> 55
Sum of digits = 10

Example 2:

Enter a number >> 365
Sum of digits = 14
```

## Reverse of digits
Reverse of digits
```
Program Explanation:

1} User must first enter the value and store it in a variable n.
2} The for loop is used and the last digit of the number is obtained by using the modulus operator.
3} The last digit is then stored at the oneโ€™s place, second last at the tenโ€™s place and so on.
4} The last digit is then removed by truly dividing the number with 10.
5} This loop terminates when the value of the number is 0.
6} The reverse of the number is then printed.

Example 1:

Enter a number: 548
Reverse of digits= 845

Example 2:

Enter a number: 987
Reverse of digits= 789

Example 3:

Enter a number: 739
Reverse of digits= 937
```

## Product of digits
Product of digits
```
Example 1:

Enter a number: 4586
Product of digits= 960

Example 2:

Enter a number: 325
Product of digits= 30

Example 3:

Enter a number: 39
Product of digits= 27
```

## Armstrong number
Armstrong number
```
Program Explanation >>

1} User must enter the number and store it in a variable.
2} The map function obtains each digit from the number and converts it to a string and stores it in a list.
3} The second map function cubes each digit and stores it in another list.
4} Then the sum of the cubes of the digits is found and is checked if it is equal to the original number.
5} If the sum of the cube of digits is equal to the original number, the number is an Armstrong number.
6} The final result is printed.

Example 1:

Enter a number: 153
Sum= 153
It is a Armstrong number

Example 2:

Enter a number: 125
Sum= 134
It is not a Armstrong number

Example 3:

Enter a number: 407
Sum= 407
It is a Armstrong number

Example 4:

Enter a number: 469
Sum= 1009
It is not a Armstrong number
```
## Range
range
```
Syntax:
for i in range(start,end):
//statement
```
```
Program Explanation:

* Start value included
* end value.(Excluded)
* I value gets auto incremented by 1 (Default)

Example:

0 Python
1 Python
2 Python
3 Python
4 Python
5 Python
```

## ๐๐š๐ฅ๐ข๐ง๐๐ซ๐จ๐ฆ๐ž ๐ง๐ฎ๐ฆ๐›๐ž๐ซ
๐๐š๐ฅ๐ข๐ง๐๐ซ๐จ๐ฆ๐ž ๐ง๐ฎ๐ฆ๐›๐ž๐ซ

๐๐ซ๐จ๐›๐ฅ๐ž๐ฆ ๐ƒ๐ž๐ฌ๐œ๐ซ๐ข๐ฉ๐ญ๐ข๐จ๐ง:
```
The program takes a number and checks whether it is a palindrome or not.
```
```
Program Explanation:

1} User must first enter the value of the integer and store it in a variable.
2} The value of the integer is then stored in another temporary variable.
3} The for loop is used and the last digit of the number is obtained by using the modulus operator.
4} The last digit is then stored at the oneโ€™s place, second last at the tenโ€™s place and so on.
5} The last digit is then removed by truly dividing the number with 10.
6} This loop terminates when the value of the number is 0.
7} The reverse of the number is then compared with the integer value stored in the temporary variable.
8} If both are equal, the number is a palindrome.
9} If both arenโ€™t equal, the number isnโ€™t a palindrome.
10} The final result is then printed.

Example 1:

Enter a number: 121
reverse= 121
it is a Palindrome number

Example 2:

Enter a number: 984
reverse= 489
it is not a Palindrome number

Example 3:

Enter a number: 737
reverse= 737
it is a Palindrome number

Example 4:

Enter a number: 845
reverse= 548
it is not a Palindrome number
```

## Sum of cubes
Sum of cubes
```
Example 1:

Enter a number: 12
Sum of cubes= 6084

Example 2:

Enter a number: 3
Sum of cubes= 36

Example 3:

Enter a number: 5
Sum of cubes= 225
```

## Factorial
Factorial
```
Program Explanation:

* User must enter a number.
* A factorial variable is initialized to 1.
* A for loop is used to multiply the number to the factorial variable and then the number is decremented each time.
* This continues till the value of n+1.
* The factorial of the number is printed.

Example 1:

Enter a number: 5
Factorial= 120

Example 2:

Enter a number: 3
Factorial= 6

Example 3:

Enter a number: 10
Factorial= 3628800
```

## Number of factors
Number of factors
```
Example 1:

Enter a number: 22
factors are
1
2
11
22
Number of factors= 4

Example 2:

Enter a number: 5
factors are
1
5
Number of factors= 2

Example 3:

Enter a number: 8
factors are
1
2
4
8
Number of factors= 4

Example 4:

Enter a number: 20
factors are
1
2
4
5
10
20
Number of factors= 6
```

## Prime number and Not a prime number
Prime number and Not a prime number
```
Program Explanation:

* User must enter the number to be checked and store it in a different variable.
* The count variable is first initialized to 0.
* The for loop ranges from 1 of the number.
* The if statement then checks for the modules of the number if the remainder is equal to 0.
* The count variable counts the number of modules and if the count is equal to 2, the number is a prime number.
* If the count is not equal 2, the number is nโ€™t a prime number.
* The final result is printed.

Example 1:

Enter a number: 12
factors are
1
2
3
4
6
12
Number of factors= 6
Not a Prime number

Example 2:

Enter a number: 11
factors are
1
11
Number of factors= 2
Prime number

Example 3:

Enter a number: 22
factors are
1
2
11
22
Number of factors= 4
Not a Prime number

Example 4:

Enter a number: 31
factors are
1
31
Number of factors= 2
Prime number
```

## Sum of factors
Sum of factors
```
Example 1:

Enter a number: 28
Sum of factors= 28
Perfect number

Example 2:

Enter a number: 25
Sum of factors= 6
Not a Perfect number

Example 3:

Enter a number: 6
Sum of factors= 6
Perfect number

Example 4:

Enter a number: 8
Sum of factors= 7
Not a Perfect number
```

## Factors
Factors are
```
Example 1:

Enter a number: 12
Factors are
1
2
3
4
6
12

Example 2:

Enter a number: 11
Factors are
1
11

Example 3:

Enter a number: 6
Factors are
1
2
3
6

Example 4:

Enter a number: 31
Factors are
1
31
```

## factorial of n integer
factorial of n integer

Problem Description:
```
The program takes a number and finds the factorial of that number without using recursion.
```
```
Program Explanation:

1} User must enter a number.
2} A factorial variable is initialized to 1.
3} A for loop is used to multiply the number to the factorial variable and then the number is decremented each time.
4} This continues till the value of the number is greater than 0.
5} The factorial of the number is printed.

Example 1:

Enter a number: 10
Factorial of integer is 3628800

Example 2:

Enter a number: 8
Factorial of integer is 40320

Example 3:

Enter a number: 9
Factorial of integer is 362880

Example 4:

Enter a number: 25
Factorial of integer is 15511210043330985984000000
```

## Number Sum Calculator
Number sum calculator
```
Enter a number(0 to quit)>> 2
Enter a number(0 to quit)>> -5
Enter a number(0 to quit)>> 5
Enter a number(0 to quit)>> 10
Enter a number(0 to quit)>> -4
Enter a number(0 to quit)>> 0
Total is 8
```