• 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?
 
fridomConnect With a Mentor CEO/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
 
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
 
Infinity08Connect With a Mentor Commented:
>> Ï 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.