Saturday, October 25, 2014

How to find the area of a rectangle using C and java

USING C

#include<stdio.h>
#include<conio.h>
int main()
{
   int length,width,area;
   length=4;
   width=3;
   area=length*width;
   printf(" the area of the rectangle is: %d ", area);
// %d is use of integer type variables//
   return 0;
}

USING JAVA

public static void main(String[] args)
{
  int length = 4;
  int width = 3;
  int area = length * width ;
  System.out.println("the area of the rectangle is: " + area);
}

..........................................................................................................

in both  java and c the output will be

the result is: 12


...........................................................................................................

IF WE WANT THE USER TO INPUT THE NUMBERS:

USING C

#include<stdio.h>
#include<conio.h>
int main()
{
   int x,y,z;
   printf("enter the length:");
   scanf("%d", &x);
   printf("Enter the width:");
   scanf("%d", &y);
   z=x*y;
   printf(" the area of the rectangle is: %d ", z);
// %d is use of integer type variables//
   return 0;
}

USING JAVA

import java.util.Scanner;
{
 static Scanner sc= new Scanner(System.in);
        public static void main(String[] args)
       {
  int firstvalue;
  int secondvalue;
  System.out.print("Enter the length:");
  firstvalue= sc.nextInt();
  System.out.print("Enter the width:");
  secondvalue=sc.nextInt();
  int result = firstvalue * secondvalue ;
  System.out.println("the area of the rectangle is:" + result);



 }



}

0 comments:

Post a Comment