Write a C program to find the largest of three numbers using a pointer. First, we assign the user entered three integers to pointers. Next, this c example uses the nested if else statements to check and find the largest pointer number among the three.
#include <stdio.h> int main() { int a, b, c, *pa, *pb, *pc; printf("Please Enter three Number to find Largest\n"); scanf("%d %d %d", &a, &b, &c); pa = &a; pb = &b; pc = &c; if (*pa > *pb) { if(*pa > *pc) { printf("\nThe Largest Among Three = %d\n", *pa); } else { printf("\nThe Largest Among Three = %d\n", *pc); } } else { if(*pb > *pc) { printf("\nThe Largest Among Three = %d\n", *pb); } else { printf("\nThe Largest Among Three = %d\n", *pc); } } }
This c program uses the else if statements and pointer to find the largest of three numbers.
#include <stdio.h> int main() { int a, b, c, *pa, *pb, *pc; printf("Please Enter three Numbers\n"); scanf("%d %d %d", &a, &b, &c); pa = &a; pb = &b; pc = &c; if (*pa > *pb && *pa > *pc) { printf("\nHighest = %d\n", *pa); } else if (*pb > *pa && *pb > *pc) { printf("\nHighest = %d\n", *pb); } else if (*pc > *pa && *pc > *pb) { printf("\nHighest = %d\n", *pc); } }
Please Enter three Numbers
19
90
120
Highest = 120
In this c example, we used the nested conditional operator and pointer to find the largest among three numbers.
#include<stdio.h> int main() { int a, b, c, *pa, *pb, *pc, *largest; printf("Please Enter three Numberst\n"); scanf("%d %d %d", &a, &b, &c); pa = &a; pb = &b; pc = &c; largest =((*pa > *pb && *pa > *pc)? pa: (*pb > *pc) ? pb : pc); printf("\nHighest = %d\n", *largest); return 0; }
Please Enter three Numbers
22
44
19
Highest = 44