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

Python Lab File

This document contains details of 15 experiments conducted in a Python programming lab. It includes the student's name, roll number, program, section, and details of each experiment including the date, aim, code, and output. The experiments cover Python concepts like if/else statements, nested if, checking even/odd numbers, prime numbers, Armstrong numbers, Fibonacci series, and summing ranges of numbers.

Uploaded by

sajwanisanskar3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Python Lab File

This document contains details of 15 experiments conducted in a Python programming lab. It includes the student's name, roll number, program, section, and details of each experiment including the date, aim, code, and output. The experiments cover Python concepts like if/else statements, nested if, checking even/odd numbers, prime numbers, Armstrong numbers, Fibonacci series, and summing ranges of numbers.

Uploaded by

sajwanisanskar3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Bhavya|210160212089|T2

Python programming Lab


CAP 2009L

School of Engineering and Sciences


Department of Computer Sciences

Submitted By
Student Name Bhavya
Roll No 210160212089
Programme BCA
Section/Group T2
Department Computer Science and Engineering
Session/Semester 2021-24/5th Semester
Submitted To
Faculty Name Ms. Sapna Pandey

G.D Goenka University


Gurugram, Haryana
Bhavya|210160212089|T2

S No Date Aim the Experiment Signature

1 10/10/23 Write a python program using the If statement.

2 10/10/23 Write a python program using the Else statement.

3 10/10/23 Write a python program using the Elif statement.

4 10/10/23 Write a python program using the Nested-If


statement.
5 11/10/23 Write a program with Python script to check if a
number is even or odd.
6 11/10/23 Write a python program to check if the year is a
leap year or not.
7 11/10/23 Write a python program to add 2 numbers.

8 11/10/23 Write a python program to add the first 10 natural


numbers.
9 11/10/23 Write a python program to add the first 10 natural
numbers in reverse.

10 12/10/23 Write a python program to check if the given


number is prime or not.
11 12/10/23 Write a python program to find the fibonacci
series.
12 12/10/23 Write a python program to check if the given
number is an Armstrong number or not.
13 12/10/23 Write a python program to add the numbers upto
“n” numbers.
Bhavya|210160212089|T2

14 12/10/23 Write a python program to add the first 10 natural


numbers in reverse.

15 12/10/23 Write a python program to check if the given


number is prime or not.
Bhavya|210160212089|T2

Experiment No.1

Aim: Write a python program using the “If “ statement.

Code:
a = 33
b = 200
if b > a:
print("b is greater than a")

Output:
b is greater than a
Bhavya|210160212089|T2

Experiment No.2

Aim: Write a python program using the Else-if statement.

Code:
number = 10
if number > 0:
print('Positive number')
else:
print('Negative number')
print('This statement is always executed')

Output:
Positive number This statement is always executed
Bhavya|210160212089|T2

Experiment No. 3

Aim: Write a python program using the Elif statement.

Code:
number = 0

if number > 0:
print("Positive number")

elif number == 0:
print('Zero')
else:
print('Negative number')

print('This statement is always executed')

Output:
Zero
This statement is always executed
Bhavya|210160212089|T2

Experiment No. 4

Aim: Write a python program using the Nested if statement.

Code:
number = 5

# outer if statement
if (number >= 0):
# inner if statement
if number == 0:
print('Number is 0')

# inner else statement


else:
print('Number is positive')

# outer else statement


else:
print('Number is negative')

Output:
Number is positive
Bhavya|210160212089|T2

Experiment No. 7

Aim: Write a program with Python script to check if a number is even or odd.

Code:
num = int (input (“Enter any number to test whether it is odd or even: “)
if (num % 2) == 0:
print (“The number is even”)
else:
print (“The provided number is odd”)

Output:

Enter any number to test whether it is odd or even:

887

887 is odd
Bhavya|210160212089|T2

Experiment No. 8

Aim: Write a python program to check if the year is a leap year or not.

Code:
year = int(input("Enter year to be checked: "))

if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print("The year is a leap year!")
else:
print("The year is not a leap year!")
else:
print("The year is a leap year!")
else:
print("The year is not a leap year!")

Output:
Enter year to be checked: 2012
The year is a leap year!
Bhavya|210160212089|T2

Experiment No. 9

Aim: Write a python program to add 2 numbers.

Code:
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')

sum = float(num1) + float(num2)

print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

Output:
Enter first number: 1.5
Enter second number: 6.3
The sum of 1.5 and 6.3 is 7.8
Bhavya|210160212089|T2

Experiment No. 10

Aim: Write a python program to add the first 10 natural numbers.

Code:
print("Enter the Value of n: ")
n = int(input())
sum = 0
i=1
while i<=n:
sum = sum+i
i = i+1

print("\nSum =", sum)

Output:
Enter the Value of n:
10

Sum = 55
Bhavya|210160212089|T2

Experiment No. 11

Aim: Write a python program to find the sum of the first 10 natural numbers in
reverse order.

Code:
sum = 0

for i in range(10, 0, -1):

sum += i

print("The sum of the first 10 natural numbers in reverse order is:", sum)

Output:

The sum of the first 10 natural numbers in reverse order is: 55


Bhavya|210160212089|T2

Experiment No. 12

Aim: Write a python program to check if the given number is a prime number or
not.

Code:

def is_prime(num):
if num <= 1:
return False
elif num <= 3:
return True
elif num % 2 == 0 or num % 3 == 0:
return False
i=5
while i * i <= num:
if num % i == 0 or num % (i + 2) == 0:
return False
i += 6
return True

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


if is_prime(number):
print(f"{number} is a prime number")
else:
print(f"{number} is not a prime number")
Output:

Enter a number: 7

7 is a prime number

Enter a number: 12

12 is not a prime number


Bhavya|210160212089|T2

Experiment No. 13

Aim: Write a python program for the Fibonacci series of the number given by the
user.

Code:

def generate_fibonacci(n):
fibonacci_series = [0, 1]
while True:
next_number = fibonacci_series[-1] + fibonacci_series[-2]
if next_number <= n:
fibonacci_series.append(next_number)
else:
break
return fibonacci_series
num = int(input("Enter a number to generate Fibonacci series up to that number:
"))

if num <= 0:
print("Please enter a positive number.")
else:
fibonacci_series = generate_fibonacci(num)
print("Fibonacci series up to", num, "is:", fibonacci_series)
Output:

Enter a number to generate Fibonacci series up to that number: 20


Bhavya|210160212089|T2

Fibonacci series up to 20 is: [0, 1, 1, 2, 3, 5, 8, 13]

Experiment No. 14

Aim: Write a python number to check if the number given by the user is an
Armstrong number or not.

Code:

def is_armstrong_number(num)
num_str = str(num)
num_digits = len(num_str)

armstrong_sum = sum(int(digit) ** num_digits for digit in num_str)

return armstrong_sum == num

num = int(input("Enter a number to check if it's an Armstrong number: "))

if is_armstrong_number(num):
print(f"{num} is an Armstrong number")
else:
print(f"{num} is not an Armstrong number")

Output:

Enter a number to check if it's an Armstrong number: 153

153 is an Armstrong number


Bhavya|210160212089|T2

Experiment No. 15

Aim: Write a python program to find the sum upto “n” numbers.

Code:

n = int(input("Enter a positive integer 'n': "))


if n < 0:
print("Please enter a positive integer.")
else:
total = 0
for i in range(1, n + 1):
total += i

print(f"The sum of numbers up to {n} is: {total}")

Output:

Enter a positive integer 'n': 5

The sum of numbers up to 5 is: 15


Bhavya|210160212089|T2

You might also like