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

Python Programs for Basic Number Operations

The document contains a series of Python programming exercises that cover basic concepts such as even/odd checking, positive/negative/zero identification, summation of natural numbers, multiplication tables, counting even and odd numbers, summing digits, skipping multiples of three, finding prime numbers, and reversing integers. Each exercise includes the code implementation along with user input prompts and output statements. The author identifies themselves as Nabihah with a specific roll number.

Uploaded by

story forever
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)
7 views4 pages

Python Programs for Basic Number Operations

The document contains a series of Python programming exercises that cover basic concepts such as even/odd checking, positive/negative/zero identification, summation of natural numbers, multiplication tables, counting even and odd numbers, summing digits, skipping multiples of three, finding prime numbers, and reversing integers. Each exercise includes the code implementation along with user input prompts and output statements. The author identifies themselves as Nabihah with a specific roll number.

Uploaded by

story forever
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

#Easy

"""
1. Write a Python program that takes an integer as input and prints whether it is
even or odd using an if-else statement.
"""
#Name and roll number:
print("Nabihah, PG/01/MSASDS/2025/014")

#Taking input:
number = int(input("Enter a number to check whethere it is even or odd: "))

#if-else statments to find even and odd:


if number % 2 == 0: #Even
print("The number ", number, " is even.")

else: #Odd
print("The number ", number, " is odd.")

"""
2. Write a program that accepts a number from the user and checks whether it is
positive, negative, or zero using if-elif-else.
"""
#Name and roll number:
print("Nabihah, PG/01/MSASDS/2025/014")

#Taking input:
number = int(input("Enter a number to check whethere it is +ve, -ve or 0: "))

#if-elif-else statments:
if number > 0:
print("The number ", number, " is +ve.")
elif number < 0:
print("The number ", number, " is -ve.")
else:
print("The number ", number, " is 0.")

"""
3. Take a number n as input and use a while loop to calculate the sum of the
first n natural numbers.
"""
#Name and roll number:
print("Nabihah, PG/01/MSASDS/2025/014")

#Taking input:
n = int(input("Enter the number upto which we calculate the sum of: "))

#Variables to calculate the sum:


ans = 0
i = 0 #iteration count

#Sum of first n natural numbers:


while i <= n:
ans = ans + i
i = i + 1

#Printing the answer:


print("The sum of first ", n, " natural numbers is: ", ans)

1
"""
4. Write a program to print the multiplication table of a number entered by the
user.
"""
#Name and roll number:
print("Nabihah, PG/01/MSASDS/2025/014")

#Taking input:
n = int(input("Enter the number for which the multiplication table will be genrated: "))

#Generating multiples upto 10:


for i in range(1, 11):
print(n, " x " , i, " = ", n*i)

"""
5. Use a for loop and if-else to count how many even and odd numbers are there
between 1 and a given number N.
"""
#Name and roll number:
print("Nabihah, PG/01/MSASDS/2025/014")

#Taking input:
N = int(input("Enter a number for which we are going to show the of count how many",
"even and odd numbers are there between 1 and the given number: "))

#For keeping count of the number of odd/even numbers:


count_even = 0
count_odd = 0
i = 1

#A 'for' loop going upto the given number:


for i in range(1, N+1):
#An 'if-else' loop to count the number of even/odd numbers:
if i % 2 == 0:
#Even
count_even = count_even + 1
else:
#Odd
count_odd = count_odd + 1

#Printing the answer:


print("Upto ", N, " natural number, there are ", count_even, " even numbers and ",
count_odd, " odd numbers.")

"""
6. Write a program that takes a positive integer as input and finds the sum of its digits
using a while loop.
"""
#Name and roll number:
print("Nabihah, PG/01/MSASDS/2025/014")

#Taking inputs:
number = int(input("Enter a +ve integer to find the sum of its digits: "))

#Making sure number given is +ve:

2
if number < 0:
print("Enter the proper number!!")
number = int(input("Enter a +ve integer to find the sum of its digits: "))

#Counting the number of digits in the number:


number_string = str(number) #Convert int to str
number_digit_max = len(number_string) #Number of digits in the given number

#Keeping track of the sum of digits:


sum_digits = 0
i = 0

while i in range(0, number_digit_max):


number_digit = number_string[i]
sum_digits = sum_digits + int(number_digit)
i = i + 1

#Printing the answer:


print("The sum of digits of ", number, " is: ", sum_digits)

"""
7. Write a program that prints numbers from 1 to 20, but skips the numbers that are
multiples of 3 using the continue statement.
"""
#Name and roll number:
print("Nabihah, PG/01/MSASDS/2025/014")

#Printing all numbers from 1 to 20:


for i in range(1, 21):
#Skip, if multiple of three
if i % 3 == 0:
continue #Skiped
print(i)

"""
8. Take an integer N and find the first prime number greater than n using loops and
the break statement.
"""
#Name and roll number:
print("Nabihah, PG/01/MSASDS/2025/014")

#A function to find prime numbers:


def Prime(n, i):
if i == 1 or i == 2:
return True
if n % i == 0:
return False
return Prime(n, i - 1)

#Taking input:
n = int(input("Enter an integer to find the first prime number greater than it: "))

#Smallest number greater than the given number:


number = n + 1

#A loop to check if the number greater than the given is prime:


while True:

3
#A var to hold the bollean response of Prime function:
is_prime = Prime(number, number - 1)
#If number is prime, break:
if is_prime:
print("The first prime number greater than", n, "is", number)
break
number += 1

"""
9. Write a program that reverses an integer without converting it to a string, using
arithmetic and a while loop.
"""
#Name and roll number:
print("Nabihah, PG/01/MSASDS/2025/014")

#Taking input:
n = int(input("Enter an integer to be reversed: "))

#Store the sign:


if n < 0:
sign = -1
else:
sign = 1

#Storing the value of n:


n = abs(n)

#A variable to save the reversed number:


reversed_num = 0

#Reversing the number:


while n > 0:
digit = n % 10 # Gets the last digit
reversed_num = reversed_num * 10 + digit # Add to the reversed number
n //= 10 # Remove last digit from n

#Apply the original sign:


reversed_num *= sign

#Printing the solution:


print("The reversed integer is: ", reversed_num)

"""
------------------------------ End ------------------------------------------
"""

You might also like