Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Friday, September 30, 2022

C program to calculate the sum and average of input two numbers using function

 C program to calculate the sum and average of input two numbers using function



What is a function?


A function is a block of code that performs a specific task. Functions can be used to organize code and make it more reusable.

How to write a function in C?


To write a function in C, you need to use the following syntax:

return_type function_name (parameter1, parameter2, ...) { // Function body }

Example C program to calculate the sum and average of input two numbers using function


#include <stdio.h>

int sume(int, int); 

float average(int, int); 

int main()

{

    int num1, num2;

    int sum;

    float avg;


    printf("Enter the first number: ");

    scanf("%d", &num1);

    printf("Enter the second number: ");

    scanf("%d", &num2);

    sum = sume(num1, num2);

    avg = average(num1, num2);

    printf("First number: %d, Second number: %d\n", num1, num2);

    printf("Sum: %d, Average: %f\n", sum, avg);

    return 0;

}


int sume(int x, int y)

{

    int sum;

    sum = x + y;

    return sum;

}


float average(int x, int y)

{

      float average;

    return ((float)(x) + (float)(y)) / 2;

}


Output:



Conclusion:


We learned how to write a function in C and use it to calculate the sum and average of two numbers in this article. Functions are a powerful tool for organizing code and making it more reusable.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages