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
a
andb
using theinput()
function and convert them into integers using theint()
function. - We loop through all the numbers between
a
andb
(inclusive) using therange()
function and store each number in thenum
variable. - We check if the
num
is greater than 1 because 1 is neither a prime nor a composite number. - If
num
is 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 thei
variable. - If
num
is divisible byi
without leaving any remainder, then it is a composite number. In that case, we print thenum
and break out of the inner loop using thebreak
statement. - If
num
is 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