C Program to Find the Largest of Two Numbers using a Pointer

Write a C program to find the largest of two numbers using a pointer. It allows the user to enter two integer values, and then we assign them to two pointer variables of int type. In this example, we used the else if statement to find the largest of two pointer numbers.

#include <stdio.h>     

int main() {  

    int x, y, *p1, *p2;  

    printf("Please Enter Two different values\n");  
    scanf("%d %d", &x, &y);  
    
    p1 = &x;
    p2 = &y;

    if(*p1 > *p2) 
	{
         printf("The Larges = %d\n", *p1);  
    } 
	else if (*p2 > *p1)
	{ 
        printf("The Largest = %d\n", *p2); 
    } 
	else 
	{
		printf("Both are Equal\n");
    }
   
    return 0;  
} 
Please Enter Two different values
99
15
The Largest = 99


Please Enter Two different values
12
19
The Largest = 19


Please Enter Two different values
15
15
Both are Equal

This c program uses the conditional operator and pointer to find the largest number among the two numbers.

#include <stdio.h>  
   
int main() 
{  
    int x, y, *p1, *p2, *largest;

    p1 = &x;
    p2 = &y;

    printf("Please Enter Two Different Values\n");  
    scanf("%d %d", p1, p2);  

    largest = (*p1 > *p2) ? p1 : p2;
    printf("The Largest of Two Number = %d\n", *largest);
    
    return 0;  
} 
C Program to Find the Largest of Two Numbers using a Pointer 2

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.