Object

Write a program to read a number and find its square.

Algorithm

  1. Declare a function int square(int x), which accepts an integer as an argument and returns an integer value.
    The Function main
  2. Declare integer variables a and b.
  3. Read a.
  4. Call the function square(a) and store the value it returns in b. (where a is the actual argument)
  5. Write b.
  6. Exit.
    Defining the Function int square(int x)
    (where x is the formal argument)
  7. Declare integer variable y.
  8. y = x * x.
  9. Return (y).
  10. Exit.

Flowchart

C++ Source Code

// program 77
#include<iostream>
using namespace std;

int square(int x);

int main()
{
  int a, b;
  cout << "Enter any number: ";
  cin >> a;
  b = square(a);
  cout << "The square of " << a << " is = " << b;

  return 0;
}

int square(int x)
{
  int y;
  y = x * x;
  return y;
}

C Source Code

/*program 77*/
#include<stdio.h>

int square(int x);

int main()
{
  int a, b;
  printf("Enter any numbers: ");
  scanf("%d", &a);
  b = square(a);
  printf("The square of %d is = %d", a, b);
  
  return 0;
}

int square(int x)
{
  int y;
  y = x * x;
  return(y);
}

Output

Enter any number: 6
The square of 6 is = 36

Design a site like this with WordPress.com
Get started