#Ex: Write a program To print Hello 10-times without any loops
print('Hello')
print('Hello')
print('Hello')
print('Hello')
print('Hello')
print('Hello')
print('Hello')
print('Hello')
print('Hello')
print('Hello')
________________________________________________________________________________________________________________________
#Ex: Write a program To read a number from user e.g. 50. Add numbers from 1 to 10 and display results without loops
number = int( input("Enter number: "))
print(number)
print(number + 1 )
print(number + 2 )
print(number + 3 )
print(number + 4 )
print(number + 5 )
print(number + 6 )
print(number + 7 )
print(number + 8 )
print(number + 9 )
print(number + 10 )
________________________________________________________________________________________________________________________
Identify the problems or difficulties in the above two programs.
repeat of statements
repeat of same actions / operation
Solutions :
Loops usage simplify the above repeated statements
________________________________________________________________________________________________________________________
While loop syntax:
Variable intialization
while condition :
logic
increment or decrement
______________________________________________________________________________________________________
#Ex: Write a program To
1. print Hello 10-times.
2. Use the While loop and without any built-in functions like range().
a = 1
while (a <= 10):
print(a, 'Hello')
a = a + 1
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program To
1. print numbers from 1 to 10 .
2. Use the While loop and without any built-in functions like range().
a = 1
while (a <= 10):
a = a + 1
print(a)
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program To
1. print numbers from 10 to 1 .
2. Use the While loop and without any built-in functions like range().
a = 10
while (a >= 1):
a = a - 1
print(a)
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program To
1. print 5 multiples
2. Use the While loop and without any built-in functions like range().
a = 5
number = 50
while (a <= 50):
a = a + 5
print(a)
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program To
1. print Hello 10-times.
2. Use the While loop and without any built-in functions like range().
a = 1
number = 10
while (a <= number):
print(a, 'Hello')
a = a + 1
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program To
1. print numbers from 1 to 10 .
2. Use the While loop and without any built-in functions like range().
a = 1
number = 10
while (a <= number):
a = a + 1
print(a)
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program To
1. print numbers from 10 to 1 .
2. Use the While loop and without any built-in functions like range().
a = 10
number = 1
while (a >= 1):
a = a - 1
print(a)
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program To
1. print 5 multiples
2. Use the While loop and without any built-in functions like range().
a = 5
number = 50
while (a <= number):
a = a + 5
print(a)
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program To
1. print even numbers from 1 to 20
2. Use the While loop and without any built-in functions like range().
a = 0
number = 20
while (a <= number):
a = a + 2
print(a)
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program To
1. print odd numbers from 1 to 20
2. Use the While loop and without any built-in functions like range().
a = 1
number = 20
while (a <= number):
a = a + 2
print(a)
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program to
1. display number as long as it is less than value of 10.
2. Use the While loop and without any built-in functions like range().
a = 1
number = int(input("Enter the number:"))
while (a <= number):
print(a)
a = a + 1
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program to
1. display number as long as it is less than value of 10.
2. Use the While loop and without any built-in functions like range().
a = 10
number = int(input("Enter the number:"))
while (a <= number):
print(a)
a = a + 1
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program to
1. display number as long as it is less than value of 10.
2. Use the While loop and without any built-in functions like range().
3. Write the numbers in the format - 1, 2, 3, 4, 5, 6, 7, 8, 9,10
a = 1
number = int(input("Enter the number:"))
while (a <= number):
print(a, end+",")
a = a + 1
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program to
1. display the numbers to display number as long as it is less than value of 20 and more than 5.
2. Use the While loop and without any built-in functions like range().
a = 1
number = int(input("Enter the number:"))
while (a <= number):
if ( a > 5):
print(a)
a = a + 1
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program
1. To read the range from user ( like start number is 1 and end number is 50)
2. Display the multiples of 5 with in the given range.
3. Use the While loop and without any built-in functions like range().
a = 1
number = int(input("Enter the number:"))
while (a <= number):
if ( a % 5 == 0):
print(a)
a = a + 1
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program
1. To read the range from user ( like start number is 1 and end number is 50)
2. Display the multiples of 2 and 3 with in the given range.
3. Use the While loop and without any built-in functions like range().
a = 1
end_number = int(input("Enter the number:"))
while (a <= end_number):
if ( a % 2 == 0):
print("2 multiples :\t ", a, end=" |")
if ( a % 3 == 0):
print("3 multiples :\t ", a)
a = a + 1
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program
1. To read the range from user ( like start number is 1 and end number is 50)
2. Display the numbers with a step of 5 with in the given range, not the multiples of 5.
3. Use the While loop and without any built-in functions like range().
a = 1
number = int(input("Enter the number:"))
while (a <= number):
print(a)
a = a + 5
else:
print("Loop completed")
________________________________________________________________________________________________________________________
# Write a program to display numbers in reverse direction from 10 to 1.
Use the While loop and without any built-in functions like range().
number = int(input("Enter the number:"))
while (number >= 1):
print(a)
a = a - 1
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Write a program to list the numbers upto 20 and break the loop when the number 10 reaches.
Use the While loop and without any built-in functions like range().
a = 1
number = int(input("Enter the number:"))
while (a <= number):
print(a)
if ( a >= 10):
print("Break the Loop")
break
a = a + 1
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: write a program to display the numbers from 50 to 1 with a step of every 5 numbers jump.
Use the While loop and without any built-in functions like range().
number = int(input("Enter the number:"))
while ( number >= 1):
print(a)
a = a - 5
else:
print("Loop completed")
________________________________________________________________________________________________________________________
# Write a program to display even and odd numbers between 1 and 10.
Use the While loop and without any built-in functions like range().
a=1
number=int(input('enter a number'))
while (a<=number):
if a%2==0:
print(a,'even')
else:
print(a,'odd')
a=a+1
else:
print('Loop completed')
________________________________________________________________________________________________________________________
# Write a program to display number and add "$" at the end from 1 to 10.
Use the While loop and without any built-in functions like range().
a = 1
number = int(input("Enter the number:"))
while (a <= number):
print(a, "$")
a = a + 1
else:
print("Loop completed")
________________________________________________________________________________________________________________________
# write a program to find the leap years between 2000 and 2100 years.
Use the While loop and without any built-in functions like range().
start_year =2000
end_year = 2100
while (start_year <= end_year):
if( start_year %4 ==0 and start_year%100 == 0):
print(start_year, " is a Leap Year")
start_year=start_year+1
else:
print('Loop completed')
________________________________________________________________________________________________________________________
#Ex: write a program to display the numbers divisible by 3, 5 and both 3 and 5 between 1 and 100.
Use the while loop and without any built-in functions like range().
start_num =1
end_num = 100
while (start_num <= end_num):
if n % 3 == 0 and n % 5 == 0:
print("Three and 5 are friends")
elif n % 3 == 0:
print("Three alone")
else:
print("5 alone")
start_num = start_num +1
else:
print('Loop completed')
________________________________________________________________________________________________________________________
#Ex: write a program to read the number from user and list the factors of the number.
Use the while loop and without any built-in functions like range().
start_num =1
number_str = input ("Enter the number: ")
end_num = int(number_str)
# since for loop starts with the zero indexes we need to skip it and start the loop from the first index.
while (start_num <= end_num)::
if(end_num % start_num ==0):
print(start_num, "is a factor")
start_num = start_num + 1
else:
print('Loop completed')
________________________________________________________________________________________________________________________
#Ex: write a program to read the number from user and list the perfect square or not.
Use the while loop and without any built-in functions like range().
start_num =1
number_str = input ("Enter the number: ")
end_num = int(number_str)
while (start_num <= end_num):
quotient = number / i
remainder = number % i
if( quotient ==i and remainder ==0):
print(number , f"is perfect square with {i} as a factor")
start_num = start_num + 1
else:
print('Loop completed')
________________________________________________________________________________________________________________________
#Ex: write a program to read the number from user and list the perfect number or not.
A number is said to be perfect if sum of all its factors excluding the number itself is equal to the number.
Use the while loop and without any built-in functions like range().
start_num =1
number_str = input ("Enter the number: ")
end_num = int(number_str)
total = 0
while (start_num <= end_num):
remainder = number % i
start_num = start_num + 1
if remainder == 0:
total = total + i
print(i, " + ", end=" ")
else:
print("=", total)
if total == number:
print("Perfect number")
else:
print("Not a Perfect number")
________________________________________________________________________________________________________________________
#Ex: write a program to display number pyramid like below.
5
55
555
5555
Use the while loop and without any built-in functions like range().
Program:
start_num =1
number_str = input ("Enter the number: ")
end_num = int(number_str)
total = 0
while (start_num <= end_num):
val = start_num * str(end_num)
print(val)
start_num = start_num + 1
else:
print("Loop completed")
________________________________________________________________________________________________________________________
#Ex: Python program to add 50 for each number in the list.
Use the while loop and without any built-in functions like range().
start_num =1
end_num = 10
number_str = input ("Enter the number: ")
given_number = int(number_str)
while (start_num <= end_num):
result = given_number + start_num
print (given_number," + ", start_num , " =" , result)
start_num = start_num +1
else:
print('Loop completed')
________________________________________________________________________________________________________________________
#Ex: Python program to print a multiplication table of a given number.
Use the while loop and without any built-in functions like range().
start_num =1
end_num = 10
number_str = input ("Enter the number: ")
given_number = int(number_str)
while (start_num <= end_num):
result = given_number * start_num
print (given_number," X ", start_num , " =" , result)
start_num = start_num +1
else:
print('Loop completed')
________________________________________________________________________________________________________________________
#Ex: write a program to sum of all numbers from 1 to a given number.
Use the while loop and without any built-in functions like range().
start_num =1
end_num = 10
number_str = input ("Enter the number: ")
end_num = int(number_str)
# set up a variable to store the sum with initial value of 0
sum = 0
while (start_num <= end_num):
sum = sum + start_num
start_num = start_num +1
else:
print(sum)
print('Loop completed')
________________________________________________________________________________________________________________________
#Ex: Python program to calculate the sum of all the odd numbers within the given range.
Use the while loop and without any built-in functions like range().
start_num =1
end_num = 10
number_str = input ("Enter the number: ")
end_num = int(number_str)
# set up a variable to store the sum with initial value of 0
sum = 0
while (start_num <= end_num):
if ( start_num %2 != 0):
sum = sum + start_num
start_num = start_num +1
else:
print(sum)
print('Loop completed')
________________________________________________________________________________________________________________________
#Ex: Python program to calculate the sum of all the even numbers within the given range.
Use the while loop and without any built-in functions like range().
start_num =1
end_num = 10
number_str = input ("Enter the number: ")
end_num = int(number_str)
# set up a variable to store the sum with initial value of 0
sum = 0
while (start_num <= end_num):
if ( start_num %2 == 0):
sum = sum + start_num
start_num = start_num +1
else:
print(sum)
print('Loop completed')
________________________________________________________________________________________________________________________
#Ex: Python program to count the total number of digits in a number.
Use the while loop and without any built-in functions like len(), range().
number_str = input ("Enter the number: ")
given_number = int(number_str)
# set up a variable to store the sum with initial value of 0
quotient = given_number % 10
count = 1
while (quotient > 0 ):
quotient = given_number % 10
if ( quotient > 0 ):
count = count + 1
else:
print(count)
print('Loop completed')
________________________________________________________________________________________________________________________
#52. Determine if a number is a prime number.
Use the while loop and without any built-in functions like len(), range().
num = int(input("Enter a number more than 1 : "))
max_factor = (num // 2) + 1 # make sure use //
print (f"Checking the factors between 2 and {max_factor}")
if num > 1:
start_factor = 2
while start_factor <= max_factor:
if num % start_factor == 0:
print(f"Not a prime number. Divisible by {start_factor} hence not proceeding further")
break
else:
start_factor=start_factor+1
else:
print("Prime number")
else:
print("Not a prime number")
________________________________________________________________________________________________________________________
#Ex: Python program to display all numbers within a 100 the prime numbers.
start_number = 2
end_number = 100
while start_number <= end_number:
number = start_number
start_factor = 2
max_factor = (start_number // 2) + 1 # make sure use //
while start_factor <= max_factor:
if number % start_factor == 0:
#print(f" {number} is not a prime number. Divisible by {start_factor} hence not proceeding further")
break
else:
start_factor=start_factor+1
else:
print(f"{number} is Prime number")
start_number = start_number + 1
else:
print ("Good bye!")
________________________________________________________________________________________________________________________
#53. Check if a number is a HARSHAD number
#A Harshad number (also called a Niven number) is a positive integer divisible by the sum of its digits.
# For example, 18 is a Harshad number because it's divisible by 1 + 8 = 9.
# Here are some more examples of Harshad numbers:
# 12: 1 + 2 = 3, and 12 is divisible by 3.
# 20: 2 + 0 = 2, and 20 is divisible by 2.
# 30: 3 + 0 = 3, and 30 is divisible by 3.
# 100: 1 + 0 + 0 = 1, and 100 is divisible by 1.
# 1729: 1 + 7 + 2 + 9 = 19, and 1729 is divisible by 19.
# 2025: 2 + 0 + 2 + 5 = 9, and 2025 is divisible by 9.
number_str = input("Enter a number: ")
number_length = len(number_str)
a = 0
sum_digits = 0
while a < number_length:
print(number_str[a])
sum_digits = sum_digits + int(number_str[a])
a = a + 1
else:
print(sum_digits)
num = int(number_str)
if num % sum_digits == 0:
print("Harshad Number")
else:
print("Not a Harshad Number")
________________________________________________________________________________________________________________________
#54. Find the day of the week for a given date (without built-in functions)
def day_of_week(d, m, y):
if m < 3:
m += 12
y -= 1
k = y % 100
j = y // 100
day = (d + (13 * (m + 1)) // 5 + k + (k // 4) + (j // 4) - 2 * j) % 7
days = ["Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
return days[day]
day, month, year = map(int, input("Enter date (DD MM YYYY): ").split())
print("Day of the Week:", day_of_week(day, month, year))
________________________________________________________________________________________________________________________
#55. Determine if a given date is valid
import calendar
day = int(input("Enter day: "))
month = int(input("Enter month: "))
year = int(input("Enter year: "))
if 1 <= month <= 12 and 1 <= day <= calendar.monthrange(year, month)[1]:
print("Valid date")
else:
print("Invalid date")
________________________________________________________________________________________________________________________
#Ex: Python program to convert the month name to a number of days.
# given list of month name
month = ["January", "April", "August","June","Dovember"]
# iterate through each mont in the list
for i in month:
if i == "February":
print("The month of February has 28/29 days")
elif i in ("April", "June", "September", "November"):
print("The month of",i,"has 30 days.")
elif i in ("January", "March", "May", "July", "August", "October", "December"):
print("The month of",i,"has 31 days.")
else:
print(i,"is not a valid month name.")
------------------------------------------------------------------------------------------------------------------------
#Ex: Python program to get the Fibonacci series between 0 and 50.
# given upper bound
num = 50
# initial values in the series
first_value,second_value = 0, 1
# iterate in the given range of numbers
for n in range(0, num):
# if no. is less than 1 move to next number
if(n <= 1):
next = n
# if number is within range, execute the below code block
if nextnum:
break
# print each element that satisfies all the above conditions
print(next)
---------------------------------------------------------------------------
#Ex: Python program to find the factorial of a given number.
# given number
given_number= 5
# since 1 is a factor of all numbers, set the factorial to 1
factorial = 1
counter = 1
# iterate till the given number
while (counter < given_number + 1):
factorial = factorial * counter
counter = counter + 1
print("The factorial of ", given_number, " is ", factorial)
------------------------------------------------------------------------------------------------------------------------
# With the continue statement we can stop the current iteration, and continue with the next:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
------------------------------------------------------------------------------------------------------------------------
#Ex: Write a Python program that iterates the integers from 1 to 25.
# given range
given_number = 25
counter = 1
while (counter < given_number + 1):
# if no. is multiple of 4 and 5 - print fizzbuzz
if i % 4 == 0 and i % 5 == 0:
print("fizzbuzz")
# continue with the loop
continue
# if no. is divisible by 4 - print fizz and no by 5
if i % 4 == 0 and i%5!=0:
print("fizz")
# continue with the loop
continue
# if no. is divisible by 5 - print buzz and not by 4
if i % 5 == 0 and i % 4!= 0:
print("buzz")
else:
# else just print the no.
print(i)
counter = counter + 1
------------------------------------------------------------------------------------------------------------------------
#Ex: Write a Python program to write the reverse of the given number.
number = int(input("Enter a positive integer: "))
rev = 0
while (number != 0):
digit = number % 10
rev = (rev * 10) + digit
number = number // 10
print(rev)
------------------------------------------------------------------------------------------------------------------------
# Python code to print cube sum of first n natural numbers using inbuilt function pow()
n = 5
s = 0
# iterating loop up to given number n
while (i <= n+1):
# adding cube sum using pow() function
s = s + pow(i, 3)
i = i + 1
else:
print(s)
------------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment