Python/C/C++/JAVA

Basic Practice Programs with Code and Concept

By D.S

Write a Program to Add Two Numbers

Okay so basically in this program, you're gonna get asked to input two whole numbers. It's not that hard, don't worry - just type in any two numbers you want. After you've done that, the program is gonna automatically add them up and store the answer for ya. Pretty cool, huh? So if you wanna try it out just go ahead and give it a shot! And hey, who knows - maybe this will be your first step towards becoming a programming whizz kid or something.

(a.) C Code

#include <stdio.h>
  int main() { 
  int  num1, num2, sum;  

  // Input  
  printf ("Enter first number: "); 
  scanf ("%d", &num1);  

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

  // Addition  
  sum  = num1 + num2;  

  // Output  
  printf ("Sum: %d
", sum);  

  return  0; 
  }
Output:-
Enter first number: 5
Enter second number: 6
Sum: 11

(b.) C++ Code

#include <iostream> 
  int main () { 
  int  num1, num2, sum;  
 
  // Input  
  std::cout  << "Enter first number: ";  
  std::cin  >> num1;  
 
  std::cout  << "Enter second number: ";  
  std::cin  >> num2;  
 
  // Addition  
  sum  = num1 + num2;  
 
  // Output  
  std::cout  << "Sum: " << sum << std::endl;  
 
  return  0; 
  }
Output:-
Enter first number: 5
Enter second number: 6
Sum: 11

(c.) Python Code

num1  =  int (input ("Enter first number: ")) 
num2  =  int (input ("Enter second number: "))  
# Addition  
sum_result  = num1  + num2   
  
# Output  
print ("Sum:", sum_result ) 
Output:-
Enter first number: 5
Enter second number: 6
Sum: 11

(d.) Java Code

import  java.util.Scanner;  

  public class AddTwoNumbers {  
  public static void main(String[] args) {  
  int  num1, num2, sum;  

  // Input  
  Scanner  scanner =  new  Scanner(System.in); 
  System.out.print ("Enter first number: ");  
  num1  = scanner.nextInt();  

  System.out.print ("Enter second number: ");  
  num2  = scanner.nextInt();  

  // Addition  
  sum  = num1 + num2;  

  // Output  
  System.out.println ("Sum: " + sum);  
  }  
  } 
Output:-
Enter first number: 5
Enter second number: 6
Sum: 11