C Programs - Call by Reference
1. Program to Calculate the Sum of Two Numbers
#include <stdio.h>
// Function to calculate the sum of two numbers
void CALCULATE_SUM(int *a, int *b, int *sum) {
*sum = *a + *b;
int main() {
int num1, num2, sum;
printf("Enter two numbers for calculating the sum: ");
scanf("%d %d", &num1, &num2);
CALCULATE_SUM(&num1, &num2, &sum);
printf("The sum of %d and %d is: %d\n", num1, num2, sum);
return 0;
2. Program to Find the Average of Five Numbers
#include <stdio.h>
// Function to find the average of five numbers
void FIND_AVG(int *numbers, int *avg) {
int total = 0;
for (int i = 0; i < 5; i++) {
total += numbers[i];
*avg = total / 5;
int main() {
int numbers[5], avg;
printf("Enter five numbers for finding the average: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &numbers[i]);
FIND_AVG(numbers, &avg);
printf("The average of the numbers is: %d\n", avg);
return 0;
3. Program to Find the Sum of Natural Numbers
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function to calculate the sum of all natural numbers up to a given number
void GET_SUM(int *n, int *sum) {
*sum = (*n * (*n + 1)) / 2;
}
int main() {
srand(time(0));
int random_num = (rand() % 100) + 1;
int sum;
GET_SUM(&random_num, &sum);
printf("The random number is: %d\n", random_num);
printf("The sum of all natural numbers up to %d is: %d\n", random_num, sum);
return 0;