Skip to main content
improved formatting
Source Link
MARS
  • 5.2k
  • 3
  • 14
  • 23

here is the code and error is"control may reach end of non-void function [-Werror,-Wreturn-type]"

#include #include int collatz( int n, int c); int main (void) { printf("please enter a positive int\n"); int n=GetInt(); int count=0; collatz (n,count); } int collatz (int n,int c) { while (n>0) { //base case is when n==1 if (n==1) { c++; //print number of repition to make it 1 printf("%i\n", c); return 0; } // if even else if ((n%2)==0) { return 1 + collatz(n/2,c++); } //if odd else return 1 + collatz(3*n+1,c++); } }>
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
int collatz( int n, int c);
int main (void)
{
    printf("please enter a positive int\n");
    int n=GetInt();
    int count=0;
    collatz (n,count);
}
    int collatz (int n,int c)
{   
   
    
    while (n>0)
    {   
        //base case is when n==1
        
        if (n==1)
        {
            c++;
            //print number of repition to make it 1
            printf("%i\n", c);
            return 0;
        }
        // if even
       else if ((n%2)==0)
        {
            return 1 + collatz(n/2,c++);
        }
        //if odd
        else
        return 1 + collatz(3*n+1,c++);
    }

}

here is the code and error is"control may reach end of non-void function [-Werror,-Wreturn-type]"

#include #include int collatz( int n, int c); int main (void) { printf("please enter a positive int\n"); int n=GetInt(); int count=0; collatz (n,count); } int collatz (int n,int c) { while (n>0) { //base case is when n==1 if (n==1) { c++; //print number of repition to make it 1 printf("%i\n", c); return 0; } // if even else if ((n%2)==0) { return 1 + collatz(n/2,c++); } //if odd else return 1 + collatz(3*n+1,c++); } }>

here is the code and error is"control may reach end of non-void function [-Werror,-Wreturn-type]"

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
int collatz( int n, int c);
int main (void)
{
    printf("please enter a positive int\n");
    int n=GetInt();
    int count=0;
    collatz (n,count);
}
    int collatz (int n,int c)
{   
   
    
    while (n>0)
    {   
        //base case is when n==1
        
        if (n==1)
        {
            c++;
            //print number of repition to make it 1
            printf("%i\n", c);
            return 0;
        }
        // if even
       else if ((n%2)==0)
        {
            return 1 + collatz(n/2,c++);
        }
        //if odd
        else
        return 1 + collatz(3*n+1,c++);
    }

}
added 175 characters in body
Source Link

here is the code and error is"control may reach end of non-void function [-Werror,-Wreturn-type]"

#include #include int collatz( int n, int c);
int collatz (int n)
int main (void)

{
//count for counting the number of repition to complete this int count=0; while (n>0) {
//base case is when n==1 count++; if (n==1) { //print number of repition to make it 1 printf("%i\n", count); return 0; } // if even else if ((n%2)==0) { return 1 + collatz(n/2); } //if odd else return 1 + collatz(3*n+1); }

{ printf("please enter a positive int\n"); int n=GetInt(); int count=0; collatz (n,count);

}

} int collatz (int n,int c) { while (n>0) { //base case is when n==1 if (n==1) { c++; //print number of repition to make it 1 printf("%i\n", c); return 0; } // if even else if ((n%2)==0) { return 1 + collatz(n/2,c++); } //if odd else return 1 + collatz(3*n+1,c++); } }>

here is the code and error is"control may reach end of non-void function [-Werror,-Wreturn-type]"

int collatz (int n)

{
//count for counting the number of repition to complete this int count=0; while (n>0) {
//base case is when n==1 count++; if (n==1) { //print number of repition to make it 1 printf("%i\n", count); return 0; } // if even else if ((n%2)==0) { return 1 + collatz(n/2); } //if odd else return 1 + collatz(3*n+1); }

}

here is the code and error is"control may reach end of non-void function [-Werror,-Wreturn-type]"

#include #include int collatz( int n, int c); int main (void) { printf("please enter a positive int\n"); int n=GetInt(); int count=0; collatz (n,count); } int collatz (int n,int c) { while (n>0) { //base case is when n==1 if (n==1) { c++; //print number of repition to make it 1 printf("%i\n", c); return 0; } // if even else if ((n%2)==0) { return 1 + collatz(n/2,c++); } //if odd else return 1 + collatz(3*n+1,c++); } }>
Source Link

I wrote this program as per the recursion video but its giving one error

here is the code and error is"control may reach end of non-void function [-Werror,-Wreturn-type]"

int collatz (int n)

{
//count for counting the number of repition to complete this int count=0; while (n>0) {
//base case is when n==1 count++; if (n==1) { //print number of repition to make it 1 printf("%i\n", count); return 0; } // if even else if ((n%2)==0) { return 1 + collatz(n/2); } //if odd else return 1 + collatz(3*n+1); }

}