Add Two Numbers

Description

The variables firstNumber and secondNumber are added using the arithmetic
operator + and the result is stored in the variable sum.

C/C++

/* C Program to add two numbers */
//Save it as AddTwoNumber.c

#include<stdio.h>
int main(){

    int firstNumber, secondNumber, sum;

    printf("Enter the first number : ");
    scanf("%d",&firstNumber);

    printf("Enter the second number : ");
    scanf("%d",&secondNumber);

    sum = firstNumber + secondNumber;

    printf("The sum of %d and %d is %d",firstNumber,secondNumber,sum);

    return 0;
}
Input:
Enter the first number : 
15
Enter the second number : 
2

Output:
The sum of 15 and 2 is 17

Java

/* Java Program to add two number */
//Save it as AddTwoNumber.java

import java.io.*;
import java.util.Scanner;
public class AddTwoNumber {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        int firstNumber, secondNumber, sum;

        System.out.println("Enter the first number : ");
        firstNumber = scanner.nextInt();

        System.out.println("Enter the second number : ");
        secondNumber = scanner.nextInt();

        sum = firstNumber + secondNumber;

        System.out.println("The sum of "+firstNumber+" and "+secondNumber+" is "+ sum);
    }
}
Input:
Enter the first number : 
15
Enter the second number : 
2

Output:
The sum of 15 and 2 is 17

Related Programs

1) Program to subtract two numbers
2) Program to multiply two numbers
3) Program to divide two numbers
4) Program to find modulus of two numbers
5) Program to find Quotient and Remainder
6) Program to calculate power of a number
7) Program to calculate Permutation And Combination
8) Program to find simple interest
9) Program to calculate Gross Salary
10) Program to calculate percentage mark of student
Share Me

Leave a Reply