hi,
there...ive come up with these codes and they work.but i dont know how to modify the program so that it prints sequence of asterisks in a single line based on the input gained using :
1.while
2.do-while
3.for

please help...


#include <stdio.h>

int main()

{

int x;

printf("Enter an integer\n");
scanf("%d", &x);


if((x<=20)&&(x>0))
{


}

else
{
printf("You have entered an invalid integer.Please enter a number smaller or equals to 20");


}

while((x>20)||(x<=0)){
printf("\nEnter a valid integer \n");
scanf("%d",&x);

}
printf("valid");
return 0;


}

In future, remember to use code tags.

Here is the code how I would have formatted it, after cleaning it up a little for you.

#include <stdio.h>

int main() {
  int x;

  printf("Enter an integer:\n");
  scanf("%d", &x);

  if ( !((x <= 20) && (x > 0)) ) {
    printf("\nYou have entered an invalid integer.\nPlease enter a number smaller or equal to 20:\n");
  }

  while ( (x > 20) || (x <= 0) ){
    printf("\nEnter a valid integer:\n");
    scanf("%d",&x);
  }

  printf("\nvalid");

  return 0;
}

prints sequence of asterisks in a single line based on the input gained using :
1.while
2.do-while
3.for

First, all three of these will do a very similar thing (loop until a condition is met), so the code for each will look quite similar.

So, if I understood correctly, you want to make a loop which will print as many asterisks' as the user inputs.

  • while:
    while ( x-- != 0 ) {
      printf("*");
    }

    This will keep decrementing x and displaying one asterisks at a time, until x == 0.

  • do-while:
    do {
      printf("*");
    } while ( --x != 0 );

    Same applies for this one, but as the order in which the code executes has been changed, you have to decrement x before comparing it with 0 (by changing it to --x instead of x--).

  • for:
    for (; x != 0; --x) {
      printf("*");
    }

    Once again, the same thing is happening, except the code looks a little different...

Hope this helps.

commented: Good explanation for this newbie +14

idk if its the same for everybody, but my teacher takes points off unless we use i, j, or k for loops. so you could do

do{
printf("\nEnter valid integer less than 20: ");
scanf("%d", &x);
}while(x > 0 && x <= 20);

for(i = 0; i < x; i++){
printf("*");
}

etc...

idk if its the same for everybody, but my teacher takes points off unless we use i, j, or k for loops.

For real? I agree that 'i,j,k' are quite commonly used as vars for a loop, but I can think of countless examples where i,j or k would be a very bad choice.
I personally only use 1-character-variables (such as i,j,k) if the loop is very short (no more then 3-4 lines). And I try to never use the same name twice in a program. That's to keep the code more readable.

I think your teacher is probably just a lazy guy and wants his students to use these names so that he can easily identify the loops in a code or something...

True, what about if you're making a graph... and need to plot some x & y coordinates, what would be more readable?

for (int i = 0; i < 10; ++i) {
  for (int j = 0; j < 10; ++j) {
    graph.plot(i, j);
  }
}

or...

for (int x = 0; x < 10; ++x) {
  for (int y = 0; y < 10; ++y) {
    graph.plot(x, y);
  }
}

Just use what seems appropriate, and don't stick with i, j, k just because they are more commonly used.

>True, what about if you're making a graph... and need to plot some x & y coordinates, what would be more readable?

Since we are splitting hair, let's declare those int variables outside those for loops. Portability to the lowest common denominator should be a concern.

Since we are splitting hair, let's declare those int variables outside those for loops. Portability to the lowest common denominator should be a concern.

Switching between the C and C++ forum can be so confusing sometimes :)

Splitting hairs somewhat further, a better formatting for the printf with the long string is:

printf("\nYou have entered an invalid integer.\n"
           "Please enter a number smaller or equal to 20:\n");

String literals separated only by whitespace are concatenated by the compiler. A useful fact.

I think your teacher is probably just a lazy guy and wants his students to use these names so that he can easily identify the loops in a code or something...

lol i think its the exact opposite of that actually. i think hees such a perfectionist that he just has to give us rules like that to make us learn more things... same reason he makes us learn the exact titles of every chapter, section of the chapter, and every sub-section of the chapter.

I hope that you at least recognize the importance of the programming concepts rather than memorizing the text of the book, like your neurotic teacher seems to think.

haha, yeah that gets annoying but he makes sure we learn EVERYTHING, definitely the coding, and knowing how to 'doodle' what is going on in the code, and memory templates, and all that good shtuff.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.