C# Basic
C# Interview Programs
- C# Interview Programs
- C# Abundant Number
- C# Amicable Number
- C# Armstrong Number
- C# Average of N Numbers
- C# Automorphic Number
- C# Biggest of three numbers
- C# Binary to Decimal
- C# Common Divisors
- C# Composite Number
- C# Condense a Number
- C# Cube Number
- C# Decimal to Binary
- C# Decimal to Octal
- C# Disarium Number
- C# Even Number
- C# Evil Number
- C# Factorial of a Number
- C# Fibonacci Series
- C# GCD
- C# Happy Number
- C# Harshad Number
- C# LCM
- C# Leap Year
- C# Magic Number
- C# Matrix Addition
- C# Matrix Division
- C# Matrix Multiplication
- C# Matrix Subtraction
- C# Matrix Transpose
- C# Maximum Value of an Array
- C# Minimum Value of an Array
- C# Multiplication Table
- C# Natural Number
- C# Number Combination
- C# Odd Number
- C# Palindrome Number
- C# Pascalβs Triangle
- C# Perfect Number
- C# Perfect Square
- C# Power of 2
- C# Power of 3
- C# Pronic Number
- C# Prime Factor
- C# Prime Number
- C# Smith Number
- C# Strong Number
- C# Sum of Array
- C# Sum of Digits
- C# Swap Two Numbers
- C# Triangular Number
C# Program to Check Pronic Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, mathematical patterns and properties often become interesting challenges. One such intriguing concept is the "Pronic Number."
A pronic number, also known as a rectangular number, oblong number, or heteromecic number, is the product of two consecutive integers.
In this tutorial, we will walk through a C# program designed to check whether a given number is a pronic number or not.
π Example
Now, let's delve into the C# code that checks whether a given number is a pronic number or not.
using System;
class Program {
// Function to check if a number is a pronic number
static bool IsPronic(int num) {
int sqrtNum = (int) Math.Sqrt(num);
return (sqrtNum * (sqrtNum + 1) == num);
}
// Driver program
static void Main() {
// Replace this value with your desired number
int number = 12;
// Check if the number is a pronic number
if (IsPronic(number)) {
Console.WriteLine($"{number} is a pronic number.");
} else {
Console.WriteLine($"{number} is not a pronic number.");
}
}
}
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the Main method.
12 is a pronic number.
Compile and run the program to check if the given number is a pronic number.
π§ How the Program Works
- The program defines a function IsPronic that takes a number as input and returns true if the number is a pronic number and false otherwise.
- Inside the function, it calculates the square root of the input number.
- It checks whether the product of the square root and the square root incremented by 1 equals the input number. If yes, the number is a pronic number.
- The driver program tests the function with a sample number and prints the result.
π Between the Given Range
Let's take a look at the C# code that checks for pronic numbers in the range from 1 to 20.
using System;
class Program {
// Function to check if a number is pronic
static bool IsPronic(int num) {
for (int i = 0; i <= Math.Sqrt(num); i++) {
if (i * (i + 1) == num) {
return true;
}
}
return false;
}
// Driver program
static void Main() {
Console.Write("Pronic Numbers in the range 1 to 20: \n");
// Check for pronic numbers in the range
for (int i = 1; i <= 20; i++) {
if (IsPronic(i)) {
Console.Write($ "{i} ");
}
}
Console.WriteLine();
}
}
π» Testing the Program
Pronic Numbers in the range 1 to 20: 2 6 12 20
Compile and run the program to see the pronic numbers in the specified range.
π§ How the Program Works
- The program defines a function IsPronic that checks if a given number is a pronic number by iterating through possible values.
- The Main method iterates through numbers from 1 to 20 and prints those that are pronic.
π§ Understanding the Concept of Pronic Numbers
Before delving into the code, let's take a moment to understand the concept of pronic numbers.
A pronic number is a product of two consecutive integers, and it has applications in various mathematical and geometric contexts.
A pronic number, denoted by Pn, is a number of the form Pn=n Γ (n+1) where n is a non-negative integer. The sequence of pronic numbers starts as 0, 2, 6, 12, 20, and so on.
For example, let's take n = 3. The corresponding pronic number would be P3 = 3x(3+1) = 12.
π’ Optimizing the Program
While the provided program is effective, consider exploring ways to optimize the computation of pronic numbers for larger inputs.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (C# Program to Check Pronic Number), please comment here. I will help you immediately.