mercredi 17 novembre 2021

Output display unwanted output

Hye guys..i just learnt C programming.Im facing problem with my nested if..else statement.Im trying make a program to calculate the output for 2 integers based on user input for the 2 integers and arithmetic equation.User will choose the 2 integer and arithmetic equation.I actually managed to get the output.But the output came out two times.The first one is the correct output.But i dont know the second one came out from where.Here is my coding.Thankyou for ur help.

#include <stdio.h>
#include <math.h>

int main()
{
    int n1,n2,ans;
    char c;
    printf("Arithmetic operation(+, -, x, /) :");
    scanf("%c", &c);
    printf("Insert first number:");
    scanf("%d", &n1);
    printf("Insert second number:");
    scanf("%d", &n2);

    if (c==1)
    {
        if (c=='+')
        {
            ans=n1+n2;
        printf("%d %c %d = %d ",n1,c,n2,ans);
        }
           else if (c=='-')
            {
            ans=n1-n2;
            printf("%d %c %d = %d ",n1,c,n2,ans);
            }
                     else if (c=='x')
                        {
                        ans=n1*n2;
                        printf("%d %c %d = %d ",n1,c,n2,ans);
                        }
                           else (c=='/');

                            {
                            ans=n1/n2;
                            printf(" %d %c %d = %d ",n1,c,n2,ans);
                            }
    }
    else
    {
        printf("Error! Unknown arithmetic operation.");
    }
    return 0;
}

And this is my output example:

Arithmetic operation(+, -, x, /) :+

Insert first number:4

Insert second number:2

4 + 2 = 6 4 + 2 = 2

Aucun commentaire:

Enregistrer un commentaire