In this blog post, we learn how to write a C program to find square of a number?. We will write the C program to find a square of a number. How to display input number square C programming. Logic to find a square of a given number in C programming.
Example,
Input: 2 Output: 4 Input: 20 Output: 400
C Program to calculate the square of a number:
The below program ask the user to enter the value. After getting the value from the user it will calculate the square of entered number by using arithmetic multiplication operator.
#include<stdio.h>
int main()
{
float number, square;
printf("Please Enter any integer Value : ");
scanf("%f", &number);
square = number * number;
printf("square of a given number %.2f is = %.2f", number, square);
return 0;
}
Output 1:
Please Enter any integer Value : 20
square of a given number 20.00 is = 400.00
Output 2:
Please Enter any integer Value : 4.5
square of a given number 4.50 is = 20.25
C Program to calculate the square of a number using a function:
The below program ask the user to enter the value. After getting the value from the user it will call a function squareOfNumber() to calculate the square of the entered number. The function squareOfNumber() is using an arithmetic multiplication operator to calculate the square of the input number.
#include<stdio.h>
//function to calculate square of number
float squareOfNumber(float num)
{
return (num*num);
}
int main()
{
float number, square;
printf("Please Enter any integer Value : ");
scanf("%f", &number);
square = squareOfNumber(number);
printf("square of a given number %.2f is = %.2f", number, square);
return 0;
}
Output:
Please Enter any integer Value : 4.5
square of a given number 4.50 is = 20.25
Recommended Articles for you:
- Programing language MCQ.
- Best mouse for programmers.
- Best Keyboard for programmers and gamers.
- List of some best laptops for programmers and gammers.
- Interview questions on C/C++ pointers.
- 100 C interview Questions PART- 2.
- 100 C interview Questions PART- 3.
- 10 questions about dynamic memory allocation.
- 15 Common mistakes with memory allocation.
- Top 11 Structure Padding Interview Questions in C.