0

I am running this program

#include <stdio.h>
int main ()
{
  int n;
  int a = 1;
  int b = 2;
  int product;
  int i;

  printf("How many numbers of the sequence would you like \n");
  scanf("%d",&n);

  for (i=0;i<n;i++)
  {
    printf("%d\n",a);
    product = a * b;
    a = b;
    b = product;
  }

   return 0;
}

When I enter n = 3, the result is 1 2 2
Why is it ? I meant to make it so it show 1 2 4 , what have I done wrong ? And why is it print 1 2 2 .

4
  • 6
    Use the Debugger, Luke! Commented Sep 13, 2015 at 12:54
  • 1
    @UweKeim, who is Luke BTW? Commented Sep 13, 2015 at 12:55
  • What is your recursion formula? Commented Sep 13, 2015 at 12:58
  • You did it right, just not printing what you want Commented Sep 13, 2015 at 13:10

4 Answers 4

2

And why is it print 1 2 2 .

Step-by-step trace at printf("%d\n",a);:

i  a  b  product
0  1  2  ?
1  2  2  2
2  2  4  4
3  4  8  8
4  8  32 32
Sign up to request clarification or add additional context in comments.

Comments

0

I meant to make it so it show 1 2 4

Then interchange the below two lines

 a = b;
 b = product;

It should be

for (i=0;i<n;i++)
 {
 printf("%d\n",a);
 product = a * b;
 b = product;
 a = b;
 }

4 Comments

Your recursion formula is u(n+1) = u(n)² , i think he wants u(n+2) = u(n+1)*u(n)
I'm sorry but that answer is wrong. The program will show 1 2 4 for n = 3, but also 1 2 4 16 for n = 4.
@Saiph, that could be possible but how do you know my recursion formula isn't correct. Did OP mentioned anywhere in post about the specific series he is looking for? What you are saying is based on your own speculation.
He didn't give the formula, but he obviously implemented u(n+2) = u(n+1)*u(n). Also if that was what he wanted, just b = b*b would be enough and all the other variables would be useless.
0

Your code does exactly what it's supposed to do.

Maybe you wanted :

 #include <stdio.h>
 int main ()
 {
 int n;
 int a = 1;
 int b = 2;
 int product;
 int i;

 printf("How many numbers of the sequence would you like \n");
 scanf("%d",&n);

 printf("%d\n",a);

     for (i=1;i<n;i++)
     {
     product = a * b;
     a = b;
     b = product;
     printf("%d\n",b);
     }    

 return 0;
 }

1 Comment

but don't input 0 please
0

1 2 2 because you print a not product and value of a in each iteration is -

  1st iteration a=1
  2nd iteration a=2        // a=b and b=2 i.e a=2
  3rd iteration a=2        // why ? because again a=b and b did not change 
  thus output 1 2 2

Simple solution would be -

1.Initialize product to 1 // int product =1;

2.print product instead of printf("%d\n",a); // printf("%d\n",product);

1 Comment

Thank you, your answer was helpful too, I confused myseft , but I got it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.