0% found this document useful (0 votes)
2 views21 pages

Programming Exercises and Solutions

EXTERNAL PYTHON FILE

Uploaded by

imemyselfff01
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)
2 views21 pages

Programming Exercises and Solutions

EXTERNAL PYTHON FILE

Uploaded by

imemyselfff01
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

INDEX

[Link] Question Pg No. Sign


1 Write a program to enter two integers, two floating numbers 1
and then perform all arithmetic operations on them.
2 In this scenario, two friends are eating dinner at a restaurant. 2
The bill comes in the amount of 547.28. The friends decide to
split the bill evenly between them, after adding 15% tip for the
service. Calculate the tip, the total amount to pay, and each
friend's share, then output a message saying "Each person
needs to pay: " followed by the resulting number.
3 This code is supposed to take two numbers, divide one by 3
another so that the result is equal to 1, and display the result
on the screen. Write the code.
4 Write a program that receives a name, then returns a greeting 3
based on whether or not that name is "Taylor".
5 Write a program to check the no is odd or even. 4
6 Write a program using bitwise operator. 4
7 Write a program to find the largest no out of 2 numbers. 5
8 Write a program to find the largest no out of 3 numbers. 6
9 Find the sum of odd no. and even no from a range between 1 to 6
101.
10 Write a program to take 5 subjects marks, and compute total, 7
average and grade based on the marks.
11 Write a program to input any choice and to implement the 8-9
following.
Choice Find
1. Area of square
2. Area of rectangle
3. Area of triangle
12 Write a program using logical operator. 9
13 Write a program to check whether a number is an Armstrong 10
number or not.
14 Write a program to print the sum of all the primes between 10
two ranges.
15 Write a program to swap two strings. 11
16 Write a program to find whether the year is a leap year or not. 12
17 Write a menu driven program to accept two strings from the 13
user and perform the various functions using user-defined
functions.
18 Write a program to find the smallest and largest number in a list. 14

19 Write a program that uses a user defined function that accepts 15


name and gender (as M for Male, F for Female) and prefixes
Mr./Ms. on the basis of the gender.

20 Write a program to print the Fibonacci series using 16


recursion.
1. Write a program to enter two integers, two floating numbers and then
perform all arithmetic operations on them.

Input

Output

Priyanshu Sharma BCA 2nd Shift


2. In this scenario, two friends are eating dinner at a restaurant. The bill
comes in the amount of 547.28. The friends decide to split the bill evenly
between them, after adding 15% tip for the service. Calculate the tip, the
total amount to pay, and each friend's share, then output a message saying
"Each person needs to pay: " followed by the resulting number.

Input

Output

Priyanshu Sharma BCA 2nd Shift


3. This code is supposed to take two numbers, divide one by another so that
the result is equal to 1, and display the result on the screen. Write the
code.
Input

Output

4. Write a program that receives a name, then returns a greeting based on


whether or not that name is "Taylor".
Input

Output

Priyanshu Sharma BCA 2nd Shift


5. Write a program to check the no is odd or even.
Input

Output

6. Write a program using bitwise operator.


Input

Output

Priyanshu Sharma BCA 2nd Shift


7. Write a program to find the largest no out of 2 numbers.

Input

Output

Priyanshu Sharma BCA 2nd Shift


8. Write a program to find the largest no out of 3 numbers.
Input

Output

Priyanshu Sharma BCA 2nd Shift


9. Find the sum of odd no. and even no. from a range between 1 to 101.
Input

Output

Priyanshu Sharma BCA 2nd Shift


10. Write a program to take 5 subjects marks, and compute total, average and
grade based on the marks.

Input

Output

Priyanshu Sharma BCA 2nd Shift


11. Write a program to input any choice and to implement the following.
Choice Find
1. Area of square
2. Area of rectangle
3. Area of triangle

Input

Priyanshu Sharma BCA 2nd Shift


Output

12. Write a program using logical operator.


Input

Output

Priyanshu Sharma BCA 2nd Shift


13. Write a program to check whether a number is an Armstrong number or
not.
Input

Output

14. Write a program to print the sum of all the primes between two ranges.
Input

Output

Priyanshu Sharma BCA 2nd Shift


15. Write a program to swap two strings.

Input

Output

Priyanshu Sharma BCA 2nd Shift


16. Write a program to find out whether the year is a leap year or not.

Input

Output

Priyanshu Sharma BCA 2nd Shift


17. Write a menu-driven program to accept two strings from the user and
perform the various functions using user-defined functions.

Input

Priyanshu Sharma BCA 2nd Shift


Output

Priyanshu Sharma BCA 2nd Shift


18. Write a program to find the smallest and largest number in a list.

Input

Output

Priyanshu Sharma BCA 2nd Shift


19. Write a program that uses a user-defined function that accepts name and
gender (as M for Male, F for Female) and prefixes Mr./Ms. based on the
gender.

Input

Output

Priyanshu Sharma BCA 2nd Shift


Priyanshu Sharma BCA 2nd Shift
20. Write a program to print the Fibonacci series using recursion.
Input

Output

Priyanshu Sharma BCA 2nd Shift

Common questions

Powered by AI

Recursion in Fibonacci series involves a function calling itself with decremented counter values to calculate the previous two numbers in the series. For instance, the base case is defined for the first two numbers in the series (0 and 1). The recursive case computes the nth Fibonacci number as the sum of (n-1)th and (n-2)th Fibonacci numbers, allowing the function to build the series through successive self-reference. This approach elegantly breaks down the problem but can be inefficient due to repeated calculations without optimization techniques like memoization.

A menu-driven program presents a list of options, prompting the user to choose an operation (e.g., area calculation of a square, rectangle, or triangle). Based on user input, the program executes conditional statements or calls predefined functions to perform calculations using corresponding formulas: side² for a square, length × width for a rectangle, and 0.5 × base × height for a triangle. This structure allows for modular, user-interactive design.

The process involves iterating through each number in the range, applying a primality check to determine if the number is prime. For each prime identified, add the number to a running total. The primality check typically utilizes optimizations like checking divisibility only up to the square root of the number and using known prime numbers as factors to enhance efficiency.

User-defined functions encapsulate specific functionalities (e.g., string swapping, concatenation) that can be independently developed, tested, and reused in multiple contexts. By decoupling logic into separate functions, programs achieve higher modularity, cleaner main code, and improved maintainability. This approach also facilitates testing and scaling as individual components can be modified without affecting others.

A recursive algorithm for Fibonacci might be employed for educational purposes to demonstrate recursion's conceptual power or when the sequence's simplicity matches constraints like low input values. However, recursion introduces time complexity and stack overhead without memoization optimizations, leading to exponential runtime characteristics versus iterative solutions, which are linear. Therefore, the choice hinges on understanding computational limits versus clarity benefits.

First, calculate the 15% tip on the bill amount of $547.28, which is $82.092. Add this tip to the original bill to get the total amount of $629.372. Then, divide this total amount by 2 to find each friend's share, which results in $314.686 per person.

The algorithm involves pairwise comparisons among the three numbers. Begin by comparing the first two numbers and storing the larger of the two. Compare this stored number with the third, and store the larger from this comparison as the final largest number. This approach minimizes comparisons and efficiently identifies the largest number using conditional checks.

Bitwise operators perform operations at the binary level, manipulating bits directly, while standard arithmetic operations work on the numerical value as a whole. Bitwise operations are efficient for tasks like shifting, masking, and toggling bits, leading to potentially faster execution if used appropriately. For example, to check if a number is even, a bitwise AND with 1 can be used, rather than a modulo operation.

The program reads an integer input, applies the modulo operator with 2, and evaluates the result. If the remainder is 0, the number is even; otherwise, it is odd. This logical evaluation checks for divisibility by 2 to categorize the number as odd or even.

To determine if a number is an Armstrong number, first calculate the number of digits 'n'. For each digit, raise it to the nth power, summing these values. If the resulting sum equals the original number, it is an Armstrong number. The program requires iteration through each digit and handling of power operations. A detailed comprehension of digit extraction and mathematical computations is required for implementation.

You might also like