• C

loop program

how to do this program
thanks guys

Ï initialize an integer 'i' to 23
Ï print a new line with the value of 'i'
Ï run a for loop with the following requirements
Ë use a local integer variable named 'i' as the loop variable
Ë initialize the for loop to 0
Ë the for loop must loop 10 times
Ë post increment the loop variable
Ë print a new line with the value of the loop variable inside the for loop code block
Ï after the for loop, print a new line with the value of 'i'
LVL 1
tsburtAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tsburtAuthor Commented:
this is what i ahve so far..

#include <stdio.h>

int main(void)
{
int x;
for x=0; x<=23; x++) printf("%d ", x);
0
fridomCEO/ProgrammerCommented:
Well you are nearly done
int main (void) {
   int i = 23;
   printf("%d\n", i); /* the \n prints the new line */
   /* then in the loop do just wha you were asked to do:
   for (i = 0;  i < 10; i++) {
     /* output */

i++ the postincrement

Now in C99 this probably is allowed also
    int i = 23

    for(int i = 0;

The for may open a new scope, but I doubt that was meant it seems you want to reuse the
local variable i.

Regards
Friedrich
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Infinity08Commented:
>> Ï initialize an integer 'i' to 23

As fridom already pointed out : you used x instead of i. And you didn't initialize it yet.


>> Ï print a new line with the value of 'i'

Take a look at the printf standard function (in <stdio.h>)


>> Ï run a for loop with the following requirements

Your for loop is a good start, but you're missing a ( somewhere.

>> Ë use a local integer variable named 'i' as the loop variable

Same comment as earlier : i instead of x

>> Ë initialize the for loop to 0

That's ok.

>> Ë the for loop must loop 10 times

This still has to be fixed. Also consider using < instead of <=

>> Ë post increment the loop variable

That's ok too.

>> Ë print a new line with the value of the loop variable inside the for loop code block

That's almost ok - you need to add a new line. You can use the newline character \n for that. Take a look at this reference page for printf eg. :

        http://www.cplusplus.com/reference/clibrary/cstdio/printf.html

(especially at the example on that page)


>> Ï after the for loop, print a new line with the value of 'i'

You'll have to add one more printf here ...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.