Addition of two numbers
Table of Contents
- Program for addition of two numbers in java
- Program for addition of two numbers in groovy
- Program for addition of two numbers in c++
- Program for addition of two numbers in c
- Program for addition of two numbers in c#
- Program for addition of two numbers in vb.net
- Program for addition of two numbers in php
- Program for addition of two numbers in node js
- Program for addition of two numbers in typescript
- Program for addition of two numbers in python
- Program for addition of two numbers in ruby
- Program for addition of two numbers in scala
- Program for addition of two numbers in swift
- Program for addition of two numbers in kotlin
- Program for addition of two numbers in rust
Program for addition of two numbers in java
// Java program for addition of two numbers
public class Main
{
// x and y are take parameter value
public static double sum(double x, double y)
{
// Here return sum of x and y
return x + y;
}
public static void main(String[] args)
{
// Test
System.out.println(sum(1, 2));
System.out.println(sum(1.5, 2.5));
}
}
Output
3.0
4.0
Program for addition of two numbers in groovy
// Groovy program for addition of two numbers
public class Main
{
// x and y are take parameter value
public static double sum(double x, double y)
{
// Here return sum of x and y
return x + y;
}
public static void main(String[] args)
{
// Test
println(Main.sum(1, 2));
println(Main.sum(1.5, 2.5));
}
}
Output
3.0
4.0
Program for addition of two numbers in c++
// Include header file
#include <iostream>
// Stdc++11 program for addition of two numbers
class Task
{
// x and y are take parameter value
public:
static double sum(double x, double y)
{
// Here return sum of x and y
return x + y;
}
};
int main(int argc, char **argv){
// Test
std::cout << Task::sum(1, 2) << std::endl;
std::cout << Task::sum(1.5, 2.5) << std::endl;
return 0;
};
Output
3
4
Program for addition of two numbers in c
// Include header file
#include <stdio.h>
// C program for addition of two numbers
// x and y are take parameter value
double sum(double x, double y)
{
// Here return sum of x and y
return x + y;
}
int main()
{
// Test
printf("%lf\n", sum(1, 2));
printf("%lf\n", sum(1.5, 2.5));
return 0;
}
Output
3.000000
4.000000
Program for addition of two numbers in c#
// Include namespace system
using System;
// C# program for addition of two numbers
public class Task
{
// x and y are take parameter value
public static double sum(double x, double y)
{
// Here return sum of x and y
return x + y;
}
public static void Main(String[] args)
{
// Test
Console.WriteLine(Task.sum(1, 2));
Console.WriteLine(Task.sum(1.5, 2.5));
}
}
Output
3
4
Program for addition of two numbers in vb.net
' Include namespace system
Imports System
' Vb.net program for addition of two numbers
public Class Task
' x and y are take parameter value
Public Shared Function sum(ByVal x As Double,
ByVal y As Double) As Double
' Here return sum of x and y
Return x + y
End Function
Public Shared Sub Main(ByVal args As String())
' Test
Console.WriteLine(sum(1, 2))
Console.WriteLine(sum(1.5, 2.5))
End Sub
End Class
Output
3
4
Program for addition of two numbers in php
<?php
// Php program for addition of two numbers
class Task
{
// x and y are take parameter value
function sum($x, $y)
{
// Here return sum of x and y
return $x + $y;
}
}
// Test
printf("%f\n",Task::sum(1, 2));
printf("%f\n",Task::sum(1.5, 2.5));
Output
3.000000
4.000000
Program for addition of two numbers in node js
// Node Js program for addition of two numbers
// x and y are take parameter value
function sum(x, y)
{
// Here return sum of x and y
return x + y;
}
// Test
console.log(sum(1, 2));
console.log(sum(1.5, 2.5));
Output
3
4
Program for addition of two numbers in typescript
// Typescript program for addition of two numbers
class Task
{
// x and y are take parameter value
public static number sum(x: number, y: number)
{
// Here return sum of x and y
return x + y;
}
}
var work = new Task();
// Test
console.log(work.sum(1, 2));
console.log(work.sum(1.5, 2.5));
/*
file : code.ts
tsc --target es6 code.ts
node code.js
*/
Output
3
4
Program for addition of two numbers in python
# Python 3 program for addition of two numbers
class Task :
# x and y are take parameter value
@staticmethod
def sum( x, y) :
# Here return sum of x and y
return x + y
if __name__=="__main__":
# Test
print(Task.sum(1, 2))
print(Task.sum(1.5, 2.5))
Output
3
4.0
Program for addition of two numbers in ruby
# Ruby program for addition of two numbers
class Task
# x and y are take parameter value
def self.sum( x, y)
# Here return sum of x and y
return x + y
end
end
# Test
print(Task.sum(1, 2),"\n")
print(Task.sum(1.5, 2.5),"\n")
Output
3
4.0
Program for addition of two numbers in scala
// Scala program for addition of two numbers
class Task ()
{
// x and y are take parameter value
def sum(x : Double, y : Double) : Double=
{
// Here return sum of x and y
return x + y
}
}
object Main
{
def main(args : Array[String]) : Unit=
{
var work = new Task();
// Test
println(work.sum(1, 2))
println(work.sum(1.5, 2.5))
}
}
Output
3.0
4.0
Program for addition of two numbers in swift
import Foundation
// Swift program for addition of two numbers
class Task
{
// x and y are take parameter value
static func sum(_ x: Double, _ y: Double) -> Double
{
// Here return sum of x and y
return x + y;
}
}
// Test
print(Task.sum(1, 2));
print(Task.sum(1.5, 2.5));
Output
3.0
4.0
Program for addition of two numbers in kotlin
// Kotlin program for addition of two numbers
class Task
{
companion object
{
// x and y are take parameter value
fun sum(x: Number, y: Number): Double
{
var a = x.toDouble();
var b = y.toDouble();
// Here return sum of x and y
return a + b;
}
}
}
fun main(args: Array < String > ): Unit
{
// Test
println(Task.sum(1, 2));
println(Task.sum(1.5, 2.5));
}
Output
3.0
4.0
Program for addition of two numbers in rust
// Rust program for addition of two numbers
fn main()
{
// Test
println!("{}",sum(1.0, 2.0));
println!("{}",sum(1.5, 2.5));
}
// x and y are take parameter value
fn sum(x: f64, y: f64) -> f64
{
// Here return sum of x and y
return x + y;
}
Output
3
4
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment