Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why my code is not working plz help !!!!

+5 votes
asked Apr 11, 2021 by ismail andrey (470 points)
// ***** CALCULATOR *****

#include <stdio.h>

int main()
{
    int num1,num2;
    char ch,a,b,c,d;
    printf("Enter the 1st number : ");
    scanf("%d",&num1);
    printf("Enter the 2nd number : ");
    scanf("%d",&num2);

    printf("1. Enter 'a' for addition\n");
    printf("2. Enter 'b' for minus\n");
    printf("3. Enter 'c' for multipoltion\n");
    printf("4. Enter 'd' for divition\n");
    scanf("%c",&ch);

    a = '+';
    b = '-';
    c = '*';
    d = '/';

    if(ch == 'a') {
        printf("Ans is %d\n",num1 + num2);
    }

    else if(ch == 'b') {
        printf("Ans is %d\n",num1 - num2);
    }

    else if(ch == 'e') {
        printf("Ans is %d\n",num1 * num2);
    }

    else if(ch == 'd') {
        printf("Ans is %d\n",num1 / num2);
    }

    else {
        printf("Incorrect input\n");
    }

    return 0;
}

13 Answers

0 votes
answered Jul 1, 2021 by Mysteryplayz (270 points)
Dont put printf just print
0 votes
answered Jul 7, 2021 by CPP Programmer (140 points)
CORRECT CODE:-
#include <stdio.h>
int main(){
    int num1,num2;
    char ch,a,b,c,d;
    
    printf("1. Enter 'a' for addition\n");
    printf("2. Enter 'b' for minus\n");
    printf("3. Enter 'c' for multipoltion\n");
    printf("4. Enter 'd' for divition\n");
    scanf("%c",&ch);
    
    printf("Enter the 1st number : ");
    scanf("%d",&num1);
    printf("Enter the 2nd number : ");
    scanf("%d",&num2);

    a = '+';
    b = '-';
    c = '*';
    d = '/';

    if(ch == 'a') {
        printf("Ans is %d\n",num1 + num2);
    }else if(ch == 'b') {
        printf("Ans is %d\n",num1 - num2);
    }else if(ch == 'c') {
        printf("Ans is %d\n",num1 * num2);
    }else if(ch == 'd') {
        printf("Ans is %d\n",num1 / num2);
    }else {
        printf("Incorrect input\n");
    }
    return 0;
}
0 votes
answered Jul 10, 2021 by Future Computers Jhalrapatan (140 points)

Just Simply change %c to %s when you take value of ch from user and you dont have to writhe this

a = '+';
b = '-';
c = '*';
d = '/';

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...