Featured

    Featured Posts

For loop in Python||Part-2



Write a program to display table for given number in Python 

n=int(input('enter number'))
for i in range(1,11):
    t=n*i
    print(n," * ",i," = ",t)


 

§  Write a program Calculate the sum and average of first n natural numbers.


n = int(input("Enter Number to calculate sum and avg of first n natural number"))
sum = 0
for num in range(1, n+1):
    sum = sum+num
print("SUM of first ", n, "numbers is: ", sum )
avg=sum/n
print("Average of first ", n, "numbers is: ", avg )

Output:
Enter Number to calculate sum and avg of first n natural number 5
SUM of first  5 numbers is:  15
Average of first  5 numbers is:  3.0

Write a program to display the sum first n even and odd number in Python 


n=int(input('enter number'))

esum=0
osum=0
for i in range(1,n+1):
    if i%2==0:
        esum=esum+i
    else:
        osum=osum+i
print('Sum of First ',n,'Even number = ',esum)
print('Sum of First ',n,'Odd number = ',osum)

Output:
enter number10
Sum of First  10 Even number =  30
Sum of First  10 Odd number =  25


§  Write a program to display the factor of a given number.
n=int(input('enter number'))
print('Factor of ',n,end='')
for i in range(1,n+1):
    if n%i==0:
        print(i,sep=',',end='')
Output:
enter number 6
Factor of  6 is 1,2,3,6

§  Write a program to find the factorial of a given number.
What is a factorial?

§  In maths, the factorial of a non-negative integer, is the product of all positive integers less than or equal to this non-negative integer.

For example:

5! = 5 * 4 * 3 * 2 * 1 = 120

4! = 4 * 3 * 2 * 1 = 24

n factorial” is “
n! = n * (n — 1) * (n — 2) *…* 2 * 1,” where ’n’ is a positive integer.

n=int(input('enter number'))
f=1
for i in range(1,n+1):
    f=f*i
print('factorial of',n,' is ',f)
Output:
enter number5
factorial of 5  is  120


author

Author Name

Author Description!

Get Free Email Updates to your Inbox!

1 comments:

Post a Comment

www.CodeNirvana.in

Powered by Blogger.

About

Site Links

Popular Posts

Translate

Total Pageviews