Knowee
Questions
Features
Study Tools

Write a program to print all Armstrong numbers and Prime Numbers in the range inputted by the user. Also print the total count of Prime Numbers in the given range.

Question

Write a program to print all Armstrong numbers and Prime Numbers in the range inputted by the user. Also print the total count of Prime Numbers in the given range.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python program that does that:

def is_prime(n):
    if n <= 1:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False
    i = 3
    while i * i <= n:
        if n % i == 0:
            return False
        i += 2
    return True

def is_armstrong(n):
    num = n
    result = 0
    n = len(str(n))
    while(num != 0):
        digit = num % 10
        result = result + digit ** n
        num = num//10
    return result

start = int(input("Enter the start of the range: "))
end = int(input("Enter the end of the range: "))

prime_count = 0
print("Prime numbers in the given range are: ")
for i in range(start, end+1):
    if is_prime(i):
        print(i, end=" ")
        prime_count += 1

print("\nArmstrong numbers in the given range are: ")
for i in range(start, end+1):
    if i == is_armstrong(i):
        print(i, end=" ")

print("\nTotal count of prime numbers in the given range is: ", prime_count)

This program first defines two helper functions is_prime and is_armstrong to check if a number is prime and Armstrong respectively. Then it takes the start and end of the range as input from the user. It then iterates over the range and checks each number if it's prime or Armstrong using the helper functions and prints them. It also keeps a count of the prime numbers and prints it at the end.

This problem has been solved

Similar Questions

write a program to print armstrong numbers to n

Write a code to print the prime numbers in the given range by using the function isPrime() for the given start and end as range. isPrime() function is predefined, it return 1 if number is prime else it return 0.Input Format:Accept two integers(start and end range) as inputOutput Format:Display the prime numbers as space sepratedConstraints:1 <=  start, end <= 10^6Sample Input 1:29 50Sample Output 1:29 31 37 41 43 47Sample Input 2:1 100Sample Output 2:1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Write a query to print all prime numbers less than or equal to . Print your result on a single line, and use the ampersand () character as your separator (instead of a space).For example, the output for all prime numbers would be:2&3&5&7

Write the codeWrite a Java Program that prints out all prime numbers within a given integerSample Test CasesTest Case 1:Expected Output:Enter·an·integer:·5Prime·numbers·within·5:2·3·5·Test Case 2:Expected Output:Enter·an·integer:·30Prime·numbers·within·30:2·3·5·7·11·13·17·19·23·29·

Write a single line Python code to print all the prime numbers between 1 and 200.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.