17.Sample Programs using-loops and nested loops
Sum of the digits of a number
sum=0
n=int(input("Enter a number.."))
while n!=0:
sum=sum+n%10;
n=n//10
print("Sum of the digits=",sum)
Enter the 5 Numbers
4
2
6
8
1
Sum of even numbers.. 20
Write a Python program which takes a positive integer n as input and finds the sum of cubes of all positive even numbers less than or equal to the number.
output
enter (N)..5
Sum of cubes of even numbers.. 72
Input 4 integers (+ve and −ve). Write a Python code to find the sum of negative numbers, positive numbers, and print them. Also, find the averages of these two groups of numbers and print.
print("Enter a number")
num=int(input())
while num!=0:
d=num%10
rev=rev*10+d
num=num//10
print("Reverse of the number=",rev)
4 3 2 1
3 2 1
2 1
A B
sum=0
n=int(input("Enter a number.."))
while n!=0:
sum=sum+n%10;
n=n//10
print("Sum of the digits=",sum)
Output:
Enter a number..123
Sum of the digits=6
Program to check whether the given number is prime or not
n=int(input("Enter a number.."))
i=2
prime=True
while i<=n//2:
if n%i==0:
prime=False
break
i=i+1
if prime==True:
print('Prime number')
else:
print('Not a prime number')
i=2
prime=True
while i<=n//2:
if n%i==0:
prime=False
break
i=i+1
if prime==True:
print('Prime number')
else:
print('Not a prime number')
Output:
Enter a number..7
Prime number
Enter a number..4
Not a Prime number
Python program to find the sum of even numbers from N given numbers
sum=0
N=int(input("enter the number of numbers (N).."))
print("Enter the ", N ," Numbers")
for i in range(N):
num=int(input())
if num%2==0:
sum=sum+num
print("Sum of even numbers..",sum)
sum=0
N=int(input("enter the number of numbers (N).."))
print("Enter the ", N ," Numbers")
for i in range(N):
num=int(input())
if num%2==0:
sum=sum+num
print("Sum of even numbers..",sum)
Output
Enter the number of numbers (N)..5Enter the 5 Numbers
4
2
6
8
1
Sum of even numbers.. 20
sum=0
N=int(input("enter (N).."))
for num in range(1,N+1):
if num%2==0:
sum=sum+num**3
print("Sum of cubes of even numbers..",sum)
enter (N)..5
Sum of cubes of even numbers.. 72
psum=0
nsum=0
pc=0
nc=0
print("Enter the 4 Numbers +ve and -ve")
for i in range(4):
num=int(input())
if num>0:
psum=psum+num
pc=pc+1
else:
nsum=nsum+num
nc=nc+1
print("Sum of +ve numbers..",psum)
if pc!=0:print("Avg of +ve numbers..",psum/pc)
print("Sum of -ve numbers..",nsum)
if nc!=0:print("Avg of -ve numbers..",nsum/nc)
nsum=0
pc=0
nc=0
print("Enter the 4 Numbers +ve and -ve")
for i in range(4):
num=int(input())
if num>0:
psum=psum+num
pc=pc+1
else:
nsum=nsum+num
nc=nc+1
print("Sum of +ve numbers..",psum)
if pc!=0:print("Avg of +ve numbers..",psum/pc)
print("Sum of -ve numbers..",nsum)
if nc!=0:print("Avg of -ve numbers..",nsum/nc)
output
Enter the 4 Numbers +ve and -ve
2
3
-4
-1
Sum of +ve numbers.. 5
Avg of +ve numbers.. 2.5
Sum of -ve numbers.. -5
Avg of -ve numbers.. -2.5
rev=02
3
-4
-1
Sum of +ve numbers.. 5
Avg of +ve numbers.. 2.5
Sum of -ve numbers.. -5
Avg of -ve numbers.. -2.5
Write a Python program to reverse a number. Prompt the user for input.
print("Enter a number")
num=int(input())
while num!=0:
d=num%10
rev=rev*10+d
num=num//10
print("Reverse of the number=",rev)
output
Enter a number
123
Reverse of the number= 321
Generate first 10 Fibonacci numbers
a=0
b=1
for i in range(10):
c=a+b
a,b=b,c
print(c,end=' ')
ouput
1 2 3 5 8 13 21 34 55 89
Print Prime numbers less than 1000 ( university question)
print("Prime numbers less than 1000")
for n in range(2,1000):
i=2
while i<=n/2:
if n%i==0:
break
i=i+1
else:
print (n,end=' ')
output
Prime numbers less than 1000
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997
Write a python program to generate prime numbers within a certain range ( University Question)
ll=int(input('Enter lower limit '))
ul=int(input('Enter upper limit '))
print("Prime numbers in a range")
for n in range(ll+1,ul):
i=2
while i<=n/2:
if n%i==0:
break
i=i+1
else:
print (n,end=' ')
Write a Nested loop to print the following pattern
5 4 3 2 14 3 2 1
3 2 1
2 1
1
n=int(input("Enter a number::"))
for i in range(n,0,-1):
for j in range(i,0,-1):
print (j,end=' ')
print()
Print Multiplication table of 1-n numbers
n=int(input("Enter n::"))
for k in range(1,n+1):
for i in range(1,11):
print(k ,"X",i,"=",k*i)
print()
output
Enter n::2
1 X 1 = 1
1 X 2 = 2
1 X 3 = 3
1 X 4 = 4
1 X 5 = 5
1 X 6 = 6
1 X 7 = 7
1 X 8 = 8
1 X 9 = 9
1 X 10 = 10
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
Armstrong Numbers
positive integer is called an Armstrong number of order n if
abcd... = a^n + b^n + c^n + d^n + ...
In case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself.
In case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself.
For example:153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.
# Python program to check if the given 3 digit number is an Armstrong number or not
# Python program to check if the given 3 digit number is an Armstrong number or not
# take input from the user
num = int(input("Enter a number: "))
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Output
Enter a number: 663
663 is not an Armstrong number
Enter a number: 407
Enter a number: 407
407 is an Armstrong number
#Check Armstrong number of n digits
Eg:1634= 1**4+6**4+3**4+4**4=1634
num =int(input("Enter a number..."))
# Changed num variable to string, and calculated the length (number of digits)
order = len(str(num))
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
output
Enter a number...1634
1634 is an Armstrong number
Write a Python program to count number of even numbers and odd numbers in a given set of n numbers.(university question)
ecount=0
ocount=0
n=int(input('Enter n'))
for i in range(n):
num=int(input())
if num%2==0:
ecount=ecount+1
else:
ocount=ocount+1
print("Count of even numbers..",ecount)
print("Count of odd numbers..",ocount)
Write a python program to generate the following type of pattern for the given N rows .( university question)
example: N=4
1
1 2
1 2 3
1 2 3 4
r=int(input('enter number of rows'))
for i in range(1,r+1):
for j in range(1,i+1):
print(j,end=' ')
print()
Write a python program to generate the following type of pattern for the given N rows where N <= 26.(University Question)
AA B
A B C
A B C D
A B C D E
A B C D
A B C D E
s="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
r=int(input('enter number of rows r<=26'))
for i in range(1,r+1):
for j in range(0,i):
print(s[j],end=' ')
print()
OR
n=int(input("Entter the value of n : "))
for i in range(1,n+1):
for j in range(1, i+1):
print(chr(64+j),end=" ")
print()
Find the square root of a number using Newtons Method
x=int(input("Enter a number: "))
estmt=1.0
tolerance=0.000001
while True:
estmt=(estmt+x/estmt)/2
diff=abs(x-estmt**2)
if diff<=tolerance:
break
print(estmt)
print("square root of x=%0.2f"%estmt)
Read the value of x and evaluate the series e^x=1+x^1/1!+x^2/2!+....+x^n/n! up to n terms(university question)
from math import factorial
n=int(input('enter n:'))
x=int(input('enter x:'))
sumex=0
for i in range(n):
sumex=sumex+(x**i/factorial(i))
print("sum of the series=",sumex)
Write a python program to find the sum of the cosine series 1 - x^2/2! + x^4/4!-..n ( University question)
import math
n=int(input('enter n:'))
x=int(input('enter x in degrees:'))
x=math.radians(x)
cosx=0.0
for i in range(n):
term=((-1)**i) *( x**(2*i) )/math.factorial(2*i)
cosx=cosx+term
print("sum of the series=",cosx)
Output:
enter n:10 enter x in degrees:60
sum of the series= 0.5000000000000001
Comments
Post a Comment