Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Python_Programs_All_Questions

Pdfm

Uploaded by

pandi6382u
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)
15 views

Python_Programs_All_Questions

Pdfm

Uploaded by

pandi6382u
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
You are on page 1/ 5

Python Programs with Outputs

1. Program to find the sum of first 'n' odd numbers.

def sum_of_odds(n):

return sum(2 * i + 1 for i in range(n))

n = int(input("Enter the value of n: "))

print("Sum of first", n, "odd numbers is:", sum_of_odds(n))

Example Output:

Input: n = 5

Output: Sum of first 5 odd numbers is: 25

2. Program to find the roots of a quadratic equation.

import cmath

def quadratic_roots(a, b, c):

d = cmath.sqrt(b**2 - 4*a*c)

root1 = (-b + d) / (2 * a)

root2 = (-b - d) / (2 * a)

return root1, root2

a = float(input("Enter coefficient a: "))

b = float(input("Enter coefficient b: "))

c = float(input("Enter coefficient c: "))


roots = quadratic_roots(a, b, c)

print("Roots of the quadratic equation are:", roots)

Example Output:

Input: a=1, b=-3, c=2

Output: Roots of the quadratic equation are: (2+0j), (1+0j)

3. Program to print the digit at one's place of a number.

number = int(input("Enter a number: "))

print("The digit at one's place is:", number % 10)

Example Output:

Input: number = 1234

Output: The digit at one's place is: 4

4. Program to multiply two numbers.

def multiply(a, b):

return a * b

a = float(input("Enter first number: "))

b = float(input("Enter second number: "))

print("The product of", a, "and", b, "is:", multiply(a, b))

Example Output:

Input: a=4, b=5

Output: The product of 4 and 5 is: 20


5. Program to print the sum of cubes of the values of n variables.

n = int(input("Enter the number of values: "))

values = [int(input("Enter value {}: ".format(i + 1))) for i in range(n)]

print("Sum of cubes is:", sum(x**3 for x in values))

Example Output:

Input: n=3, values=[1,2,3]

Output: Sum of cubes is: 36

6. Program to accept two numbers, find the greatest, and print the result.

a = int(input("Enter the first number: "))

b = int(input("Enter the second number: "))

print("The greatest number is:", max(a, b))

Example Output:

Input: a=10, b=15

Output: The greatest number is: 15

7. Program to find the sum of 'n' even numbers.

def sum_of_evens(n):

return sum(2 * i for i in range(1, n + 1))

n = int(input("Enter the value of n: "))

print("Sum of first", n, "even numbers is:", sum_of_evens(n))

Example Output:

Input: n=4
Output: Sum of first 4 even numbers is: 20

8. Program to find the factorial of a number (with and without recursion).

# Without recursion

def factorial_iterative(n):

result = 1

for i in range(1, n + 1):

result *= i

return result

# With recursion

def factorial_recursive(n):

if n == 0 or n == 1:

return 1

return n * factorial_recursive(n - 1)

n = int(input("Enter a number: "))

print("Factorial (iterative):", factorial_iterative(n))

print("Factorial (recursive):", factorial_recursive(n))

Example Output:

Input: n=5

Output: Factorial (iterative): 120

Factorial (recursive): 120

9. Program to generate first 'n' Fibonacci numbers.


def fibonacci(n):

fib_sequence = [0, 1]

for i in range(2, n):

fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])

return fib_sequence[:n]

n = int(input("Enter the value of n: "))

print("First", n, "Fibonacci numbers are:", fibonacci(n))

Example Output:

Input: n=5

Output: First 5 Fibonacci numbers are: [0, 1, 1, 2, 3]

10. Program to find the sum of first 'n' odd numbers (function-based).

def sum_of_odds(n):

return sum(2 * i + 1 for i in range(n))

n = int(input("Enter the value of n: "))

print("Sum of first", n, "odd numbers is:", sum_of_odds(n))

Example Output:

Input: n=3

Output: Sum of first 3 odd numbers is: 9

You might also like