C Exercises: Find the maximum number between two numbers
C Pointer : Exercise-6 with Solution
Write a program in C to find the maximum number between two numbers using a pointer.
Visual Presentation:
Sample Solution:
C Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int fno, sno, *ptr1 = &fno, *ptr2 = &sno;
printf("\n\n Pointer : Find the maximum number between two numbers :\n");
printf("------------------------------------------------------------\n");
printf(" Input the first number : ");
scanf("%d", ptr1); // Read the first number from the user and store it using ptr1
printf(" Input the second number : ");
scanf("%d", ptr2); // Read the second number from the user and store it using ptr2
// Compare the values pointed by ptr1 and ptr2 to find the maximum number
if (*ptr1 > *ptr2) {
printf("\n\n %d is the maximum number.\n\n", *ptr1); // Print the maximum number
} else {
printf("\n\n %d is the maximum number.\n\n", *ptr2); // Print the maximum number
}
return 0;
}
Sample Output:
Pointer : Find the maximum number between two numbers : ------------------------------------------------------------ Input the first number : 5 Input the second number : 6 6 is the maximum number.
Flowchart:
C Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a program in C to add numbers using call by reference.
Next: Write a program in C to store n elements in an array and print the elements using pointer.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics