Class 10 Computer Science Chapter 4 SEBA : Introduction to Loops – Exercise Answers
In this chapter, we learn about loops in C programming. Loops are used to repeat a group of statements several times. This chapter covers different types of loops such as for loop, while loop and do-while loop, along with their uses in C programs.
Below are the SEBA Class 10 Computer Science Chapter 4 exercise questions and answers, written in simple English for easy understanding and examination preparation.
SEBA Class 10 Computer Science Chapter 4 – Introduction to Loops : Exercise Questions and Answers :
1. Why do we use a loop in a C program?
Answer: We use a loop to repeat the same statements several times. It reduces the length of the program and saves time because we do not have to write the same code again and again.
2. Do we need to use only one type of loop in a C program? Justify your answer with a C program.
Answer: No. We can use different types of loops in the same C program. The three commonly used loops are: while loop, do-while loop for loop.
| A program is given below to justify: #include <stdio.h> int main() { int i=0; while(i<5) { printf(“I read in class 10 \n”); i++; } for(i=0; i<5; i++) { printf(“I read in class 10 \n”); } return 0; } |
3. What will happen if we write a while with 1 in place of the condition?
Answer: If we write 1 as the condition of a while loop, the condition will always be true. Therefore, the loop will continue to execute and will not stop by itself. Such a loop is called an infinite loop.
Example:
#include<stdio.h>
int main()
{
while(1)
{
printf("We must...against corruption \n");
}
return 0;
}
//This program will continue running until it is manually stopped.
4. Name the different parts of a for loop. Can we put more than one statement within a portion?
Answer: The three main parts of a for loop are: 1) Initialization expression 2) Condition checking or testing expression 3) Update expression Yes, we can put more than one statement in a portion of a for loop. |
5. Answer with TRUE or FALSE
| No. | Statement | Answer |
|---|---|---|
| (i) | If the condition of a while loop is false, control comes to the second statement inside the loop. | False |
| (ii) | We can use at most three loops in a C program. | False |
| (iii) | The statements inside a do-while loop execute at least once even if the condition is false. | True |
| (iv) | A for loop is an entry-controlled loop. | True |
| (v) | In a do-while loop, the condition is written at the end of the loop. | True |
6. Programming Exercises
(A) Write a C program to find the summation of the following series.
(a) 1² + 2² + 3² + … + N²
Answer:
#include<stdio.h>
int main()
{
int i, N, sum=0;
printf("Enter the value of N: ");
scanf("%d", &N);
for(i=1; i<=N; i++)
{
sum=sum + i * i;
}
printf("Summation is %d", sum);
return 0;
}
(b) 1³ + 2³ + 3³ + … + N³
Answer:
#include<stdio.h>
int main()
{
int i, N, sum=0;
printf("Enter the value of N: ");
scanf("%d", &N);
for(i=1; i<=N; i++)
{
sum=sum + i * i * i;
}
printf("Summation is %d", sum);
return 0;
}
(c) 1*2 + 2*3 + 3*4 + … + N*(N + 1)
Answer:
#include<stdio.h>
int main()
{
int i, N, sum=0;
printf("Enter the value of N: ");
scanf("%d", &N);
for(i=1; i<=N; i++)
{
sum = sum + i * (i+1);
}
printf("Summation is %d", sum);
return 0;
}
(B) Write a C program to continuously take a number as input and announce whether the number is odd or even.
Answer:
#include<stdio.h>
int main()
{
int number, choice=1;
do
{
printf("\n Enter a number: ");
scanf("%d", &number);
if(number % 2 == 0)
printf("Even");
else
printf("Odd");
printf("\n Enter 0 to stop or any number to continue:");
scanf("%d", &choice);
}
while(choice != 0);
return 0;
}
(C) Write a C program to display the following pattern.
1
11
111
1111
11111
Answer:
#include<stdio.h>
int main()
{
int i;
for(i=1; i<=1; i++)
printf("1");
printf("\n");
for(i=1; i<=2; i++)
printf("1");
printf("\n");
for(i=1; i<=3; i++)
printf("1");
printf("\n");
for(i=1; i<=4; i++)
printf("1");
printf("\n");
for(i=1; i<=5; i++)
printf("1");
printf("\n");
return 0;
}
(D) Write a C program to display the following pattern.
5
54
543
5432
54321
Answer:
#include<stdio.h>
int main()
{
int i;
for(i=5; i>=5; i--)
printf("%d",i);
printf("\n");
for(i=5; i>=4; i--)
printf("%d",i);
printf("\n");
for(i=5; i>=3; i--)
printf("%d",i);
printf("\n");
for(i=5; i>=2; i--)
printf("%d",i);
printf("\n");
for(i=5; i>=1; i--)
printf("%d",i);
printf("\n");
return 0;
}
(E) Write a C program to display the following pattern.
54321
5432
543
54
5
Answer:
#include<stdio.h>
int main()
{
int i;
for(i=5; i>=1; i--)
printf("%d",i);
printf("\n");
for(i=5; i>=2; i--)
printf("%d",i);
printf("\n");
for(i=5; i>=3; i--)
printf("%d",i);
printf("\n");
for(i=5; i>=4; i--)
printf("%d",i);
printf("\n");
for(i=5; i>=5; i--)
printf("%d",i);
printf("\n");
return 0;
}
Important Points to Remember
- A loop is used to repeat statements.
- The three main loops in C are for, while and do-while.
- A for loop is an entry-controlled loop.
- A while loop is also an entry-controlled loop.
- A do-while loop is an exit-controlled loop.
- A
do-whileloop executes at least once. while(1)creates an infinite loop.
Conclusion
The Introduction to Loops chapter is an important part of Class 10 Computer Science because loops are widely used in C programming. Understanding for, while, and do-while loops will help students write shorter and more efficient programs.
These SEBA Class 10 Computer Science Chapter 4 exercise answers are written in simple English to help students understand the concepts and prepare for examinations.
