[go: up one dir, main page]

0% found this document useful (0 votes)
18 views7 pages

Assignment 3

The document provides various examples of Python programming concepts, including loops (while and for), and tasks such as printing personal information, generating multiplication tables, calculating sums, checking for palindromes and Armstrong numbers, computing factorials, and identifying prime numbers. Each example includes code snippets demonstrating the implementation of these concepts. Additionally, it covers more complex tasks like finding numbers divisible by the sum of their digits and generating Fibonacci sequences.

Uploaded by

yash1215singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Assignment 3

The document provides various examples of Python programming concepts, including loops (while and for), and tasks such as printing personal information, generating multiplication tables, calculating sums, checking for palindromes and Armstrong numbers, computing factorials, and identifying prime numbers. Each example includes code snippets demonstrating the implementation of these concepts. Additionally, it covers more complex tasks like finding numbers divisible by the sum of their digits and generating Fibonacci sequences.

Uploaded by

yash1215singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Examples:-

while loop:

In [13]: i=1
while i<=10:
print(i,end=' ')
i+=1

1 2 3 4 5 6 7 8 9 10

For loop:

In [8]: i=1
n=10
for i in range(i,n+1):
print(i,end=' ')

1 2 3 4 5 6 7 8 9 10

In [12]: i=0
n=20
for i in range(i,n+1,2):
print(i,end=' ')

0 2 4 6 8 10 12 14 16 18 20

Assignment-3
1. Write a Python program to your name, phone number, and email 10 times.

In [3]: i=1
while i<=10:
print('Sash, 73***55, sash@gmail.com')
i+=1

Sash, 73***55, sash@gmail.com


Sash, 73***55, sash@gmail.com
Sash, 73***55, sash@gmail.com
Sash, 73***55, sash@gmail.com
Sash, 73***55, sash@gmail.com
Sash, 73***55, sash@gmail.com
Sash, 73***55, sash@gmail.com
Sash, 73***55, sash@gmail.com
Sash, 73***55, sash@gmail.com
Sash, 73***55, sash@gmail.com

2. Write a Python program to print the multiplication table of a given number.

In [4]: i=1
n=int(input("enter a no. for multiplication table"))
while i<=10:
print(n,' x ',i,'=',n*i)
i+=1

11 x 1 = 11
11 x 2 = 22
11 x 3 = 33
11 x 4 = 44
11 x 5 = 55
11 x 6 = 66
11 x 7 = 77
11 x 8 = 88
11 x 9 = 99
11 x 10 = 110

3. Write a Python program to compute the sum of squares of first n natural


numbers(12+ 22 + 32 + ... + n2) using loop.

In [5]: i=1
n=int(input("enter a natural no."))
for i in range(i,n+1):
s=(i*(i+1)*(2*i+1))/6
print(s)

385.0

4. Write a Python program to compute the sum 1/1 + 2/3 + 3/5 + 4/7 +.......... nth term

In [6]: i=1
s=0
n=int(input("enter a natural no."))
for i in range(i,n+1):
s=i/(2*i-1)+s
print(s)

6.066627765079777

5. Write a Python program to compute the sum of digits of a given number.

In [7]: i=1
s=0
n=int(input("enter a natural no."))
for i in range(i,n+1):
s+=n%10
n//=10
print(s)

15

6. Write a Python program to check whether the given number is a palindrome or


not.

In [10]: n = int(input("Enter a natural number: "))


t = n
rev = 0
while t > 0:
rem=t%10
rev=(rev*10)+rem
t//= 10
if n == rev:
print("Palindrome")
else:
print("Not Palindrome")

Palindrome

7. Write a Program to check whether the given number is an Armstrong number or


not. (e.g. 153 is Armstrong’s number, as 13 + 53 + 33 = 153.)

In [11]: n = int(input("Enter a 3 digit number: "))


t = n
arm = 0
while t > 0:
rem=t%10
arm+=rem*rem*rem
t//= 10
if n == arm:
print("Armstrong")
else:
print("Not Armstrong")

Armstrong

8. Write a Python program to compute the factorial of a number.

In [13]: i=1
fact=1
n=int(input("enter a natural no."))
for i in range(i,n+1):
fact*=i
print('factorial of ',n,'is :',fact)

factorial of 5 is : 120

9. Write a Python program to print prime numbers between a given range.

In [12]: s=int(input("enter start of range"))


e=int(input("enter end of range"))
for num in range(s,e+1):
if num > 1:
isPrime = True
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
isPrime = False
break
if isPrime:
print(num, end=" ")

11 13 17 19 23 29

10. Write a Python program to print first n Fibonacci numbers.


In [14]: i,n1,n2=1,0,1
n=int(input("enter a no.:"))
print(n1,n2)
for i in range(i,n-1):
n3=n1+n2
print(n3, end=' ')
n1=n2
n2=n3

0 1
1 2 3 5 8 13 21 34

11. Write a Python program to find the numbers, which are divisible by the sum of
their digits. (e.g. 12) between 1 to 10000.

In [17]: print("Numbers divisible by the sum of their digits :")


for num in range(1, 10001):
t = num
digit_sum = 0
while t > 0:
digit_sum += t % 10
t //= 10
if num % digit_sum == 0:
print(num, end=" ")
Numbers divisible by the sum of their digits :
1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 45 48 50 54 60 63 70 72 80 81 84 90
100 102 108 110 111 112 114 117 120 126 132 133 135 140 144 150 152 153 156 162 171 18
0 190 192 195 198 200 201 204 207 209 210 216 220 222 224 225 228 230 234 240 243 247
252 261 264 266 270 280 285 288 300 306 308 312 315 320 322 324 330 333 336 342 351 36
0 364 370 372 375 378 392 396 399 400 402 405 407 408 410 414 420 423 432 440 441 444
448 450 460 465 468 476 480 481 486 500 504 506 510 511 512 513 516 518 522 531 540 55
0 552 555 558 576 588 592 594 600 603 605 612 621 624 629 630 640 644 645 648 660 666
684 690 700 702 704 711 715 720 730 732 735 736 738 756 770 774 777 780 782 792 800 80
1 803 804 810 820 825 828 832 840 846 864 870 874 880 882 888 900 902 910 912 915 918
935 936 954 960 966 972 990 999 1000 1002 1008 1010 1011 1012 1014 1015 1016 1017 1020
1026 1032 1035 1040 1044 1050 1053 1056 1062 1066 1071 1080 1088 1090 1092 1095 1098 1
100 1101 1104 1107 1110 1116 1120 1122 1125 1128 1130 1134 1140 1141 1143 1148 1152 11
60 1161 1164 1168 1170 1180 1183 1185 1188 1200 1204 1206 1212 1215 1220 1224 1230 123
2 1233 1236 1242 1251 1260 1270 1272 1274 1275 1278 1296 1300 1302 1304 1305 1308 1310
1314 1320 1323 1330 1332 1341 1344 1350 1360 1365 1368 1380 1386 1387 1394 1400 1404 1
410 1413 1416 1417 1422 1431 1440 1450 1452 1455 1456 1458 1476 1494 1500 1503 1512 15
20 1521 1524 1526 1530 1534 1540 1545 1547 1548 1558 1560 1566 1584 1590 1596 1602 161
1 1620 1630 1632 1635 1638 1651 1652 1656 1659 1674 1679 1680 1692 1701 1704 1710 1720
1725 1728 1729 1740 1744 1746 1764 1770 1782 1785 1800 1810 1812 1815 1818 1836 1848 1
853 1854 1860 1872 1886 1890 1896 1898 1900 1904 1905 1908 1920 1926 1944 1950 1962 19
68 1974 1980 1998 2000 2001 2004 2007 2010 2016 2020 2022 2023 2024 2025 2028 2030 203
4 2040 2043 2052 2061 2064 2070 2080 2085 2088 2090 2100 2106 2112 2115 2119 2120 2124
2130 2133 2136 2142 2151 2156 2159 2160 2170 2172 2175 2176 2178 2196 2200 2202 2205 2
208 2210 2212 2214 2220 2223 2232 2236 2240 2241 2244 2250 2260 2265 2268 2280 2282 22
86 2289 2300 2304 2310 2312 2313 2316 2322 2331 2340 2350 2352 2353 2355 2358 2376 239
4 2398 2400 2401 2403 2408 2412 2421 2424 2430 2440 2445 2448 2460 2464 2465 2466 2470
2478 2484 2490 2502 2511 2520 2530 2532 2534 2535 2538 2556 2574 2580 2584 2592 2596 2
600 2601 2604 2608 2610 2618 2620 2625 2628 2640 2646 2660 2664 2667 2670 2682 2688 27
00 2704 2710 2712 2715 2718 2736 2752 2754 2755 2760 2771 2772 2790 2793 2794 2800 280
5 2808 2820 2821 2826 2844 2850 2856 2862 2880 2889 2912 2916 2919 2924 2926 2934 2940
2952 2970 2976 2982 2992 2997 3000 3006 3012 3015 3020 3024 3030 3031 3032 3033 3036 3
038 3042 3051 3055 3060 3070 3072 3075 3077 3078 3080 3096 3097 3100 3102 3104 3105 31
08 3110 3114 3120 3123 3132 3141 3144 3150 3160 3164 3165 3168 3172 3180 3184 3186 320
0 3204 3210 3213 3216 3220 3222 3231 3240 3250 3252 3255 3258 3268 3276 3290 3294 3297
3300 3303 3312 3320 3321 3324 3328 3330 3340 3345 3348 3360 3366 3383 3384 3388 3390 3
402 3406 3411 3416 3420 3430 3432 3435 3438 3439 3456 3472 3474 3480 3486 3492 3501 35
04 3510 3520 3523 3525 3528 3536 3540 3542 3546 3549 3564 3570 3582 3586 3600 3610 361
2 3615 3616 3618 3636 3640 3654 3660 3672 3675 3690 3696 3699 3700 3705 3708 3720 3726
3738 3744 3749 3750 3760 3762 3768 3780 3781 3784 3816 3834 3840 3842 3852 3864 3870 3
888 3900 3904 3906 3920 3924 3927 3930 3942 3952 3956 3960 3969 3980 3982 3984 3990 39
96 4000 4002 4005 4008 4010 4014 4020 4023 4032 4040 4041 4044 4046 4048 4050 4060 406
5 4068 4070 4080 4086 4100 4102 4104 4108 4110 4112 4113 4116 4122 4131 4140 4148 4150
4152 4155 4158 4172 4176 4179 4192 4194 4200 4203 4212 4221 4224 4225 4230 4240 4245 4
248 4260 4266 4284 4290 4294 4302 4311 4320 4330 4332 4335 4336 4338 4342 4356 4368 43
74 4378 4380 4392 4400 4401 4404 4410 4420 4424 4425 4428 4440 4446 4454 4464 4465 447
0 4480 4482 4488 4494 4500 4510 4512 4515 4518 4536 4550 4554 4557 4560 4572 4576 4577
4590 4600 4605 4607 4608 4620 4624 4626 4636 4644 4650 4662 4680 4683 4698 4716 4734 4
740 4746 4752 4760 4770 4774 4776 4779 4784 4800 4802 4806 4807 4809 4810 4824 4830 48
42 4848 4860 4872 4880 4887 4912 4913 4914 4920 4932 4935 4950 4968 4972 4975 4988 499
1 4992 4995 5000 5004 5010 5013 5016 5022 5031 5040 5044 5050 5052 5054 5055 5056 5058
5060 5066 5076 5094 5100 5103 5110 5112 5120 5121 5124 5130 5140 5145 5148 5149 5160 5
161 5166 5180 5184 5187 5190 5198 5202 5211 5219 5220 5230 5232 5235 5238 5256 5274 52
80 5292 5301 5304 5306 5310 5320 5325 5328 5340 5344 5346 5364 5368 5370 5372 5376 538
2 5400 5410 5412 5415 5418 5432 5436 5439 5454 5460 5472 5490 5491 5496 5500 5505 5508
5512 5520 5525 5526 5544 5550 5562 5565 5566 5568 5580 5589 5616 5628 5632 5634 5640 5
652 5662 5670 5691 5697 5700 5706 5724 5730 5742 5754 5760 5764 5778 5780 5784 5810 58
14 5817 5819 5820 5831 5832 5833 5850 5856 5859 5875 5876 5880 5886 5904 5910 5920 592
2 5928 5940 5943 5960 5962 5967 5994 6000 6003 6012 6021 6024 6030 6040 6045 6048 6050
6060 6062 6064 6066 6069 6084 6090 6102 6111 6120 6130 6132 6135 6137 6138 6156 6174 6
175 6180 6192 6195 6200 6201 6204 6208 6210 6214 6220 6225 6228 6240 6246 6258 6264 62
70 6282 6288 6290 6300 6310 6312 6314 6315 6318 6331 6336 6346 6352 6354 6358 6360 637
2 6384 6390 6399 6400 6405 6408 6420 6426 6440 6443 6444 6447 6450 6462 6480 6516 6517
6534 6540 6552 6556 6570 6573 6576 6578 6588 6600 6606 6624 6630 6636 6640 6642 6647 6
648 6660 6669 6680 6696 6714 6720 6732 6750 6754 6762 6775 6777 6792 6804 6810 6822 68
25 6840 6854 6858 6860 6864 6885 6900 6902 6912 6930 6936 6939 6951 6952 6966 6993 700
0 7002 7011 7020 7030 7032 7033 7035 7038 7040 7055 7056 7070 7072 7074 7077 7080 7092
7101 7104 7110 7120 7125 7128 7140 7146 7150 7164 7170 7182 7200 7208 7210 7212 7215 7
216 7218 7236 7254 7260 7266 7268 7272 7290 7296 7300 7305 7308 7320 7322 7326 7329 73
44 7348 7350 7360 7361 7362 7368 7372 7380 7392 7398 7416 7434 7440 7452 7455 7470 747
5 7479 7500 7501 7504 7506 7514 7518 7524 7530 7542 7543 7546 7560 7580 7581 7584 7587
7588 7598 7614 7620 7632 7644 7650 7656 7668 7675 7682 7695 7700 7704 7707 7710 7714 7
722 7728 7740 7744 7748 7749 7760 7770 7776 7800 7812 7820 7830 7833 7857 7859 7872 78
84 7902 7920 7938 7940 7942 7944 7965 7982 7992 8000 8001 8004 8010 8020 8025 8028 803
0 8040 8046 8056 8064 8070 8080 8082 8085 8088 8096 8100 8110 8112 8115 8118 8126 8136
8148 8154 8160 8172 8190 8200 8203 8204 8205 8208 8220 8224 8226 8227 8244 8250 8262 8
274 8280 8289 8316 8320 8330 8334 8337 8338 8340 8352 8370 8376 8397 8400 8406 8424 84
30 8432 8442 8448 8460 8463 8478 8480 8512 8514 8520 8526 8532 8536 8550 8559 8575 858
6 8592 8596 8604 8610 8622 8640 8652 8660 8664 8667 8684 8694 8700 8712 8715 8717 8730
8734 8736 8740 8748 8775 8800 8802 8808 8820 8829 8840 8841 8848 8856 8880 8883 8904 8
910 8911 8918 8924 8932 8937 8952 8959 8964 8991 9000 9010 9012 9015 9018 9020 9022 90
36 9044 9054 9060 9072 9082 9090 9093 9096 9099 9100 9105 9108 9120 9126 9144 9150 915
6 9162 9168 9180 9212 9216 9219 9232 9234 9240 9252 9253 9270 9282 9288 9300 9306 9324
9328 9330 9338 9342 9345 9350 9360 9369 9380 9384 9386 9396 9408 9414 9420 9424 9432 9
450 9456 9471 9475 9477 9503 9504 9510 9520 9522 9526 9528 9534 9540 9545 9558 9560 95
85 9600 9612 9630 9639 9660 9666 9672 9686 9693 9702 9720 9723 9724 9740 9744 9747 975
2 9774 9796 9810 9816 9828 9854 9855 9856 9882 9900 9909 9912 9920 9922 9925 9936 9947
9960 9963 9990 10000

12. Write a Python program to find the nearest number to 1000, which is less than
1000, and divisible by 18 and 32.

In [20]: n = 0
for i in range(1, 1001):
if i % 18 == 0 and i % 32 == 0:
n = i
print(n)

864

13. Write a Python program to check whether a given number is a perfect square or
not.

In [21]: num = int(input("Enter a number: "))


i = 1
while i * i <= num:
if i * i == num:
print(f"{num} is a Perfect Square")
break
i += 1
else:
print(f"{num} is NOT a Perfect Square")

49 is a Perfect Square

14. Write a Python program to print the “nth” digit of a number from the right. (e.g.
in 18568, 6 is second from right)

In [31]: num = int(input("Enter a number: "))


n = int(input("Enter the position from right: "))
for i in range(n-1):
num //= 10
if num > 0:
digit = num % 10
print(f"The {n}th digit from right is {digit}")
else:
print("Number is too short to have that many digits")

The 3th digit from right is 5

15. Write a Python program to check whether the digits of a given number are equal.

In [26]: num = int(input("Enter a number: "))


last_digit = num % 10
same = True
while num > 0:
digit = num % 10
if digit != last_digit:
same = False
break
num //= 10
if same:
print("All digits are equal")
else:
print("Digits are NOT equal")

All digits are equal

Name:Santosh Dalei
Roll No.:26
SIC:23BCEA27
Branch:CEN-A2

You might also like