0% found this document useful (0 votes)
8 views1 page

Python Programming Exercises List

The document lists programming exercises including writing functions to calculate sums and averages, print names, compare and swap numbers, check for prime and perfect squares, and perform other basic math and string operations.

Uploaded by

vijaymuttevi
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)
8 views1 page

Python Programming Exercises List

The document lists programming exercises including writing functions to calculate sums and averages, print names, compare and swap numbers, check for prime and perfect squares, and perform other basic math and string operations.

Uploaded by

vijaymuttevi
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

• Write a function to calculate sum of two numbers.

• Write a program to print your name 100 times.


• Write a program to Accept Student Roll No, Marks in 3 Subjects and
print it’s Average.
• Write a program to accept Three Numbers & Print the Biggest of
Given Three Numbers
• Write a program to swap two numbers using a third variable called
temp.
• Write a program to print ODD numbers from 1 to 10
• Write a program to print natural numbers from 1 to 10 in Reverse
• Write a program to accept a string in any case and print it by
another case.
• Write a program to check whether a given number is perfect square
or not.
• Write a program to print table of any number.
• Write a program to accept a string in upper case and print it by
lower case.
• Write a program to accept any single digit number and print it in
words.
• Write a program to reverse a number
• Write a program to find largest number of a list of numbers
• Write a program to calculate and print the sum of even and odd
integers of the first n natural numbers.
• Write a program to find the area of a triangle when three sides are
given.
• Write a program to check whether a number is prime number or not
• Write a program to find sum of series 1 + 2 + 3 +......+ n for any given
n.
• Write a program to find the sum of series 12+32+52+......+n2 for any
given n.
• Write a program to print given series:1 2 4 8 16 32 64 128 for any given
n; n being count of numbers in series.
• Write a program to swap two numbers without using a third variable.
• Write a program to perform all arithmetic calculations (=, -, *, /)
using switch case.
• Write a program to find the sum of individual digits of a positive
integer.
• Write a program to the number count of letters in a given text.
• Write a program to generate square root of 1 to 10.

Common questions

Powered by AI

To print a multiplication table for a given number n, iterate over a range, typically from 1 to 10 or another specified maximum. For each iteration i, compute and print the product of n and i. This can be done using a simple loop structure, effectively displaying 'n * i = result' for each line.

To determine if a given number is a perfect square, you can take the square root of the number and check if the result is an integer. In a program, this can be implemented by calculating the integer square root and squaring it again to see if you return to the original number. For example, for a given number n, compute sqrtN = integer(sqrt(n)). If sqrtN * sqrtN == n, then n is a perfect square.

To calculate the area of a triangle given its three sides a, b, and c, use Heron's formula: First compute the semi-perimeter s = (a + b + c) / 2. Then the area is sqrt(s * (s-a) * (s-b) * (s-c)). This method is efficient for any triangle as long as the sides satisfy the triangle inequality theorem.

To check if a number n is prime, first eliminate numbers less than 2. Then, check divisibility from 2 up to the square root of n. If any number in this range divides n evenly, it's not prime. The algorithm can be optimized by checking divisibility by 2 and 3 first, then using a loop with i and i+2 increments (for numbers 6k ± 1). If none divide, n is prime.

To swap two numbers without a third variable, you can use arithmetic operations. For example, if you have two numbers a and b, you can swap them by doing: 1) a = a + b, 2) b = a - b, 3) a = a - b. Alternatively, you can use bitwise XOR operations: 1) a = a ^ b, 2) b = a ^ b, 3) a = a ^ b. Both methods will result in the values of a and b being swapped without using a third variable.

To perform arithmetic operations using a switch case, first accept two operands and an operator as inputs. Use the switch construct, passing the operator as the switch expression. For each case within the switch, perform the respective operation: addition, subtraction, multiplication, or division, and print the result. Include a default case to handle invalid operators by displaying an error message.

To reverse a number programmatically, you can repeatedly extract the last digit and construct the reversed number. Initialize a result variable to 0. While the original number n is not zero, extract the last digit (n % 10), and update the result by multiplying it by 10 and adding the extracted digit. Divide n by 10 to remove the last digit. Continue until n becomes zero.

To convert a string from any case to its opposite case, iterate through each character in the string. For each character, check if it is uppercase using a function like isupper(), and if true, convert it to lowercase using tolower(). Conversely, if the character is lowercase, convert it to uppercase using toupper(). This can be implemented in most programming languages that support character operations.

To calculate the average marks from three subjects, you first need to accept the marks as inputs. Sum these three values and then divide the total by 3. In a program, you can create a function that accepts three arguments (marks in the three subjects), sums them up, and divides the result by 3 to get the average.

To find the largest number in a list, iterate through each element, maintaining a variable that tracks the largest value encountered. Initialize this variable to the first element of the list, then iterate, update this tracker if a larger value is found. After completing the loop, the tracker holds the largest number.

You might also like