Add Two Numbers Program Pseudocode Algorithm
1 2 3 4 5 6 7 8 9 10 11 |
BEGIN NUMBER s1, s2, sum OUTPUT("Input number1:") INPUT s1 OUTPUT("Input number2:") INPUT s2 sum=s1+s2 OUTPUT sum END |
You May Also Like: Pseudocode Examples
C# Console Code: Write a program to add two numbers in C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Program { static void Main(string[] args) { int num1, num2, sum; Console.WriteLine("Calculate the sum of two numbers:"); Console.Write("Input number1:"); num1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Input number2:"); num2 = Convert.ToInt32(Console.ReadLine()); sum = num1 + num2; Console.Write("Result:"+sum); Console.ReadKey(); } } |
Java Code: Write a program to add two numbers in Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.print("Enter first number: "); int first = reader.nextInt(); System.out.print("Enter second number: "); int second = reader.nextInt(); int sum = first + second; String output = String.format("%d + %d = %d",first,second,sum); System.out.println(output); } |
Python Code: Write a program to add two numbers in Python
1 2 3 4 5 6 |
num1 = input('Enter #1 : ') num2= input('Enter #2 ') sum=float(num1)+float(num2) print("SUM:{0} ".format(sum)) |
PHP Code: Write a program to add two numbers in PHP
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 |
<?php $num1=0; $num2=0; $sum=0; if(isset($_POST["add"])) { $num1=$_POST["num1"]; $num2=$_POST["num2"]; $sum=$num1+$num2; } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Add 2 Numbers in PHP</title> <style> label{ display: block; } </style> </head> <body> <h1>Sum of Two Numbers:<?=$sum?></h1> <form method="post"> <label>Number 1: <input type="text" name="num1" value="<?=$num1?>"> </label> <label>Number 2: <input type="text" name="num2" value="<?=$num2?>"> </label> <button type="submit" name="add">Add Numbers</button> </form> </body> </html> |
JavaScript Code: Write a program to add two numbers in JavaScript
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 |
<!doctype html> <html> <head> <title>Code4Examples</title> <meta charset="utf-8"> <style> label{ display: block; } </style> </head> <body> <h1 id="msg"></h1> <label for="num1">Number 1: <input type="text" id="num1"> </label> <label for="num2">Number 2: <input type="text" id="num2"> </label> <button id="add">Add Numbers</button> <script> //get inputs and button element from document var num1El=document.querySelector("#num1"); var num2El=document.querySelector("#num2"); var addBtn=document.querySelector("#add"); var msgEl=document.querySelector("#msg"); //bind a function tothe onClick event the AddBtn addBtn.onclick=function(){ //add two numbers sum=Number(num1.value)+ Number(num2.value); //write the sum into #msg document msgEl.innerHTML=sum; } </script> </body> </html> |
C# Windows Form Code: Write a program to add two numbers in C# Windows Form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Add_button_Click(object sender, EventArgs e) { try { double a = Convert.ToDouble(textBox1.Text); double b = Convert.ToDouble(textBox2.Text); textBox3.Text = (a + b).ToString(); } catch(Exception) { MessageBox.Show("invalid input"); } } } |
Python Tkinter Code: Write a program to add two numbers in Python Tkinter
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 |
from tkinter import * def addNumbers(): res=int(e1.get())+int(e2.get()) myText.set(res) master = Tk() myText=StringVar(); Label(master, text="First").grid(row=0, sticky=W) Label(master, text="Second").grid(row=1, sticky=W) Label(master, text="Result:").grid(row=3, sticky=W) result=Label(master, text="", textvariable=myText).grid(row=3,column=1, sticky=W) e1 = Entry(master) e2 = Entry(master) e1.grid(row=0, column=1) e2.grid(row=1, column=1) b = Button(master, text="Calculate", command=addNumbers) b.grid(row=0, column=2,columnspan=2, rowspan=2,sticky=W+E+N+S, padx=5, pady=5) mainloop() |
Flowchart of Pseudocode
Everything is very open with a really clear explanation of the challenges.
It was definitely informative. Your site is very helpful.
Many thanks for sharing!