In this post, we are going to learn How to Divide two numbers using pointers in C with examples.We will take two numbers input from the user and store them in the pointer variable
C program to Divide two number using pointers
In this c program, we are asking users to input two numbers and store them in pointers and use them to find the division of two numbers.
#include <stdio.h>
int main()
{
// Declare 2 numbers
int num1, num2;
float DivResult;
// Declare 2 integer pointers
int *ptr1, *ptr2;
// Assign numbers address to pointers
ptr1 = &num1;
ptr2 = &num2;
printf("Please enter two non-zero numbers for Multiplication: ");
scanf("%d%d", ptr1, ptr2);
if(*ptr1 == 0 || *ptr2 == 0)
{
printf("Please enter non-zero Numbers for Division. Exiting code");
exit(0);
}
DivResult = *ptr1 + *ptr2;
printf("The Multiplication Results are = %f", DivResult);
return 0;
}
Output
Please enter two non-zero numbers for Division: 0
12
Please enter non-zero Numbers for Division. Exiting code
Please enter two non-zero numbers for Division: 15
3
The Division Results are = 5.000000