BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to Find the Largest of three numbers using Pointers

By Chaitanya Singh | Filed Under: C Programs

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:
C Program to Find the Largest of three numbers using Pointers

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

Comments

  1. Ert1 Collaku says

    March 27, 2020 at 9:03 AM

    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.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap