English

Write a program to display all Pronic numbers in the range from 1 to n. Hint: A Pronic number is a number which is the product of two consecutive numbers in the form of n* (n + 1). - Computer Applications

Advertisements
Advertisements

Question

Write a program to display all Pronic numbers in the range from 1 to n.

Hint: A Pronic number is a number which is the product of two consecutive numbers in the form of n* (n + 1). 

For example,
2, 6, 12, 20, 30, ................... are Pronic numbers. 

Answer in Brief

Solution

import java.util.Scanner;

public class Q16
{
    public static void main(String args[])  {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n = in.nextInt();
        
        System.out.println("Pronic numbers from 1 to " + n + " : ");
        
        for (int i = 1; i <= n; i++) {
            for(int j = 1; j <= i-1; j++) {
                if (j * (j + 1) == i) { 
                    System.out.print(i + " ");
                    break;
                }
            }
        }
    }
}
shaalaa.com
  Is there an error in this question or solution?
Chapter 1.09: Iterative Constructs in Java - EXERCISES [Page 126]

APPEARS IN

Avichal Computer Applications [English] Class 10 ICSE
Chapter 1.09 Iterative Constructs in Java
EXERCISES | Q VI. 16. | Page 126
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×