Class 10 Computer Science Chapter 4 | Introduction To Loops

Class 10 Computer Science Chapter 4| Introduction To Loops Question Answer: Welcome to our website! We are happy to provide you with Class 10th Notes for your academic journey. Class IX is an important year for learners as they prepare themselves for the upcoming board exams, which will determine their academic success.

Today, I will discuss your Class 10th “Introduction To Loops” Long and Short Questions| We provide solutions for almost all long and short questions.

Our goal is to meet your needs. We provide the Notes here for free. We wish you all the best for your upcoming exams. If you have any doubts, don’t hesitate to get in touch with us.

Introduction To Loops Question Answer

TEXTUAL QUESTIONS AND ANSWERS

1. Why do we use a loop in a C program?

Ans. The looping simplifies complex problems into easy ones. It enables us to alter the flow of the program so that instead of writing 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 that 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 three types of loops.

Example:

Printing “Class X” 5 times using all 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; a++)

{

printf(“Class X \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. The while(1) is used for an infinite loop. There is no condition for while. As 1 or any not zero value is present, then the condition is always true. So, what is 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 a natural number and the loop will run forever.

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.

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.

Ans. FALSE

(ii) We can use at most three loops in a single C program.

Ans. FALSE

(iii) The statement inside the do-while loop exécutes when the condition is false.

Ans. FALSE

(iv) Only the first statement inside the do-while loop executes when the condition is false.

Ans. TRUE

(v) 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=1;i<=N; i++)

{

b=i*i;

sum = sum +b;

}

printf(“\n The summation is %d”, sum);

return 0;

}

Output:

Enter the value of N: 4

The summation is 30

Process returned 0 (0x0)

Press any key to continue.

(b) 13+23+33 +43…..+N3

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:

Enter the value of N: 4

The summation is 100

Process returned 0 (0x0) execution time: 2.483 s

Press any key to continue.

(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:

The summation is 20

Process returned 0 (0x0) execution time: 2.529 s

Press any key to continue.

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:

Enter the value of N: 4

Enter a number: 56

The number entered is even

Enter a number63

The number entered is odd

Enter a number54

The number entered is even

Enter a number32

The number entered is even

Process returned 0 (0x0) execution time: 14.327 #

Press any key to continue.

C. Write the C program to display the following pattern.

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(“\n”);

for(i=1;i<=4; i++)’

{

printf(“%d “,a);

}

printf(“%d”,a);

}

printf(“\n”);

for(i=1;i<=5; i++)

{

printf(“%d”,a);

}.

return 0;

Output:

1

1 1

1 1 1

1 1 1 1

1 1 1 1 1

Process returned 0 (0x0) execution time: 0.344 s

Press any key to continue.

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>=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);

}

return 0;

}

Output:

5

5 4

5 4 3

5 4 3 2

5 4 3 2 1

Process returned 0 (0x0)

Press any key to continue.

execution time: 0.326 s

F. 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:

5 4 3 2 1

5 4 3 2

5 4 3

5 4

5

Process returned 0 (0x0) execution time: 0.090 s

Press any key to continue.

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
  • while loop
  • do… 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 true.

At first, the condition of the while is checked. In case it evaluates as true, statements inside the loop are executed. After executing all the statements the every time, the condition is checked.

If the condition happens to be true, the statements are executed again. When 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 that 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 an infinite loop only. The while(0) is the opposite of the while(1). It means the 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 be 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(“\nEnter 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:

Enter the integer: 156

Extracted digit is 6

Extracted digit is 5

Extracted digit is 1

Process returned 0 (0x0)

Press any key to continue.

execution time: 9.512 s

(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(“\nEnter 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:

Enter the integer : 156

The summation of digits is 12

Process returned 0 (0x0) execution time: 4.969 s

Press any key to continue.

(c) Write a C program to read 10 numbers from the keyboard and find their sum average.

Solution:

Code:

#include<stdio.h>

int main()

{

int i, n, 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:

Enter the number : 4

Enter the number: 6

Enter the number : 3

Enter the number: 5

Enter the number : 4

Enter the number: 9

Enter the number: 5

Enter the number: 6

Enter the number: 3

Enter the number: 8

The summation is 53

The average is S

Process returned 0 (0x0) execution time: 15.419 s

Press any key to continue.

(d) Write a 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 x %d = %d “, n, i, b);

}

return 0;

}

Output:

Enter the number: 5

5 x 1 = 5

5 x 2 = 10

5 x 3 = 15

5 x 4 = 20

5 x 5 = 25

5 x 6 = 30

5 x 7 = 35

5 x 8 = 48

5 x 9 = 45

5 x 10 = 50

Process returned e (exe) execution time: 1.898 s

Press any key to continue.

(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:

Enter the number: 5

Factorial is 120

Process returned 0 (0x0) execution time: 2.645 s

Press any key to continue.

(f) Write a C program to print natural numbers 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>=e; i–)

{

printf(“%d”, i);

}

return 0;

}

Output:

Enter the starting natural number: 10

Enter the ending natural number: 1

10 9 8 7 6 5 4 3 2 1

Process returned 0 (0x0) execution time: 4.385 s

Press any key to continue

(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 + i;

else

sum2 = sum2 + i;

}

printf(“\n Sum of even numbers %d: “, sum1);

printf(“\n Sum of odd numbers %d : “, sum2);

return 0;

}

Output:

Enter the starting natural number: 1

Enter the ending natural number : 10

Sum of even numbers 30 :

Sum of odd numbers 25 :

Process returned 0 (0x0) Execution time: 5.022 s

Press any key to continue…

(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:

Input a number: 53

53 is a prime number

Process returned 0 (0x0) execution time: 1.538 s

Press any key to continue.

(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:

#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:

Input a number: 53

53 is a prime number

Process returned 0 (0x0) execution time: 1.538 s

Press any key to continue.

Conclusion:

We believe these notes will help learners develop a better understanding of the topics and boost their confidence for their exams.

We are confident that these notes will help learners achieve their goals and enhance their academic performance. If you like this article and find it helpful, please share it with your friends.

Leave a Comment

error: Content is protected !!