Program to print sum of two numbers: 1 and 2.
Code:
section .text
global _start ; must be declared for using gcc
_start: ; tell linker entry point
mov eax, '1'
sub eax, '0' ; eax='1'-'0'=1
mov ebx, '2'
sub ebx, '0' ; ebx='2'-'0'=2
add eax, ebx
add eax, '0'
mov [sum], eax
mov ecx, msg
mov edx, len
mov ebx, 1 ; file descriptor (stdout)
mov eax, 4 ; system call number (sys_write)
int 0x80 ; call kernel
mov ecx, sum
mov edx, 1
mov ebx, 1 ; file descriptor (stdout)
mov eax, 4 ; system call number (sys_write)
int 0x80 ; call kernel
mov eax, 1 ; system call number (sys_exit)
int 0x80 ; call kernel
section .data
msg db "The sum is: ", 0xA
len equ $ - msg
segment .bss
sum resb 1
Output:
>>
The sum is:
3>>
Program to print sum of two numbers: 1 and 2.
Code:
a=1
b=2
c=$((a+b))
echo "The sum is: $c"
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers using a function: 1 and 2.
Code:
Function Sum (x As Integer, y as Integer) As Integer
Sum = x + y
End Function
Dim As Integer a, b, c
a = 1
b = 2
c = Sum(a, b)
Print "The sum is: ", c
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
#include <stdio.h>
int sum(int x, int y) {
int sum = x + y;
return sum;
}
int main() {
int a = 1, b = 2, c;
c = sum(a, b);
printf("The sum is: %d", c);
return 0;
}
Output:
>>
The sum is: 3>>
Program to print sum of two numbers: 1 and 2.
Code:
#include<iostream>
using namespace std;
int sum(int x, int y) {
int sum = x + y;
return sum;
}
int main() {
int a = 1, b = 2, c;
c = sum(a, b);
cout << "The sum is: " << c;
return 0;
}
Output:
>>
The sum is: 3>>
Program to print sum of two numbers: 1 and 2.
Code:
(defn sum [x y] (+ x y))
(let [a 1
b 2]
(println (str "The sum is: " (sum a b)))
)
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
using System;
public class funcsum
{
// Define a public static method 'Sum' that takes two integer parameters and returns their sum
public static int Sum(int x, int y)
{
int sum;
sum = x + y;
return sum;
}
// Main method, the entry point of the program
public static void Main()
{
int a = 1;
int b = 2;
int c = Sum(a, b);
Console.WriteLine("The sum is: {0}", c);
}
}
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
IDENTIFICATION DIVISION.
PROGRAM-ID. PROGSUM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9(1) VALUE 1.
01 WS-B PIC 9(1) VALUE 2.
01 WS-SUM PIC 9(1).
PROCEDURE DIVISION.
COMPUTE WS-SUM = WS-A + WS-B.
DISPLAY "The sum is: " WS-SUM.
STOP RUN.
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
(defun sum (x y)
(+ x y))
(let ((a 1) (b 2))
(format t "The sum is: ~d" (sum a b) )
)
Output:
>>
The sum is: 3>>
Program to print sum of two numbers: 1 and 2.
Code:
import std.stdio;
int sum(int x, int y) {
int sum = x + y;
return sum;
}
void main() {
int a = 1, b = 2, c;
writefln("The sum is: %d", sum(a, b));
}
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
defmodule Math do
def sum(x, y) do
x + y
end
end
a = 1
b = 2
IO.puts("The sum is: #{Math.sum(a, b)}")
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Note:
Module must be namedmain
.
Module must export functionmain
which accepts one argument.
Code:
-module(main).
-export([main/1]).
sum(X,Y) ->
SUM = X+Y,
SUM.
main(_) ->
A = 1,
B = 2,
C = sum(A, B),
io:fwrite("The sum is: ~w", [C] ).
Output:
>>
The sum is: 3>>
Program to print sum of two numbers: 1 and 2.
Code:
let sum x y = x + y
let a = 1
let b = 2
let c = sum a b
System.Console.WriteLine("The sum is: {0}", c)
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
function sumAB(X, Y) result(sum)
integer, intent (in) :: X
integer, intent (in) :: Y
integer :: sum
sum = X + Y
end function
program main
implicit none
integer :: A = 1
integer :: B = 2
integer :: C
integer :: sumAB
C = sumAB(A, B)
print *, "The sum is: ", C
end program
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
package main
import "fmt"
func sum(x int, y int) int {
return x + y
}
func main() {
a := 1
b := 2
c := sum(a, b)
fmt.Println("The sum is: ", c)
}
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
def sum(int x, int y) {
return x + y;
}
a = 1
b = 2
c = sum(a, b)
printf("The sum is: %d", c)
Output:
>>
The sum is: 3>>
Program to print sum of two numbers: 1 and 2.
Code:
sumAB :: Integer -> Integer -> Integer --function declaration
sumAB x y = x + y --function definition
main = do
let a = 1
let b = 2
let c = (sumAB a b) --calling a function
putStr "The sum is: "
print(c)
Output:
>>
[1 of 1] Compiling Main ( main.hs, main.o )
Linking main ...
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
import java.io.*;
public class Main {
public static int sum(int x, int y) {
return x + y;
}
public static void main(String[] args) {
int a = 1;
int b = 2;
int c = sum(a, b);
System.out.printf("The sum is: %d", c);
}
}
Output:
>>
The sum is: 3>>
Program to print sum of two numbers: 1 and 2.
Code:
function sum(x, y) {
return x + y;
}
var a = 1;
var b = 2;
var c = sum(a, b);
console.log("The sum is: ", c);
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
function sum(x, y)
return x + y
end
a = 1
b = 2
c = sum(a, b)
println("The sum is: ", c);
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
fun sum(x: Int, y: Int): Int {
return x + y
}
fun main() {
val a = 1
val b = 2
val c = sum(a, b)
println("The sum is: $c")
}
Output:
>>
OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
function sum(x, y)
return x + y
end
a = 1
b = 2
c = sum(a, b)
print(string.format("The sum is: %d", c))
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
#import <Foundation/Foundation.h>
@interface SumClass:NSObject
/* Method declaration */
- (int) num1: (int) x num2: (int) y;
@end
@implementation SumClass
/* Method returning the sum of two numbers */
- (int) num1: (int) x num2: (int) y {
return x + y;
}
@end
int main () {
@autoreleasepool {
int a = 1;
int b = 2;
int c;
SumClass *obj = [[SumClass alloc]init];
/* Call the sum method */
c = [obj num1:a num2:b];
//NSLog(@"The sum is: %d\n", c );
NSString *message = [NSString stringWithFormat:@"The sum is: %d\n", c];
printf("%s", message.UTF8String);
}
return 0;
}
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
let sum = fun x y -> x + y;;
let a = 1;;
let b = 2;;
let c = sum a b;;
Printf.printf "The sum is: %i\n" c
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Note: Top level function must be named
script()
Code:
function script()
a = 1;
b = 2;
c = sumAB(a, b);
printf("The sum is: %d\n", c);
endfunction
function retval = sumAB(x, y)
retval = x + y;
endfunction
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
program main;
var
a, b, c : integer;
function sum(x, y: integer): integer;
begin
sum := x + y;
end;
begin
a := 1;
b := 2;
c := sum(a, b);
writeln('The sum is: ', c);
end.
Output:
>>
Free Pascal Compiler version 3.0.4 [2017/10/03] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling main.pas
Linking main
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
15 lines compiled, 0.1 sec
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
sub sum {
my ($x, $y) = @_;
my $sum = $x + $y;
return $sum;
}
$a = 1;
$b = 2;
$c = sum($a, $b);
print "The sum is: $c\n";
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
<?php
function sum($x, $y)
{
return $x + $y;
}
$a = 1;
$b = 2;
$c = sum($a, $b);
echo "The sum is: $c\n";
?>
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Note: Top level predicate must be
main
.
Code:
:- initialization(main).
set(X, Val) :- X is Val.
sum(X, Y, Z) :- Z is X + Y.
main :- set(A, 1),
set(B, 2),
sum(A, B, C),
format('The sum is: ~w', [C]).
Output:
>>
The sum is: 3>>
Program to print sum of two numbers: 1 and 2.
Code:
def sum(x, y):
return x + y
a = 1
b = 2
c = sum(a, b)
print "The sum is: %d"%(c)
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
def sum(x, y):
return x + y
a = 1
b = 2
c = sum(a, b)
print("The sum is: %d"%(c))
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
sum <- function(x, y) {
return (x + y)
}
a <- 1
b <- 2
c <- sum(a, b)
sprintf("The sum is: %i", c)
Output:
>>
[1] "The sum is: 3"
>>
Program to print sum of two numbers: 1 and 2.
Code:
def sum (x, y)
return x + y
end
a = 1
b = 2
c = sum(a, b)
puts "The sum is: #{c}"
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
fn sum(x: i32, y: i32) -> i32 {
return x + y;
}
fn main() {
let a = 1;
let b = 2;
let c = sum(a, b);
println!("The sum is: {c}", c=c);
}
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Note: Object must be named
Main
.
Code:
object Main {
def sum(x: Int, y: Int): Int = {
return x + y
}
def main(args: Array[String]) = {
val a = 1
val b = 2
val c = sum(a, b)
println("The sum is: " + c)
}
}
Output:
>>
The sum is: 3
>>
Program to print sum of two columns with the values: 1 and 2.
Code:
CREATE TABLE data (
id INTEGER PRIMARY KEY,
a INTEGER,
b INTEGER
);
INSERT INTO data (a, b)
VALUES( 1, 2);
ALTER TABLE `data` ADD COLUMN `sum` INTEGER;
UPDATE data
SET sum = (SELECT a + b FROM data);
.mode tabs
.header on
SELECT * FROM data;
Output:
>>
id a b sum
1 1 2 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
func sum(x: Int, y: Int) -> Int {
return x + y
}
var a = 1
var b = 2
var c = sum(x:a, y:b)
print("The sum is: ", c)
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
#include <systemc.h>
SC_MODULE (SUM_MODULE) { // module named hello
int a = 1, b = 2, c;
SC_CTOR (SUM_MODULE) { //constructor phase, which is empty in this case
SC_THREAD(sum);
}
void sum() {
c = a + b;
std::cout << "The sum is: " << c << std::endl;
}
};
int sc_main(int argc, char* argv[]) {
SUM_MODULE sum_mod("sum");
sc_start();
return 0;
}
Output:
>>
SystemC 3.0.0-Accellera --- Oct 5 2024 19:28:18
Copyright (c) 1996-2024 by all Contributors,
ALL RIGHTS RESERVED
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
proc sum {x y} {
return [expr $x + $y]
}
set a 1
set b 2
set c [sum $a $b]
puts "The sum is: $c"
Output:
>>
The sum is: 3
>>
Program to print sum of two numbers: 1 and 2.
Code:
function sum(x: number, y: number) {
return x + y;
}
let a = 1;
let b = 2;
let c = sum(a, b);
console.log("The sum is:", c);
Output:
>>
The sum is: 3
>>
Program to print output of full adder to which random inputs are provided using a testbench.
Code:
// Full adder module
module fulladd ( input [3:0] a,
input [3:0] b,
input c_in,
output reg c_out,
output reg [3:0] sum);
always @ (a or b or c_in)
begin
{c_out, sum} = a + b + c_in;
end
endmodule
// Testbench
module tb_fulladd;
// 1. Declare testbench variables
reg [3:0] a;
reg [3:0] b;
reg c_in;
wire [3:0] sum;
integer i;
// 2. Instantiate the design and connect to testbench variables
fulladd fa0 ( .a (a),
.b (b),
.c_in (c_in),
.c_out (c_out),
.sum (sum));
// 3. Provide stimulus to test the design
initial begin
a <= 0;
b <= 0;
c_in <= 0;
$monitor ("a=0x%0h b=0x%0h c_in=0x%0h c_out=0x%0h sum=0x%0h", a, b, c_in, c_out, sum);
// Use a for loop to apply random values to the input
for (i = 0; i < 5; i = i+1)
begin
#10 a <= $random;
b <= $random;
end
end
endmodule
Output:
>>
a=0x0 b=0x0 c_in=0x0 c_out=0x0 sum=0x0
a=0x4 b=0x1 c_in=0x1 c_out=0x0 sum=0x6
a=0x3 b=0xd c_in=0x1 c_out=0x1 sum=0x1
a=0x5 b=0x2 c_in=0x1 c_out=0x0 sum=0x8
a=0xd b=0x6 c_in=0x1 c_out=0x1 sum=0x4
a=0xd b=0xc c_in=0x1 c_out=0x1 sum=0xa
>>
Program to print output of counter that increments every 10ns until 50ns.
Note:
Top level entity must be namedmain
.
Usefinish
orstop
available in std.env package to stop the testbench
Usewriteline()
to print output
Code:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use std.textio.all;
use IEEE.std_logic_textio.all;
entity counter is
port (
clk: in std_logic;
n_rst: in std_logic;
cout: out std_logic_vector (7 downto 0)
);
end entity;
architecture behaviour of counter is
signal cnt: std_logic_vector (7 downto 0);
begin
process (clk, n_rst)
begin
if n_rst = '0' then
cnt <= (others => '0');
elsif rising_edge(clk) then
cnt <= cnt + 1;
end if;
end process;
cout <= cnt;
end architecture;
entity main is
end entity;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use std.textio.all;
use IEEE.std_logic_textio.all;
use std.env.all;
architecture testbench of main is
component counter
port(
clk: in std_logic;
n_rst: in std_logic;
cout: out std_logic_vector (7 downto 0)
);
end component;
signal clk: std_logic;
signal n_rst: std_logic;
signal cout: std_logic_vector (7 downto 0);
begin
dut: counter port map (clk, n_rst, cout);
process is
variable my_line : line;
begin
clk <= '0';
wait for 5 ns;
write(my_line, cout);
writeline(output, my_line);
-- report my_line;
clk <= '1';
wait for 5 ns;
end process;
process
begin
n_rst <= '0';
wait for 10 ns;
n_rst <= '1';
wait for 50 ns;
-- report "done." severity failure;
-- stop;
finish;
end process;
end;
Output:
>>
00000000
00000000
00000001
00000010
00000011
00000100
simulation finished @60ns
>>