0% found this document useful (0 votes)
9 views2 pages

Number Checks and Swaps in Python

The document provides Python code snippets for basic number operations including checking if a number is even or odd, determining if a number is positive, negative, or zero, swapping two numbers, checking if a number is prime, and verifying if a number is an Armstrong number. Each section includes example code demonstrating the respective functionality. These examples serve as foundational programming exercises for beginners.

Uploaded by

vithyathar14
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)
9 views2 pages

Number Checks and Swaps in Python

The document provides Python code snippets for basic number operations including checking if a number is even or odd, determining if a number is positive, negative, or zero, swapping two numbers, checking if a number is prime, and verifying if a number is an Armstrong number. Each section includes example code demonstrating the respective functionality. These examples serve as foundational programming exercises for beginners.

Uploaded by

vithyathar14
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

1.

Even or Odd

Check if a number is even or odd using the modulo operator.

num = int(input('Enter a number: '))

if num % 2 == 0:

print('Even')

else:

print('Odd')

2. Positive, Negative, or Zero

Determine the sign of a number.

num = float(input('Enter a number: '))

if num > 0:

print('Positive')

elif num < 0:

print('Negative')

else:

print('Zero')

3. Swap Two Numbers

Swap values of two variables with and without a third variable.

# Using third variable

a, b = 5, 10

temp = a

a = b

b = temp

print(a, b)

# Without third variable


a, b = 5, 10

a, b = b, a

print(a, b)

4. Prime Number Check

Check whether a number is prime.

num = int(input('Enter a number: '))

if num > 1:

for i in range(2, int(num**0.5)+1):

if num % i == 0:

print('Not Prime')

break

else:

print('Prime')

else:

print('Not Prime')

5. Armstrong Number

Check if a number is an Armstrong number.

num = int(input('Enter a number: '))

digits = [int(d) for d in str(num)]

if sum(d**len(digits) for d in digits) == num:

print('Armstrong')

else:

print('Not Armstrong')

You might also like