Write a C++ program to calculate the area of a square using an inline function.input and outputEnter the side length of the square: 5 Area of the square with side length 5 is: 25
Question
Write a C++ program to calculate the area of a square using an inline function.input and outputEnter the side length of the square: 5 Area of the square with side length 5 is: 25
Solution
Sure, here is a simple C++ program that uses an inline function to calculate the area of a square:
#include<iostream>
using namespace std;
inline int squareArea(int side) {
return side * side;
}
int main() {
int side;
cout << "Enter the side length of the square: ";
cin >> side;
cout << "Area of the square with side length " << side << " is: " << squareArea(side) << endl;
return 0;
}
In this program:
- We first include the iostream library to allow for input/output operations.
- We declare an inline function
squareAreathat takes an integer argumentsideand returns the square ofside(which is the area of the square). - In the
mainfunction, we declare an integer variablesideto store the side length of the square. - We prompt the user to enter the side length of the square and store the input in
side. - We then call the
squareAreafunction withsideas the argument and print the result.
Similar Questions
Write a program to calculate the area of circle, triangle, rectangle and square using function overloading.
What will be the output of the below C program?#include <stdio.h>#define square(x) x*x int main(){int x; x = 36/square(6); printf("%d", x); getchar(); return 0; }1636None of the above
Which of the following lists the main steps of a program which calculates and prints the area of a square?
Compare local and global variables. Write a program with function to find area of rectangle in C with and without global variable.
find the output of given code.#include <stdio.h>int main(){int a,b=110,c=20;printf("%d",a=b-=c*=5);return 0;}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.