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

Python LabExp1

Python

Uploaded by

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

Python LabExp1

Python

Uploaded by

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

EX.NO.

: 1
DISPLAYING FIRST ‘N’ NATURAL NUMBERS
DATE:

AIM:
To write a Python program for displaying first ‘N’ natural numbers.

ALGORITHM:

Step 1 : Start the program


Step 2 : Read the value of n
Step 3 : for i in range (1,n+1,1) do
print the value of i
Step 4 : Stop the program

PYTHON CODE:

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


print("The first",n,"natural numbers are:")
for i in range(1,n+1,1):
print(i)

OUTPUT:

Enter the value of n: 5


The first 5 natural numbers are:
1
2
3
4
5

RESULT:

Thus the Python program for displaying first ‘N’ natural numbers is written and output is
verified.
EX.NO.: 2
DISPLAYING FIRST ‘N’ ODD NUMBERS
DATE:

AIM:
To write a Python program for displaying first ‘N’ odd numbers.

ALGORITHM:

Step 1 : Start the program


Step 2 : Read the value of n
Step 3 : for i in range (1,n+1,1) do
print the value of 2*i-1
Step 4 : Stop the program

PYTHON CODE:

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


print("The first",n,"odd numbers are:")
for i in range(1,n+1,1):
print(2*i-1)

OUTPUT:

Enter the value of n: 5


The first 5 odd numbers are:
1
3
5
7
9

RESULT:

Thus the Python program for displaying first ‘N’ odd numbers is written and output is verified.
EX.NO.: 3
DISPLAYING FIRST ‘N’ EVEN NUMBERS
DATE:

AIM:
To write a Python program for displaying first ‘N’ even numbers.

ALGORITHM:

Step 1 : Start the program


Step 2 : Read the value of n
Step 3 : for i in range (1,n+1,1) do
print the value of 2*i
Step 4 : Stop the program

PYTHON CODE:

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


print("The first",n,"even numbers are:")
for i in range(1,n+1,1):
print(2*i)

OUTPUT:

Enter the value of n: 5


The first 5 even numbers are:
2
4
6
8
10

RESULT:

Thus the Python program for displaying first ‘N’ even numbers is written and output is verified.
EX.NO.:4
ODD OR EVEN NUMBER (ALTERNATIVE IF-ELSE)
DATE:

AIM:
To write a Python program to check whether the given number is odd or even.

ALGORITHM:

Step 1 : Start the program


Step 2 : Read the value of n
Step 3 : if (n%2==0)
Print n is even
else
Print n is odd
Step 4 : Stop the program

PYTHON CODE:

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


if(n%2==0):
print(n,"is even")
else:
print(n,"is odd")

OUTPUT:

Enter the value of n: 3


3 is odd

Enter the value of n: 4


4 is even

RESULT:

Thus the Python program to check whether the given number is odd or even is written and
output is verified.
EX.NO.:6
COUNTING NUMBER OF DIGITS IN A NUMBER
DATE:

AIM:
To write a Python program for counting number of digits in a given number.

ALGORITHM:

Step 1 :
Start the program
Step 2 :
Read the value of n
Step 3 :
Assign n1=n
Step 4 :
Initialize count=0
Step 5 :
if (n>0), goto step 6 else goto step 7
Step 6 :
Compute count=count+1
n=n//10, goto step 5
Step 7 : Print the value of count
Step 8 : Stop the program
PYTHON CODE:

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


n1=n
count=0
while(n>0):
count=count+1
n=n//10
print("Number of digits in",n1,"is:",count)

OUTPUT:

Enter the value of n: 178


Number of digits in 178 is: 3

RESULT:

Thus the Python program for counting number of digits in a given number is written and
output is verified.
EX.NO.: 18
COMPUTING REVERSE OF A NUMBER
DATE:

AIM:
To write a Python program for computing reverse of a given number.

ALGORITHM:

Step 1 :
Start the program
Step 2 :
Read the value of n
Step 3 :
Assign n1=n
Step 4 :
Initialize sum=0
Step 5 :
if (n>0), goto step 6 else goto step 7
Step 6 :
Compute rem=n%10
sum=sum*10+rem
n=n//10, goto step 5
Step 7 : Print the value of sum
Step 8 : Stop the program
PYTHON CODE:

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


n1=n
sum=0
while(n>0):
rem=n%10
sum=sum*10+rem
n=n//10
print("Reverse of",n1,"is",sum)

OUTPUT:

Enter the value of n: 234


Reverse of 234 is 432

RESULT:

Thus the Python program for computing reverse of a given number is written and output is
verified.
EX.NO.:17
PALINDROME NUMBER OR NOT
DATE:

AIM:
To write a Python program to check whether the given number is palindrome or not.

ALGORITHM:

Step 1 :
Start the program
Step 2 :
Read the value of n
Step 3 :
Assign n1=n
Step 4 :
Initialize sum=0
Step 5 :
if (n>0), goto step 6 else goto step 7
Step 6 :
Compute rem=n%10
sum=sum*10+rem
n=n//10, goto step 5
Step 7 : if (sum==n1)
Print n1 is palindrome
else
Print n1 is not palindrome
Step 8 : Stop the program
PYTHON CODE:

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


n1=n
sum=0
while(n>0):
rem=n%10
sum=sum*10+rem
n=n//10
if(sum==n1):
print(n1, "is a palindrome number")
else:
print(n1, "is not a palindrome number")

OUTPUT:

Enter the value of n: 232


232 is a palindrome number

Enter the value of n: 233


233 is not a palindrome number

RESULT:

Thus the Python program to check whether the given number is palindrome or not is written
and output is verified.
EX.NO.: 16
ARMSTRONG NUMBER OR NOT
DATE:

AIM:
To write a Python program to check whether the given number is Armstrong number or not.

ALGORITHM:

Step 1 :
Start the program
Step 2 :
Read the value of n
Step 3 :
Assign n1=n
Step 4 :
Initialize sum=0
Step 5 :
if (n>0), goto step 6 else goto step 7
Step 6 :
Compute rem=n%10
sum=sum+rem**3
n=n//10, goto step 5
Step 7 : if (sum==n1)
Print n1 is an Armstrong number
else
Print n1 is not an Armstrong number
Step 8 : Stop the program
PYTHON CODE:

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


n1=n
sum=0
while(n>0):
rem=n%10
sum=sum+rem**3
n=n//10
if(sum==n1):
print(n1, "is an Armstrong number")
else:
print(n1, "is not an Armstrong number")

OUTPUT:

Enter the value of n: 153


153 is an Armstrong number

Enter the value of n: 123


123 is not an Armstrong number

RESULT:

Thus the Python program to check whether the given number is Armstrong number or not is
written and output is verified.
EX.NO.:9
FACTORIAL OF A GIVEN NUMBER
DATE:

AIM:
To write a Python program for computing the factorial of a given number.

ALGORITHM:

Step 1 :
Start the program
Step 2 :
Read the value of n
Step 3 :
Initialize fact=1
Step 4 :
for i in range (1,n+1,1) do
compute fact=fact* i
Step 5 : Print the value of fact
Step 6 : Stop the program

PYTHON CODE:

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


fact=1
for i in range(1,n+1,1):
fact=fact*i
print(n,"factorial is",fact)

OUTPUT:

Enter the value of n: 4


4 factorial is 24

RESULT:

Thus the Python program for computing the factorial of a given number is written and output
is verified.

You might also like