Python Practical No.3 While Loop Programs
Python Practical No.3 While Loop Programs
Python Practical No.3 While Loop Programs
1. WRITE A PYTHON PROGRAM using while loop that asks the user for a number, and
prints the digits of the number on screen
Enter a number:456
Digits in number are: 4 5 6
SOURCE CODE:
var_num = int(input("Enter the number\n"))
var_digit=0
var_temp=var_num
var_str1=""
while var_temp>0 :
var_digit = var_temp%10
var_temp = var_temp//10
var_str1 = var_str1 + str(var_digit)+ " "
print(var_str1[::-1])
OUTPUT:
2. WRITE A PYTHON PROGRAM to perform addition of 2 numbers till the user wishes
SOURCE CODE:
var_condition="Yes"
else:
print("Program end initiated by the user")
OUTPUT:
OUTPUT:
OUTPUT:
temp=var_num
while temp > 0:
var_digit = temp % 10
var_sum = var_sum + var_digit ** 3
temp = temp//10
if var_num == var_sum:
print("Entered number {} is an armstrong number".format(var_num))
else:
print("Entered number {} is not an armstrong number".format(var_num))
OUTPUT:
6. WRITE A PYTHON PROGRAM to display prime numbers between 1to100
SOURCE CODE:
var_num=1
while(var_num<=100):
var_count=0
i=2
while(i<=var_num//2):
if(var_num%i==0):
var_count=var_count+1
break
i=i+1
if(var_count==0 and var_num!=1):
print("%d"%var_num, end=' ')
var_num=var_num+1
OUTPUT:
OUTPUT:
while(i<=var_num//2):
if(var_num%i==0):
var_sum=var_sum+i
i=i+1
if(var_sum==var_num):
print("Yes the entered number {} is a perfect number: ".format(var_num))
else:
print("No the entered number {} is not a perfect number".format(var_num))
OUTPUT:
OUTPUT:
OUTPUT: