0
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int choice,num,i,fact;
    while(1)
    {
        printf("\n 1-factorial\n");
        printf("\n2-prime\n");
        printf("\n3.odd/even\n");
        printf("\n4-exit\n");
        printf("your choice?\n");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
            printf("enter a number");
            scanf("%d",&num);
            fact=1;
            for(i=1;i<=num;i++)
            {
                fact=fact*i;//factorial of a number//
            }
                printf("factorial value  is %d\n",fact);
            break;//ends the loop here//
            
            case 2:
            printf("enter a number");
            scanf("%d",&num);
            for(i=2;i<num;i++)
            {
                if(num%i==0)
                {
                    printf("the number is not a prime number\n");
                    break;
                }
                if(i==num)
                {
                    printf("the number is prime\n");
                    break;
                }
            }
            case 3:
            printf("enter a number");
            scanf("%d",&num);
            if(num%2==0)
            {
                printf("the number is even\n");
            }
            else
            {
                printf("the number is odd\n");
                break;
            }
            case 4:
            exit(0);//terminates program execution included in the <stdlib.h>//
            default:
            printf("the choice is invalid\a\n");//\a means alarm//


        }

    }
    return 0;
}

i m not sure about the while loop like i have tried for(;;) too but i dont think thats the issue when i input 1 the factorial part works fine but when i press 2 or 3 it says - enter a number and when i enter one it again prompts me to enter a number and then breaks what maybe the issue here

i tried changing some logics but that didn't help i,googled it and it said something about buffer of \n but i couldn't understand it

  • see https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer – pm100 Jun 11 '23 at 17:32
  • Does this answer your question? [scanf() leaves the newline character in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer) – pm100 Jun 11 '23 at 17:33
  • @pm100 `"%d"` consumes leading whitespace, and is the only specifier used in this program (where `scanf` is the only input function used). There is the issue of conversion failure leaving data in the input buffer and producing an infinite loop. – Oka Jun 11 '23 at 18:26

1 Answers1

1

I didn't quite understand your question, but I see a syntax error in your code. All switch cases must be followed by a break statement. Here as you can see there's only 1 break statement, that's after the first case. Try putting break; after the block of codes of case 2,3,4 and default and see if that resolves your problem

Try something like this:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int choice,num,i,fact;
    while(1){
        printf("1-factorial\n");
        printf("\n2-prime\n");
        printf("\n3.odd/even\n");
        printf("\n4-exit\n");
        printf("your choice?\n");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
            printf("enter a number");
            scanf("%d",&num);
            fact=1;
            for(i=1;i<=num;i++)
            {
                fact=fact*i;//factorial of a number//
            }
                printf("factorial value  is %d\n",fact);
            break;//ends the loop here//
            
            case 2:
            printf("enter a number");
            scanf("%d",&num);
            for(i=2;i<num;i++)
            {
                if(num%i==0)
                {
                    printf("the number is not a prime number\n");
                    break;
                }
                if(i==num)
                {
                    printf("the number is prime\n");
                    break;
                }
            }
            break;

            case 3:
            printf("enter a number");
            scanf("%d",&num);
            if(num%2==0)
            {
                printf("the number is even\n");
            }
            else
            {
                printf("the number is odd\n");
                break;
            }
            break;

            case 4:
            exit(0);//terminates program execution included in the <stdlib.h>//
            break;
            default:
            printf("the choice is invalid\a\n");//\a means alarm//
            break;
        }
    }
    return 0;
}

Also note the factorial case won't work after a certain set of integers since the factorial value exceeds the size of int

Aevo
  • 70
  • 6
  • thanksit did help but there is still one slight error after i enter a number and it shows it is a prime number after that another prompt comes to enter a number and when i enter one it shows whether it is odd or even even though i didnt ask it to show me – akshat.1010 Jun 11 '23 at 18:06
  • If you want your program to only perform 1 task and exit, remove the while(1) and it's respective curly braces, and don't forget to put the break; after each case – Aevo Jun 11 '23 at 18:12
  • 3
    Re: *"All switch cases **must** be followed by a break statement."* - No. A missing [`break`](https://en.cppreference.com/w/c/language/break) is not a [syntax error](https://en.wikipedia.org/wiki/Syntax_error), but it may be a [logic error](https://en.wikipedia.org/wiki/Logic_error). Fall-through is well defined (and occasionally useful). – Oka Jun 11 '23 at 18:17