Not getting output for division and modulus












1















The below C program gives me output only for Addition(+), difference(-), multiplication(*).
But when I try to use division(/) and modulus(%) the program just close itself without giving any error. Help me I'm a newbie to C programming.



//A simple calculator.

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, sum, diff, rem, multi;
float div;
char character;
clrscr();
printf("Choose the character you want to use(+, -, *, /, %): ");
scanf("%c", &character);
switch(character)
{
case '+': //will be used for addition.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
sum = a+b;
printf("The sum of the %d and %d is %d", a, b, sum);
break;
case '-': //will be used for difference.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
diff = a-b;
printf("The difference between %d and %d is %d", a, b, diff);
break;
case '%': //will be used for modulus.
printf("Enter the first and second number: ");
scanf("%f %f", &a, &b);
rem = a%b;
printf("The remainder of %f and %f is %f", a, b, rem);
break;
case '*': //will be used for product of 2 no.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
multi = a*b;
printf("The multiplication of %d and %d is %d", a, b, multi);
break;
case '/': //will be used for the division.
printf("Enter the first and second number: ");
scanf("%f %f", &a, &b);
div = a/b;
printf("The division of %f and %f is %f", a, b, div);
break;
default:
printf("Error! character please retry");
}
getch();
}









share|improve this question























  • Please add -Wall to your gcc command line and it will start to through a lot of warnings. Fe.x. %f is an incorrect format, it should be %d.

    – Stefan Becker
    Jan 19 at 9:38
















1















The below C program gives me output only for Addition(+), difference(-), multiplication(*).
But when I try to use division(/) and modulus(%) the program just close itself without giving any error. Help me I'm a newbie to C programming.



//A simple calculator.

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, sum, diff, rem, multi;
float div;
char character;
clrscr();
printf("Choose the character you want to use(+, -, *, /, %): ");
scanf("%c", &character);
switch(character)
{
case '+': //will be used for addition.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
sum = a+b;
printf("The sum of the %d and %d is %d", a, b, sum);
break;
case '-': //will be used for difference.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
diff = a-b;
printf("The difference between %d and %d is %d", a, b, diff);
break;
case '%': //will be used for modulus.
printf("Enter the first and second number: ");
scanf("%f %f", &a, &b);
rem = a%b;
printf("The remainder of %f and %f is %f", a, b, rem);
break;
case '*': //will be used for product of 2 no.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
multi = a*b;
printf("The multiplication of %d and %d is %d", a, b, multi);
break;
case '/': //will be used for the division.
printf("Enter the first and second number: ");
scanf("%f %f", &a, &b);
div = a/b;
printf("The division of %f and %f is %f", a, b, div);
break;
default:
printf("Error! character please retry");
}
getch();
}









share|improve this question























  • Please add -Wall to your gcc command line and it will start to through a lot of warnings. Fe.x. %f is an incorrect format, it should be %d.

    – Stefan Becker
    Jan 19 at 9:38














1












1








1








The below C program gives me output only for Addition(+), difference(-), multiplication(*).
But when I try to use division(/) and modulus(%) the program just close itself without giving any error. Help me I'm a newbie to C programming.



//A simple calculator.

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, sum, diff, rem, multi;
float div;
char character;
clrscr();
printf("Choose the character you want to use(+, -, *, /, %): ");
scanf("%c", &character);
switch(character)
{
case '+': //will be used for addition.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
sum = a+b;
printf("The sum of the %d and %d is %d", a, b, sum);
break;
case '-': //will be used for difference.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
diff = a-b;
printf("The difference between %d and %d is %d", a, b, diff);
break;
case '%': //will be used for modulus.
printf("Enter the first and second number: ");
scanf("%f %f", &a, &b);
rem = a%b;
printf("The remainder of %f and %f is %f", a, b, rem);
break;
case '*': //will be used for product of 2 no.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
multi = a*b;
printf("The multiplication of %d and %d is %d", a, b, multi);
break;
case '/': //will be used for the division.
printf("Enter the first and second number: ");
scanf("%f %f", &a, &b);
div = a/b;
printf("The division of %f and %f is %f", a, b, div);
break;
default:
printf("Error! character please retry");
}
getch();
}









share|improve this question














The below C program gives me output only for Addition(+), difference(-), multiplication(*).
But when I try to use division(/) and modulus(%) the program just close itself without giving any error. Help me I'm a newbie to C programming.



//A simple calculator.

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, sum, diff, rem, multi;
float div;
char character;
clrscr();
printf("Choose the character you want to use(+, -, *, /, %): ");
scanf("%c", &character);
switch(character)
{
case '+': //will be used for addition.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
sum = a+b;
printf("The sum of the %d and %d is %d", a, b, sum);
break;
case '-': //will be used for difference.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
diff = a-b;
printf("The difference between %d and %d is %d", a, b, diff);
break;
case '%': //will be used for modulus.
printf("Enter the first and second number: ");
scanf("%f %f", &a, &b);
rem = a%b;
printf("The remainder of %f and %f is %f", a, b, rem);
break;
case '*': //will be used for product of 2 no.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
multi = a*b;
printf("The multiplication of %d and %d is %d", a, b, multi);
break;
case '/': //will be used for the division.
printf("Enter the first and second number: ");
scanf("%f %f", &a, &b);
div = a/b;
printf("The division of %f and %f is %f", a, b, div);
break;
default:
printf("Error! character please retry");
}
getch();
}






c






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 19 at 9:31









GAMING INCGAMING INC

83




83













  • Please add -Wall to your gcc command line and it will start to through a lot of warnings. Fe.x. %f is an incorrect format, it should be %d.

    – Stefan Becker
    Jan 19 at 9:38



















  • Please add -Wall to your gcc command line and it will start to through a lot of warnings. Fe.x. %f is an incorrect format, it should be %d.

    – Stefan Becker
    Jan 19 at 9:38

















Please add -Wall to your gcc command line and it will start to through a lot of warnings. Fe.x. %f is an incorrect format, it should be %d.

– Stefan Becker
Jan 19 at 9:38





Please add -Wall to your gcc command line and it will start to through a lot of warnings. Fe.x. %f is an incorrect format, it should be %d.

– Stefan Becker
Jan 19 at 9:38












3 Answers
3






active

oldest

votes


















0














You are using %f format specifier for int variables in / and % cases.



scanf("%f %f", &a, &b);


Thus invoking undefined behavior.



Change it to as below.



scanf("%d %d", &a, &b);



%f is used to readin the float variables.






If you want float result for division, you need to cast the one of the argument to float instead reading them as float.



  div = (float)a/b;





share|improve this answer

































    0














    When using the / and % use



    scanf("%d %d", &a, &b);


    instead of



    scanf("%f %f", &a, &b);


    because %f is used for float vars whereas in case of / and % %d is used






    share|improve this answer
























    • I'm new to C program and thanks a-lot.

      – GAMING INC
      Jan 19 at 10:00











    • No problem and good luck

      – Talha Israr
      Jan 19 at 10:02



















    0














    You are using wrong formats. After enabling -Wall in gcc and fixing the warnings I get a working program. You also missing the n in your answer printf()'s



    #include <stdio.h>

    int main(int argc, char **argv)
    {
    int a, b, sum, diff, rem, multi;
    float div;
    char character;
    printf("Choose the character you want to use(+, -, *, /, %%): ");
    scanf("%c", &character);
    switch(character)
    {
    case '+': //will be used for addition.
    printf("Enter the first and second number: ");
    scanf("%d %d", &a, &b);
    sum = a+b;
    printf("The sum of the %d and %d is %dn", a, b, sum);
    break;
    case '-': //will be used for difference.
    printf("Enter the first and second number: ");
    scanf("%d %d", &a, &b);
    diff = a-b;
    printf("The difference between %d and %d is %dn", a, b, diff);
    break;
    case '%': //will be used for modulus.
    printf("Enter the first and second number: ");
    scanf("%d %d", &a, &b);
    rem = a%b;
    printf("The remainder of %d and %d is %dn", a, b, rem);
    break;
    case '*': //will be used for product of 2 no.
    printf("Enter the first and second number: ");
    scanf("%d %d", &a, &b);
    multi = a*b;
    printf("The multiplication of %d and %d is %dn", a, b, multi);
    break;
    case '/': //will be used for the division.
    printf("Enter the first and second number: ");
    scanf("%d %d", &a, &b);
    div = a/b;
    printf("The division of %d and %d is %fn", a, b, div);
    break;
    default:
    printf("Error! character please retry");
    }
    }


    Test output:



    $ ./dummy
    Choose the character you want to use(+, -, *, /, %): %
    Enter the first and second number: 5 2
    The remainder of 5 and 2 is 1
    $





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265729%2fnot-getting-output-for-division-and-modulus%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You are using %f format specifier for int variables in / and % cases.



      scanf("%f %f", &a, &b);


      Thus invoking undefined behavior.



      Change it to as below.



      scanf("%d %d", &a, &b);



      %f is used to readin the float variables.






      If you want float result for division, you need to cast the one of the argument to float instead reading them as float.



        div = (float)a/b;





      share|improve this answer






























        0














        You are using %f format specifier for int variables in / and % cases.



        scanf("%f %f", &a, &b);


        Thus invoking undefined behavior.



        Change it to as below.



        scanf("%d %d", &a, &b);



        %f is used to readin the float variables.






        If you want float result for division, you need to cast the one of the argument to float instead reading them as float.



          div = (float)a/b;





        share|improve this answer




























          0












          0








          0







          You are using %f format specifier for int variables in / and % cases.



          scanf("%f %f", &a, &b);


          Thus invoking undefined behavior.



          Change it to as below.



          scanf("%d %d", &a, &b);



          %f is used to readin the float variables.






          If you want float result for division, you need to cast the one of the argument to float instead reading them as float.



            div = (float)a/b;





          share|improve this answer















          You are using %f format specifier for int variables in / and % cases.



          scanf("%f %f", &a, &b);


          Thus invoking undefined behavior.



          Change it to as below.



          scanf("%d %d", &a, &b);



          %f is used to readin the float variables.






          If you want float result for division, you need to cast the one of the argument to float instead reading them as float.



            div = (float)a/b;






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 19 at 9:41

























          answered Jan 19 at 9:36









          kiran Biradarkiran Biradar

          5,2392926




          5,2392926

























              0














              When using the / and % use



              scanf("%d %d", &a, &b);


              instead of



              scanf("%f %f", &a, &b);


              because %f is used for float vars whereas in case of / and % %d is used






              share|improve this answer
























              • I'm new to C program and thanks a-lot.

                – GAMING INC
                Jan 19 at 10:00











              • No problem and good luck

                – Talha Israr
                Jan 19 at 10:02
















              0














              When using the / and % use



              scanf("%d %d", &a, &b);


              instead of



              scanf("%f %f", &a, &b);


              because %f is used for float vars whereas in case of / and % %d is used






              share|improve this answer
























              • I'm new to C program and thanks a-lot.

                – GAMING INC
                Jan 19 at 10:00











              • No problem and good luck

                – Talha Israr
                Jan 19 at 10:02














              0












              0








              0







              When using the / and % use



              scanf("%d %d", &a, &b);


              instead of



              scanf("%f %f", &a, &b);


              because %f is used for float vars whereas in case of / and % %d is used






              share|improve this answer













              When using the / and % use



              scanf("%d %d", &a, &b);


              instead of



              scanf("%f %f", &a, &b);


              because %f is used for float vars whereas in case of / and % %d is used







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 19 at 9:42









              Talha IsrarTalha Israr

              465212




              465212













              • I'm new to C program and thanks a-lot.

                – GAMING INC
                Jan 19 at 10:00











              • No problem and good luck

                – Talha Israr
                Jan 19 at 10:02



















              • I'm new to C program and thanks a-lot.

                – GAMING INC
                Jan 19 at 10:00











              • No problem and good luck

                – Talha Israr
                Jan 19 at 10:02

















              I'm new to C program and thanks a-lot.

              – GAMING INC
              Jan 19 at 10:00





              I'm new to C program and thanks a-lot.

              – GAMING INC
              Jan 19 at 10:00













              No problem and good luck

              – Talha Israr
              Jan 19 at 10:02





              No problem and good luck

              – Talha Israr
              Jan 19 at 10:02











              0














              You are using wrong formats. After enabling -Wall in gcc and fixing the warnings I get a working program. You also missing the n in your answer printf()'s



              #include <stdio.h>

              int main(int argc, char **argv)
              {
              int a, b, sum, diff, rem, multi;
              float div;
              char character;
              printf("Choose the character you want to use(+, -, *, /, %%): ");
              scanf("%c", &character);
              switch(character)
              {
              case '+': //will be used for addition.
              printf("Enter the first and second number: ");
              scanf("%d %d", &a, &b);
              sum = a+b;
              printf("The sum of the %d and %d is %dn", a, b, sum);
              break;
              case '-': //will be used for difference.
              printf("Enter the first and second number: ");
              scanf("%d %d", &a, &b);
              diff = a-b;
              printf("The difference between %d and %d is %dn", a, b, diff);
              break;
              case '%': //will be used for modulus.
              printf("Enter the first and second number: ");
              scanf("%d %d", &a, &b);
              rem = a%b;
              printf("The remainder of %d and %d is %dn", a, b, rem);
              break;
              case '*': //will be used for product of 2 no.
              printf("Enter the first and second number: ");
              scanf("%d %d", &a, &b);
              multi = a*b;
              printf("The multiplication of %d and %d is %dn", a, b, multi);
              break;
              case '/': //will be used for the division.
              printf("Enter the first and second number: ");
              scanf("%d %d", &a, &b);
              div = a/b;
              printf("The division of %d and %d is %fn", a, b, div);
              break;
              default:
              printf("Error! character please retry");
              }
              }


              Test output:



              $ ./dummy
              Choose the character you want to use(+, -, *, /, %): %
              Enter the first and second number: 5 2
              The remainder of 5 and 2 is 1
              $





              share|improve this answer




























                0














                You are using wrong formats. After enabling -Wall in gcc and fixing the warnings I get a working program. You also missing the n in your answer printf()'s



                #include <stdio.h>

                int main(int argc, char **argv)
                {
                int a, b, sum, diff, rem, multi;
                float div;
                char character;
                printf("Choose the character you want to use(+, -, *, /, %%): ");
                scanf("%c", &character);
                switch(character)
                {
                case '+': //will be used for addition.
                printf("Enter the first and second number: ");
                scanf("%d %d", &a, &b);
                sum = a+b;
                printf("The sum of the %d and %d is %dn", a, b, sum);
                break;
                case '-': //will be used for difference.
                printf("Enter the first and second number: ");
                scanf("%d %d", &a, &b);
                diff = a-b;
                printf("The difference between %d and %d is %dn", a, b, diff);
                break;
                case '%': //will be used for modulus.
                printf("Enter the first and second number: ");
                scanf("%d %d", &a, &b);
                rem = a%b;
                printf("The remainder of %d and %d is %dn", a, b, rem);
                break;
                case '*': //will be used for product of 2 no.
                printf("Enter the first and second number: ");
                scanf("%d %d", &a, &b);
                multi = a*b;
                printf("The multiplication of %d and %d is %dn", a, b, multi);
                break;
                case '/': //will be used for the division.
                printf("Enter the first and second number: ");
                scanf("%d %d", &a, &b);
                div = a/b;
                printf("The division of %d and %d is %fn", a, b, div);
                break;
                default:
                printf("Error! character please retry");
                }
                }


                Test output:



                $ ./dummy
                Choose the character you want to use(+, -, *, /, %): %
                Enter the first and second number: 5 2
                The remainder of 5 and 2 is 1
                $





                share|improve this answer


























                  0












                  0








                  0







                  You are using wrong formats. After enabling -Wall in gcc and fixing the warnings I get a working program. You also missing the n in your answer printf()'s



                  #include <stdio.h>

                  int main(int argc, char **argv)
                  {
                  int a, b, sum, diff, rem, multi;
                  float div;
                  char character;
                  printf("Choose the character you want to use(+, -, *, /, %%): ");
                  scanf("%c", &character);
                  switch(character)
                  {
                  case '+': //will be used for addition.
                  printf("Enter the first and second number: ");
                  scanf("%d %d", &a, &b);
                  sum = a+b;
                  printf("The sum of the %d and %d is %dn", a, b, sum);
                  break;
                  case '-': //will be used for difference.
                  printf("Enter the first and second number: ");
                  scanf("%d %d", &a, &b);
                  diff = a-b;
                  printf("The difference between %d and %d is %dn", a, b, diff);
                  break;
                  case '%': //will be used for modulus.
                  printf("Enter the first and second number: ");
                  scanf("%d %d", &a, &b);
                  rem = a%b;
                  printf("The remainder of %d and %d is %dn", a, b, rem);
                  break;
                  case '*': //will be used for product of 2 no.
                  printf("Enter the first and second number: ");
                  scanf("%d %d", &a, &b);
                  multi = a*b;
                  printf("The multiplication of %d and %d is %dn", a, b, multi);
                  break;
                  case '/': //will be used for the division.
                  printf("Enter the first and second number: ");
                  scanf("%d %d", &a, &b);
                  div = a/b;
                  printf("The division of %d and %d is %fn", a, b, div);
                  break;
                  default:
                  printf("Error! character please retry");
                  }
                  }


                  Test output:



                  $ ./dummy
                  Choose the character you want to use(+, -, *, /, %): %
                  Enter the first and second number: 5 2
                  The remainder of 5 and 2 is 1
                  $





                  share|improve this answer













                  You are using wrong formats. After enabling -Wall in gcc and fixing the warnings I get a working program. You also missing the n in your answer printf()'s



                  #include <stdio.h>

                  int main(int argc, char **argv)
                  {
                  int a, b, sum, diff, rem, multi;
                  float div;
                  char character;
                  printf("Choose the character you want to use(+, -, *, /, %%): ");
                  scanf("%c", &character);
                  switch(character)
                  {
                  case '+': //will be used for addition.
                  printf("Enter the first and second number: ");
                  scanf("%d %d", &a, &b);
                  sum = a+b;
                  printf("The sum of the %d and %d is %dn", a, b, sum);
                  break;
                  case '-': //will be used for difference.
                  printf("Enter the first and second number: ");
                  scanf("%d %d", &a, &b);
                  diff = a-b;
                  printf("The difference between %d and %d is %dn", a, b, diff);
                  break;
                  case '%': //will be used for modulus.
                  printf("Enter the first and second number: ");
                  scanf("%d %d", &a, &b);
                  rem = a%b;
                  printf("The remainder of %d and %d is %dn", a, b, rem);
                  break;
                  case '*': //will be used for product of 2 no.
                  printf("Enter the first and second number: ");
                  scanf("%d %d", &a, &b);
                  multi = a*b;
                  printf("The multiplication of %d and %d is %dn", a, b, multi);
                  break;
                  case '/': //will be used for the division.
                  printf("Enter the first and second number: ");
                  scanf("%d %d", &a, &b);
                  div = a/b;
                  printf("The division of %d and %d is %fn", a, b, div);
                  break;
                  default:
                  printf("Error! character please retry");
                  }
                  }


                  Test output:



                  $ ./dummy
                  Choose the character you want to use(+, -, *, /, %): %
                  Enter the first and second number: 5 2
                  The remainder of 5 and 2 is 1
                  $






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 19 at 9:44









                  Stefan BeckerStefan Becker

                  1,453116




                  1,453116






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265729%2fnot-getting-output-for-division-and-modulus%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Homophylophilia

                      Updating UILabel text programmatically using a function

                      Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage