C++ Program to Display Prime Numbers Between Two Intervals

Prime Numbers are fundamental elements in mathematics and computer science, representing numbers greater than 1 that have no divisors other than 1 and themselves. The task of identifying prime numbers between two intervals highlights key programming concepts such as loops, conditionals, and functions, and is an excellent example of applying computational thinking to solve mathematical problems.

  • Understanding the Task

Displaying prime numbers between two intervals involves iterating through all the numbers in the specified range and checking each number to determine if it is prime. This task is computationally intensive, especially for large intervals, due to the nature of prime number determination. Hence, optimizing the prime check process is crucial to improving the program’s efficiency.

Optimized Approach to Prime Checking

The standard approach to check if a number is prime involves dividing the number by all smaller numbers up to 2. This method can be optimized by observing that:

  1. No prime number is divisible by any number greater than its square root.
  2. Numbers less than 2 are not prime.

Based on these observations, the prime checking function can significantly reduce the number of iterations, enhancing performance.

Implementing the Program

The program will consist of two parts:

  1. A function to check if a number is prime.
  2. The main function that iterates through the range, uses the prime-checking function, and displays the primes.

C++ Program to Display Prime Numbers Between Two Intervals

#include <iostream>

#include <cmath> // For the sqrt() function

using namespace std;

// Function to check if a number is prime

bool isPrime(int num) {

    if (num < 2) return false; // Numbers less than 2 are not prime

    for (int i = 2; i <= sqrt(num); ++i) {

        if (num % i == 0) return false; // If divisible, not prime

    }

    return true; // Number is prime

}

int main() {

    int start, end;

    // Prompt the user for the interval

    cout << “Enter two numbers (intervals): “;

    cin >> start >> end;

    cout << “Prime numbers between ” << start << ” and ” << end << ” are: ” << endl;

    // Iterate through the range and display prime numbers

    for (int num = start; num <= end; ++num) {

        if (isPrime(num)) {

            cout << num << ” “;

        }

    }

    return 0;

}

 

Program Explanation

  • Prime Checking Function (isPrime):

This function takes an integer as input and returns true if the number is prime, otherwise false. It efficiently checks divisibility only up to the square root of the number, leveraging the mathematical insight that factors of a number are always found in pairs, one less than and one greater than the square root of the number.

  • Main Function:

The main function begins by asking the user to input two numbers, defining the interval within which to find prime numbers. It then iterates through each number in this interval, using the isPrime function to check for primality. Prime numbers are displayed to the user.

  • Efficiency and Optimization:

The program is optimized for efficiency by reducing the number of divisions needed to determine if a number is prime. This is critical for larger numbers and wider ranges, where naive approaches might lead to significantly longer computation times.

C++ Program to Display Factors of a Natural Number

Displaying the factors of a natural number involves finding all the numbers that divide the given number without leaving a remainder. Factors are integral to various mathematical calculations and are particularly interesting when exploring number theory, prime numbers, and algorithms that involve divisibility.

In this context, a factor of a number n is any integer i where n % i == 0. To find all such numbers, a straightforward approach involves iterating through all integers from 1 to n and checking if n % i == 0. If the condition holds true, i is a factor of n.

Let’s write a C++ program that takes a natural number as input from the user and displays all its factors:

#include <iostream>

using namespace std;

// Function to print the factors of a given number n

void printFactors(int n) {

    cout << “The factors of ” << n << ” are: “;

    for (int i = 1; i <= n; ++i) {

        if (n % i == 0) {

            cout << i << ” “;

        }

    }

    cout << endl;

}

int main() {

    int number;

    cout << “Enter a natural number: “;

    cin >> number;

    // Check for natural number input

    if (number < 1) {

        cout << “Please enter a natural number greater than 0.” << endl;

    } else {

        printFactors(number);

    }

    return 0;

}

This program begins by prompting the user to enter a natural number. It checks whether the input is a natural number (i.e., an integer greater than 0). If the check passes, the program proceeds to find and print all the factors of the input number using the printFactors function.

Within printFactors, the program iterates from 1 to n, inclusive. For each number i in this range, it checks if n % i == 0. If the condition is true, it means i is a divisor (or factor) of n, and the program prints i.

This method is simple and effective for small to moderately sized integers. However, for very large numbers, more efficient algorithms may be necessary to reduce computational time, such as only checking up to the square root of n and adding both the divisor and the quotient when applicable.

C++ Program to Display Armstrong Numbers Between 1 to 1000

Displaying Armstrong numbers within a specific range, such as from 1 to 1000, is a common programming task that showcases the application of loops, conditionals, and arithmetic operations in C++. Armstrong numbers, as previously discussed, are numbers that equal the sum of their own digits each raised to the power of the number of digits in the number. This property makes them particularly interesting from both mathematical and programming perspectives.

To create a program that identifies and displays Armstrong numbers within a given range, the steps are as follows:

  1. Loop through the Range:

Iterate over each number in the specified range (1 to 1000 in this case).

  1. Check for Armstrong Number:

For each number, determine if it is an Armstrong number.

  1. Display the Number:

If a number is identified as an Armstrong number, display it.

This process requires a function to check if a given number is an Armstrong number, similar to what was outlined previously.

C++ Program

 

#include <iostream>

#include <cmath>

using namespace std;

// Function to calculate the number of digits in a number

int countDigits(int n) {

    int count = 0;

    while (n != 0) {

        count++;

        n /= 10;

    }

    return count;

}

// Function to check if a number is an Armstrong number

bool isArmstrong(int num) {

    int originalNum = num;

    int numOfDigits = countDigits(num);

    int sumOfPowers = 0;

    while (num != 0) {

        int digit = num % 10;

        sumOfPowers += pow(digit, numOfDigits);

        num /= 10;

    }

    return sumOfPowers == originalNum;

}

int main() {

    cout << “Armstrong numbers between 1 to 1000 are:” << endl;

    for (int num = 1; num <= 1000; num++) {

        if (isArmstrong(num)) {

            cout << num << endl;

        }

    }

    return 0;

}

 

Explanation

  1. Counting Digits (countDigits function):

This function, as before, calculates the number of digits in a given number. This is crucial for determining the power to which each digit should be raised.

  1. Armstrong Number Check (isArmstrong function):

This function determines if a given number is an Armstrong number by comparing it to the sum of its digits each raised to the power of the number of digits in the number.

  1. Main Logic:

The main function iterates through numbers from 1 to 1000, utilizing the isArmstrong function to check for Armstrong numbers. When an Armstrong number is found, it is printed out.

C++ Program to Check Whether a Number is Prime or Not

Prime Number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In other words, a prime number is a number that cannot be exactly divided by any number other than 1 and itself. The task of determining whether a given number is prime is fundamental in various fields of computer science, cryptography, and mathematical computation.

Understanding Prime Numbers

Before delving into the program, it’s essential to understand the characteristics of prime numbers and the logic used to identify them. A prime number:

  1. Must be greater than 1.
  2. Has exactly two distinct positive divisors: 1 and itself.

For example, the numbers 2, 3, 5, 7, 11, and 13 are prime because each can only be divided by 1 and the number itself without leaving a remainder. On the other hand, numbers like 4, 6, 8, 9, and 10 are not prime because they have more than two divisors.

Algorithm for Checking Prime Numbers

The simplest method to check if a number is prime is to try dividing it by all numbers from 2 to the number itself (exclusive) and check for a remainder. If any division operation results in no remainder, the number is not prime. However, this method can be optimized. Since a factor of a number n cannot be more than n/2 (except for n itself), you only need to check up to n/2. Further optimization notes that a factor of n cannot be more than √n, significantly reducing the number of checks needed.

C++ Program to Determine Prime Numbers

Let’s now look at a C++ program that implements the optimized approach to check if a given number is prime.

#include <iostream>

#include <cmath> // For the sqrt() function

using namespace std;

// Function to check if a number is prime

bool isPrime(int num) {

    // Handle base cases: if num is less than 2, it’s not prime

    if (num < 2) {

        return false;

    }

    // Only need to check for divisors up to the square root of num

    for (int i = 2; i <= sqrt(num); ++i) {

        // If num is divisible by any number between 2 and sqrt(num), it’s not prime

        if (num % i == 0) {

            return false;

        }

    }

    // If no divisors were found, num is prime

    return true;

}

int main() {

    int num;

    cout << “Enter a number: “;

    cin >> num;

    // Check if the number is prime and output the result

    if (isPrime(num)) {

        cout << num << ” is a prime number.” << endl;

    } else {

        cout << num << ” is not a prime number.” << endl;

    }

    return 0;

}

 

Detailed Explanation

  • Function isPrime:

This function takes an integer num as input and returns true if num is prime; otherwise, it returns false. It first handles the base case—if num is less than 2, it returns false because prime numbers are greater than 1. Then, it checks for divisors of num from 2 up to the square root of num (inclusive). This range suffices due to the mathematical property that if num has a divisor greater than its square root, it must also have a divisor smaller than the square root, thereby ensuring all potential divisors are checked. If any divisor is found (indicated by num % i == 0), the function returns false.

  • Main Program:

In the main function, the user is prompted to enter a number. The program then calls isPrime to check if the entered number is prime and outputs the appropriate message based on the function’s return value.

C++ Program to Check Whether a Number is a Palindrome or Not

Palindrome is a sequence that reads the same backward as forward. Palindrome checking is a classic problem in computer science and programming education, illustrating basic concepts such as loops, conditionals, and the manipulation of numbers or strings. In the case of numeric palindromes, the problem typically involves reversing a number and comparing it to the original to determine if they are the same.

This task can be approached in various ways, but one straightforward method involves reversing the given number and then comparing the reversed number with the original number. If they are identical, the number is a palindrome.

C++ Program to Check Numeric Palindrome

Here is a simple C++ program that checks if a given integer is a palindrome. The program outlines the process of reversing a number and comparing the reversed number with the original.

#include <iostream>

using namespace std;

// Function to reverse a number

int reverseNumber(int num) {

    int reversed = 0;

    while(num > 0) {

        reversed = reversed * 10 + num % 10;

        num /= 10;

    }

    return reversed;

}

// Function to check if a number is a palindrome

bool isPalindrome(int num) {

    // Negative numbers are not considered palindromes

    if (num < 0) return false;

   

    // Compare original number with its reverse

    return num == reverseNumber(num);

}

int main() {

    int num;

    cout << “Enter a number: “;

    cin >> num;

   

    if (isPalindrome(num)) {

        cout << num << ” is a palindrome.” << endl;

    } else {

        cout << num << ” is not a palindrome.” << endl;

    }

   

    return 0;

}

 

Explanation

  • Reversing a Number:

The reverseNumber function reverses the digits of the given number. It initializes an integer reversed to 0 and iteratively extracts the last digit of num (using num % 10), appends it to reversed, and removes the last digit from num (using num /= 10) until num becomes 0.

  • Checking for Palindrome:

The isPalindrome function first handles a special case by returning false if the number is negative, as negative numbers are not considered palindromes due to the leading minus sign. It then calls reverseNumber to reverse the input number and compares the reversed number with the original. If they are the same, it returns true, indicating the number is a palindrome; otherwise, it returns false.

  • Main Program Flow:

The main function asks the user to enter a number, checks if it’s a palindrome by calling isPalindrome, and outputs the result.

C++ Program to Check Neon Numbers in a Given Range

A Neon number is an intriguing concept in the realm of mathematics and computer science. It is a number where the sum of the digits of its square equals the number itself. For instance, 9 is a neon number because 9^2 = 81, and the sum of the digits of 81 (8 + 1) equals 9.

Understanding Neon Numbers

To identify neon numbers, one must perform the following steps for each number in the given range:

  • Square the Number:

First, compute the square of the number.

  • Sum the Digits:

Next, calculate the sum of the digits of the squared number.

  • Compare with Original:

Finally, check if the sum of the digits equals the original number. If so, the number is a neon number.

This process involves fundamental operations like multiplication and digit extraction, the latter typically achieved through division and modulus operations.

Algorithm for Finding Neon Numbers in a Range

Before implementing the program, let’s outline the algorithm to find and display all neon numbers within a specified range:

  1. Input Range: Obtain the lower and upper bounds of the range from the user.
  2. Iterate Through the Range: For each number in the range, perform the following steps:
    • Square the number.
    • Calculate the sum of the digits of the square.
    • Check if the sum equals the original number.
    • If so, display the number as a neon number.
  3. Repeat until all numbers in the range have been checked.

Implementing the Program

Now, let’s translate the above algorithm into a C++ program:

#include <iostream>

using namespace std;

// Function to calculate the sum of digits of a number

int sumOfDigits(int n) {

    int sum = 0;

    while (n > 0) {

        sum += n % 10; // Add the rightmost digit to sum

        n /= 10;       // Remove the rightmost digit from n

    }

    return sum;

}

// Function to check if a number is a Neon Number

bool isNeon(int num) {

    int square = num * num; // Calculate square of num

    int sum = sumOfDigits(square); // Sum the digits of the square

    return sum == num; // Return true if sum of digits equals num

}

int main() {

    int lower, upper;

    // Prompt user for the range

    cout << “Enter the lower and upper bounds of the range: “;

    cin >> lower >> upper;

    cout << “Neon numbers between ” << lower << ” and ” << upper << ” are:” << endl;

    // Iterate through the range and check for Neon Numbers

    for (int i = lower; i <= upper; ++i) {

        if (isNeon(i)) {

            cout << i << ” “;

        }

    }

    return 0;

}

 

Program Explanation

  1. Sum of Digits Function (sumOfDigits):

This helper function takes an integer n and returns the sum of its digits. It does so by repeatedly extracting the rightmost digit (using n % 10), adding it to the sum, and then removing this digit from n (using n /= 10).

  1. Neon Number Checker Function (isNeon):

The isNeon function checks whether a given number is a neon number. It squares the number, uses sumOfDigits to calculate the sum of the digits of the square, and then checks if this sum equals the original number.

  1. Main Function:

After obtaining the range from the user, the program iterates through each number in this range, using the isNeon function to determine if it is a neon number. If a number is found to be neon, it is printed to the console.

C++ Program to Check Armstrong Number

An Armstrong number, also known as a narcissistic number, is an intriguing concept in number theory that finds practical applications in programming exercises, particularly those focusing on algorithm design, control structures, and number manipulation. An Armstrong number of a given number of digits is a number in which the sum of its own digits each raised to the power of the number of digits equals the number itself. For example, 153 is an Armstrong number because 5^3+3^3 = 153.

Understanding and implementing a program to check for Armstrong numbers not only helps in grasping basic programming concepts but also in appreciating the elegance of numerical properties. This exercise typically involves breaking down the problem into key steps: determining the number of digits, calculating the sum of the digits each raised to a certain power, and comparing this sum to the original number.

Breakdown of the Solution

  1. Determine the Number of Digits:

This involves a loop where you repeatedly divide the number by 10 until it becomes 0. The number of iterations gives you the number of digits.

  1. Calculate the Sum of Powers of the Digits:

Using the number of digits calculated in step 1, you raise each digit to this power and sum these values. This requires isolating each digit (using modulus and division operations) and then using the pow function from the <cmath> library to raise each digit to the power of the number of digits.

  1. Compare the Sum to the Original Number:

If the sum obtained in step 2 equals the original number, it is an Armstrong number.

Implementing the Program

Let’s now translate the solution into a C++ program:

#include <iostream>

#include <cmath> // For pow() function

using namespace std;

// Function to calculate the number of digits in the number

int countDigits(int n) {

    int count = 0;

    while (n != 0) {

        count++;

        n /= 10;

    }

    return count;

}

// Function to check if the given number is an Armstrong number

bool isArmstrong(int num) {

    int originalNum = num;

    int numOfDigits = countDigits(num);

    int sumOfPowers = 0;

    while (num != 0) {

        int digit = num % 10;

        sumOfPowers += pow(digit, numOfDigits);

        num /= 10;

    }

    return sumOfPowers == originalNum;

}

int main() {

    int num;

    // Prompt the user for input

    cout << “Enter a number: “;

    cin >> num;

    if (isArmstrong(num)) {

        cout << num << ” is an Armstrong number.” << endl;

    } else {

        cout << num << ” is not an Armstrong number.” << endl;

    }

    return 0;

}

 

Detailed Explanation

  1. Counting Digits (countDigits function):

This function takes an integer as input and returns the count of digits in that number. It does so by dividing the number by 10 in a loop until the number becomes 0, incrementing a counter at each step.

  1. Checking for Armstrong Number (isArmstrong function):

The isArmstrong function assesses whether a given number is an Armstrong number. It first stores the original number for later comparison. It then calculates the sum of each digit raised to the power equal to the number of digits in the number. This is accomplished by isolating each digit using modulus and division operations and raising it to the power calculated by the countDigits function.

  1. Main Logic:

The main function prompts the user for a number, uses the isArmstrong function to check if the number is an Armstrong number, and displays the result accordingly.

C++ Program to Calculate the Power of a Number

Calculating the power of a number involves multiplying the number by itself a certain number of times, indicated by the exponent. In C++, this operation can be performed using a simple loop, the std::pow function from the <cmath> library for a more straightforward approach, or even recursively for educational purposes.

Iterative Approach

An iterative approach to calculating the power of a number allows us to understand the basic principle behind exponentiation. It involves multiplying the base number by itself n-1 times, where n is the exponent.

#include <iostream>

using namespace std;

// Function to calculate power

long long power(int base, int exponent) {

    long long result = 1;

    for(int i = 0; i < exponent; i++) {

        result *= base;

    }

    return result;

}

int main() {

    int base, exponent;

    cout << “Enter the base: “;

    cin >> base;

    cout << “Enter the exponent: “;

    cin >> exponent;

    long long result = power(base, exponent);

    cout << base << ” raised to the power of ” << exponent << ” is ” << result << “.” << endl;

    return 0;

}

 

Using std::pow Function

For many applications, the std::pow function offers a more direct and possibly more efficient way to compute powers, especially when dealing with floating-point numbers. Here’s how you can use it:

#include <iostream>

#include <cmath> // Include for std::pow

int main() {

    double base;

    double exponent;

    cout << “Enter the base: “;

    cin >> base;

    cout << “Enter the exponent: “;

    cin >> exponent;

    double result = std::pow(base, exponent);

    cout << base << ” raised to the power of ” << exponent << ” is ” << result << “.” << endl;

    return 0;

}

 

Recursive Approach

For educational purposes, here’s how you might implement a simple recursive function to calculate powers. This method is not the most efficient but demonstrates the concept of recursion.

#include <iostream>

using namespace std;

// Recursive function to calculate power

long long power(int base, int exponent) {

    if (exponent == 0)

        return 1; // Base case: any number to the power of 0 is 1

    else

        return base * power(base, exponent – 1);

}

int main() {

    int base, exponent;

    cout << “Enter the base: “;

    cin >> base;

    cout << “Enter the exponent: “;

    cin >> exponent;

    long long result = power(base, exponent);

    cout << base << ” raised to the power of ” << exponent << ” is ” << result << “.” << endl;

    return 0;

}

Each of these approaches has its use cases:

  • The iterative approach gives you a solid understanding of how powers are calculated from the ground up.
  • The std::pow function is convenient and efficient, especially for floating-point arithmetic.
  • The recursive approach is educational, illustrating the concept of recursion but may not be as efficient due to the function call overhead and the risk of stack overflow for large exponents.

C++ Program to Print Multiplication Table of a Number

Creating a C++ program to print the multiplication table for a given number is an excellent exercise for understanding loops, user input/output, and the fundamental concepts of arithmetic operations within a programming context.

A multiplication table for a number displays that number multiplied by a range of other numbers, typically from 1 to 10 or any range specified by the user. This is not just about executing arithmetic operations; it’s also about demonstrating how loops can automate repetitive tasks efficiently. The C++ programming language, with its robust control structures, offers an ideal platform for implementing this functionality.

Setting the Stage with Basic Input and Output

Any C++ program begins with the inclusion of header files necessary for input and output operations. In our case, iostream is essential:

#include <iostream>

This allows us to use std::cin for input from the user and std::cout for output to the console. To avoid prefixing standard library items with std::, we declare the use of the standard namespace:

using namespace std;

 

Soliciting User Input

An interactive program must communicate with its user. Prompting for and receiving a number for which to print the multiplication table demonstrates this interaction:

int main() {

    int num;

    cout << “Enter a number to print its multiplication table: “;

    cin >> num;

This snippet establishes the groundwork for user interaction, storing the user’s input in the variable num.

The Core: Implementing the Multiplication Logic with Loops

With the number in hand, the next step is to iterate through a range of values, multiplying each by the user’s number. This is where loops come into play. A for loop is particularly well-suited for this task, as we know the specific range we want to iterate over:

    int range;

    cout << “Enter the range of the multiplication table: “;

    cin >> range;

   

    cout << “Multiplication table of ” << num << ” up to ” << range << “:” << endl;

   

    for(int i = 1; i <= range; ++i) {

        cout << num << ” * ” << i << ” = ” << num * i << endl;

    }

    return 0;

}

This loop starts at 1 and continues up to and including the range specified by the user, each time calculating the product of the user’s number and the loop’s counter, i, and printing the result.

Understanding Loops

The for loop is central to writing efficient and concise code, especially when dealing with repetitive tasks. In our example, it automates the multiplication and presentation of the entire table with minimal code. This not only makes the program more readable but also significantly reduces the potential for errors that could arise from manually writing out each calculation and print statement.

Arithmetic in Programming

Performing arithmetic in programming languages like C++ is straightforward, closely mirroring the way mathematical operations are conducted in algebra. However, the context of these operations—being part of an automated process that can handle inputs dynamically—showcases the power of programming to perform calculations at a speed and scale that manual computation cannot match.

Enhancing the Program

The basic program above can be expanded in several ways to make it more robust and user-friendly. For instance, adding input validation ensures that the program behaves as expected even when given unusual or incorrect input. This might include checking that the user has entered a positive number for the number and the range. Moreover, introducing error messages for invalid inputs enhances the interactive experience by guiding the user towards correct usage.

C++ Program to Find Largest Among 3 Numbers

To determine the largest among them is a foundational skill that touches on conditional statements, user input/output, and logical thinking. This task not only reinforces basic programming concepts but also serves as a stepping stone to more complex decision-making and data manipulation operations.

Understanding the Problem

The core objective is straightforward: given three numbers, identify the largest one. This problem is a classic example of applying conditional logic to perform comparisons. It’s crucial not only to understand how to compare numbers but also to grasp the importance of designing a program that efficiently navigates multiple conditional checks.

Setting Up the Program

The journey of writing our C++ program begins with including essential header files and declaring the use of the standard namespace to simplify subsequent code.

#include <iostream>

using namespace std;

The above lines ensure that we can use input and output operations like cin for input and cout for output without prefixing them with std::.

Soliciting User Input

A program becomes interactive and user-friendly when it can accept user input. Therefore, the next step involves prompting the user to input three numbers and storing these numbers in variables.

int main() {

    double num1, num2, num3;

    cout << “Enter three numbers: “;

    cin >> num1 >> num2 >> num3;

Here, we declare three variables (num1, num2, and num3) to hold the numbers input by the user. The choice of double as the data type allows the user to input both integer and floating-point numbers, making our program more versatile.

Implementing the Logic to Find the Largest Number

The crux of this program lies in comparing the three numbers and determining the largest. This can be achieved using a series of if-else statements.

    double largest;

    if (num1 >= num2 && num1 >= num3) {

        largest = num1;

    } else if (num2 >= num1 && num2 >= num3) {

        largest = num2;

    } else {

        largest = num3;

    }

    cout << “The largest number is ” << largest << endl;

    return 0;

}

In this segment, we introduce a variable largest to store the largest number. We then use if-else statements to compare the numbers:

  • The first if checks if num1 is greater than or equal to both num2 and num3. If true, it means num1 is the largest (or tied for the largest), so we assign num1 to largest.
  • The else if handles the scenario where num2 is greater than or equal to both num1 and num3. If this condition is true, num2 is assigned to largest.
  • If neither of the above conditions is true, it logically follows that num3 must be the largest, so it is assigned to largest in the else block.

Finally, we display the largest number using cout.

Complete Program

Combining all the components, the complete program is as follows:

#include <iostream>

using namespace std;

int main() {

    double num1, num2, num3;

    cout << “Enter three numbers: “;

    cin >> num1 >> num2 >> num3;

    double largest;

    if (num1 >= num2 && num1 >= num3) {

        largest = num1;

    } else if (num2 >= num1 && num2 >= num3) {

        largest = num2;

    } else {

        largest = num3;

    }

    cout << “The largest number is ” << largest << endl;

    return 0;

}

 

Key Concepts illustrated

  • Conditional Statements:

This program underscores the utility of if-else statements in making decisions based on comparisons. Understanding how to structure these conditions efficiently is crucial for problem-solving in programming.

  • User Input and Output:

By interacting with the user through prompts and displaying the result, the program demonstrates dynamic behavior, adapting its output based on user input.

  • Data Type Selection:

The use of double for the number variables showcases the consideration of data types in accommodating various kinds of numerical input.

  • Logical Thinking:

The process of figuring out how to compare the numbers and structure the conditional logic fosters logical thinking—a skill that’s invaluable in programming and algorithm design.

error: Content is protected !!