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

Python Number Algorithms Explained

The document outlines algorithms and Python programs for four tasks: checking if a number is positive/negative and even/odd, finding the sum of the first 10 natural numbers, checking if a number is a palindrome, and finding the sum of the digits of a number. Each task includes a step-by-step algorithm followed by the corresponding Python code implementation. The document serves as a guide for basic programming exercises involving number manipulation.

Uploaded by

Soumen Swarnakar
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)
102 views2 pages

Python Number Algorithms Explained

The document outlines algorithms and Python programs for four tasks: checking if a number is positive/negative and even/odd, finding the sum of the first 10 natural numbers, checking if a number is a palindrome, and finding the sum of the digits of a number. Each task includes a step-by-step algorithm followed by the corresponding Python code implementation. The document serves as a guide for basic programming exercises involving number manipulation.

Uploaded by

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

Check whether number is positive/negative and even/odd

Algorithm:
a. Start
b. Input a number → num
c. If num > 0:
If num % 2 == 0: print “Positive and Even”
Else: print “Positive and Odd”
d. Else if num < 0:
If num % 2 == 0: print “Negative and Even”
Else: print “Negative and Odd”
e. Else: print “Number is Zero”
f. Stop

Python Program:
num = int(input("Enter a number: ")) if num > 0: if num % 2 == 0: print("Positive
and Even") else: print("Positive and Odd") elif num < 0: if num % 2 == 0:
print("Negative and Even") else: print("Negative and Odd") else: print("The number
is Zero")

2. Find sum of first 10 natural numbers

Algorithm:
a. Start
b. Initialize sum = 0
c. Loop i from 1 to 10
sum = sum + i
d. After loop ends, print sum
e. Stop

Python Program:
sum = 0 for i in range(1, 11): sum += i print("Sum of first 10 natural numbers:",
sum)

3. Check whether a number is palindrome or not

Algorithm:
a. Start
b. Input a number → num
c. Store a copy → temp = num
d. Reverse the number:
Initialize rev = 0
While num > 0:
digit = num % 10
rev = rev * 10 + digit
num = num // 10
e. If temp == rev: print “Palindrome”
Else: print “Not Palindrome”
f. Stop
Python Program:
num = int(input("Enter a number: ")) temp = num rev = 0 while num > 0: digit = num %
10 rev = rev * 10 + digit num //= 10 if temp == rev: print("Palindrome Number")
else: print("Not a Palindrome Number")

4. Find sum of digits of a number

Algorithm:
a. Start
b. Input a number → num
c. Initialize sum = 0
d. While num > 0:
digit = num % 10
sum = sum + digit
num = num // 10
e. Print sum
f. Stop

Python Program:
num = int(input("Enter a number: ")) sum = 0 while num > 0: digit = num % 10 sum +=
digit num //= 10 print("Sum of digits:", sum)

You might also like