Knowee
Questions
Features
Study Tools

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

🧐 Not the exact question you are looking for?Go ask a question

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:

  1. We first include the iostream library to allow for input/output operations.
  2. We declare an inline function squareArea that takes an integer argument side and returns the square of side (which is the area of the square).
  3. In the main function, we declare an integer variable side to store the side length of the square.
  4. We prompt the user to enter the side length of the square and store the input in side.
  5. We then call the squareArea function with side as the argument and print the result.

This problem has been solved

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;}

1/3

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.