Class 10 Computer Science Chapter 4 Introduction To Loops Question Answer, SEBA Class 10 Computer Science Solutions, NCERT Class 10 Computer Science Chapter 4 Introduction To Loops to each chapter is provided in the list so that you can easily browse throughout different chapters SCERT Class 10 Computer Science Chapter 4 Introduction To Loops and select needs one.
Class 10 Computer Science Chapter 4 Introduction To Loops
Also, you can read the SCERT book online in these sections Solutions by Expert Teachers as per SCERT (CBSE) Book guidelines. NCERT Solution of Class 10 Computer Science Chapter 4 Introduction To Loops is part of AHSEC All Subject Solutions. Here we have given Class 10 Computer Science Chapter 4 Introduction To Loops Notes for All Subjects, You can practice these here in Class 10 Computer Science.
INTRODUCTION TO LOOPS
Chapter – 4
COMPUTER SCIENCE
Key Terms (Basic Concepts)
● Loop: Loop is used to execute the block of code several times according to the condition given in the loop.
● Iteration: Iteration is the process where a set of instructions or statements is expected repeatedly for a specified number of time or until a condition is met.
● Break: The Break statement is used to exit from the loop constructs.
TEXTUAL QUESTIONS AND ANSWERS
QUESTIONS
1. Why do we use a loop in a C program?
Ans. The looping simplifies the complex problems into the easy ones. It enables us to alter the flow of program so that instead of waiting the same code again and again, we can repeat the same code for a finite number of times.
For example, if we need to print the first 10 natural numbers then, instead of using the printf() statement 10 times, we can print inside a loop which runs up to 10 iterations.
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 can use all types of loops in a C program.
C program has three different types of loops –
● for loop.
● do…while loop.
● while loop.
A C program can be written in all the three types of loops.
Example:
Printing “Class X” 5 times using all the three types of loop.
#include<stdio.h>
int main()
{
int n = 1,a;
while (n<=5)
{
printf(“Class X \n”)
n++;
}
do
{
printf(Class X\n”);
n++
} while (n(<=5);
for (a=1; a<=5;++)
{
printf(“Class X\n”);
}
return 0;
}
(3) What will happen if we write a while loop with 1 in place of condition? Try it in a simple C program.
Ans. The while (1) is used for infinite loop. There is no condition for while. As 1 or any non-zero value is present, then the condition is always true. So, what are present inside the loop that will be executed forever.
To come out from this infinite loop, we have to use conditional statement and break statement.
Example:
#include<stdio.h>
int main()
{
int n = 1;
while (1)
{
printf(n);
printf(“\n”);
n++;
}
return 0;
}
Explanation:
The program will print natural number and the loop will run forever.
4. Name different portions of a for loop. Can we put more than one statement within a portion?
Ans. The three portions of for loop are —
● Initialization expression.
● Condition / test expression.
● Update expression.
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. FALSE.
(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²
Solution:
Code:
#include<stdio.h>
int main()
{
int i, b, N, sum=0;
printf(Enter the value of N.“);
scanf (“%d“, &N);
for(i=; i<=N; i++)
{
b=i * i;
sum = sum + b;
}
printf(“\n The summation is %d”, sum);
return 0;
}
Output:

(b) 1³ + 2³ + 3³ + 4³ + …..+ N³
Solution:
Code:
#include<stdio.h>
int main()
{
int i, b, N, sum=0;
printf(“Enter the value of N: “);
scanf(“%d“,&N);
for(i=1; i<=N; i++)
{
b=i * i * i;
sum = sum + b;
}
printf(“\n The summation is %d”, sum);
return 0;
}
Output:

(c) 1*2 + 2*3+ 3*4 + ……+ N*(N+1)
Solution:
Code:
#include<stdio.h>
int main()
{
int i, b, N, sum = 0;
printf(Enter the value of N:“);
scanf(“%d“, &N);
for(i=1; i<=N; i++)
{
b=i * (i + 1);
sum = sum + b;
}
printf (“\n The summation is %d”, sum);
return 0;
Output:

B. Write a C program to continuously take a number as input and announce whether the number is odd or even.
Solution:
Code:
#include<stdio.h>
int main()
{
int i, a, N;
printf(“\n Enter the value of N:“);
scanf(“%d“, &N) :
for(i=1; i<=N; i++)
{
printf(“\n\n Enter a number“);
scanf(“%d”, &a);
if(a%2 == 0)
printf(“\n The number entered is even”);
else
printf(“\n The number entered is odd”);
}
return 0;
}
Output:

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
Solution:
Code:
#include<stdio.h>
int main()
{
int i, a=1;
for(i=1; i<=1; i++)
{
printf(“%d “,a);
printf(“\n”);
for(i=1; i<2; i++)
{
printf(“%d “, a);
}
printf(“\n”);
for(i=1; i<=3; i++)
{
printf(“%d “,a);
}
printf(“\n”);
for(i=1; i<=4; i++)
{
printf(“ %d “, a)
}
printf(“\n”);
for(i=1; i<=5; i++)
{
printf(“ %d “, a);
}
return 0;
}
Output:

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
Solution:
Code:
#include<stdio.h>
int main()
{
int i;
for(i=5; i>=5; i—)
{
printf(“%d “,i);
}
printf(“\n”);
for(i=5; i>=; 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);
}
return 0;
}
Output:

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
Solution:
Code:
#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);
}
return 0;
}
Output:

ADDITIONAL QUESTIONS AND ANSWERS
1. Why loop is used in C?
Ans. Loop is used to execute the block of code several times according to the condition given in the loop.
2. What are loop statements?
Ans. Looping statements are used to repeat a single statement or a set of statements as long as the desired condition remains true.
3. How many types of loops are there?
Ans. C program has three different types of loops –
● for loop.
● do … while loop.
● while loop.
4. Explain the syntax / signature of a while loop.
Ans. The syntax / signature of while loop is –
while(condition)
{
// statements
}
// statements outside the loop
Explanation:
The statements inside the while loop are executed as long as the condition evaluates as true.
At first, the condition of the while is checked. In case it evaluates as true, all the statements inside the loop are executed. After executing all the statements of the loop every time, the condition is checked.
If the condition happens to be true, the statements are executed again. When the condition evaluates as false, the control comes out of the loop and executes the statements outside the loop.
5. What is the difference between while(1) and while(0)?
Ans. The while(1) is an infinite loop which will run till a break statement is issued explicitly. Interestingly not while(1) but any integer which is non-zero will give a similar effect as while(1). Therefore, while(1), while(2) or while (-255), all will give infinite loop only.
The while(0) is opposite of while(1). It means condition will always be false and thus code in while will never get executed.
6. Explain the syntax / signature of do… while loop.
Ans. The syntax/signature of do… while loop is –
Do
{
//statements
} while(condition);
//statements outside the loop
Explanation:
A do… while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time.
Here, the conditional expression appears at the end of the loop, so the statement (s) in the loop executes once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement (s) in the loop executes again. This process repeats until the given condition becomes false.
7. Explain the syntax / signature of for loop.
Ans. The syntax / signature of for loop is –
for (Initialization expression; text expression; update expression)
{
//body of the loop
}
//statements outside the loop
Explanation:
The initialization statement is executed only once. Then, the test expression is evaluated. If the test expression is evaluated to false, the for loop is terminated.
However, if the test expression is evaluated to true, statements inside the body of the for loop are executed and the update expression is updated. Again, the test expression is evaluated.
8. What is the difference between for loop and while loop?
Ans. The difference between for loop and while loop is that in for loop the number of iterations to done is already known and is used to obtain a certain result whereas in while loop the command runs until a certain condition is reached and the statement is proved to be false.
9. What is the difference between while and do… while loop?
Ans. While the loop is an entry control loop because firstly, the condition is checked, then the loop’s body is executed.
The do-while loop is an exit control loop because in this, first of all, the body of the loop is executed then the condition is checked true or false.
10. Program execution:
(a) Write a C program to extract the individual digits from a given integer.
Solution:
Code:
#include<stdio.h>
int main()
{
int n, temp, digit;
printf(“\n Enter the integer : “);
scanf(“%d”, &n);
temp = n;
while (temp > 0)
{
digit = temp % 10;
printf(“\n Extracted digit is %d “, digit);
temp = temp / 10;
}
return 0;
}
Output:

(b) Write a program to find the summation of digits of a given integer.
Solution:
Code:
#include<stdio.h>
Int main()
{
int n, temp, digit, sum = 0;
printf(“\n Enter the integer : “);
scanf(“%d”, &n);
temp = n;
while (temp > 0)
{
digit = temp % 10;
sum = sum + digit;
temp = temp / 10;
}
printf(“\n The summation of digits is %d “, sum);
return 0;
}
Output:

(c) Write a C program to read 10 numbers from keyboard and find their sum and average.
Solution:
Code:
#include<stdio.h>
int main()
{
int i, sum = 0, avg;
for (i = 1; i<=10; i++)
{
printf(“\n Enter the number : “);
scanf(“%d”, &n);
sum = sum + n;
}
avg = sum / 10;
printf(“\n The summation is %d“, sum);
printf(“\n The average is %d“, avg);
return 0;
}
Output:

(d) Write C program to display the multiplication table of a given integer.
Solution:
Code:
#include<stdio.h>
int main()
{
int i, n, b;
printf(“\n Enter the number : “);
scanf(“%d”, &n);
for (i = 1; i<=10; i++)
{
b = n * i;
printf(“\n %d × %d = %d “, n, i, b);
}
return 0;
}
Output:

(e) Write a C program to calculate the factorial of a given number.
Solution:
Code:
#include<stdio.h>
int main()
{
int i, n, fact = 1;
printf(“\n Enter the number :“);
scanf(“%d”, &n);
for (i = 1; i<=n; i++)
{
fact = fact * i;
}
printf(“\n Factorial is %d“, fact);
return 0;
}
Output:

(f) Write a C program to print natural number in reverse order.
Solution:
Code:
#include<stdio.h>
int main()
{
int i, s, e,
printf(“\n Enter the starting natural number :“);
scanf(“%d”, &s);
printf(“\n Enter the ending natural number : “);
scanf(“%d”, &e);
for (i = s; i>=; i—)
{
printf(“%d “,);
}
return 0;
}
Output:

(g) Write a C program to print the sum of even and odd numbers.
Solution:
Code:
#include<stdio.h>
int main()
{
int i, s, e, sum1 = 0, sum2 = 0;
printf(“\n Enter the starting natural number : “);
scanf(“%d”, &s);
printf(“\n Enter the ending natural number : “);
scanf(“%d”, &e);
for (i = s; i<=e; i++)
{
if (i % 2 == 0)
sum1 = sum1 +;
else
sum2 = sum2 + i;
}
printf(“\n Sum of even numbers %d : “, sum1);
printf(“\n Sum of odd numbers %d : “, sum2);
return 0;
}
Output:

(h) Write a C program to check whether a number is prime or not.
Solution:
Code:
#include<stdio.h>
int main()
{
int i, n, flag = 0;
printf(“Input a number : “);
scanf(“%d”, &n);
for (i=2; i<=n/2; i++)
{
if(n % i == 0)
{
flag = 1;
break;
}
}
if(flag == 0 && n ! = 1)
printf(“\n %d is a prime number “, n);
else
printf(“\n %d is not a prime number “, n);
return 0;
}
Output:

(i) Write a C program to draw the following pattern.
1
2 2
3 3 3
4 4 4 4
5 5 5 5
Solution:
Code:
#include<stdio.h>
int main()
{
int i;
for (i=1; i<=1; i++)
{
printf(“ 1 “);
}
printf(“\n”);
for(i=1; i<=2; i++)
{
printf(“ 2 “);
}
printf(“\n”);
for(i=1; i<=3; i++)
{
printf(“ 3 “ );
}
printf(“\n”);
for(i=1 ; i<=4; i++)
{
printf(“ 4 “);
}
printf(“\n”);
for(i=1; i<=5; i++)
{
printf(“ 5 “);
}
return 0;
}
Output:

Kindly correct the true and false statements.
Most of them are wrongly answered otherwise everything else is quite helpful.