Program in C to Find Square of a Number

Program in C to Find Square of a Number

Finding the square of a number is a basic yet essential task in programming. Squaring a number means multiplying the number by itself. This guide shows multiple ways to calculate the square of a number in C, from simple multiplication to using functions and libraries, providing flexibility and understanding for beginners.

Understanding The Problem

The goal is to write a C program that takes a number input from the user and outputs its square. The program should handle both integers and decimal numbers and can be implemented in several ways. Understanding this allows you to choose the most appropriate method depending on your needs.

  • Input: A single number (integer or decimal).
  • Output: The square of that number.

Methods to solve this problem include direct multiplication, using library functions, and custom functions.

Solution 1: Using Simple Multiplication

This is the most straightforward approach. You take the number entered by the user and multiply it by itself.

#include <stdio.h>

int main() {

    int number, square;

    // Prompt the user to enter a number
    printf("Enter an integer: ");
    scanf("%d", &number);

    // Multiply the number by itself
    square = number * number;

    // Display the result
    printf("Square of %d is %d\n", number, square);

    return 0;

}

In this program, the user input is stored in the variable number. By multiplying number with itself and storing the result in square, we achieve the desired output. This method is efficient and easy to understand.

Solution 2: Using the pow() Function

C provides a built-in function pow() in the <math.h> library to calculate powers of numbers. Using pow() is useful when you want a general solution for any exponent, not just squaring.

#include <stdio.h>
#include <math.h>

int main() {

    double number, square;

    // Prompt the user for input
    printf("Enter a number: ");
    scanf("%lf", &number);

    // Calculate the square using pow()
    square = pow(number, 2);

    // Display the result
    printf("Square of %.2lf is %.2lf\n", number, square);

    return 0;

}

Here, pow(number, 2) calculates the square. Using double allows us to handle both integers and decimal numbers. This method is slightly slower than direct multiplication but is flexible for different powers.

Solution 3: Using a Custom Function

Creating a function helps organize the code, especially if you plan to calculate squares multiple times in a program.

#include <stdio.h>

// Function to calculate the square
double squareNumber(double num) {
    return num * num;
}

int main() {

    double number;

    // Ask the user to enter a number
    printf("Enter a number: ");
    scanf("%lf", &number);

    // Call the function and display the result
    printf("Square of %.2lf is %.2lf\n", number, squareNumber(number));

    return 0;

}

The function squareNumber() takes a number as input and returns its square. This approach improves readability and reusability, making the program easier to manage if calculations are repeated.

FAQs

Q1: Can we square negative numbers?
Yes. Multiplying a negative number by itself results in a positive number.

Q2: Which method is best?
For most cases, simple multiplication is fastest and easiest. The pow() function is flexible, and custom functions are helpful for repeated calculations.

Q3: Can decimal numbers be squared?
Yes, using float or double types allows handling decimals easily.

Conclusion

Calculating the square of a number in C can be done in various ways. Simple multiplication is straightforward and efficient. Using pow() offers flexibility, custom functions improve readability, and recursion demonstrates programming concepts. Choosing the right approach depends on the problem context and program requirements.

References & Additional Resources

  1. C Programming Tutorial – Provides beginner-friendly explanations and examples of C programs.
  2. GeeksforGeeks – C Language Basics – Comprehensive C programming tutorials and problem-solving examples.
  3. TutorialsPoint – C Math Library – Detailed reference for math functions in C including pow() and other operations.

Scroll to Top