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.
- Comprehensive examples of
forandwhileloops. - Practical exercises covering common use cases of loops.
- Clear and well-documented Python code for learning and reference.
- For Loops: Iterating over sequences, ranges, and collections.
- While Loops: Looping with conditions and control mechanisms.
Syntax: while(condition): #statement Program to remove unit digit in a number
Example 1:
Enter a number: 4596 Number after removing unit digit= 459 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 while loop count numbers
condition : Run loop till count is less than 10 1 2 3 4 5 6 7 8 9 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 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
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
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
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
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
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
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
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
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
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 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
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
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