TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to print all Pronic numbers between 1 to 100

Program to print all Pronic numbers between 1 to 100 on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Program to print all Pronic numbers between 1 and 100

Explanation

In this program, we need to display all Pronic numbers between 1 and 100.

Pronic number

The pronic number can be defined as the number which is a product of two consecutive numbers. Mathematically, the Pronic number can be represented as

N x (N + 1)

To find whether a number n is a Pronic number or not, use for loop to iterate from i = 1 to i = n and check whether i*(i+1) is equal to n for any value of i.

Some of the examples of Pronic numbers are

6 = 2 x 3

72 = 8 x 9

Algorithm

  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.
  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.

Solution

Python

#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(" "),

Output:

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

C

#include <stdio.h>
#include <stdbool.h>
 
//isPronicNumber() will determine whether a given number is pronic number or not
bool isPronicNumber(int num){
    bool flag = false;
    
    for(int j = 1; j <= num; j++){
        //Checks for pronic number by multiplying consecutive numbers
        if((j*(j+1)) == num){
            flag = true;
            break;
        }
    }
    return flag;
}
 
int main()
{
    //Displays pronic numbers between 1 and 100
    printf("Pronic numbers between 1 and 100: \n");
    for(int i = 1; i <= 100; i++){
        if(isPronicNumber(i))
            printf("%d ", i);
    }
 
    return 0;
}

Output:

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

JAVA

public class PronicNumbers
{
    //isPronicNumber() will determine whether a given number is pronic number or not
    public static boolean isPronicNumber(int num){
        boolean flag = false;
        
        for(int j = 1; j <= num; j++){
            //Checks for pronic number by multiplying consecutive numbers
            if((j*(j+1)) == num){
                flag = true;
                break;
            }
        }
        return flag;
    }
    
    public static void main(String[] args) {
        
        //Displays pronic numbers between 1 and 100
        System.out.println("Pronic numbers between 1 and 100: ");
        for(int i = 1; i <= 100; i++){
            if(isPronicNumber(i))
                System.out.print(i + " ");
        }
    }
}

Output:

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

C#

using System;
                    
public class PronicNumbers
{
    //isPronicNumber() will determine whether a given number is pronic number or not
    public static Boolean isPronicNumber(int num){
        Boolean flag = false;
        
        for(int j = 1; j <= num; j++){
            //Checks for pronic number by multiplying consecutive numbers
            if((j*(j+1)) == num){
                flag = true;
                break;
            }
        }
        return flag;
    }
    
    public static void Main()
    {
        //Displays pronic numbers between 1 and 100
        Console.WriteLine("Pronic numbers between 1 and 100: ");
        for(int i = 1; i <= 100; i++){
            if(isPronicNumber(i))
                Console.Write(i + " ");
        }
    }
} 

Output:

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

PHP

<!DOCTYPE html>
<html>
<body>
<?php
//isPronicNumber() will determine whether a given number is pronic number or not
function isPronicNumber($num){
    $flag = false;
    
    for($j = 1; $j <= $num; $j++){
        //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: <br>");
for($i = 1; $i <= 100; $i++){
    if(isPronicNumber($i))
        print($i . " ");
}
?>
</body>
</html>

Output:

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

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf