Home > Software engineering >  Ending a program with a period statement
Ending a program with a period statement

Time:09-17

I am trying to code a Fibonacci generator, and so far I was able to calculate it and got accurate results, but I keep receiving trailing commas and I wanted to add a period(.) at the end of the result.

    #include <stdio.h>
    int f1=0,f2=1,i,sum=0,n,a;
    
    printf("Please enter the number for Fibonacci numbers.\n");
    scanf("%d",&n);
    
    if(n==0){
        printf("%d.",n);
    }
    else if(n==1){
        a = n-1;
        printf("%d, %d.",a,n);
    }
    else{
        for(i=0;i<=n;i  ){
            printf("%d, ",f1);
            sum = f1 f2;
            f1 = f2;
            f2 = sum;
        }   
    }
return 0;
}

For example, n = 6

I wanted: 0, 1, 1, 2, 3, 5, 8.

What I got earlier: 0, 1, 1, 2, 3, 5, 8,

CodePudding user response:

Fetching the idea from the comments:

// I'd prefer unsigned as negative numbers are meaningless anyway
unsigned int f1 = 0, f2 = 1;
unsigned int n = 7;

printf("0"); // 0 is printed in *any* case

while(--n) // or with for loop from 0 as long as < (not <= any more!)
           // note the first value *is* printed already
{
    printf(", %d", f2); // prepending the comma, one value is already there
                        // f2: now using the larger of the two,
                        // as the smaller *is* printed already
    unsigned int sum = f1   f2; // (limiting scope of to where it
                                // actually is needed)
    f1 = f2;
    f2 = sum;
}
puts(".");

Bonus: No special cases necessary any more...

CodePudding user response:

Just start the loop with the second number :-)

else{                                   else{
                                            printf("0");           // 0 here
    for(i=0;i<=n;i  ){                      for(i=1;i<=n;i  ){     // start at 1
        printf("%d, ",f1);                      printf(", %d",f1); // change order
        sum = f1 f2;                            sum = f1 f2;
        f1 = f2;                                f1 = f2;
        f2 = sum;                               f2 = sum;
    }                                       }
                                            printf(".\n");         // add period
}                                       }

CodePudding user response:

If it was me, i'd keep the code simple, i wouldn't use negative indices, and i wouldn't start from position 1 instead of 0. What I would do is as follows:

#include <stdio.h>

int main(){
  // Always initialise variables, integers cost nothing to initialise
  // We use long because Fibonacci numbers can get quite large quite quickly.
  long long int f1=0,f2=1,iterations=0;

  printf("Please enter the number for Fibonacci numbers.\n");
  if(scanf("%lld",&iterations)!=1){
    // This means we've failed to read a number. 
    puts("Something is wrong with the input, exiting.");
    return -1;
  }

  //   iterations; // uncomment if you want iterations 1 Fibonacci numbers
  
  for(long long int i=0;i<iterations;  i){
    printf("%lld",f1);
    int f3=f1 f2;
    f1=f2;
    f2=f3;
    if(i<iterations-1){
      printf(", ");
    }
  }
  if(iterations>0){
    puts(".");
  }
  return 0;
}

To compile and run:

martyn@Atlas:~$ gcc Deleteme.c
martyn@Atlas:~$ ./a.out 
Please enter the number of Fibonacci numbers.
6
0, 1, 1, 2, 3, 5.
martyn@Atlas:~$ 

Note that your original program outputs 7 Fibonacci numbers, rather than 6. If you want 1 more than you input, then increment iterations before the loop, by doing something like iterations;

CodePudding user response:

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char **argv)
{
        int n = argc > 1 ? strtol(argv[1], NULL, 0) : 10;
        int f[2] = {1,1};
        while( n-- ) {
                printf("%d%s", f[0], n ? ", " : ".\n");
                int t = f[0]   f[1];
                f[0] = f[1];
                f[1] = t;
        }
        return 0;
}
  • Related