Skip to main content

Python program to check prime number or not

 Given an integer,Write a python program to check if an integer is a prime number or not.

Prime Number

A given a number is said to be prime if the number has exactly two factor which is 1 and itself or the number is said to be prime  if the number is divided by 1 and itself.

example -2 = factor of 2 is 1 and 2 so it is a prime because it has exactly two factor

Example:

1. 2 -prime

2. 1 - Not a prime

3. 3 -Prime

Python Program

def prime(n):
    #To count the factor of a number
    count = 0
    #we check from 2 because 1 is not a prime number
    if n > 1:
        for i in range(1,n+1):
            if n%i==0:
                count+=1
    else:
        return "Not a prime"
    #If the number has exactly 2 factor then we return as a prime number
    if count==2:
        return "Prime"
    else:
        return "Not a prime"
#Taking user input
n = int(input())
#calling the prime number function
print(prime(n))

Input:

2

Output:

Prime

Input:

1

Output:

Not a prime

Comments

Popular posts from this blog

I built a Django weather app using openweatherapp API

 I built a Django weather app using openweatherapp API.  Yes i built this simple weather app using openweatherapp API, it was fun an easy to built with django. How do i built it? I install the module needed for this project, the only module we need is a request module for python since i am using python to built this project. After installing and setting up the project with django i download a template and start with creating the views for the project. Here how goes: 1.First go to openweather app website : https://openweathermap.org/ 2.Signup for an account so that the openweather app will send you an api key 3.After singup with an account go to API and select the API you want to use 4.In an API doc it will attach a link which look like this api.openweathermap.org/data/2.5/weather?q= {city name} &appid= {API key} So we need to replace the {API key} with our own API key. 5.That it for the API so in order to make a request or to call the API in python we use a request module...

Build a tic tac toe game using python

 Hello, In this blog i am going to teach you how to create a tic tac toe game using python. If you know the basic of python like function, loops, conditional, list then you are good to go. In this project we are going to create it with a simple format of python without using any import or any object oriented programming. Requirement: 1.Board 2.Player(Computer and a user) 3.Winner Python fundamental needed: 1.Variable 2.List 3.If elif else 4.While 5.Function Here we are going to start A tic tac toe game is a game which has three rows and three column so we need a list which is the size of 3X3 which is nine to store the empty dash for the board Line 4: We assign a game_running to True which mean that the game is still runing when we first start the game. Line 6: Defining a function to display the board when ever we call the display_board function. Line 18: When we first start the game the board will display as empty with dash in it. Output is in line 8 Line 19:  We set the curre...

Best office computer accesories

 Here are some of the best computer accesories for your office at an affordable price in India. 1. Oxyura Gaming Mouse Pad Click here to see the Mouse pad https://amzn.to/3pVY8KL 2. Zebronics Zeb-Alex Wired Usb optical mouse Click here to see and buy the Mouse https://amzn.to/3ILT2t6 3. Usb wired keyboard Click here to see and buy the keyboard https://amzn.to/3mmzjqv 4.Laptop stand Click here to see and buy the laptop stand https://amzn.to/3dTgkih All link are from amazon so go ahead and check it out it is at an afordable price.