C - Program to compare two integer numbers using pointers.


IncludeHelp 20 September 2016

In this code snippet/program/example we will learn how to compare two integer values using pointers in c programming language?

This program will read two integer values in integer pointer variables and compares them, if they are equal program will print “Integers are equal” and if they are not equal program will print “Integers are not equal”.

We will implement this program by using two methods. First simple integer pointer declarations and comparison in main () and second by using user define function – User define function to compare two integers will take two integer pointers and compares them inside the function. Function will return 0 if they are equal otherwise it will return -1.

C Code Snippet/ Program - Compare Two Integer Numbers using Integer Pointers in C programming language

/*C - Program to compare two integer numbers 
using pointers.*/

#include <stdio.h>
int main(){
	int a,b;
	int *pa,*pb;
	
	pa=&a; pb=&b;
	
	printf("Enter first integer: ");
	scanf("%d",pa);
	printf("Enter second integer: ");
	scanf("%d",pb);
	
	//compare 
	if(*pa==*pb)
		printf("Integers are equal.\n");
	else
		printf("Integers are not equal.\n");
	
	return 0;
}

Using user define function

#include <stdio.h>

char compareIntegers(int a,int b){
	if(a==b)
		return 0;
	else
		return -1;
}
int main(){
	int a,b;
	int *pa,*pb;
	
	pa=&a; pb=&b;
	
	printf("Enter first integer: ");
	scanf("%d",pa);
	printf("Enter second integer: ");
	scanf("%d",pb);
	
	//compare 
	if(compareIntegers(*pa,*pb)==0)
		printf("Integers are equal.\n");
	else
		printf("Integers are not equal.\n");
	
	return 0;
}

    Enter first integer: 100
    Enter second integer: 100 
    Integers are equal.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.