Quantcast

Jump to content

» «
Photo

For Loop in C

3 replies to this topic
Moestry
  • Moestry

    Player Hater

  • Members
  • Joined: 13 Jan 2017
  • United-Kingdom

#1

Posted 13 January 2017 - 12:18 PM Edited by Moestry, 13 January 2017 - 12:18 PM.

Hello every one , i want to do an For loop for this console output.

 

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10

And i want have something like this.

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

 

It need to have 2 For Loops.

Can someone script me that in C?.

I'm new in programming and i dont know how that work

 

Thx.


Parik
  • Parik

    Transform

  • Members
  • Joined: 03 Dec 2012
  • Olympics

#2

Posted 13 January 2017 - 12:44 PM Edited by Parik, 29 May 2017 - 07:48 PM.

#include <stdio.h>

int
main(void)
{
    int i;
    for(i = 1; i <= 10; i++)
    {
        int j;
        for(j = 1; j<=i; j++)
        {
            printf("%d ",j);
        }
        printf("\n");
    } 
    return 0;
}

For the second one

#include <stdio.h>

int
main(void)
{
    int i;
    for(i = 0; i <= 10; i++)
    {
        int j;
        for(j = i; j <= 10; j++)
        {
            printf("%d ",j);
        }
        printf("\n");
    }
   return 0;
}

The first code works like this

It runs a loop for i from 1 to 10 , and prints the numbers between 1 to i.

 

The second code does the same except it prints numbers i to 10.


Moestry
  • Moestry

    Player Hater

  • Members
  • Joined: 13 Jan 2017
  • United-Kingdom

#3

Posted 13 January 2017 - 12:45 PM

Thank you :D


K^2
  • K^2

    Vidi Vici Veni

  • Moderator
  • Joined: 14 Apr 2004
  • United-States
  • Best Poster [Technology / Programming] 2016
    Best Poster [Programming] 2015
    Most Knowledgeable [Web Development/Programming] 2013
    Most Knowledgeable [GTA Series] 2011
    Best Debater 2010

#4

Posted 13 January 2017 - 01:31 PM

Some important corrections to Parik's answer having to do with language, rather than algorithm. I'm honestly only adding these because question asks about C and not C/C++ in general. It might sound like nit-picking, but there are many little pedantic habits that can make you a better programmer.

1) Declaration of variable inside the for loop initializer is not always allowed. Specifically, this became a C feature only with C99, and not all C compilers have it enabled by default. This code might not compile. On the other hand, C99 and any C++ compiler should have for loops declared exactly like that.
2) C is not C++. "int main()" does not mean in C what it does in C++. Correct C definition is "int main(void)". This will compile, but can have undesired side-effects.
3) Since main is declared int, it must return a value. Any half-decent C/C++ compiler will throw a warning. Don't just ignore warnings. They are there to protect you from stupid things. Like your code terminating with an "error" because E/RAX register was not empty when main returned.

Finally, purely a style note which becomes important in C++. In modern use, the preferred increment in a loop is pre-indrecment (++i) instead of post-increment (i++). The reason has to do with operator overloading in C++ and what these two operators imply for an object with non-trivial constructor.
  • Plank., Mr.Arrow and thehambone like this




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users