Anda di halaman 1dari 6

GCD

def GCD(x,y):
if x>y:
smaller=y
else:
smaller=x
for i in range(1,smaller+1):
if((x%i== 0) and (y%i==0)):
gcd=i
return gcd
num1=int(input("Enter the no.1"))
num2=int(input("Enter the no.2"))
print("The GCD. of", num1,"and", num2,"is",GCD(num1,num2))
Enter the no.112
Enter the no.224
The GCD. of 12 and 24 is 12
>>>

GCD
n1=int(input("Enter the no.1"))
n2=int(input("Enter the no.2"))
rem=n1%n2
while rem!=0:
n1=n2
n2=rem
rem=n1%n2
print("The GCD. of given number is ",n2)

Output
Enter the no.112
Enter the no.224
The GCD. of given number is 12
>>>
Find maximum
a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
b=int(input("Enter element:"))
a.append(b)
a.sort()
print("Maximum of a List of Number is:",a[n-1])

Output
Enter number of elements:5
Enter element:34
Enter element:43
Enter element:777
Enter element:23
Enter element:54
Maximum of a List of Number is: 777
>>>

Prime no

p=int(input("Enter number: "))


q=0
for i in range(2,p//2):
if(p%i==0):
q=q+1
if(q<=0):
print("Number is prime")
else:
print("Number isn't prime")
Output
Enter number: 12
Number isn't prime
>>>
Enter number: 5
Number is prime
Prime no
n=int(input("Enter the limit"))
for num in range(1,n):
for i in range(2,num):
if num%i==0:
j=num/i
print("%d equals %d * %d" %(num,i,j))
break
else:
print(num,"is a prime")

Output
Enter the limit25
1 is a prime
2 is a prime
3 is a prime
4 equals 2 * 2
5 is a prime
6 equals 2 * 3
7 is a prime
8 equals 2 * 4
9 equals 3 * 3
10 equals 2 * 5
11 is a prime
12 equals 2 * 6
13 is a prime
14 equals 2 * 7
15 equals 3 * 5
16 equals 2 * 8
17 is a prime
18 equals 2 * 9
19 is a prime
20 equals 2 * 10
21 equals 3 * 7
22 equals 2 * 11
23 is a prime
24 equals 2 * 12
>>>
Find maximum and minimum
list1=[]
n=int(input("Enter number of elements:"))
print("Enter the numbers")
for i in range(0,n):
a=int(input())
list1.append(a)
max=list1[0]
min=list1[0]
for i in range(len(list1)):
if list1[i]>max:
max=list1[i]
if list1[i]<min:
min=list1[i]
print("Maximum of the List of Number is:",max)
print("Minimum of the List of Number is:",min)

Ouput
Enter number of elements:5
Enter the numbers
23
32
432
-1
6
Maximum of the List of Number is: 432
Minimum of the List of Number is: -1
>>>
Factorial

fact=1
num=int(input("Enter the number"))
for i in range(1,num+1):
fact=fact*i
print ("The factorial of", num , "is" ,fact)

Output
Enter the number5
The factorial of 5 is 120
>>>

Fibonacci series
fib=0;a=0;b=1
num=int(input("Enter the limit"))
if(num==0):
print("0")
else:
for i in range(1,num+1):
fib=fib+a
a=b
b=fib
print(fib,end="\t")

Output

Enter the limit10


0 1 1 2 3 5 8 13 21 34
No of words count in the file

fname = input("Enter file name: ")


num_words = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
num_words += len(words)
print("Number of words:")
print(num_words)

python.txt
Hello World

Output
Enter file name: python.txt
Number of words:
2
>>>

Anda mungkin juga menyukai