Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it

14 Jan 2024

C program example that uses functions - This program calculates the sum of two numbers using separate functions for input, addition, and output.


#include <stdio.h>

// Function prototypes
    int getInput();
    int addNumbers(int num1, int num2);
    void displayResult(int result);

int main() {
    // Get input from the user
    int number1 = getInput();
    int number2 = getInput();

    // Perform addition
    int sum = addNumbers(number1, number2);

    // Display the result
    displayResult(sum);
    return 0;
}


// Function to get input from the user
    int getInput() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    return num;
}

// Function to add two numbers
    int addNumbers(int num1, int num2) {
    return num1 + num2;
}

// Function to display the result
    void displayResult(int result) {
    printf("The sum is: %d\n", result);
}


No comments:

Post a Comment