Tuesday, 29 March 2016

C and C++ program to add two numbers Problem Solution.

Write a program in C and C++ to enter two numbers from user and calculate their sum. C and C++ program to add two numbers and display their sum as output.

Example: 
A = 30
B = 20 
A + B = 50 


Solution In C:


#include<stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){
    int A, B, C;

    printf("Enter first number: \n");
    scanf("%d", &A);  // Read first number
    printf("Enter second number: \n");
    scanf("%d", &B);  // Read second number

    //Adding both number is simple and fundamental
    C = A + B;

    // Prints the sum of two numbers
    printf("Sum of %d and %d = %d\n",A, B, C);

    return 0;
}

Output:



Solution In C++:

#include<iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){
    int A, B, C;

    cout << "Enter first number:" << endl;;
    cin >> A;  // Read first number
    cout << "Enter second number:" << endl;;
    cin >> B;  // Read second number

    //Adding both number
    C = A + B;

    // Prints the sum of two numbers
    cout << "Sum of " << A << " and " << B <<" = "<< C << endl;

    return 0;
}

Output:


No comments:

Post a Comment