Pseudocode is a kind of structured english for describing algorithms. It allows the designer to focus on the logic of the algorithm without being distracted by details of language syntax. But in this post we will use the C style syntax
Calculate Area and Perimeter of Rectangle
1 2 3 4 5 6 7 8 9 |
int b1,b2,area,perimeter; read b1; read b2; area=b1*b2; perimeter=2*(b1+b2); print area; print perimeter; |
C Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> #include <stdlib.h> int main() { int b1, b2, area, perimeter; printf("Width: "); scanf("%d", &b1); printf("Height: "); scanf("%d", &b2); area = b1 * b2; perimeter = 2 * (b1 + b2); printf("area = %d\n", area); printf("perimeter = %d", perimeter); return 0; } |
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include<stdlib.h> using namespace std; int main() { int l,w,perimeter,area; cout<<"Length : "; cin>>l; cout<<"Width : "; cin>>w; area=l*w; perimeter=2*(l+w); cout<<"Area of Rectangle : "<<area<<endl; cout<<"Perimeter of Rectangle : "<<perimeter; } |
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 |
/*Calculate Area and Perimeter of Rectangle */ int b1, b2, area, perimeter; Console.Write("Width:"); b1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Height:"); b2 = Convert.ToInt32(Console.ReadLine()); area = b1 * b2; perimeter = 2 * (b1 + b2); Console.Write("area="+area); Console.Write("perimeter=" + perimeter); |
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package javaexamples; import java.util.Scanner; public class JavaExamples { public static void main(String[] args) { /*Calculate Area and Perimeter of Rectangle */ Scanner in = new Scanner(System.in); int b1, b2, area, perimeter; System.out.println("Width: "); b1 = in.nextInt(); System.out.println("Height: "); b2 = in.nextInt(); area = b1 * b2; perimeter = 2 * (b1 + b2); System.out.println("area="+area); System.out.println("perimeter=" + perimeter); } } |
Python Code:
1 2 3 4 5 6 7 8 |
l=int(input("Length : ")) w=int(input("Width : ")) area=l*w perimeter=2*(l+w) print("Area of Rectangle : ",area) print("Perimeter of Rectangle : ",perimeter) |
JavaScript Code:
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
<!doctype html> <html> <head> <title>Code4Examples</title> <meta charset="utf-8"> <style> label{ display: block; } </style> </head> <body> <h1>Area and Perimeter of Rectangle</h1> <h2 id="area"></h2> <h2 id="perimeter"></h2> <label for="len">Length: <input type="text" id="len"> </label> <label for="wid">Width: <input type="text" id="wid"> </label> <button id="calcBtn">Calculate</button> <script> //get inputs and button element from document var lenEl=document.querySelector("#len"); var widEl=document.querySelector("#wid"); var calcBtn=document.querySelector("#calcBtn"); var areagEl=document.querySelector("#area"); var perimeterEl=document.querySelector("#perimeter"); //bind a function tothe onClick event the AddBtn calcBtn.onclick=function(){ //area formule length*width area=Number(lenEl.value)*Number(widEl.value) //perimeter formule 2*(length+width) perimeter=2*(Number(lenEl.value)+Number(widEl.value)) //write the results into #area #perimeter document areagEl.innerHTML="Area of rectange:"+area; perimeterEl.innerHTML="Perimeter of rectange:"+perimeter; } </script> </body> </html> |
PHP Code:
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<?php $area=0; $perimeter=0; $len=""; $wid=""; if(isset($_POST["calcBtn"])) { $len= $_POST["len"]; $wid= $_POST["wid"]; //area formule length*width $area=$len*$wid; //perimeter formule 2*(length+width) $perimeter=2*($len+$wid); } ?> <!doctype html> <html> <head> <title>Code4Examples</title> <meta charset="utf-8"> <style> label{ display: block; } </style> </head> <body> <h1>Area and Perimeter of Rectangle</h1> <h2>Area: <?=$area?></h2> <h2>Perimeter:<?=$perimeter?></h2> <form action="" method="post"> <label for="len">Length: <input type="text" name="len" value="<?=$len?>"> </label> <label for="wid">Width: <input type="text" name="wid" value="<?=$wid?>"> </label> <button name="calcBtn" type="submit">Calculate</button> </form> </body> </html> |