Skip to main content
Prime Number:A prime number is a natural number greater than 1 that has no positive divisors other  than 1 and itself. Example 2,3,5,7,11,13,17

Program-1

# Python code to check a given number is prime or not
num = 15
if num > 1:
   for i in range(2, num//2):
       if (num % i) == 0:
           print(num, "is not a prime number")
           break
   else:
       print(num, "is a prime number")
  
else:
   print(num, "is not a prime number"
 
Program-2
 
# Python code to print all the prime numbers within an interval 
low = 6 
high = 51 
print("Prime numbers between", low, "&", high, "are:") 
    for num in range(low, high + 1):
   if num > 1:
       for i in range(2, num):
           if (num % i) == 0:
               break
       else:
            print(num)
 

Comments

Popular posts from this blog

Assignment: 1-Which of these is not a core data type? (A) Lists (B) Dictionary (C) Tuples (D) Class    - 2-What data type is the object below ? L = [1, 23, ‘hello’, 1] (A) List   - (B) Dictionary (C) Tuple (D) Array 3-What is the output of the expression : 3*1**3 (A) 27 (B) 9 (C) 3  - (D) 1 4-Which of the following function convert a string to a float in python? (A) int(x [,base]) (B) long(x [,base] ) (C) float(x)  - (D) str(x) 5- What is the output of the following code? i = 0 while i < 3:      print i      i += 1 else:      print 0 6-What is the output of the following code? i = 0 while i < 5:     print(i)     i += 1     if i == 3:         break else:     print(0) 7-What is the output of the following code? def f(value, values):     v = 1     values[0] = 44 t = 3 v = [1, 2, 3]...
Factorial: The factorial of a number is the product of all the integers from 1 to that number. Program-1   num=5   fact = 1   if num < 0:       print("Sorry, factorial does not exist for negative numbers")   elif num == 0:       print("The factorial of 0 is 1")   else:       for i in range(1,num + 1):           fact = fact*i       print("The factorial of",num,"is",fact) Program-2 num=int(input("Enter any number:")) fact=1 while num:    fact=fact*num    num=num-1 print("factorial is ",fact) Program-3 # Factorial of a number using recursion   def fact(n):    if n == 0 or n==1:           return 1     else:        ...