This code is runnig perfectly for values perfectly divisible by 2, that is 2,4,8 But the i is not incrementing when values are given such as 7,5

could u plz tell me the error
Thanks

#include<stdio.h>
#include<conio.h>

int fun(int);
int main()
{
    int num;
    float mod,prime,m;
    
    printf("enter the num");
    scanf("%d",&num);
    
    fun(num);

    getch();
    
    }
    
int fun(int n)
{
    int i=2,temp;
    float div;
    
       div=n/i;
       if(n==1)
       {
          return(1);
       }
                  
       else if(n%i==0)
       {
           printf("\n The prime factor of number is %d",i);
           return(fun(n=div));
       }
           else 
            i++;
}

So if you run this on value, say 3, what would the program trace look like?
You'd enter fun at line 19 with value 3,
fail the test at line 25 (n is 3),
fail the test at line 30 (3%2 != 0),
enter the 'else' block
increment i to 3 and ... hmm...

A meta comment: Your various amounts of indentation and lack of curly braces around the final block indicate that you are not being obsessively neat about things. Obsessive neatness is a positive trait in programmers: It makes syntax and logic errors more easily visible by making the regularity of the program more obvious (or by making the lack of regularity more obviously wrong). I recommend that you get these three habits:

  1. Use only single spaces (never tabs) for indentation
  2. Pick a brace style and use it everywhere. Yours appears to be 'open brace on next line, braces not indented' which is a good one, but you aren't using it consistently (hint: type both braces before you write the code that goes between them.)
  3. Never, ever, have a logic block without braces. Not even for a one-line block.

Thanks a lot dude for the reply......

and Thanks Thanks a ton for those suggestions, I will keep them in mind

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.