I. Theory Questions
1. Why do we use a loop in a C program?
We use loops in C programs to execute a block of code repeatedly until a specific condition is met. Loops help in:
- Reducing code repetition
- Processing data structures like arrays
- Implementing iterative algorithms
- Creating menus and interactive programs
- Performing tasks that need repetition without writing the same code multiple times
2. Do we need to use only one type of loop in a C program? Justify your answer by writing a C program.
No, we don't need to use only one type of loop in a C program. We can use different types of loops (while, do-while, for) based on the specific requirements of different parts of our program.
Here's a C program that demonstrates using all three types of loops:
#include <stdio.h>
int main() {
int i, n = 5;
// Using for loop to print numbers 1 to 5
printf("Using for loop: ");
for(i = 1; i <= n; i++) {
printf("%d ", i);
}
printf("\n");
// Using while loop to print numbers 1 to 5
printf("Using while loop: ");
i = 1;
while(i <= n) {
printf("%d ", i);
i++;
}
printf("\n");
// Using do-while loop to print numbers 1 to 5
printf("Using do-while loop: ");
i = 1;
do {
printf("%d ", i);
i++;
} while(i <= n);
printf("\n");
return 0;
}
3. What will happen if we write a while loop with 1 in place of the condition? Try it in a simple C program.
If we write a while loop with 1 as the condition, it will create an infinite loop because the condition always evaluates to true (non-zero). The loop will continue executing indefinitely until the program is forcibly terminated.
Example:
#include <stdio.h>
int main() {
while(1) {
printf("We must raise our voice against corruption \n");
}
return 0;
}
This program will continuously print "We must raise our voice against corruption" until you stop the execution manually (usually by pressing Ctrl+C).
4. Name different portions of a for loop. Can we put more than one statement within a portion?
A for loop in C has three portions:
- Initialization: Executed once before the loop starts
- Condition: Checked before each iteration
- Increment/Decrement: Executed after each iteration
Yes, we can put more than one statement within a portion by using comma operators. For example:
for(i = 0, j = 10; i < 5 && j > 5; i++, j--) {
printf("i = %d, j = %d\n", i, j);
}
In this example:
- Initialization portion has two statements: i = 0 and j = 10
- Condition portion has two conditions: i < 5 and j > 5
- Increment portion has two statements: i++ and j--
II. True/False Questions
5. Answer with TRUE or FALSE.
(i) If the condition of the while loop is false, the control comes to the second statement inside the loop.
FALSE
If the condition of a while loop is false, the control does not enter the loop at all. It goes to the statement immediately after the loop.
(ii) We can use at most three loops in a single C program.
FALSE
There is no limit on the number of loops we can use in a C program. We can use as many loops as needed.
(iii) The statements inside the do-while loop executes at least once even if the condition is false.
TRUE
In a do-while loop, the condition is checked after the execution of the loop body, so the statements inside execute at least once regardless of the condition.
(iv) Only the first statement inside the do-while loop executes when the condition is false.
FALSE
All statements inside the do-while loop execute at least once, regardless of the condition. If we want to execute multiple statements, we need to enclose them in curly braces {}.
(v) In a do-while loop, the condition is written at the end of the loop.
TRUE
In a do-while loop, the condition is checked after the loop body executes, so it's written at the end of the loop.
III. Programming Exercises
A. Series Summation Programs
(a) Write a C program to find the summation of the series: 1² + 2² + 3² + 4² + ... + N²
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter the value of N: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
sum += i * i;
}
printf("Sum of the series 1² + 2² + ... + %d² = %d\n", n, sum);
return 0;
}
(b) Write a C program to find the summation of the series: 1³ + 2³ + 3³ + 4³ + ... + N³
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter the value of N: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
sum += i * i * i;
}
printf("Sum of the series 1³ + 2³ + ... + %d³ = %d\n", n, sum);
return 0;
}
(c) Write a C program to find the summation of the series: 1×2 + 2×3 + 3×4 + ... + N×(N+1)
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter the value of N: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
sum += i * (i + 1);
}
printf("Sum of the series 1×2 + 2×3 + ... + %d×%d = %d\n", n, n+1, sum);
return 0;
}
B. Odd/Even Number Checker
Write a C program to continuously take a number as input and announce whether the number is odd or even. Hint: use do-while loop.
#include <stdio.h>
int main() {
int num;
char choice;
do {
printf("Enter a number: ");
scanf("%d", &num);
if(num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
printf("Do you want to check another number? (y/n): ");
scanf(" %c", &choice); // Note the space before %c to consume newline
} while(choice == 'y' || choice == 'Y');
printf("Program ended.\n");
return 0;
}
C. Pattern Printing Programs
C. Write a C program to display the following pattern:
1
1 1
1 1 1
1 1 1 1
#include <stdio.h>
int main() {
int i, j, rows = 4;
for(i = 1; i <= rows; i++) {
for(j = 1; j <= i; j++) {
printf("1 ");
}
printf("\n");
}
return 0;
}
D. Write a C program to display the following pattern:
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
#include <stdio.h>
int main() {
int i, j, rows = 5;
for(i = 1; i <= rows; i++) {
for(j = 1; j <= i; j++) {
printf("%d ", rows - j + 1);
}
printf("\n");
}
return 0;
}
E. Write a C program to display the following pattern:
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
#include <stdio.h>
int main() {
int i, j, rows = 5;
for(i = 1; i <= rows; i++) {
for(j = rows; j >= i; j--) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}