5) Python program to determine whether the given number is a Harshad Number.
If a number is divisible by the sum of its digits, then it will be known as a Harshad Number.
For example:
The number 156 is divisible by the sum (12) of its digits (1, 5, 6 ).
Some Harshad numbers are 8, 54, 120, etc.
In this program, integer value is predefined, we don?t need to put integer value to determine whether the given number is a Harshad number or not by following the algorithm as given below:
below:
ALGORITHM:
-
STEP 1: START
-
STEP 2: SET num = 156, rem =0, sum =0
-
STEP 3: DEFINE n
-
STEP 4: n = num
-
STEP 5: REPEAT STEP 6 to STEP 8 UNTIL (num>0)
-
STEP 6: rem =num%10
-
STEP 7: sum = sum + rem
-
STEP 8: num = num/10
-
STEP 9: if(n%sum==0) then PRINT "Yes" else PRINT "No"
-
STEP 10: END
num = 156;
rem = sum = 0;
#Make a copy of num and store it in variable n
n = num;
#Calculates sum of digits
while(num > 0):
rem = num%10;
sum = sum + rem;
num = num//10;
#Checks whether the number is divisible by the sum of digits
if(n%sum == 0):
print(str(n) + " is a harshad number");
else:
print(str(n) + " is not a harshad number");
6)Python program to print all pronic numbers between 1 and 100
The pronic number is a product of two consecutive integers of the form: n(n+1).
For example:
6 = 2(2+1)= n(n+1),
72 =8(8+1) = n(n+1)
Some pronic numbers are: 0, 2, 6, 12, 20, 30, 42, 56 etc.
In this program, we need to print all pronic numbers between 1 and 100 by following the algorithm as given below:
ALGORITHM:
-
STEP 1: isPronicNumber() determines whether a given number is the Pronic number or not.
-
Define a boolean variable flag and set its value to false.
-
Use for loop to iterate from 1 to given number and check whether i * (i + 1) is equal to the given number, for any value of i.
-
If a match is found, then set the flag to true, break the loop and returns the value of the flag.
-
-
STEP 2: To display all Pronic numbers between 1 and 100,
-
Start a loop from 1 to 100, and make a call to isPronicNumber() method for each value from 1 to 100.
-
If isPronicNumber() returns true which signifies that number is Pronic, then display that number.
-
#isPronicNumber() will determine whether a given number is a pronic number or not
def isPronicNumber(num):
flag = False;
for j in range(1, num+1):
#Checks for pronic number by multiplying consecutive numbers
if((j*(j+1)) == num):
flag = True;
break;
return flag;
#Displays pronic numbers between 1 and 100
print("Pronic numbers between 1 and 100: ");
for i in range(1, 101):
if(isPronicNumber(i)):
print(i),
print(" "),