When I type the following code, I dont get the required output and I fail to understand why.
Problem:
Write a program to find the product of all the positive even numbers less than or equal to 30.
Possible solution according to me:

#include <stdio.h>
void main(void)
{
 int n, prod, even;
 prod=1;
 n<=30;
   {
     for(even=2;even<n;even=even+2)
     prod=prod*even;
     even=even+2;
     printf("Product of all the positive even numbers less than %d is %d\n", n, prod);
   }
}

The only statement inside your for-loop is prod = prod*even;. The others are outside of the loop. The braces around your for-loop do not affect what's inside the for-loop - they only introduce a new local scope. To affect the extent of the for loop, the opening brace should be after the for(...).

Can you be a bit more specific about what is actually happening, then? Simply saying, 'This is not helping' gives us little to go on.

#include <stdio.h>
int main(void)
{
    int n, prod, even;
    prod = 1;
    n = 30;
    for(even = 2; even < n; even += 2)
    {
        prod *= even;
        printf("Product of all the positive even numbers less than %d is %d\n", n, prod);
    }

    return 0;
}

You might note a few things here:
* I replaced void main(void) with int main(void). While 'void main' is a common idiom, it is strictly speaking incorrect, or at least not portable.
* I corrected the assignment for n; as you had it, it was a comparison, not an assignment, and had no effect on the rest of the program.
* I used the accumulate-assignment statements for the increment and the product, and eliminated an extraneous increment.

Edited 3 Years Ago by Schol-R-LEA

Also leave out line 10, you are doing that already in the for loop.
By the way:Your integer type will overflow.
Write your for statement like this:

for (even = 2; even <= 30; even = even + 2)
{
    prod = prod * even;
}

Edited 3 Years Ago by ddanbe: addition

find the product of all the positive even numbers less than or equal to 30

You're only calculating the product for positive even numbers less than 30.
So it should be:

prod = 1;
n = 30;

for (even = 2; even <= n; even += 2)
    prod *= even;

printf("Product of all the positive even numbers less than %d is %d\n", n, prod);

Edited 3 Years Ago by nullptr

You are incrementing the variable even twice

No, line 10 in the OP's post is not part of the loop, has no bearing on the result and should be omitted.

for(even=2;even<n;even=even+2)
    prod=prod*even; // only this is calculated

Edited 3 Years Ago by nullptr

Doing it this way, the compiling is done. No errors are being shown but no output is printed either. This is what I coded:

#include <stdio.h>
void main(void)
{
 int n, prod, even;
 even=2;
 prod=1;
 n<=30;
   {
   for(even=2;even<=n;even+=2);
         prod*=even;
    printf("Product of all the positive even numbers less than %d is %d\n", n,prod);
    }       
}

Remove the semi colon at the end of line 9, and change the variable type to unsigned int so that you don't get integer overflow.

#include <stdio.h>

int main(void)
{
    unsigned int n, prod, even;
    prod = 1;
    n = 30;

    for (even = 2 ; even <= n; even += 2)
        prod *= even;

    printf("product of all positive even numbers less than or equal to %u is %u\n", n, prod);

    printf("\npress Enter to exit...");
    getchar();

    return 0;
}

Edited 3 Years Ago by nullptr

This article has been dead for over six months. Start a new discussion instead.