Write a program which takes two integer a and b and prints all composite numbers between a and b.(both numbers are inclusive)
Input:
10
20
Output:
10
12
14
15
16
18
20
Python Code:
# Taking input of the two integers a and b
a=int(input())
b=int(input())
# Looping through all numbers between a and b (inclusive)
for num in range(a, b+1):
# Checking if the number is composite
if num > 1:
for i in range(2, num):
if num % i == 0:
# If the number is composite, print it and break out of the inner loop
print(num)
break
Explanation:
- We take input of two integers
aandbusing theinput()function and convert them into integers using theint()function. - We loop through all the numbers between
aandb(inclusive) using therange()function and store each number in thenumvariable. - We check if the
numis greater than 1 because 1 is neither a prime nor a composite number. - If
numis greater than 1, we check if it is composite or not by looping through all numbers between 2 andnum-1(inclusive) using anotherrange()function and storing each number in theivariable. - If
numis divisible byiwithout leaving any remainder, then it is a composite number. In that case, we print thenumand break out of the inner loop using thebreakstatement. - If
numis not composite, then the inner loop completes without finding any factor ofnum, and the program continues to the next iteration of the outer loop.
Note: A composite number is a positive integer greater than 1 that is not a prime number. The program checks for composite numbers between a and b (inclusive) by testing each number in this range for divisibility by integers between 2 and the number itself minus one. If the number is divisible by any integer in this range, it is composite, and the program prints it. Otherwise, the program skips the number and continues to the next one.
No comments:
Post a Comment