Program to find the surface area of the square pyramid
Last Updated :
07 Aug, 2022
Given the base length(b) and slant height(s) of the square pyramid. The task is to find the surface area of the Square Pyramid. A Pyramid with a square base, 4 triangular faces, and an apex is a square pyramid.
In this figure,
b – base length of the square pyramid.
s – slant height of the square pyramid.
h – height of the square pyramid.
Examples:
Input: b = 3, s = 4
Output: 33
Input: b = 4, s = 5
Output: 56
Formula for calculating the surface are of the square pyramid with (b) base length and (s) slant height.
Below is the implementation using the above formula:
C++
#include <bits/stdc++.h>
using namespace std;
int surfaceArea( int b, int s)
{
return 2 * b * s + pow (b, 2);
}
int main()
{
int b = 3, s = 4;
cout << surfaceArea(b, s) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int surfaceArea( int b, int s)
{
return 2 * b * s + ( int )Math.pow(b, 2 );
}
public static void main (String[] args) {
int b = 3 , s = 4 ;
System.out.println( surfaceArea(b, s));
}
}
|
Python 3
def surfaceArea(b, s):
return 2 * b * s + pow (b, 2 )
if __name__ = = "__main__" :
b = 3
s = 4
print (surfaceArea(b, s))
|
C#
using System;
class GFG
{
static int surfaceArea( int b, int s)
{
return 2 * b * s + ( int )Math.Pow(b, 2);
}
public static void Main ()
{
int b = 3, s = 4;
Console.WriteLine(surfaceArea(b, s));
}
}
|
PHP
<?php
function surfaceArea( $b , $s )
{
return 2 * $b * $s + pow( $b , 2);
}
$b = 3; $s = 4;
echo surfaceArea( $b , $s );
?>
|
Javascript
<script>
function surfaceArea(b , s)
{
return 2 * b * s + parseInt(Math.pow(b, 2));
}
var b = 3, s = 4;
document.write( surfaceArea(b, s));
</script>
|
Time complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...