Thread: Program Control (for loop)

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38

    Program Control (for loop)

    hey there.. i'm having a hard time to get output like below...

    0
    .1
    ..2
    ...3
    ....4
    .....5
    ......6
    .......7
    ........8
    .........9


    However.. i dont know how to insert the full stop symbol (.) inside my output..
    here is my code which get output like below..

    0
    11
    222
    3333
    44444
    555555
    6666666
    77777777
    888888888
    9999999999

    PHP Code:
    #include <stdio.h>
    int main () {

    int xyi;

    for (
    0<= 9x++) {
      for (
    0<= xy++) {
         
    printf("%d"x);
         
         }
       
    printf("\n");
    }

    return 
    0;

    i really hope u guys can help me.. =)

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    In your inner loop, just print a period instead of the value of x if y < x. You are very close to the answer.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I'll give you a clue: you have the newline in the right place, right? It prints once for every value of x, right? So...if I wanted the value of x printed only once, where would I put it inside that nest? Maybe with that newline...

    You can print the full stop character thusly:
    Code:
    printf(".");
    OR
    putchar('.');
    Notice the difference between "." and '.', since printf works with a string (even if it is only one char long), whereas putchar only take as single character.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    got it guys!!
    thanks for guiding me!! =)
    here is my new and correct code...

    Code:
    #include <stdio.h>
    int main () {
    
    int x, y, i;
    
    for (x = 0; x <= 9; x++) {
      for (y = 1; y <= x ; y++) {
         printf(".");
         
         }
       printf("%d", x);
       printf("\n");
    }
    
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM
  4. Edit Control problem
    By Malek in forum Windows Programming
    Replies: 3
    Last Post: 06-16-2002, 01:12 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM