cflowchart

Converting a flowchart to a program in C language (I'm new to programming and need help)


flowchart:

enter image description here

My attempt at solving it:

#include <stdio.h>
int main()
{
  int n;
  int m;
  printf("enter two numbers:");
  scanf("%d%d", &n, &m);
  if (n > 0 || n-m>0) {
    puts("A");
  }
  else {
    puts("C");
  }
  if (n-m < 0 || n <= 13){
    puts ("B");
  }
}

the program runs but i wanted some advice on whether or not i did it right as I'm a beginner... Thank You!


Solution

  • So a few things, one your ||s should be &&s, since you want to make sure both conditionals apply. Second your last if statement should be an else if, otherwise you can get multiple outputs.

    As well your first conditional has the n-m as >, should be <.

    For instance if the input are -5 and -10 you respond with A B, however the correct response should be C.

    #include <stdio.h>
    int main()
    {
      int n;
      int m;
      printf("enter two numbers:");
      scanf("%d%d", &n, &m);
      if (n > 0 && n-m<0) {
        puts("A");
      }
      else if (n-m < 0 && n <= 13){
        puts ("B");
      }
      else if (n <= 0){
        puts("C");
      }
    
    }
    

    However it might be beneficial and readable to nest the conditionals to more closely match the diagram:

    #include <stdio.h>
    int main()
    {
      int n;
      int m;
      printf("enter two numbers:");
      scanf("%d%d", &n, &m);
      if (n > 0) {
        if(n - m < 0){
          puts("A");
        }
        else if (n <= 13){
          puts("B");
        }
      }
      else{
        puts("C");
      }
    
    }