This section lists the programs covered under Iterative Statements (Loops). Click on a program title to jump directly to its explanation.
Write a C program to find the sum of first n natural numbers (1 + 2 + 3 + … + n).
nsum = 0n and add each number to sumsum
#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;
}
Enter value of n:
5
Sum of first 5 natural numbers = 15
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.
Write a C program to find the factorial of a given number.
nfact = 1fact by numbers from 1 to n using a loop
#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;
}
Enter a number:
5
Factorial of 5 = 120
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.
Write a C program to print Fibonacci numbers up to n terms.
na = 0, b = 1n terms: print a, compute next term as c = a + b, then update a = b, b = c
#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;
}
Enter number of terms:
7
Fibonacci Series:
0 1 1 2 3 5 8
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.
Write a C program to find the reverse of a given number.
nrev = 0n != 0:
rem = n % 10rev = rev * 10 + remn = n / 10rev
#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;
}
Enter a number:
1234
Reversed number = 4321
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.
Write a C program to check whether the binary representation of a positive number is a palindrome.
nn in temprev = 0temp > 0:
bit = temp % 2rev = rev * 2 + bittemp = temp / 2n and rev
#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;
}
Enter a positive number:
9
Binary representation is a Palindrome
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.
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)
#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;
}
Enter password:
1111
Incorrect password
Enter password:
1234
Correct password
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.
Write a C program to check whether a given number is prime or not.
nn <= 1, it is not primen/2
#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;
}
Enter a number:
17
Prime Number
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.
Write a C program to find the Greatest Common Divisor (GCD) of two numbers.
a and bb != 0:
b in tempb = a % ba = tempb becomes 0, a holds the GCD
#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;
}
Enter two numbers:
48 18
GCD = 6
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.
Write a C program to print triangle patterns using nested for loops.
nn to control rows
#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;
}
Enter number of rows:
4
*
* *
* * *
* * * *
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.
Write a C program to find the sum of a Geometric Progression (GP).
a, common ratio r and number of terms nsum = 0i from 0 to n - 1, add a * r^i to sumsum
#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;
}
Enter first term, common ratio and number of terms:
2 3 4
Sum of GP = 80.00
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.