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;
}
No comments:
Post a Comment