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

Python Programming Basics: 10 Programs

Uploaded by

MADHAV D
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views21 pages

Python Programming Basics: 10 Programs

Uploaded by

MADHAV D
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

INDEX

PYTHON PROGRAMMING

No Experiments [Link] Signature

1 Program to perform arithmetic operations

2 Program to find largest of three numbers

3 Program to check whether a given number is


prime or not

4 Program to find sum of digits of a number

5 Program to display multiplication table

6 Program to display fibonacci sequence

7 Program to check whether a given number is


even or odd using function

8 Program to find factorial of number using


recursion

9 Program to print pattern

10 Program to implement string operations

Output:
enter the number:2
enter the number:3
enter the operation to be performed(+,-,/,*,%):+
the sum: 5

enter the number:3


enter the number:1
enter the operation to be performed(+,-,/,*,%):-
the difference: 2

1. Program to perform arithmetic operations.


a=int(input("enter the number:"))
b=int(input("enter the number:"))
c=input("enter the operation to be performed(+,-,/,*,%):")
if(c=='+'):
print("the sum:",a+b)
elif(c=='-'):
print("the difference:",a-b)
elif(c=='*'):
print("the product:",a*b)
elif(c=='/'):
print("the division:",a/b)
elif(c=='%'):
print("the modulus:",a%b)
else:
print("the invalid operation")

Output:
enter the number:2
enter the number:3 17 enter the number:4
the largest number is 4

2. Program to find largest of three numbers.


a=int(input("enter the number:"))
b=int(input("enter the number:"))
c=int(input("enter the number:"))
if(a>b and a>c):
print("the largest number is",a)
elif(a<b and b>c):
print("the largest number is",b)
elif(c>a and b<c):
print("the largest number is",c)
else:
print(“all numbers are equal")

Output:
enter the number:5
the given number is prime

[Link] to check whether a given number prime or not.


a=int(input("enter the number:"))
for i in range(2,a):
if(a%i==0):
print("the given number is not prime")
break
else:
print("the given number is prime")
break

Output:
enter the number:123
6

4. Program to find sum of digits of a number.


def getsum(a):
sum=0
while(a!=0):
sum=sum+(a%10)
a=a//10
return sum
a=int(input("enter the number:"))
print(getsum(a))

Output:
Enter the number:4
multiplication table of: 4
4*1=4
4*2=8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40

5. Program to display multiplication table.


n=int(input("Enter the number:"))
print("multiplication table of:",n)
for i in range(1,11):
print(n,'*',i,'=',n*i)

Output:
how many terms?5
fibonacci sequence:
0
1
1
2
3

6. Program to display fibanocci sequence.


nterms=int(input("how many terms?"))
n1,n2=0,1
count=0
if nterms<=0:
print("enter a positive number")
elif nterms==1:
print("fibonacci sequence upto",nterms,":")
print(n1)
else:
print("fibonacci sequence:")
while count<nterms:
print(n1)
nth=n1+n2
n1=n2
n2=nth
count+=1

Output:
enter the number:4
4 is EVEN

7. Program to check whether a given number is even or odd using function.


n=int(input("enter the number:"))
def pro(a):
if(a%2)==0:
print(a,"is EVEN")
else:
print(a,"is not EVEN")
pro(n)

Output:
Enter the number:5
120

8. Program to find factorial of a number using recursion.


def recur_factorial(n):
if(n==1):
return n
else:
return n* recur_factorial(n-1)
b=int(input("enter the number:"))
print(recur_factorial(b))

Output:
enter the number:6
*
**
***
****
*****
******

9. Program to print pattern.


def pypart(n):
for i in range(0,n):
for j in range(0,i+1):
print("*",end="")
print("\r")
c=int(input("enter the number:"))
pypart(c)

Output:
Enter a string::123
Enter another string:456
Length of the string: 3
Reapeating: 123123
Concatenation: 123456
Changing case: 123 456
Counting substrings in a string: 0

10. Program to implement string operations.


a=input("Enter a string::")
b=input("Enter another string:")
print("Length of the string:",len(a))
print("Reapeating:",a*2)
print("Concatenation:",a+b)
print("Changing case:",[Link](),"\t",[Link]())
print("Counting substrings in a string:",[Link](b))

Common questions

Powered by AI

Recursion in Python for calculating factorials can lead to excessive use of stack space, especially with large numbers, due to each recursive call using additional stack space. This can result in stack overflow errors. Python's recursion limit could be hit if the number is too large, making iteration a safer choice for very large calculations .

A base case is essential in recursion to terminate the recursive calls and begin unwinding the stack, thus preventing infinite recursion. In the factorial example, if(n==1): return n serves as the base case. Without it, the function would continue calling itself indefinitely, leading to a stack overflow. The base case provides a stopping condition that allows the stack to reverse and compute a final result .

The Fibonacci sequence is generated using a loop that maintains the current and next Fibonacci numbers, updating them in each iteration until the required number of terms is generated. The sequence starts with 0 and 1, updating these values iteratively. Optimization can be achieved using a matrix exponentiation or dynamic programming to reduce redundant calculations, which can be important when generating a large number of terms .

The method checks if a number a is not divisible by any number from 2 to a-1, using a loop to perform these divisibility tests. An improvement would be only checking up to the square root of a, as any factor larger than the square root would have a corresponding factor smaller than the square root. This reduces the number of checks, making it more efficient, especially for larger numbers .

The pattern printing program uses nested loops to print an increasing number of asterisks in each row, effectively printing a right-angled triangle. Flexibility could be improved by allowing users to specify different symbols or patterns and differentiate input for row count and column alterations more dynamically. Incorporating parameters for alignment and justification could also be beneficial .

The program demonstrates basic string operations like length calculation, repetition, concatenation, case transformation, and substring counting. A strength is its demonstration of essential string manipulations. However, it could be improved by handling scenarios like case insensitivity in substring counting and applying checks for invalid input to increase robustness. Additionally, including more complex operations like regex-based manipulations could make it more comprehensive .

Recursion in finding factorial works by having a function that calls itself with a decremented value until it reaches the base case. In the example provided, the function recur_factorial(n) multiplies n by the result of recur_factorial(n-1) until n equals 1, at which point it returns 1. This allows the intermediate results to be multiplied back up to get the final factorial value .

The algorithm employs a while loop that iteratively reduces the input number by extracting and removing its last digit until it becomes zero, accumulating the extracted digits' sum. This approach is efficient for small numbers but executes in linear time with respect to the number of digits (O(d)). It can't be significantly optimized in terms of speed as each digit must be read, but ensuring input validation and using memoization for common calls could improve resource use .

Key considerations include validating that inputs are of the expected type and format, handling exceptions that may arise from improper inputs, providing clear prompts and error messages for users, and ensuring inputs are sanitized to prevent potential security issues. In these programs, failure to perform such checks could lead to runtime errors or incorrect operation results .

The program determines if a number is even or odd by using modulo division. It checks if the number modulo 2 equals zero, which indicates an even number, otherwise, it's odd. This could be simplified by directly using the expression n % 2 == 0 within a print statement for succinctness, as defining a separate function for this operation is unnecessary in simple scripts .

You might also like