What's new

Help Mga lods pa help c++ code

Phc - Jemz

Forum Guru
Elite
write a c++ function called calculateSum that makes two integers as parameters and returns their sum. write a program that user input for two integers, calls the calculate sum function; and prints result.
Output
Enter the first integer: 5
Enter the second integer: 6
Sum: 11


pa help po, puro error po kasi code ko
 
Solution
1699923233833.png
#include <iostream>

// Function Declaration
int calculateSum(int num1, int num2);

int main() {
int number1, number2, sum;

std::cout << "Enter the first integer: ";
std::cin >> number1; // First Input from user

std::cout << "Enter the second integer: ";
std::cin >> number2; // Second Input from user

// Function Call
sum = calculateSum(number1, number2);

std::cout << "Sum: " << sum << std::endl; // Prints the addition of two numbers

return 0;
}

// Function Definition
int calculateSum(int num1, int num2) {
return num1 + num2;
}
 
Sure, here's the corrected code for your program:

C++:
#include <iostream>

// Function to calculate the sum of two integers
int calculateSum(int num1, int num2) {
    int sum = num1 + num2;
    return sum;
}

int main() {
    int num1, num2;

    // User input for two integers
    std::cout << "Enter the first integer: ";
    std::cin >> num1;

    std::cout << "Enter the second integer: ";
    std::cin >> num2;

    // Calling the calculateSum function
    int result = calculateSum(num1, num2);

    // Printing the result
    std::cout << "Sum: " << result << std::endl;

    return 0;
}

In this code, the calculateSum function takes two integers as parameters num1 and num2 and returns their sum. The main function prompts the user to enter two integers, calls the calculateSum function with the user input, and stores the result in the result variable. Finally, it prints the result as "Sum: [result]".

I hope this helps! Let me know if you have any further questions.
 

Similar threads

Back
Top