C Programs

C Programming – Iterative Statements


Programs at a Glance

This section lists the programs covered under Iterative Statements (Loops). Click on a program title to jump directly to its explanation.

  1. Sum of n Natural Numbers
  2. Factorial of a Given Number
  3. Fibonacci Series
  4. Reverse of a Given Number
  5. Binary Palindrome Check
  6. Password Validation using Loop
  7. Prime Number Check
  8. GCD of Two Numbers
  9. Triangle Pattern using Nested Loops
  10. Sum of a Geometric Progression

1. Sum of n Natural Numbers


Problem Statement

Write a C program to find the sum of first n natural numbers (1 + 2 + 3 + … + n).

Algorithm
  1. Start
  2. Read value of n
  3. Initialize sum = 0
  4. Use a loop from 1 to n and add each number to sum
  5. Display sum
  6. Stop
C Program

#include <stdio.h>

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

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

    for (i = 1; i <= n; i++) {
        sum = sum + i;
    }

    printf("Sum of first %d natural numbers = %d\n", n, sum);
    return 0;
}
    
Sample Output

Enter value of n:
5
Sum of first 5 natural numbers = 15
    
Explanation

The for loop starts from 1 and runs up to n, adding each number to the variable sum. At the end of the loop, sum stores the total of the series 1 + 2 + 3 + … + n.

2. Factorial of a Given Number


Problem Statement

Write a C program to find the factorial of a given number.

Algorithm
  1. Start
  2. Read integer n
  3. Initialize fact = 1
  4. Multiply fact by numbers from 1 to n using a loop
  5. Display factorial
  6. Stop
C Program

#include <stdio.h>

int main() {
    int n, i;
    long fact = 1;

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

    for (i = 1; i <= n; i++) {
        fact = fact * i;
    }

    printf("Factorial of %d = %ld\n", n, fact);
    return 0;
}
    
Sample Output

Enter a number:
5
Factorial of 5 = 120
    
Explanation

Factorial of n (written as n!) is the product 1 × 2 × 3 × … × n. The loop multiplies fact by each integer from 1 up to n, so at the end fact holds the factorial value.

3. Fibonacci Series


Problem Statement

Write a C program to print Fibonacci numbers up to n terms.

Algorithm
  1. Start
  2. Read number of terms n
  3. Initialize a = 0, b = 1
  4. Repeat for n terms: print a, compute next term as c = a + b, then update a = b, b = c
  5. Stop
C Program

#include <stdio.h>

int main() {
    int n, i;
    int a = 0, b = 1, c;

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

    printf("Fibonacci Series:\n");
    for (i = 1; i <= n; i++) {
        printf("%d ", a);
        c = a + b;
        a = b;
        b = c;
    }

    return 0;
}
    
Sample Output

Enter number of terms:
7
Fibonacci Series:
0 1 1 2 3 5 8
    
Explanation

In the Fibonacci series, each term (from the third term onward) is the sum of the previous two terms. The variables a and b track the last two values, and are updated in each iteration to generate the next term.

4. Reverse of a Given Number


Problem Statement

Write a C program to find the reverse of a given number.

Algorithm
  1. Start
  2. Read integer n
  3. Initialize rev = 0
  4. Repeat while n != 0:
    • Extract last digit using rem = n % 10
    • Append digit to reverse using rev = rev * 10 + rem
    • Remove last digit using n = n / 10
  5. Display rev
  6. Stop
C Program

#include <stdio.h>

int main() {
    int n, rev = 0, rem;

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

    while (n != 0) {
        rem = n % 10;
        rev = rev * 10 + rem;
        n = n / 10;
    }

    printf("Reversed number = %d\n", rev);
    return 0;
}
    
Sample Output

Enter a number:
1234
Reversed number = 4321
    
Explanation

The last digit of the number is extracted using the modulus operator % 10 and then appended to rev by shifting existing digits left (multiplying by 10). Repeating this process for all digits builds the reversed number.

5. Binary Palindrome Check


Problem Statement

Write a C program to check whether the binary representation of a positive number is a palindrome.

Algorithm
  1. Start
  2. Read positive integer n
  3. Store the original value of n in temp
  4. Initialize rev = 0
  5. Repeat while temp > 0:
    • Find last binary bit using bit = temp % 2
    • Build reversed binary value using rev = rev * 2 + bit
    • Update temp = temp / 2
  6. Compare n and rev
  7. If equal, binary representation is palindrome; otherwise not
  8. Stop
C Program

#include <stdio.h>

int main() {
    int n, temp, rev = 0, bit;

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

    temp = n;

    while (temp > 0) {
        bit = temp % 2;
        rev = rev * 2 + bit;
        temp = temp / 2;
    }

    if (n == rev)
        printf("Binary representation is a Palindrome\n");
    else
        printf("Binary representation is NOT a Palindrome\n");

    return 0;
}
    
Sample Output

Enter a positive number:
9
Binary representation is a Palindrome
    
Explanation

The program reconstructs the value represented by the reversed binary digits of n in the variable rev. If this reversed-binary value is equal to the original number, then the binary representation reads the same forwards and backwards, so it is a palindrome.

6. Password Validation using Loop


Problem Statement

Write a C program to read a password until it is correct. For wrong password print Incorrect password. For correct password print Correct password and terminate the program. (Correct password is 1234)

Algorithm
  1. Start
  2. Set correct password as 1234
  3. Repeat:
    • Read password from user
    • If password is correct, display message and stop
    • Else, display message and repeat
C Program

#include <stdio.h>

int main() {
    int password;

    while (1) {
        printf("Enter password:\n");
        scanf("%d", &password);

        if (password == 1234) {
            printf("Correct password\n");
            break;
        } else {
            printf("Incorrect password\n");
        }
    }

    return 0;
}
      
Sample Output

Enter password:
1111
Incorrect password
Enter password:
1234
Correct password
      
Explanation

The loop is an infinite while(1) loop that keeps asking for the password until the correct value is entered. When the user types 1234, the break statement exits the loop and the program terminates after printing the success message.

7. Prime Number Check


Problem Statement

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

Algorithm
  1. Start
  2. Read integer n
  3. If n <= 1, it is not prime
  4. Else, check divisibility from 2 to n/2
  5. If any divisor is found, number is not prime
  6. If no divisor is found, number is prime
  7. Display result
  8. Stop
C Program

#include <stdio.h>

int main() {
    int n, i, flag = 1;

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

    if (n <= 1) {
        flag = 0;
    } else {
        for (i = 2; i <= n / 2; i++) {
            if (n % i == 0) {
                flag = 0;
                break;
            }
        }
    }

    if (flag)
        printf("Prime Number\n");
    else
        printf("Not a Prime Number\n");

    return 0;
}
      
Sample Output

Enter a number:
17
Prime Number
      
Explanation

A prime number has exactly two distinct positive divisors: 1 and itself. The loop checks whether any number between 2 and n/2 divides n; if such a divisor is found, the flag is cleared and the loop stops early.

8. GCD of Two Numbers


Problem Statement

Write a C program to find the Greatest Common Divisor (GCD) of two numbers.

Algorithm
  1. Start
  2. Read two numbers a and b
  3. Repeat while b != 0:
    • Store b in temp
    • Set b = a % b
    • Set a = temp
  4. When b becomes 0, a holds the GCD
  5. Display GCD
  6. Stop
C Program

#include <stdio.h>

int main() {
    int a, b, temp;

    printf("Enter two numbers:\n");
    scanf("%d %d", &a, &b);

    while (b != 0) {
        temp = b;
        b = a % b;
        a = temp;
    }

    printf("GCD = %d\n", a);
    return 0;
}
      
Sample Output

Enter two numbers:
48 18
GCD = 6
      
Explanation

Euclid’s algorithm repeatedly replaces the pair (a, b) by (b, a % b) until the second number becomes zero. At that point, the first number stores the greatest common divisor of the original inputs.

9. Triangle Patterns using Nested Loops


Problem Statement

Write a C program to print triangle patterns using nested for loops.

Algorithm
  1. Start
  2. Read number of rows n
  3. Use outer loop from 1 to n to control rows
  4. Use inner loop from 1 to current row number to print symbols
  5. Move to the next line after each row
  6. Stop
C Program

#include <stdio.h>

int main() {
    int i, j, n;

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

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

    return 0;
}
      
Sample Output

Enter number of rows:
4
* 
* * 
* * * 
* * * * 
      
Explanation

The outer loop controls the number of rows, while the inner loop controls how many stars are printed in each row. On row i, exactly i stars are printed, forming a right-angled triangular pattern.

10. Sum of a Geometric Progression


Problem Statement

Write a C program to find the sum of a Geometric Progression (GP).

Algorithm
  1. Start
  2. Read first term a, common ratio r and number of terms n
  3. Initialize sum = 0
  4. For each term index i from 0 to n - 1, add a * r^i to sum
  5. Display sum
  6. Stop
C Program

#include <stdio.h>
#include <math.h>

int main() {
    float a, r, sum = 0;
    int n, i;

    printf("Enter first term, common ratio and number of terms:\n");
    scanf("%f %f %d", &a, &r, &n);

    for (i = 0; i < n; i++) {
        sum = sum + a * pow(r, i);
    }

    printf("Sum of GP = %.2f\n", sum);
    return 0;
}
      
Sample Output

Enter first term, common ratio and number of terms:
2 3 4
Sum of GP = 80.00
      
Explanation

Each term of the geometric progression is computed as a * r^i, where i is the term index starting from 0. The pow() function from math.h is used to raise the common ratio r to the power i, and all terms are added to obtain the total sum.