Question

The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm: if n is e

1 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>


int main(int argc, char **argv){
   int num;
  
   //checking - is there any argument passed or not
   if(argc==1){
        printf("\nNo extra command line argument passed other than program name.\n\n");
        exit(0);
    }
  

    // converting the char into integer
    num = atoi(argv[1]);
  
    //checking - is the argument positive interger or not
    if(num <= 0){
       printf("\nThe number should be a positive interger.\n\n");
        exit(0);
    }
  
    if(fork() == 0){
       //Child process
       while(num > 1){
           printf("%d ", num);
           if(num %2 == 0)
               num = num / 2;
           else
               num = 3 * num + 1;      
       }
       printf("1\n\n");
    }
    else{
       //Parent process - waiting for child to complete
       wait(NULL);
    }
  
    return 0;
}

OUTPUT:

rounak@rounak-HP-Notebook:/Desktop$ gcc a.c rounak@rounak-HP-Notebook:-/Desktop$ ./a.out -35 The number should be a positive

Add a comment
Know the answer?
Add Answer to:
The Collatz conjecture concerns what happens when we take any positive integer n and apply the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT