Today we solve 10 basic problems using python programming language, it’s very easy to learn, also a trending language today.
1. To Add Two Numbers
We’re currently practicing performing basic addition operations with two integer numbers.
firstNum = int(input("enter 1st num"))
secondNum = int(input("enter 2nd num"))
def sum (a,b):
c = a+b
return c
sum(firstNum, secondNum)
# input 5 , 6
# output 11
2. Convert Celsius To Fahrenheit
We can implement the formula for converting Celsius to Fahrenheit in Python code by using the expression (0°C × 9/5) + 32 = 32°F.
c_temp = int(input("Enter Celsius Temperature: "))
f_temp = ((c_temp*9)/5)+32
print("Fahrenheit:",f_temp)
# input 36
# output 96.8
3. Convert Kilometers To Miles
We know 1 kilometer is approximately equal to 0.62 miles, we can easily calculate the distance in miles by multiplying the distance in kilometers by 0.62.
def kmToMiles(km):
return km*0.62
km = int(input("Enter Km: "))
print("Miles: ",kmToMiles(km))
# input 5
# output 3.1
4. Is A Number Positive Or Negative
We can easily determine whether a number is positive or negative using this logic: if the number is equal to or greater than zero, then it’s positive; otherwise, it’s negative.
def isPositive(num):
check = "positive" if num >=0 else "negetive"
return check
num = int(input("Enter number: "))
print(isPositive(num))
# input 2
# output positive
# input -3
# output negetive
10 Simple Coding Problems with Solution in Python
5. Is A Number Even Or Odd
We can determine whether a number is even or odd by dividing it by 2 and checking the remainder (modulus). If the remainder is zero, then the number is even; otherwise, it’s odd.
def evenOrOdd(num):
if num != 0:
check = "even" if num%2==0 else "odd"
return check
else:
return "not even or odd"
num = int(input("Enter number: "))
print(evenOrOdd(num))
# input 2
# output even
# input 3
# output odd
6. Find the Largest Number from a List
Here, we’re leveraging Python’s default function max()
. Later on, we’ll explore implementing custom methods using loops or recursion.
numArray = [2,5,3,1,4]
print("max number is", max(numArray))
# input [2,5,3,1,4]
# output 5
7. Determine whether a year is a leap year or not
We can determine whether a year is a leap year by the following rules: if the year is divisible by 400, it’s a leap year. If it’s not divisible by 400 but divisible by 4 and not divisible by 100, then it’s also a leap year; otherwise, it’s not leap year.
def isLeapYear(year):
if year%400==0 or (year%4==0 and year%100!=0):
print("leap year")
else:
print("not leap year")
year = int(input("Enter Year: "))
isLeapYear(year)
# input 1996
# output leap year
# input 2300
# output not leap year
8. Find The Factorial Of A Number
The factorial of a non-negative integer 𝑛n, denoted by 𝑛!n!, is the product of all positive integers less than or equal to 𝑛n. For example:
𝑛!=𝑛×(𝑛−1)×(𝑛−2)×…×3×2×1n!=n×(n−1)×(n−2)×…×3×2×1
For instance, 5!=5×4×3×2×1=1205!=5×4×3×2×1=120.
def factorial(num):
output=1
while(num>1):
output *= num
num -= 1
return output
num = int(input("Enter a number: "))
print(factorial(num))
# input 5
# output 120
9. Is A Number Prime Or Not
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In other words, a prime number is only divisible by 1 and itself. For example, 2, 3, 5, 7, 11, and 13 are prime numbers because they cannot be divided evenly by any other numbers except 1 and themselves.
If you don’t know python syntax, then you can learn from here
See same basic problems with solutions in javascript