Google News
logo
C Program to Find the Largest of three numbers using Pointers
In the following example of a C program that finds the largest of three numbers using pointers :
Program :
#include <stdio.h>

void largest(int *a, int *b, int *c);

int main() {
    int num1, num2, num3;

    printf("Enter three numbers:\n");
    scanf("%d %d %d", &num1, &num2, &num3);

    largest(&num1, &num2, &num3);

    printf("\nLargest number is %d\n", num1);

    return 0;
}

void largest(int *a, int *b, int *c) {
    if (*b > *a) {
        *a = *b;
    }
    if (*c > *a) {
        *a = *c;
    }
}
Output :
Enter three numbers:
5
7
3
Largest number is 7
* In this example, the program defines a function largest that takes three integer pointers as arguments, compares the values they point to, and modifies the value pointed to by the first argument to store the largest value.

* The program then creates three integer variables called num1, num2, and num3, initializes them with input values from the user using the scanf function, calls the largest function to find the largest of the three numbers by passing their addresses using the address-of operator &, and prints the largest number using the printf function.