Thread: Simple For Loop

1. Simple For Loop

What is wrong here?

Code:
```#import<stdio.h>
int main (){

int start, end, fact=0;

printf("Please enter a starting and larger ending integer so \n "
"I can find the factorial of each of those integer numbers between and including \n"
"the starting and ending integer numbers which are divisible by 2 as well as by 3\n");
scanf("%d%d",&start,&end);
for (int i=start;i=end;i++){
if(i%2&&i%3==0){
fact*=fact;
fact=fact-1;
printf("%d",fact);
}

}

return 0;
}```

2. Originally Posted by jjzarabi
What is wrong here?

Code:
```#import<stdio.h>
int main (){

int start, end, fact=0;

printf("Please enter a starting and larger ending integer so \n "
"I can find the factorial of each of those integer numbers between and including \n"
"the starting and ending integer numbers which are divisible by 2 as well as by 3\n");
scanf("%d%d",&start,&end);
for (int i=start;i=end;i++){
if(i%2&&i%3==0){
fact*=fact;
fact=fact-1;
printf("%d",fact);
}

}

return 0;
}```
Is "import" a C++ function? I'm not sure as I only know C. If C, then try using "#include"

Also it would help if you'd post what error you're getting since I'm not a very good

3. Several programing languages use "import" directive, but, AFAIK, it's more like C++ "using" then C/C++ preprocessor's "include". Maybe OP has previous experience in one such language, and typed "import" instead of "include" by reflex.

4. Furthermore:
Code:
`for (int i=start;i=end;i++)`
The loop condition is broken, as the result of an assignment statement is the value of left hand operand after assignment. I strongly suspect that you intended to write i == end, but that wouldn't work either, as the loop condition wouldn't be met, unless start is equal to end. The condition you want it probably i <= end, which will cause the loop to execute while i is less then or equal to end.

5. Objective-C?

6. I was thinking Python, maybe, because that's where I remember seeing "import" used. But after a quick glance at Obj-C's Wikipedia page, I'd say you've guessed correctly.

7. I meant #include
There is no output
This is a C program

8. Originally Posted by jjzarabi
There is no output
Oh ok, too bad you didn't mention that minor detail from the start. Sometimes we ask
people, if they want help that it would be much easier for us to read your code if you
could use indentation. Just in case you don't know what that means, I made your code
(with 2 corrections) more readable by indenting your code.

What was my reward? Well, I got output alright, but I don't think it is your desired
result.

The two corrections: (In Green) you declared an int i inside a for loop so I moved it with the
other int declarations. Second, I took msh's suggestion, and used " i <= end " because
that makes sense without additional detail.

Code:
```#include <stdio.h>

int main ()
{

int i, start, end, fact=0;

printf("Please enter a starting and larger ending integer so \n "
"I can find the factorial of each of those integer numbers between and including \n"
"the starting and ending integer numbers which are divisible by 2 as well as by 3\n\n");

scanf("%d%d",&start, &end);

printf("\n\n");

for (i=start; i <= end; i++)
{
if(i%2 && i%3 == 0)
{
fact*=fact;
fact=fact-1;
printf("%d",fact);
}
}

printf("\n\n");

return 0;
}```

Code:
```Here was my input:  1 100
Here was my output: -10-10-10-10-10-10-10-10-1```
You wanted output? Well, there ya go!

Popular pages Recent additions