Program to remove zero and perform operation

Program to remove zero and perform operation

Question

Given two numbers and an operation either + or -, perform the operation. Now remove any zeros if present in two numbers and perform the operation. See if the results are the same or not.

Logic

  • Get the two numbers and operation from the user.
  • Perform the operation and store the result.
  • Remove the zeros in the digit and again perform the operation.
  • See whether the results are the same or not.

Program

[code lang=”c”]
#include<stdio.h>
#include<math.h>
int Operation(char d, int num1, int num2)
{ int result;
switch(d)
{
case ‘+’: result = num1+num2;
break;
case ‘-‘: result = num1- num2;
break;
}
return result;
}

int removezero(int num)
{
int i=0,rem,ans=0; //rem->remainder

while(num) //LOOP EXECUTES FOR EACH DIGIT IN N
{
rem= num%10;
if(rem!=0)
{
ans+= rem*pow(10,i); //adding rem to sum
i++; //increment most significant bit position
}
num=num/10;
}
return ans;
}

int main()
{
int a,b,ans1=0,ans2=0;
char c;
scanf("%d%d",&a,&b);
printf("enter + or -\n");
scanf(" %c",&c);

ans1 = Operation(c,a,b); //before removal of 0’s

a= removezero(a);
b= removezero(b);

ans2 =Operation(c,a,b); //after removal

if(ans1 == ans2)
printf("\nResults in same after removal of 0’s");
else
printf("Not same");

return 0;
}
[/code]

 

Sree Hari Sanjeev

The founder of Wisdom Overflow. Software Developer at Zoho Corporation.

Leave a Reply

Your email address will not be published. Required fields are marked *