Adding 2 numbers:
num1 = float(input(“Enter the first number: “)) num2 = float(input(“Enter the second number: “)) sum = num1 + num2 print(“The sum of”, num1, “and”, num2, “is”, sum) |
Arithmetic Operations:
num1 = float(input(“Enter the first number: “)) num2 = float(input(“Enter the second number: “)) sum = num1 + num2 difference = num1 – num2 product = num1 * num2 quotient = num1 / num2 remainder = num1 % num2 floordiv = num1 // num2 print(“Sum:”, sum) print(“Difference:”, difference) print(“Product:”, product) print(“Quotient:”, quotient) print(“Remainder :”, remainder) print(“Floor Division :”, floordiv) |
Program to display the last digit of given number:
num = int(input(“Enter a number: “)) last_digit = num % 10 print(“The last digit of”, num, “is”, last_digit) |
Progam to remove last digit of given number:
num = int(input(“Enter a number: “)) num = num//10 print(“The number with the last digit removed is”, num) |
Find Total and Average of 4 numbers:
mark1 = float(input(“Enter the first mark: “)) mark2 = float(input(“Enter the second mark: “)) mark3 = float(input(“Enter the third mark: “)) mark4 = float(input(“Enter the fourth mark: “)) average = (mark1 + mark2 + mark3 + mark4) / 4 print(“The average of the four marks is”, average) |
Find sum of square and cube of given number:
num = int(input(“Enter a number: “)) square = num ** 2 cube = num ** 3 sum = square + cube print(“The sum of the square and cube of”, num, “is”, sum) |
Calculate Total Salary for given basic Salary:
basic_salary = float(input(“Enter the basic salary: “)) # Calculate the allowances and deductions hra = 0.2 * basic_salary da = 0.1 * basic_salary pf = 0.05 * basic_salary # Calculate the gross and net salary gross_salary = basic_salary + hra + da net_salary = gross_salary – pf # Print the result print(“Basic salary:”, basic_salary) print(“HRA:”, hra) print(“DA:”, da) print(“PF:”, pf) print(“Gross salary:”, gross_salary) print(“Net salary:”, net_salary) |
Swapping 2 numbers:
num1 = float(input(“Enter the first number: “)) num2 = float(input(“Enter the second number: “)) # Before swapping print(“Before swapping:”) print(“num1 =”, num1) print(“num2 =”, num2) # Swap the values temp = num1 num1 = num2 num2 = temp # After swapping print(“After swapping:”) print(“num1 =”, num1) print(“num2 =”, num2) |
Swapping 2 number without third variable:
# Take input from the user num1 = float(input(“Enter the first number: “)) num2 = float(input(“Enter the second number: “)) # Swap the values without using a third variable num1, num2 = num2, num1 # After swapping print(“After swapping:”) print(“num1 =”, num1) print(“num2 =”, num2) |
Another Way:
// Swap the values without using a third variable num1 = num1 + num2; num2 = num1 – num2; num1 = num1 – num2; |