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

#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;
}

 

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

Sree Hari Sanjeev

The founder of Wisdom Overflow. Software Developer at Zoho Corporation.
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x