C Loops

Write a C program to print 1 to 10 numbers using a while loop

Description: Print the numbers from 1 to 10 using a while loop.

Hint:

  • Start with a counter of 1, and increase it in each round until it is more than 10.

C program to print 1 to 10 using a while loop

#include <stdio.h>

int main(void) {
    int i = 1;

    while (i <= 10) {
        printf("%d ", i);
        i++;
    }
    printf("\n");
    return 0;
}

Output:

Output of the above program is

1 2 3 4 5 6 7 8 9 10

How it works

  • The counter i starts at 1.
  • The loop runs while i <= 10, prints the value and then increases it with i++.
  • When i becomes 11, the condition is false and the loop stops.

Read C While Loop.

Write a C program to print a right triangle star pattern

Description: Print a right triangle of stars with 5 rows. The first row has one star, the second has two, and so on.

Hint:

  • Use two loops. The outer loop is for rows and the inner loop is for stars in that row.

C program to print a right triangle of stars

#include <stdio.h>

int main(void) {
    int rows = 5;

    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}

Output:

Output of the above program is

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

How it works

  • The outer loop variable i is the row number.
  • The inner loop prints i stars in row i.
  • printf("\n") moves to the next row.

Read C Nested Loops.

Write a C program to print the multiplication table of a number

Description: Take a number as input and print its multiplication table from 1 to 10.

Hint:

  • Use a for loop from 1 to 10 and print n * i in each round.

C program to print a multiplication table

#include <stdio.h>

int main(void) {
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);

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

Output:

Output of the above program is

Enter a number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

How it works

  • The loop variable i goes from 1 to 10.
  • In each round the program prints the number, the multiplier and the product.

Read C For Loop.

Write a C program to find the sum of first n natural numbers

Description: Take a number n as input. Print the sum of numbers from 1 to n.

Hint:

  • Keep a variable sum = 0 and add each number to it in a loop.

C program to find the sum of natural numbers

#include <stdio.h>

int main(void) {
    int n, sum = 0;

    printf("Enter n: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        sum += i;
    }
    printf("Sum of 1 to %d = %d\n", n, sum);
    return 0;
}

Output:

Output of the above program is

Enter n: 10
Sum of 1 to 10 = 55

How it works

  • The loop adds 1, 2, 3 … up to n into sum.
  • There is also a formula: n * (n + 1) / 2, which gives 55 for n = 10 without a loop.

Write a C program to find the factorial of a number

Description: Take a number as input and print its factorial. Factorial of n is 1 x 2 x 3 x ... x n.

Hint:

  • Start with fact = 1 and multiply it by each number from 1 to n. Use unsigned long long for bigger values.

C program to find the factorial

#include <stdio.h>

int main(void) {
    int n;
    unsigned long long fact = 1;

    printf("Enter a number: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        fact *= i;
    }
    printf("Factorial of %d = %llu\n", n, fact);
    return 0;
}

Output:

Output of the above program is

Enter a number: 5
Factorial of 5 = 120

How it works

  • fact becomes 1, 2, 6, 24 and finally 120.
  • Factorial of 0 is 1, and our loop gives 1 for 0, so it is correct.
  • The type unsigned long long holds values up to 20! only.

Read C Recursion for the recursive way.

Write a C program to print the Fibonacci series

Description: Take the number of terms as input. Print that many terms of the Fibonacci series: 0, 1, 1, 2, 3, 5, 8 …

Hint:

  • Each term is the sum of the two terms before it.

C program to print the Fibonacci series

#include <stdio.h>

int main(void) {
    int n, a = 0, b = 1, next;

    printf("Enter number of terms: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        printf("%d ", a);
        next = a + b;
        a = b;
        b = next;
    }
    printf("\n");
    return 0;
}

Output:

Output of the above program is

Enter number of terms: 8
0 1 1 2 3 5 8 13

How it works

  • We start with a = 0 and b = 1.
  • In each round we print a, then move forward: a takes the value of b, and b takes the new sum.

Write a C program to reverse a number

Description: Take a number as input and print it in reverse order, like 1234 as 4321.

Hint:

  • Take the last digit with % 10, add it to the reversed number, and remove it with / 10.

C program to reverse a number

#include <stdio.h>

int main(void) {
    int n, reversed = 0;

    printf("Enter a number: ");
    scanf("%d", &n);

    while (n != 0) {
        int digit = n % 10;
        reversed = reversed * 10 + digit;
        n /= 10;
    }
    printf("Reversed number = %d\n", reversed);
    return 0;
}

Output:

Output of the above program is

Enter a number: 1234
Reversed number = 4321

How it works

  • Round 1: digit 4, reversed = 4, n = 123.
  • Round 2: digit 3, reversed = 43, n = 12.
  • It continues until n is 0, and the answer is 4321.

Write a C program to check whether a number is a palindrome

Description: Take a number as input. Print if it is a palindrome (it reads the same from both sides), like 121.

Hint:

  • Reverse the number, and compare it with the original.

C program to check a palindrome number

#include <stdio.h>

int main(void) {
    int n, original, reversed = 0;

    printf("Enter a number: ");
    scanf("%d", &n);
    original = n;

    while (n > 0) {
        reversed = reversed * 10 + n % 10;
        n /= 10;
    }

    if (original == reversed) {
        printf("%d is a palindrome\n", original);
    } else {
        printf("%d is not a palindrome\n", original);
    }
    return 0;
}

Output:

Output of the above program is

Enter a number: 121
121 is a palindrome

How it works

  • We keep a copy in original, because the loop changes n.
  • 121 reversed is 121, so both are equal.

Read Reverse a Number.

Write a C program to check whether a number is prime or not

Description: Take a number as input. Print if it is a prime number. A prime number has only two factors, 1 and itself.

Hint:

  • Try to divide the number by 2, 3, 4 … Stop when a divisor is found, or when the divisor is more than the square root.

C program to check a prime number

#include <stdio.h>

int main(void) {
    int n, isPrime = 1;

    printf("Enter a number: ");
    scanf("%d", &n);

    if (n < 2) {
        isPrime = 0;
    }
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            isPrime = 0;
            break;
        }
    }

    if (isPrime) {
        printf("%d is a prime number\n", n);
    } else {
        printf("%d is not a prime number\n", n);
    }
    return 0;
}

Output:

Output of the above program is

Enter a number: 29
29 is a prime number

How it works

  • Numbers less than 2 are not prime.
  • If n has a divisor, it has one which is not more than its square root. So the loop stops at i * i <= n.
  • break ends the loop as soon as a divisor is found.

Write a C program to check whether a number is an Armstrong number

Description: Take a 3 digit number as input. Print if it is an Armstrong number. For example, 153 is, because 13 + 53 + 33 = 153.

Hint:

  • Take each digit with % 10, add its cube to a sum, and compare the sum with the original number.

C program to check an Armstrong number

#include <stdio.h>

int main(void) {
    int n, original, digit, sum = 0;

    printf("Enter a 3 digit number: ");
    scanf("%d", &n);
    original = n;

    while (n > 0) {
        digit = n % 10;
        sum += digit * digit * digit;
        n /= 10;
    }

    if (sum == original) {
        printf("%d is an Armstrong number\n", original);
    } else {
        printf("%d is not an Armstrong number\n", original);
    }
    return 0;
}

Output:

Output of the above program is

Enter a 3 digit number: 153
153 is an Armstrong number

How it works

  • The digits of 153 are 3, 5 and 1. Their cubes are 27, 125 and 1, and the sum is 153.
  • For numbers with a different count of digits, the power is the count of digits, not 3.

Write a C program to count the digits in a number

Description: Take a number as input and print how many digits it has.

Hint:

  • Divide the number by 10 in a loop, and count how many times you can do it before it becomes 0.

C program to count the digits of a number

#include <stdio.h>

int main(void) {
    int n, count = 0;

    printf("Enter a number: ");
    scanf("%d", &n);

    if (n == 0) {
        count = 1;
    }
    while (n != 0) {
        n /= 10;
        count++;
    }
    printf("Number of digits = %d\n", count);
    return 0;
}

Output:

Output of the above program is

Enter a number: 45678
Number of digits = 5

How it works

  • 45678 becomes 4567, 456, 45, 4 and finally 0. That is 5 divisions, so 5 digits.
  • The number 0 has one digit, so we handle it separately.

Write a C program to print a pyramid star pattern

Description: Print a pyramid of stars with 4 rows.

Hint:

  • In row i print rows - i spaces first, and then 2 * i - 1 stars.

C program to print a pyramid pattern

#include <stdio.h>

int main(void) {
    int rows = 4;

    for (int i = 1; i <= rows; i++) {
        for (int s = 1; s <= rows - i; s++) {
            printf(" ");
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

Output:

Output of the above program is

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

How it works

  • The first inner loop prints the spaces on the left, so the pyramid is in the centre.
  • The second inner loop prints the stars: 1, 3, 5, 7 in the four rows.

Read C Nested Loops.