Showing posts with label Write a program which takes two integer a and b and prints all composite numbers between a and b.(both numbers are inclusive). Show all posts
Showing posts with label Write a program which takes two integer a and b and prints all composite numbers between a and b.(both numbers are inclusive). Show all posts
Saturday, April 1, 2023

Write a program which takes two integer a and b and prints all composite numbers between a and b.(both numbers are inclusive)


 

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:

  1. We take input of two integers a and b using the input() function and convert them into integers using the int() function.
  2. We loop through all the numbers between a and b (inclusive) using the range() function and store each number in the num variable.
  3. We check if the num is greater than 1 because 1 is neither a prime nor a composite number.
  4. If num is greater than 1, we check if it is composite or not by looping through all numbers between 2 and num-1 (inclusive) using another range() function and storing each number in the i variable.
  5. If num is divisible by i without leaving any remainder, then it is a composite number. In that case, we print the num and break out of the inner loop using the break statement.
  6. If num is not composite, then the inner loop completes without finding any factor of num, 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.