Pseudocode serves as a structured form of English used to describe algorithms, enabling designers to concentrate on algorithmic logic without the distraction of language syntax details. In this post, however, we will shift our focus to employing a C-style syntax.
Let’s delve into a practical example to illustrate the application of pseudocode with a C-style syntax in calculating the area and perimeter of a rectangle.
Sample Pseudocode in C-style Syntax: Calculating Area and Perimeter of a Rectangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // Define variables for length and width float length, width; // Prompt user for input Print("Enter the length of the rectangle: "); Input(length); Print("Enter the width of the rectangle: "); Input(width); // Calculate area and perimeter float area, perimeter; area = length * width; perimeter = 2 * (length + width); // Display the results Print("Area of the rectangle: ", area); Print("Perimeter of the rectangle: ", 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> |
In this journey through algorithm design using pseudocode with C-style syntax, we’ve explored a universal language that transcends the specifics of programming languages. The beauty of pseudocode lies in its ability to articulate algorithmic logic without getting entangled in the nuances of syntax.
By providing practical examples with PHP, C#, Java, Python, and more, we’ve demonstrated the versatility of pseudocode in bridging the gap between algorithmic concepts and code implementation. Whether you’re a seasoned developer or just starting your programming adventure, the principles discussed here serve as a foundation for effective algorithm design in various programming languages.
As you apply these concepts in real-world scenarios, consider the adaptability of pseudocode as a tool for expressing algorithmic ideas across diverse programming languages. Whether you find yourself immersed in the simplicity of Python, the robustness of Java, the elegance of C#, or the flexibility of PHP, the fundamental logic remains constant.
Embrace the power of pseudocode to streamline your algorithm design process, fostering clarity and comprehension. As you navigate the intricate world of programming, remember that a well-designed algorithm transcends the boundaries of language syntax, paving the way for efficient and scalable solutions.
In closing, let the knowledge gained here propel you towards a deeper understanding of algorithmic design, inspiring innovation and problem-solving across the rich tapestry of programming languages.