Feeling overwhelmed by repetitive code in Class 10 SEBA Computer Science? Chapter 4 holds the key to unlocking efficiency and automation—the power of loops! This guide provides a clear and friendly approach to help you master these coding tools.
INTRODUCTION TO LOOPS
Table of Contents
Exercise Questions
1. Why do we use a loop in a C program?
Ans: We use loops in a C program to execute a block of code repeatedly based on a certain condition or set of conditions.
2. Do we need to use only one type of loop in a C program? Justify your answer by writing a C program.
Ans: No, we don’t need to use only one type of loop in a C program. Here’s a simple C program that demonstrates the use of different types of loops:
#include <stdio.h>
int main() {
// For loop example
printf("For loop:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", i);
}
printf("\n");
// While loop example
printf("While loop:\n");
int j = 0;
while (j < 5) {
printf("%d ", j);
j++;
}
printf("\n");
// Do-while loop example
printf("Do-while loop:\n");
int k = 0;
do {
printf("%d ", k);
k++;
} while (k < 5);
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.
Ans: If we write a while loop with 1 in place of the condition, it will create an infinite loop because 1 is always considered true in C programming. Here’s a simple C program demonstrating this:
#include <stdio.h>
int main() {
// Infinite while loop
while (1) {
printf("This is an infinite loop.\n");
}
return 0;
}
4. Name different portions of a for loop. Can we put more than one statement within a portion?
Ans: In the C program, there are portions of a for loop. These are:
i) Initialization: It initializes the loop control variable.
ii) Condition: It checks the condition for continuing the loop.
iii) Update: It updates the loop control variable after each iteration.
Yes, we can put more than one statement within a portion of a for loop by using curly braces {} to create a compound statement.
5. Answer with TRUE or False.
(1) If the condition of the while loop is false, the control comes to the second statement inside the loop.
Ans: False.
(2) We can use at most three loops in a single C program.
Ans: False.
(3) The statement inside the do-while loop executes when the condition is false.
Ans: True.
(4) Only the first statement inside the do-while loop executes when the condition is false.
Ans: True.
(5) In a do-while loop, the condition is written at the end of the loop.
Ans: True.
6. Programming exercises:
A. Write the C program to find the summation of the following series.
(a) 1² + 2² + 3² + 4² + …..+ N²
Ans:
#include <stdio.h>
int main() {
int N, sum = 0;
printf("Enter the value of N: ");
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
sum += i * i;
}
printf("Sum of the series 1^2 + 2^2 + ... + %d^2 = %d\n", N, sum);
return 0;
}
(b) 1³ + 2³ + 3³ + 4³ + …..+ N³
Ans:
#include <stdio.h>
int main() {
int N, sum = 0;
printf("Enter the value of N: ");
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
sum += i * i * i;
}
printf("Sum of the series 1^3 + 2^3 + ... + %d^3 = %d\n", N, sum);
return 0;
}
(c) 1*2 + 2*3+ 3*4 + ……+ N*(N+1)
Ans:
#include <stdio.h>
int main() {
int N, sum = 0;
printf("Enter the value of N: ");
scanf("%d", &N);
for (int 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. Write a C program to continuously take a number as input and announce whether the number is odd or even.
Ans:
#include <stdio.h>
int main() {
int number;
while (1) {
printf("Enter a number (enter 0 to exit): ");
scanf("%d", &number);
if (number == 0) {
printf("Exiting the program...\n");
break;
}
if (number % 2 == 0) {
printf("%d is an even number.\n", number);
} else {
printf("%d is an odd number.\n", number);
}
}
return 0;
}
C. Write the C program to display the following pattern.
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
Ans:
#include <stdio.h>
int main() {
int rows;
printf(“Enter the number of rows: “);
scanf(“%d”, &rows);
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf(“1 “);
}
printf(“\n”);
}
return 0;
}
D. Write the C program to display the following pattern.
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
Ans:
#include <stdio.h>
int main() {
int rows;
printf(“Enter the number of rows: “);
scanf(“%d”, &rows);
for (int i = 1; i <= rows; i++) {
for (int j = 5; j >= 6 – i; j–) {
printf(“%d “, j);
}
printf(“\n”);
}
return 0;
}
E. Write the C program to display the following pattern.
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
Ans:
#include <stdio.h>
int main() {
int rows;
printf(“Enter the number of rows: “);
scanf(“%d”, &rows);
for (int i = 1; i <= rows; i++) {
for (int j = 5; j >= 6 – i; j–) {
printf(“%d “, j);
}
printf(“\n”);
}
return 0;
}
2 thoughts on “Class 10 Computer Science Notes SEBA Chapter 4”