0% found this document useful (0 votes)
4 views4 pages

Python Basic Programs With Output Explanation

The document contains basic Python programs with their respective outputs and explanations for various tasks such as checking even/odd, prime numbers, palindromes, factorials, Fibonacci series, reversing numbers, summing digits, finding GCD and LCM, converting decimal to binary, and manipulating arrays and strings. Each program is accompanied by a brief explanation of its logic. The examples demonstrate fundamental programming concepts and operations in Python.

Uploaded by

jawahar2007001
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Python Basic Programs With Output Explanation

The document contains basic Python programs with their respective outputs and explanations for various tasks such as checking even/odd, prime numbers, palindromes, factorials, Fibonacci series, reversing numbers, summing digits, finding GCD and LCM, converting decimal to binary, and manipulating arrays and strings. Each program is accompanied by a brief explanation of its logic. The examples demonstrate fundamental programming concepts and operations in Python.

Uploaded by

jawahar2007001
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Basic Programs with Output and

Explanation

1) Even / Odd
Program:
num = 4
if num % 2 == 0:
print("Even")
else:
print("Odd")

Output:
Even

Explanation:
If remainder when divided by 2 is 0 → Even, otherwise Odd.

------------------------------------------------------------

2) Prime Number
Program:
num = 7
count = 0
for i in range(1, num+1):
if num % i == 0:
count += 1
if count == 2:
print("Prime")
else:
print("Not Prime")

Output:
Prime

Explanation:
Prime number has exactly 2 factors (1 and itself).

------------------------------------------------------------

3) Palindrome Number
Program:
num = 121
temp = num
rev = 0
while num > 0:
rev = rev * 10 + num % 10
num = num // 10
if temp == rev:
print("Palindrome")
else:
print("Not Palindrome")

Output:
Palindrome

Explanation:
Reverse the number and compare with original.

------------------------------------------------------------

4) Factorial
Program:
num = 5
fact = 1
for i in range(1, num+1):
fact *= i
print(fact)

Output:
120

Explanation:
Multiply numbers from 1 to n.

------------------------------------------------------------

5) Fibonacci
Program:
n = 5
a, b = 0, 1
for i in range(n):
print(a, end=" ")
a, b = b, a+b

Output:
0 1 1 2 3

Explanation:
Each number is sum of previous two numbers.

------------------------------------------------------------

6) Reverse Number
Program:
num = 123
rev = 0
while num > 0:
rev = rev * 10 + num % 10
num = num // 10
print(rev)

Output:
321

Explanation:
Extract digits and rebuild in reverse order.

------------------------------------------------------------

7) Sum of Digits
Program:
num = 123
total = 0
while num > 0:
total += num % 10
num //= 10
print(total)

Output:
6

Explanation:
Add each digit using modulus operator.

------------------------------------------------------------

8) GCD
Program:
a, b = 12, 18
while b != 0:
a, b = b, a % b
print(a)
Output:
6

Explanation:
Using Euclid’s algorithm.

------------------------------------------------------------

9) LCM
Program:
a, b = 12, 18
x, y = a, b
while y != 0:
x, y = y, x % y
gcd = x
lcm = (a*b)//gcd
print(lcm)

Output:
36

Explanation:
LCM = (a × b) / GCD

------------------------------------------------------------

10) Decimal to Binary


Program:
num = 10
binary = ""
while num > 0:
binary = str(num%2) + binary
num //= 2
print(binary)

Output:
1010

Explanation:
Divide by 2 repeatedly and store remainders.

------------------------------------------------------------

11) Max, Min, Sum, Average (Array)


Program:
arr = [10,20,5,40]
max_val = min_val = arr[0]
total = 0
for i in arr:
if i > max_val:
max_val = i
if i < min_val:
min_val = i
total += i
print(max_val, min_val, total, total/len(arr))

Output:
40 5 75 18.75

Explanation:
Compare each element manually.

------------------------------------------------------------

12) Reverse String


Program:
s = "hello"
rev = ""
for i in s:
rev = i + rev
print(rev)

Output:
olleh

Explanation:
Add characters in reverse order.

------------------------------------------------------------

13) Triangle Pattern


Program:
n = 3
for i in range(1,n+1):
for j in range(i):
print("*", end=" ")
print()

Output:
*
* *
* * *

Explanation:
Outer loop controls rows, inner loop prints stars.

You might also like