Jump to content

Loops in C

- - - - -

  • Please log in to reply
7 replies to this topic

#1
TitanClone

TitanClone

    Newbie

  • Members
  • Pip
  • 2 posts
Trying to write a simple program, that if the user entered '5' the output would be:
0
01
012
0123
01234
012345
01234
0123
012
01
0

I'm having trouble coming up with the proper loops to do this. Any help would be appreciated.

Thanks!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You'll need a nested for loop, followed by a second nested for loop.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
rvega

rvega

    Newbie

  • Members
  • Pip
  • 6 posts
I am trying to print out a wheel/or circle to the screen. How would I go about this?

#4
agnl666

agnl666

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
  • Programming Language:C, Java, C++
  • Learning:Python, Assembly
by wheel, do your meen a wheel made out of ascii characters ?

#5
rvega

rvega

    Newbie

  • Members
  • Pip
  • 6 posts
Yes, dollar signs and or stars, using for loops.

#6
rvega

rvega

    Newbie

  • Members
  • Pip
  • 6 posts

agnl666 said:

by wheel, do your meen a wheel made out of ascii characters ?

yes, dollar signs and or stars, using for loops.

#7
mitya

mitya

    Newbie

  • Members
  • Pip
  • 4 posts

TitanClone said:

Trying to write a simple program, that if the user entered '5' the output would be:
0
01
012
0123
01234
012345
01234
0123
012
01
0

I'm having trouble coming up with the proper loops to do this. Any help would be appreciated.

Thanks!
Try something like this.


int main(int argc, char ** argv) {

  int num, i, n;

  scanf("%d", &num);


  for (i = 0; i <= num; i++) {

    for (n = 0; n <= i; n++)

      printf("%d", n);


    printf("\n");

  }


  for (i = num - 1; i >= 0; i--) {

    for (n = 0; n <= i; n++)

      printf("%d", n);


    printf("\n");

  }

}


#8
rvega

rvega

    Newbie

  • Members
  • Pip
  • 6 posts
Hi!

I am trying to get this code to print a wheel on the screen. it is kind of printing sideways. could you please look at it and tell me whats wrong?


int main()
{

int distance, x, y;



for (y=-20; y<=20; y++)

for (x=-20; x <= 20; x++)
{
if (x*x + y*y >= 20*20)
printf(" ");
else if (x*x + y*y < 2)
printf("+");
else if (x*x + y*y <= 6*6)
printf("$");
else
printf("*");
}
system("pause");
return 0;
}




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users