Program to Calculate the Sum of Two Numbers [C, C++, Python & Java]

Posted in

Program to Calculate the Sum of Two Numbers [C, C++, Python & Java]
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    This tutorial helps you write a program in C, C++, Python, and Java to calculate the sum of two numbers. It is the most basic program every individual tries at the beginning of their programming journey.

    To write this program, you must know a programming language's syntax and the logic to perform the addition operation. That's it!

    C Program to Calculate the Sum of Two Numbers

    #include<stdio.h>
    int main()
    {
       float num_1,num_2,add;
       printf("Enter the first number: ");
       scanf("%f", &num_1);
       printf("Enter the second number: ");
       scanf("%f", &num_2);
       add = num_1+num_2;
       printf("The sum of two numbers is: %f", add);
    return 0;
    }

    Output:

    Enter the first number: 56
    Enter the second number: 67
    The sum of two numbers is: 123.000000

    C++ Program to Calculate the Sum of Two Numbers

    #include<iostream>
    using namespace std;
    int main()
    {
       float num_1,num_2,add;
       cout<<"Enter the first number: ";
       cin>>num_1;
       cout<<"Enter the second number: ";
       cin>>num_2;
       cout<<"The sum of two numbers is: "<<num_1+num_2;
       return 0;
    }

    Output:

    Enter the first number: 4.7
    Enter the second number: 3.4
    The sum of two numbers is: 8.1

    Python Program to Calculate the Sum of Two Numbers

    num_1= float(input("Enter the first number: "))
    nun_2= float(input("Enter the second number: "))
    print("The sum of two numbers is:",num_1+nun_2)

    Output:

    Enter the first number: 4.7
    Enter the second number: 3.4
    The sum of two numbers is: 8.1

    Java Program to Calculate the Sum of Two Numbers

    import java.util.*;
    public class Main
    {
        public static void main(String[] args)
        {
           float num_1,num_2,total;
           Scanner sc = new Scanner(System.in);
           System.out.println("Enter the first number: ");
           num_1 = sc.nextFloat();
    	   System.out.println("Enter the first number: ");
           num_2 = sc.nextFloat();
    	   total = num_1 + num_2;
    	   System.out.println("The sum of two number is: " +total);
    }
    }

    Output:

    Enter the first number: 
    45
    Enter the first number: 
    67
    The sum of two number is: 112.0

    Conclusion

    We are done with writing a program to calculate the sum of two numbers in C, C++, Python, and Java. Implement this most basic and easy program on your system!

    People are also reading:

    Leave a Comment on this Post

    0 Comments