LearnFrenzy
Sign in Take a free test
C

C Programming · Topic 16 of 23

C Loops Questions and Concepts

  • 4 exercises
  • 40 questions
  • ~60 min
  • Answers explained

Programs & Examples

C Loops · Study material

Centered star pyramid

For row r of n: print n − r spaces, then 2r − 1 stars. Pattern programs are mostly about finding these two formulas.

#include <stdio.h>

int main(void) {
    int rows = 4;
    for (int r = 1; r <= rows; r++) {
        for (int s = 0; s < rows - r; s++)
            putchar(' ');                    /* rows - r leading spaces */
        for (int k = 0; k < 2 * r - 1; k++)
            putchar('*');                    /* 2r - 1 stars */
        putchar('\n');
    }
    return 0;
}

Output

   *
  ***
 *****
*******

Floyd's triangle

Row r prints r consecutive numbers; one counter outside the loops keeps counting across rows.

#include <stdio.h>

int main(void) {
    int n = 1;
    for (int r = 1; r <= 4; r++) {
        for (int c = 1; c <= r; c++)
            printf("%d ", n++);
        printf("\n");
    }
    return 0;
}

Output

1
2 3
4 5 6
7 8 9 10

Sieve of Eratosthenes

Nested loops cross out multiples of each prime, starting at p². It finds all primes up to N in O(N log log N) – much faster than testing each number separately.

#include <stdbool.h>
#include <stdio.h>

int main(void) {
    enum { N = 30 };
    bool composite[N + 1] = {false};
    for (int p = 2; p * p <= N; p++)
        if (!composite[p])
            for (int m = p * p; m <= N; m += p)   /* cross out the multiples of p */
                composite[m] = true;

    for (int i = 2; i <= N; i++)
        if (!composite[i])
            printf("%d ", i);
    printf("\n");
    return 0;
}

Output

2 3 5 7 11 13 17 19 23 29

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C Programming

C programming is the language most computer science courses start with and the foundation of operating systems, embedded devices and compilers. This section helps you learn C properly – data types, operators, control statements, functions, arrays, strings, pointers, structures, memory allocation and files – and then prove it with hundreds of practice questions explained line by line.

Why it matters

  • C teaches how a computer really works: memory, addresses and how data is stored
  • Pointers and memory questions are a favourite in written tests and technical interviews
  • Once you know C, learning C++, Java and Python becomes much easier
  • Embedded systems, operating systems and many companies' coding rounds still use C

Who it is for

  • School students (Class 11–12 computer science) building a strong programming base
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, aptitude-cum-coding tests and technical interviews
  • Anyone preparing for GATE CS or government IT exams

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro tests
  • Technical interviews
  • GATE CS
  • University exams
  • Government IT exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links