Wednesday, June 14, 2023

#PYTHON(DAY49)Example Programs

                                    #PYTHON(DAY49)

Example Programs

1) Python Program to Find Sum of n Natural Numbers

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)

And the output is:

Enter the value of n:
10

sum = 55

2) Python Program to Add Digits of a Number

print("Enter a Number")
num = int(input())
sum = 0
while num>0:
rem = num%10
sum = sum+rem
num = int(num/10)
print("\nSum of Digits of Given Number: ", sum)

And the output is:

Enter a Number:
123

Sum of Digits of Given Number: 6

3) Sum of First and Last digit of a Number

print("Enter a Number: ")
num = int(input())

count = 0
while num!=0:
if count==0:
last = num%10
count = count+1
rem = num%10
num = int(num/10)

sum = rem + last
print("\nSum of first and last digit =", sum)

And the output is:

Enter a Number:
1203

Sum of first and last digit = 4

No comments:

Post a Comment

Building Static Website(part6) HTML Lists

  Building Static Website (part6) HTML Lists Today, let us add some lists to our detailed view section by using html lists. Lists: List is a...