0% found this document useful (0 votes)
3 views

Example program for Control Statements

The document contains Python code snippets for various mathematical operations, including calculating the sum of n numbers, factorial of a number, sum of digits, reversing a number, checking for Armstrong numbers, and determining if a number is a palindrome. Each operation is accompanied by example inputs and outputs. The code uses loops and basic arithmetic to perform the calculations.

Uploaded by

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

Example program for Control Statements

The document contains Python code snippets for various mathematical operations, including calculating the sum of n numbers, factorial of a number, sum of digits, reversing a number, checking for Armstrong numbers, and determining if a number is a palindrome. Each operation is accompanied by example inputs and outputs. The code uses loops and basic arithmetic to perform the calculations.

Uploaded by

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

Sum of n numbers:

n=eval(input("enter n"))
i=1
sum=0
while(i<=n):
sum=sum+i
i=i+1
print(sum)

Output
enter n
10
55

Factorial of a numbers:
n=eval(input("enter n"))
i=1
fact=1
while(i<=n):
fact=fact*i
i=i+1
print(fact)

Output
enter n
5
120
Sum of digits of a number:
n=eval(input("enter a number"))
sum=0
while(n>0):
a=n%10
sum=sum+a
n=n//10
print(sum)

Output
enter a number
123
6

Reverse the given number:


n=eval(input("enter a number"))
sum=0
while(n>0):
a=n%10
sum=sum*10+a
n=n//10
print(sum)

Output
enter a number
123
321
Armstrong number or not
n=eval(input("enter a number"))
org=n
sum=0
while(n>0):
a=n%10
sum=sum+a*a*a
n=n//10
if(sum==org):
print("The given number is Armstrong number")
else:
print("The given number is not
Armstrong number")

Output
enter a number153
The given number is Armstrong number
Palindrome or not
n=eval(input("enter a number"))
org=n
sum=0
while(n>0):
a=n%10
sum=sum*10+a
n=n//10
if(sum==org):
print("The given no is palindrome")
else:
print("The given no is not palindrome")

Output
enter a number121
The given no is palindrome

You might also like