C Program to Find the Largest Among Three Numbers Using Pointers


Description- Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time. The pointer variable might be belonging to any of the data type such as int, float, char, double, short etc.
We can find out greatest number by simply comparing the values the points are pointing.

  1. // To find the largest of three numbers using pointers
  2. #include <stdio.h> // header file
  3. int main()
  4. {
  5. int no1, no2, no3; // variable declaration
  6. int *pt1, *pt2, *pt3;
  7. // here we are taking input from the user
  8. printf("Enter First Number: ");
  9. scanf("%d",&no1);
  10. printf("Enter Second Number: ");
  11. scanf("%d",&no2);
  12. printf("Enter Third Number: ");
  13. scanf("%d",&no3);
  14. //assignment of the address of input numbers to pointers
  15. pt1 = &no1;
  16. pt2 = &no2;
  17. pt3 = &no3;
  18. if(*pt1 > *pt2)
  19. {
  20. if(*pt1 > *pt3)
  21. {
  22. printf("%d is the largest number", *pt1);
  23. }
  24. else
  25. {
  26. printf("%d is the largest number", *pt3);
  27. }
  28. }
  29. else
  30. {
  31. if(*pt2 > *pt3)
  32. {
  33. printf("%d is the largest number", *pt2);
  34. }
  35. else
  36. {
  37. printf("%d is the largest number", *pt3);
  38. }
  39. }
  40. return 0;
  41. }

Output

Output!