Print area of rectangle using function and return its value to main function

Write a program to print the area of a rectangle using the function and return its value to the main function

Example:

Input:  length = 10, width= 4
Output: Area of rectangle 40

Approach

C

#include <stdio.h>

int areaOfRectangle(int lengthint width)
{
    int area = length * width;
    return area;
}
int main()
{
    int lengthwidth;
    printf("Enter length and width of rectangle : ");
    scanf("%d%d", &length, &width);
    int area = areaOfRectangle(lengthwidth);
    printf("Area of rectangle %d"area);
    return 0;
}

Java

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int areaOfRectangle =0;
       System.out.println("enter the length");
        int length = sc.nextInt();
       System.out.println("enter the width");
        int width = sc.nextInt();
        areaOfRectangle=length*width;
       System.out.println("area of rectangle is "+areaOfRectangle);
    }

}

No comments:

Post a Comment