In this tutorial, we will write a C program to find the largest of three input numbers using pointers.
Program to find largest number using pointers
In the following program we have three integers num1
, num2
& num3
. We have assigned the addresses of these three numbers to three pointers p1
, p2
& p3
respectively. Later we performed the comparison on the values stored at the addresses pointed by pointers using if else statement.
#include <stdio.h> int main() { int num1, num2, num3; int *p1, *p2, *p3; //taking input from user printf("Enter First Number: "); scanf("%d",&num1); printf("Enter Second Number: "); scanf("%d",&num2); printf("Enter Third Number: "); scanf("%d",&num3); //assigning the address of input numbers to pointers p1 = &num1; p2 = &num2; p3 = &num3; if(*p1 > *p2) { if(*p1 > *p3) { printf("%d is the largest number", *p1); } else { printf("%d is the largest number", *p3); } } else { if(*p2 > *p3) { printf("%d is the largest number", *p2); } else { printf("%d is the largest number", *p3); } } return 0; }
Output:
Related C Examples
1. C program to count vowels and consonants in a String using Pointer
2. C program to print a String using Pointer
3. C program to swap two numbers using Pointers
4. C program to create initialize and access a pointer variable
5. C program to find the largest of three numbers without using pointers
Ert1 Collaku says
How can i do the opposite? I am trying to change the arrows from > to < but nothing I am new and i was trying to learn from this ex.