Solution to Practice Exercise

Practice Exercise: Sum of Two Numbers

This exercise demonstrates how data and methods work together in C to calculate the sum of two numbers.



#include <stdio.h>

// Function to calculate the sum of two numbers

int calculateSum(int num1, int num2) {

  // Add the two numbers and store the result

  int sum = num1 + num2;

  // Return the calculated sum

  return sum;


int main() {

  // Declare variables to store two numbers

  int number1, number2;

  // Prompt the user to enter two numbers

  printf("Enter the first number: ");

  scanf("%d", &number1);

  printf("Enter the second number: ");

  scanf("%d", &number2);

  // Call the calculateSum function and store the result

  int totalSum = calculateSum(number1, number2);

  // Print the sum of the two numbers

  printf("The sum of %d and %d is %d\n", number1, number2, totalSum);

  return 0;


Use code with caution.


Compiling and Running:

This exercise showcases how data (user-entered numbers) is passed to a method (calculateSum), which performs the calculation (adding numbers) and returns the result (sum). The main function utilizes this result to display the final output.