Sunday, August 11, 2013

C++ and Java Program to add two numbers

Using C++:-

#include<iostream> 
using namespace std;
int main(){
int a,b,c;
cout<<"Enter First Number";
cin>>a;
cout<<"Enter Second Number";
cin>>b;
c=a+b;
cout<<"The sum is"<<" "<<c;
return 0;
}


 

Output:-

 Using java:-

Using Scanner class(to take input from the user):- 

import java.util.Scanner;
class Add{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int a,b,c;
System.out.println("Enter First Number");
a=s.nextInt();
System.out.println("Enter Second Number");
b=s.nextInt();
c=a+b;
System.out.println("The sum of a and b is "+c);
}
}
 

Output:-

 

Using predefined values:-

class Add{
public static void main(String args[]){
int a=10,b=20;
int c=a+b;
System.out.println("First Number="+a+"\nSecond Number="+b);
System.out.println("Sum is "+c);
}
}

Output:-

 


 

 

 

No comments:

Post a Comment

Ads Inside Post