C Program to Check Integers Are Equal Or Not Using Macro
C Program :
/* Aim: Define a macro EQUALINT which compares two parameters x and y and gives 1 if equal and 0 otherwise. Use thhis macro to accept pairs of integers from the user. Calculate the sum of digits and continue till the user enters a pair whose sum of digits is not equal. */ #include<stdio.h> #include<stdlib.h> #define EQUALINT(x,y) x==y?1:0 // Sum_of_Digits Function int Sum_of_Digits(int x) { int sum=0; while(x>0) { sum+=x%10; x/=10; } return sum; } #define ISSUMEQUAL(x,y) (Sum_of_Digits(x)==Sum_of_Digits(y))?1:0 void main(int argc,char *argv[]) { int num1,num2; do{ printf("\n Enter two numbers:- "); scanf(" %d%d",&num1,&num2); if(ISSUMEQUAL(num1,num2)) printf("\n Sum of digits of %d and %d is equal ie %d=%d \n \n",num1,num2,Sum_of_Digits(num1),Sum_of_Digits(num2)); else printf("\n Sum of digits of %d and %d is not equal \n \n",num1,num2); }while(ISSUMEQUAL(num1,num2)!=0); }
/* Output of above code:-
[root@ugilinux ~]# cc e17a3.c
[root@ugilinux ~]# ./a.out
Enter two numbers:- 100 001
Sum of digits of 100 and 1 is equal ie 1=1
Enter two numbers:- 99 98
Sum of digits of 99 and 98 is not equal
*/
Comments