# WAP TO FIND THE SUM OF N NATURAL NUMBERS. # Natural numbers ==> start from 1 # Whole Numbers ==> start from 0 n = int(input("Enter the count for the natural numbers:")) s_naturals = 0 for natural in range(1,n+1): s_naturals = s_naturals + natural print("The Sume of",n,"natural numbers is = ",s_naturals) =================================== Formula: n*(n+1)/2 ==================== # WAP IN PYTHON TO FIND THE SUM OF ALL EVEN NUMBERS AND ALSO THE SUM OF ALL ODD NUMBERS # IN THE RANGE OF N NATURAL NUMBERS. # 10 natural numbers ==> 1 2 3 ... 10 # evens ==> 2, 4, 6, 8, 10 # odds ==> 1, 3, 5, 7, 9 n = int(input("Enter n value for representing the count of natural numbers:")) s_even = 0 s_odd = 0 for i in range(1,n+1): if i % 2 == 0: s_even = s_even + i else: s_odd = s_odd + i print("The Sum of All Even Natural Numbers = ",s_even) print("The Sum of All Odd natural Numbers = ",s_odd) =================================================== Nested loops: ============= for i in range(10): for j in range(10): print(i+j) # WAP TO PRINT ALL 5 DIGIT AMSTRONG NUMBERS. """ armstrong number Ex: XYZ Ind_Dig => X, Y and Z Powers => X^3 , Y^3 and Z^3 Sum_powers ==> X^3 + Y^3 + Z^3 Sum_powers == XYZ ==> Amstrong Number """ # we required generate 5-digit numbers # Each 5-digit number consider for amstrong check for i in range(10000,100000): n = i sumPowers = 0 while n != 0: indDig = n % 10 powers = indDig ** 5 sumPowers = sumPowers + powers n = n // 10 if sumPowers == i: print(i,end = "\t") ================================================== Transfer Statements: ==================== break ====== for i in range(10): if i == 6: break # the break can give the immediate termination from the loop print(i, end="\t") continue ======== for i in range(10): if i == 6: continue # the continue can skip the current iteration and continue with remaining iterations print(i, end="\t") ============================== i = 1 while i <= 10: if i == 6: i+=1 continue print(i,end = "\t") i+=1 ========================================= Assignment: =========== WAP TO PRINT ALL PRIME NUMBERS FROM THE GIVEN RANGE.