Python Programs for Basic Calculations
Python Programs for Basic Calculations
The Python program checks if a number is a prime by confirming that no divisor exists other than 1 and itself. It first excludes numbers less than 2, since these are not prime. For numbers 2 or greater, it assumes primacy and iteratively checks divisibility from 2 up to the square root of the number. If any divisor is found (i.e., num % i == 0), the number is marked not prime and exits the loop. Otherwise, if no divisors are located, it confirms the number as prime .
The Python program checks if a character is a vowel or a consonant by first lowering the input character to handle case sensitivity. It then checks if the character is one of 'a', 'e', 'i', 'o', or 'u'. If the character matches any of these, it is identified as a vowel. If the character is alphabetic but not a vowel, it is classified as a consonant. If the input is not alphabetic, it prompts the user to enter a valid alphabetic character .
The program determines the evenness or oddness of a number by using a simple if-else conditional structure. It utilizes the modulus operator to check if num % 2 equals 0. If it does, the number is even. Otherwise, it is odd. The program then prints the result accordingly .
The Python program generates the Fibonacci series by starting with the first two terms, 0 and 1. It (conditionally) prints only 0 for n=1 or both 0 and 1 for n=2. For n greater than 2, it uses a loop to iteratively calculate subsequent terms by summing the two preceding numbers. It adjusts the values of the two preceding terms after each iteration to progress through the series. This loop runs n-2 times, effectively generating n terms of the Fibonacci sequence .
The Python program calculates compound interest using the formula: amount = principal * (1 + rate / (100 * n))^(n * time). First, the user inputs the principal amount, annual interest rate, time in years, and the number of times the interest is compounded per year. The program then computes the total amount by raising the interest term (1 + rate/(100*n)) to the power of (n*time) and multiplying by the principal. The compound interest is found by subtracting the principal from this total amount. The program finally displays both the total amount after the specified time and the calculated compound interest .
The Python program determines the largest of three numbers by using conditional statements. It compares the three numbers, num1, num2, and num3. First, it checks if num1 is greater than or equal to both num2 and num3, and if so, num1 is assigned as the largest. If not, it checks if num2 is the greatest. If neither num1 nor num2 are the largest, num3 is automatically considered the largest. This decision-making is done via if-elif-else blocks .
The Python program classifies a triangle based on the lengths of its sides using conditional checks. If all three sides are equal, the triangle is classified as 'Equilateral'. If only two sides are equal, it is labeled as 'Isosceles'. If all sides are different, the triangle is classified as 'Scalene'. These classifications are implemented through a series of if-elif-else statements which check the equality of the sides .
The Python program classifies a number as positive, negative, or zero using nested if-elif-else conditional statements. It first checks if the number is greater than zero to classify it as positive. If it is not, it checks if it is less than zero, which would make it negative. If neither condition is satisfied, the number is classified as zero .
The BMI Python program categorizes weight status by calculating the BMI value using the formula: BMI = weight (kg) / (height (m)^2). It then uses a set of conditional statements to determine the category based on standard BMI thresholds. The program identifies 'Underweight' for a BMI less than 18.5, 'Normal weight' for BMI between 18.5 and 24.9, 'Overweight' for BMI between 25 and 29.9, and 'Obese' for BMI of 30 or more. These thresholds are used in conjunction with if-elif-else structures to display the appropriate category .
The Python program converts a temperature from Celsius to Fahrenheit by using the formula: Fahrenheit = (Celsius * 9/5) + 32. It prompts the user for the Celsius temperature input, performs the conversion calculation, and then displays the resulting temperature in Fahrenheit .