Skip to main content

Posts

Showing posts with the label Python

Python programe to find the factorial of a number

 Python program to find the factorial of a number Factorial number of a number is a product from 1 to the number which is from 1 to n where n is the number Example : factorial of 5 = 1*2*3*4*5 = 120 Code: #function to find factorial number def facto(n):     factorial = 1     for i in range(1,n+1):         factorial = factorial * i     return factorial #Take the input from the user and store in n n = int(input()) if n == 0: #factorial of 0 is always 1     print(1) else:     print(facto(n)) #other than 0 we have to find the factorial number by calling the function Input : 5 Output : 120 There is another method to find factorial number using recursion. Feel free to check the code in a code editor and try it out and also i am welcome to any suggestion. Here is the previous post : https://shotwithcode.blogspot.com/2020/12/python-automation-with-sele...

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:         retur...

Python program to check whether the given number is an armstrong or not.

  Given an integer,Write a python program to check if an integer is an Armstrong or not. Armstrong A given a number is said to be armstrong if the number of order or length n is digit*n times and the sum is equal to the number given example - 153 = 1*1*1 + 5*5*5 +3*3*3 Example: 1. 153 - Armstrong 2. 345 - Not armstrong 3. 1634 - Armstrong Python Program #Check whether the given integer is an armstrong or not #function for armstrong def armstrong(n):     #convert into str to calculate the order of the digit     a = str(n)     temp,sum= n,0     while temp>0:         #modulo by 10 to get each and every digit of a number         digit = temp%10         #multiply each digit with the order of the digit and sum the values         sum+=digit**len(a) ...

Python program to check if a number or string a palindrome or not

Given a string or an integer,Write a python program to check if a string or an integer is a palindrome. Palindrome A given string or a number is said to be palindrome if the opposite or the reverse of the string or a number is same as the orginal string or number. Example: 1. 12321 - Palindrome 2. 345 - Not Palindrome 3. SOS - Palindrome 4. RUN - Not Palindrome Python Program #Function if the given input is a string def palinStr(n):      #check if the reverse of a string is equal to the original string     print('Palindrome') if n == n[::-1] else print('Not a palindrome') #Function if the given input is a number def palinInt(n):     rem = 0     temp = n     while n>0:          #Taking the last number         dig = n%10          #reverse the number from last digit to f...

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...