w3resource logo
C Programming Exercises

C Exercises: Find the square of any number

C Function : Exercise-2 with Solution

Write a program in C to find the square of any number using the function.

Sample Solution:

C Code:

#include <stdio.h>

double square(double num)
{
    return (num * num);
}
int main()
{
    int num;
    double n;
	printf("\n\n Function : find square of any number :\n");
	printf("------------------------------------------------\n");	
     
    printf("Input any number for square : ");
    scanf("%d", &num);
    n = square(num);
    printf("The square of %d is : %.2f\n", num, n); 
    return 0;
}

Sample Output:

 Function : find square of any number :
------------------------------------------------
Input any number for square : 20
The square of 20 is : 400.00

C Programming Code Editor:

#include <stdio.h>

double square(double num)
{
    return (num * num);
}
int main()
{
    int num;
    double n;
	printf("\n\n Function : find square of any number :\n");
	printf("------------------------------------------------\n");	
     
    printf("Input any number for square : ");
    scanf("%d", &num);
    n = square(num);
    printf("The square of %d is : %.2f\n", num, n); 
    return 0;
}

Improve this sample solution and post your code through Disqus.