Python program to print all pronic numbers between 1 and 100

Created with Sketch.

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.
    1. Define a boolean variable flag and set its value to false.
    2. 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.
    3. 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,
    1. Start a loop from 1 to 100, and make a call to isPronicNumber() method for each value from 1 to 100.
    2. If isPronicNumber() returns true which signifies that number is Pronic, then display that number.

PROGRAM:

  1. #isPronicNumber() will determine whether a given number is a pronic number or not  
  2. def isPronicNumber(num):
  3.     flag = False;
  4.     for j in range(1, num+1):
  5.         #Checks for pronic number by multiplying consecutive numbers  
  6.         if((j*(j+1)) == num):
  7.             flag = True;
  8.             break;
  9.     return flag;
  10. #Displays pronic numbers between 1 and 100  
  11. print(“Pronic numbers between 1 and 100: “);
  12. for i in range(1101):
  13.     if(isPronicNumber(i)):
  14.         print(i),
  15.         print(” “),

Output:

Pronic numbers between 1 and 100:
2  6  12  20  30  42  56  72  90

Leave a Reply

Your email address will not be published. Required fields are marked *