0

i have a problem here.

#include<stdio.h>
int main (){
int counter;
int x;
int y;
int product;

counter=1;
x=1;
y=1;
product=1;

while (counter<=5){
printf("%d>%d\n",x,y);
x=x+1;
y=y+2;
counter=counter+1;
product=product*y;
}

printf("Product>%d",product);
return 0;
}

well,the product should be 945.but what have printf out is 10395?what is the provlem inside the code?help pls.

6
Contributors
15
Replies
17
Views
6 Years
Discussion Span
Last Post by sing1006
0

What are you trying to do? The x is incremented. But you are not using it anywhere. It would help if you explain what you are trying to do.

0

The question is write a program that calculates and print the product of the odd integer from 1 to 10.The output of the program is shown below:
1>1
2>3
3>5
4>7
5>9
product>945

0

* ALWAYS * use code tags on code forums. Forum software turns your code into html junk, otherwise - very hard to study.

#include<stdio.h>
int main (){
  int counter;
  int x;
  int product;

  counter=0;
  x=1;
  product=1;

  while (counter<5){
    counter++; //this is C!
    product*=x;
    printf("x:%d *= product is: %d\n",x, product);
    x+=2;

  }

  printf("Product>%d",product);
  (void) getchar();
  return 0;
}

See what that adds up to - I haven't run this.

0

sry,i will do use it next time.back to the question,the product should be 1*3*5*7*9=945.but what the problem is my product become 10395.juz like 1*3*5*7*9*11.but the answer should be 945.dont know why it will become 10395.

0

erm,the product is right,but the things upper the product is wrong,it should be like this.
1>1
2>3
3>5
4>7
5>9
product>945

0

So make it describe what it's doing, the way you want it. It's 4 a.m. here, and I wanted something more descriptive. ;)

0

Write a program that calculates and print the product of the odd integer from 1 to 10.The output of the program is shown below:
1>1
2>3
3>5
4>7
5>9
product>945
*the product =945 because(1*3*5*7*9)
i get everything but juz a problem.(1*3*5*7*9)should be 945,right?but mine one become 10395.

0

ya,Adak posted is correct but not the right answer.i mean,he get the correct answer but not the right way.he not using the way like the question ask to.

0

Ah okay, that's an easy fix, to get the output that you want:

#include<stdio.h>
   
      int main (){
   
      int counter;
   
      int x;
   
      int product;
   
       
   
      counter=0;
   
      x=1;
   
      product=1;
  
       
  
      while (counter<5){
  
      counter++; //this is C!
  
      product*=x;
  
      printf("%d>%d\n",counter, x);
  
      x+=2;
  
       
  
      }
  
       
  
      printf("Product>%d",product);
  
      (void) getchar();
  
      return 0;
  
      }

try that, it should give what you want. Let me know

0

The reason that you get 10395 instead of 945 is because you are doing 11*9*7*5*3*1
In your code you go one step too fast.
Look:

STEP 1.: 1
STEP 2.: 1 * 3
STEP 3.: 1 * 3 * 5
STEP 4.: 1 * 3 * 5 * 7
STEP 5.: 1 * 3 * 5 * 7 * 9 = 945
You are doing this in the code:
STEP 1.: 1 * 3
STEP 5.: 1 * 3 * 5 * 7 * 9 * 11 = 10395

To fix this, you can do different things. but simplest is to do this:
(This is your own code, I only changed position of 1 thing)

#include<stdio.h>
int main (){
int counter;
int x;
int y;
int product;

counter=1;
x=1;
y=1;
product=1;

while (counter<=5){
printf("%d>%d\n",x,y);
x=x+1;
//y = y + 2 moved UNDER product = product * y.
counter=counter+1;
product=product*y;
y=y+2;
}

printf("Product>%d",product);
return 0;
}

You can also remove x and replace

printf("%d>%d\n",x,y);

x here with counter, since you're not really using it for anything else anyway.

Edited by [Alpha]-0mega-: n/a

0

Here is the answer.
1st,let us see what is the different between the wrong 1 and the right 1.
Below this is wrong 1.

#include<stdio.h>
int main (){
int counter;
int x;
int y;
int product;

counter=1;
x=1;
y=1;
product=1;

while (counter<=5){
printf("%d>%d\n",x,y);
x=x+1;
y=y+2;
counter=counter+1;
product=product*y;
}

printf("Product>%d",product);
return 0;
}

The right 1.

#include<stdio.h>
int main (){
int counter;
int x;
int y;
int product;

counter=1;
x=1;
y=1;
product=1;

while (counter<=5){
printf("%d>%d\n",x,y);
product=product*y;
x=x+1;
y=y+2;
counter=counter++;
}

printf("Product>%d",product);
return 0;
}

I can't get the right product from the 1st code about juz because my put the thing at wrong location.tat's all.sweat.

0

By the way,[Alpha]-0mega-,why must move y = y + 2 UNDER product = product * y?i still don't understand.the number for variable y only loop until 9 then it will stop.then,where did that 11 come from?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.