Nested Loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tricubix
    New Member
    • Sep 2007
    • 6

    Nested Loops

    Hi friends, newbie here, appreciate your help to convert this program into C languages. I'm really-really beginner into C world

    [CODE=cpp]#include <iostream.h>

    #define MAX 10


    int main()
    {
    int number, line, column;

    for( line = 1; line <= MAX; line++ )
    {
    for( column = MAX; column >= line; column-- )
    cout << ' ';

    for( column = line; column < 2*line; column++ )
    {
    number = column % 10;
    cout << number;
    }

    for( column = 2*(line-1); column >= line; column-- )
    {
    number = column % 10;
    cout << number;
    }
    cout << endl;
    }
    }[/CODE]

    it's a nested loops that can generate a pyramids of no 1-10
    so..i just need your help to convert it into C language

    TQ in advanced
    Last edited by Ganon11; Oct 1 '07, 01:22 PM. Reason: Please use the [CODE] tags provided.
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by tricubix
    Hi friends, newbie here, appreciate your help to convert this program into C languages. I'm really-really beginner into C world

    #include <iostream.h>

    #define MAX 10


    int main()
    {
    int number, line, column;

    for( line = 1; line <= MAX; line++ )
    {
    for( column = MAX; column >= line; column-- )
    cout << ' ';

    for( column = line; column < 2*line; column++ )
    {
    number = column % 10;
    cout << number;
    }

    for( column = 2*(line-1); column >= line; column-- )
    {
    number = column % 10;
    cout << number;
    }
    cout << endl;
    }
    }

    it's a nested loops that can generate a pyramids of no 1-10
    so..i just need your help to convert it into C language

    TQ in advanced
    All,you need to do is to use printf instead of cout and instead of endl manipulator you use "\n"

    Savage

    Comment

    • tricubix
      New Member
      • Sep 2007
      • 6

      #3
      Originally posted by Savage
      All,you need to do is to use printf instead of cout and instead of endl manipulator you use "\n"

      Savage
      hi savage, thanks for the reply.

      Ok, for now if i use printf, what i need to put in the printf command?? is that number, line or column?

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        In order to convert the statement:

        [CODE=cpp]cout << "The value of myIntVar is " << myIntVar << endl;[/CODE]

        you would use this statement:

        [CODE=c]printf("The value of myIntVar is %d \n", myIntVar);[/CODE]

        Any string you output is just copied into the first argument. If you need to output a variable, you use one of the format specifiers (I don't know the whole list, but I know the specifier for ints is %d) in the place in the string where you'd like it to be displayed. Every time you add a specifier, you need to add the variable you wish to display as an argument after the string. In order to print endl, you add '\n' to the appropriate place in the string.

        Comment

        Working...