Monday, August 15, 2022
Home PYTHON looping and iteration statements

# looping and iteration statements

### Python program for printing Half Pyramid pattern using numbers

n = int(input("Please enter the number of rows: ")) for num in range(1, n + 1): for i in range(num): print...

### Python program for printing Half Pyramid pattern using asterisk symbol

num_rows = int(input("Please enter the number of rows: ")) m = (2 * num_rows) - 2 for i in range(0, num_rows): for j in range(0,...

### Python program to check number is prime or not

num=int(input("Please enter any number: ")) lim=int(num/2)+1 for i in range(2,lim): rem=num%i if rem==0: ...

### Python program to print table of any given number

num=int(input("Please enter any number: ")) for a in range(1,11): print(num,'x',a,'=',num*a) Output: Please enter any...

### Python program to calculate the factorial of a number using while loop

num=int(input("Enter a number: ")) fact=1 a=1 while(a<=num): fact=fact*a #fact*=a a=a+1 ...

### Python program to print all even numbers from m to n using for loop

lower_limit = int(input("Enter the start of range: ")) upper_limit = int(input("Enter the end of range: ")) for num in range(lower_limit, upper_limit + 1): #...

### Python program to print all odd numbers from m to n using for loop

lower_limit = int(input("Enter the start of range: ")) upper_limit = int(input("Enter the end of range: ")) for num in range(lower_limit, upper_limit + 1): #...

### Python program to print all numbers from m to n using for loop

min_value = int(input("Please Enter the Minimum Value : ")) max_value = int(input("Please Enter the Maximum Value : ")) print("Numbers from {0} to {1} are".format(min_value, max_value)) for i...

### Python program to print natural numbers from 1 to n numbers using while loop

num = int(input("Please Enter any Number: ")) i = 1 print("Natural Numbers from 1 to {0} are".format(num)) while (i <= num): print (i, end = ' ') ...

### Python program to find the GCD of two numbers using recursive

def gcd(x,y):     if(y==0):       return x     else:       return gcd(y,x%y) x=int(input("Please enter first number: ")) y=int(input("Please enter second...

### Python program to check find the given year is a leap year or not

year=int(input("Please enter a year to be check leap year or not: ")) if(year%4==0 and year%100!=0 or year%400==0): print("This year is a leap...

### Python program to find the LCM of two numbers using recursive

def lcm(a,b): lcm.multiple=lcm.multiple+b if((lcm.multiple % a == 0) and (lcm.multiple % b == 0)): ...