3.Find the factorial of given number
def factorial(n):
if n < 0:
return 0
elif n == 0 or n == 1:
return 1
else:
fact = 1
while(n > 1):
fact *= n
n -= 1
return fact
num = 5;
print("Factorial of",num,"is",
factorial(num))
@python_0
def factorial(n):
if n < 0:
return 0
elif n == 0 or n == 1:
return 1
else:
fact = 1
while(n > 1):
fact *= n
n -= 1
return fact
num = 5;
print("Factorial of",num,"is",
factorial(num))
@python_0
4.Program to find sum of square of first n natural numbers
def squaresum(n) :
sm = 0
for i in range(1, n+1) :
sm = sm + (i * i)
return sm
n = 4
print(squaresum(n))
@python_0
def squaresum(n) :
sm = 0
for i in range(1, n+1) :
sm = sm + (i * i)
return sm
n = 4
print(squaresum(n))
@python_0
5.Write a Python program to sum of all digits of a number
n=int(input("Enter a number:"))
sum=0
while n>0:
rem=n%10
sum=sum+rem
n=int(n/10)
print("The sum of digits of number is:", sum)
@python_0
n=int(input("Enter a number:"))
sum=0
while n>0:
rem=n%10
sum=sum+rem
n=int(n/10)
print("The sum of digits of number is:", sum)
@python_0
6.Write a program to check a year is a leap year or not using Python
year=int(input("Enter a Year:"))
if ((year % 100 == 0 and year % 400 == 0) or (year % 100 != 0 and year % 4 == 0)):
print("It is a Leap Year")
else:
print("It is not a Leap Year")
year=int(input("Enter a Year:"))
if ((year % 100 == 0 and year % 400 == 0) or (year % 100 != 0 and year % 4 == 0)):
print("It is a Leap Year")
else:
print("It is not a Leap Year")
7.Write a program to convert Days into years, weeks and months using Python
days=int(input("Enter Day:"))
years =(int) (days / 365)
weeks =(int) (days / 7)
months =(int) (days / 30)
print("Days to Years:",years)
print("Days to Weeks:",weeks)
print("Days to Months:",months)
days=int(input("Enter Day:"))
years =(int) (days / 365)
weeks =(int) (days / 7)
months =(int) (days / 30)
print("Days to Years:",years)
print("Days to Weeks:",weeks)
print("Days to Months:",months)
8.Python program to print all prime number in an interval
start = 11
end = 25
for val in range(start, end + 1):
# If num is divisible by any number
# between 2 and val, it is not prime
if val > 1:
for n in range(2, val):
if (val % n) == 0:
break
else:
print(val)
@python_0
start = 11
end = 25
for val in range(start, end + 1):
# If num is divisible by any number
# between 2 and val, it is not prime
if val > 1:
for n in range(2, val):
if (val % n) == 0:
break
else:
print(val)
@python_0
9.Python program to find Area of a circle
def findArea(r):
PI = 3.142
return PI * (r*r);
# Driver method
print(findArea(5))
@python_0
def findArea(r):
PI = 3.142
return PI * (r*r);
# Driver method
print(findArea(5))
@python_0
10 Python 3 code to find sum of elements in given array
def _sum(arr,n):
# return sum using sum
# inbuilt sum() function
return(sum(arr))
# driver function
arr=[]
# input values to list
arr = [12, 3, 4, 15]
# calculating length of array
n = len(arr)
ans = _sum(arr,n)
# display sum
print (\'Sum of the array is \',ans)
@python_0
def _sum(arr,n):
# return sum using sum
# inbuilt sum() function
return(sum(arr))
# driver function
arr=[]
# input values to list
arr = [12, 3, 4, 15]
# calculating length of array
n = len(arr)
ans = _sum(arr,n)
# display sum
print (\'Sum of the array is \',ans)
@python_0
11.Python program to print Even Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# iterating each number in list
for num in list1:
# checking condition
if num % 2 == 0:
print(num, end = " ")
@python_0
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# iterating each number in list
for num in list1:
# checking condition
if num % 2 == 0:
print(num, end = " ")
@python_0
12.Python program to check if a string is palindrome or not
def isPalindrome(s):
rev = s[::-1]
# Checking if both string are equal or not
if (s == rev):
return True
return False
# Driver code
s = "malayalam"
ans = isPalindrome(s)
if ans == 1:
print("Yes")
else:
print("No")
@python_0
def isPalindrome(s):
rev = s[::-1]
# Checking if both string are equal or not
if (s == rev):
return True
return False
# Driver code
s = "malayalam"
ans = isPalindrome(s)
if ans == 1:
print("Yes")
else:
print("No")
@python_0
13 function to check if small string is there in big string
def check(string, sub_str):
if (string.find(sub_str) == -1):
print("NO")
else:
print("YES")
# driver code
string = "zeus is a programmer"
sub_str ="zeus"
check(string, sub_str)
@python_0
def check(string, sub_str):
if (string.find(sub_str) == -1):
print("NO")
else:
print("YES")
# driver code
string = "zeus is a programmer"
sub_str ="zeus"
check(string, sub_str)
@python_0
14.Function to find permutations of a given string
from itertools import permutations
def allPermutations(str):
# Get all permutations of string 'ABC'
permList = permutations(str)
# print all permutations
for perm in list(permList):
print (''.join(perm))
# Driver program
if name == "__main__":
str = 'ABC'
allPermutations(str)
@python_0
from itertools import permutations
def allPermutations(str):
# Get all permutations of string 'ABC'
permList = permutations(str)
# print all permutations
for perm in list(permList):
print (''.join(perm))
# Driver program
if name == "__main__":
str = 'ABC'
allPermutations(str)
@python_0
15. Python3 code to find largest prime factor of number
import math
def maxPrimeFactors (n):
maxPrime = -1
while n % 2 == 0:
maxPrime = 2
n >>= 1 # equivalent to n /= 2
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
maxPrime = i
n = n / i
if n > 2:
maxPrime = n
return int(maxPrime)
n = 15
print(maxPrimeFactors(n))
n = 25698751364526
print(maxPrimeFactors(n))
@python_0
import math
def maxPrimeFactors (n):
maxPrime = -1
while n % 2 == 0:
maxPrime = 2
n >>= 1 # equivalent to n /= 2
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
maxPrime = i
n = n / i
if n > 2:
maxPrime = n
return int(maxPrime)
n = 15
print(maxPrimeFactors(n))
n = 25698751364526
print(maxPrimeFactors(n))
@python_0
16.Python Counter| Find all duplicate characters in string
from collections import Counter
def find_dup_char(input):
WC = Counter(input)
j = -1
for i in WC.values():
j = j + 1
if( i > 1 ):
print WC.keys()[j],
if name == "__main__":
input = 'hellowelcome'
find_dup_char(input)
@python_0
from collections import Counter
def find_dup_char(input):
WC = Counter(input)
j = -1
for i in WC.values():
j = j + 1
if( i > 1 ):
print WC.keys()[j],
if name == "__main__":
input = 'hellowelcome'
find_dup_char(input)
@python_0
Forwarded from ENGINEERING [CSE/IT] NOTES/ BOOKS/ LECTURES
Please open Telegram to view this post
VIEW IN TELEGRAM
Learn python3 free
πππ
https://www.coursera.org/learn/python-basics
Regards
ππ
@python_0
@python_0
πππ
https://www.coursera.org/learn/python-basics
Regards
ππ
@python_0
@python_0
Coursera
Python Basics
Offered by University of Michigan. This course ... Enroll for free.
Forwarded from Kapil Mewada
Anonymous Poll
86%
Yes
14%
No
Forwarded from Kapil Mewada
TCS full information
πππ
β«οΈStep by Step process to Register
β«οΈPreparation tips and strategy for
TCS CODEVITA R-1
β«οΈTCS CODEVITA Last Year Coding Questions
β«οΈTCS CODEVITA Interview Experienc
β«οΈTechnical Interview Questions
β«οΈHR Interview Questions
ππ
http://codeofgeeks.com/tcs-codevita-archive
Regards
ππ
@codevita9
@codevita9
πππ
β«οΈStep by Step process to Register
β«οΈPreparation tips and strategy for
TCS CODEVITA R-1
β«οΈTCS CODEVITA Last Year Coding Questions
β«οΈTCS CODEVITA Interview Experienc
β«οΈTechnical Interview Questions
β«οΈHR Interview Questions
ππ
http://codeofgeeks.com/tcs-codevita-archive
Regards
ππ
@codevita9
@codevita9