Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python Practical No.3 While Loop Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

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"

while var_condition.casefold() =="yes":


var_num1=int(input("Enter the First number:\n"))
var_num2=int(input("Enter the Second number:\n"))
var_sum=var_num1+var_num2
print("The Sum of {0} and {1} is".format(var_num1,var_num2),var_sum)
var_condition=(input("Do you want to continue? (yes/no)"))

else:
print("Program end initiated by the user")
OUTPUT:

3. WRITE A PYTHON PROGRAM to calculate factorial of number


SOURCE CODE:
var_number=int(input("Enter the number to get thefactorial \n"))
var_holder=var_number
var_fact=1
while var_number>0:
var_fact=var_fact*var_number
var_number=var_number-1
print("{0}! is {1}".format(var_holder,var_fact))

OUTPUT:

4. WRITE A PYTHON PROGRAM to check number is prime or not


SOURCE CODE:
var_number = int(input(" Enter any Number: \n"))
var_count = 0
i=2
while(i <= var_number//2):
if(var_number % i == 0):
var_count = var_count + 1
break
i=i+1

if (var_count == 0 and var_number != 1):


print(" {} is a Prime Number".format(var_number))
else:
print(" {} is not a Prime Number".format(var_number))

OUTPUT:

5. WRITE A PYTHON PROGRAM to check whether number is Armstrong or not


SOURCE CODE:
var_num=int(input("Enter the number: \n"))
var_sum=0

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:

7. WRITE A PYTHON PROGRAM to check whether number is palindrome or not


SOURCE CODE:
var_num=int(input("Enter a number: \n"))
var_rev=0
var_temp=var_num
while(var_num>0):
var_rem=var_num%10
var_rev=(var_rev*10)+var_rem
var_num=var_num//10

print("Since reverse of the number is = %d therefore:" %var_rev)


if(var_temp==var_rev):
print("Entered number {} is a palindrome: ".format(var_temp))
else:
print("Entered number {} is not a palindrome".format(var_temp))

OUTPUT:

8. WRITE A PYTHON PROGRAM to check whether number is perfect or not


SOURCE CODE:
var_num = int(input("Enter a number: \n"))
var_sum=1
i=2

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:

9. WRITE A PYTHON PROGRAM to print Factors of a number


SOURCE CODE:
var_num = int(input("Enter the number: \n"))
var_temp = 1
print("Factors of the entered number {0} are:".format(var_num))

while (var_temp <= var_num):


if(var_num % var_temp == 0):
print("{0}".format(var_temp))
var_temp = var_temp + 1

OUTPUT:

10. WRITE A PYTHON PROGRAM to reverse a number


SOURCE CODE:
var_num = int(input("Enter the number:\n "))
var_temp=var_num
var_rev=0
while (var_num>0):
var_rem= var_num%10
var_rev=(var_rev*10)+var_rem
var_num=var_num//10

print("\n Reverse of {} is ".format(var_temp),var_rev)

OUTPUT:

You might also like