chapter 4 computer science class 10


      SEBA

      COMPUTER SCIENCE

      CHAPTER-4

      INTRODUCTION TO LOOPS


      1. Why we Use a loop in a C program?

      We use a loop in C program because it enables to alter the flow of the program so that instead of writing the same code again and again, we can execute the same code for a finite number of times.

      OR( YOU CAN ALSO WRITE)

      We use a loop in a C program because it allows us to execute a statement or group of statements 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 need not only use one type of loop in C program, we can use one or more loops inside any other like while, for or do…while loop. For example if we want to display I read in Class X 5 time  we can write the program using while or for loop also.

      Example-a): Using while loop    

      Example-b): Using do-while loop               

      a) 12 + 22 + 32 + 42 +……. + N2

      #include <stdio.h>

      int main()

      {

          int i, n, sum =0;

          printf("Enter the value of N: \n");

          scanf("%d", &n);

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

          {

              sum = sum + (i*i);

          }

          printf("Sum=%d", sum);

          return 0;

      }

       

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

       #include <stdio.h>

      int main()

      {

          int i, n, sum =0;

          printf("Enter the value of N: \n");

          scanf("%d", &n);

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

          {

              sum = sum + (i*i*i);

          }

          printf("Sum=%d", sum);

          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. Hint

      While(1)

      {

      Printf(“ We must raise our voice against corruption\n);

      }

      If we write a while loop with 1 in place of condition, a loop becomes an infinite loop as a result endless loop if a condition never becomes false.

       

      4. Name the different portion of a for loop. Can we put more than one statement within a portion?

      The different portion of a loop are:

      Initialization statement: describes the starting point of the loop, where we initialize one or mor counter variable

      Condition or Test statement: It is a place where we write conditional expression, which determine whether the body of the loop is executed or terminated.

      Update statements(Increment / decrement): It is a place where we increment or decrement initialized counter variable.

      Yes, We can put more than one statement within a portion:

      For example:

      #include<stdio.h>

      int x,y

      for (x=0, y=10; x<5, y<6; x++, y--)

      {

                    Statements;      

      }        

       

      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. FALSE       

      2.We can use at most three loops in a single C program FALSE    

      3. The statements inside the do-while loop executes at least once even if the condition is false. TRUE

      4. Only the first statement inside the do-while loop executes when the condition is false. FALSE  

      5. In a do-while loop, the condition is written at the end of the loop. TRUE                            

       

      6. Programing Excercises:

      A. Write a C program to find the summation of the following

      a)  12 + 22 + 32 + 42 +……. + N2

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

      c)  1*2 + 2*3 + 3*+3*4 +…N*(N+1)

      a) 12 + 22 + 32 + 42 +……. + N2

      #include <stdio.h>

      int main()

      {

          int i, n, sum =0;

          printf("Enter the value of N: \n");

          scanf("%d", &n);

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

          {

              sum = sum + (i*i);

          }

          printf("Sum=%d", sum);

          return 0;

      }

       

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

       #include <stdio.h>

      int main()

      {

          int i, n, sum =0;

          printf("Enter the value of N: \n");

          scanf("%d", &n);

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

          {

              sum = sum + (i*i*i);

          }

          printf("Sum=%d", sum);

          return 0;

      }

       

       

      c) 1*2 + 2*3 + 3*+3*4 +…N*(N+1)

       #include <stdio.h>

      int main()

      {

          int i, n, sum =0;

          printf("Enter the value of N: \n");

          scanf("%d", &n);

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

          {

              sum = sum + (i*(i+1));

          }

          printf("Sum=%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. Hint: use do-while loop.

      #include <stdio.h>

      int main()

      {

          int i, choose =1;

      do

      }

          printf("Enter a number : \n");

          scanf("%d", &i);

          if (i%2==0)

            printf(“%d is Even\n",i);

       else

         printf(“%d is odd\n”,i);

         printf("Enter 1 to continue. \n Enter any other number to exit.”);

         scanf(“%d”, &choose);

       }

      While(choose==1);

      Printf(“ You have selected to Exit from the loop”);

          return 0;

      }

       

       

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

      1

      11

      111

      1111

      11111

      #include<stdio.h>

      int a

      for (a=1; a<=1; a++)

      {

                    printf(“1\n”);

      }

        for (a=1; a<=2; a++)

      {

                    printf(“1\n”);

      }

        for (a=1; a<=3; a++)

      {

                    printf(“1\n”);

      }

            for (a=1; a<=4; a++)

      {

                    printf(“1\n”);

      }

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

      {

                    printf(“1\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 a

      for (a=5; a>4; a - -)

      {

                    printf(“%d\n”, a);

      }

      for (a=5; a>3; a - -)

      {

                    printf(“%d\n”, a);

      }

      for (a=5; a>2; a - -)

      {

                    printf(“%d\n”, a);

      }

      for (a=5; a>1; a - -)

      {

                    printf(“%d\n”, a);

      }

      for (a=5; a>0; a - -)

      {

                    printf(“%d\n”, a);

      }

       

       

       

       

      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 a

      for (a=5; a>0; a - -)

      {

                    printf(“%d\n”, a);

      }

      for (a=5; a>1; a - -)

      {

                    printf(“%d\n”, a);

      }

      for (a=5; a>0; a - -)

      {

                    printf(“%d\n”, a);

      }

      for (a=5; a>2; a - -)

      {

                    printf(“%d\n”, a);

      }

      for (a=5; a>3; a - -)

      {

                    printf(“%d\n”, a);

      }

      for (a=5; a>4; a - -)

      {

                    printf(“%d\n”, a);

      }

       

      return 0;

      }

       

       

       

      ADDITIONAL QUESTIONS:

      1. What is an infinite loop?

      Infinite loop is the loop which never ends.

       

      2. Name different types of loops.

      Different types of loops are:

      i) for loop

      ii) while loop

      iii) do-while loop

       

      3. What is the different between while loop and do-while loop?

      The different between while loop and do-while loop is that-

      While loop: in this loop statement(s) will execute repeatedly as long as the condition is true. The statement can be simple or compound. Whereas,

      Do-while loop: In this loop the statement(s) will execute at least one time even if the condition is true. The statement can be either simple or compound.

       

      4. What happens to a while loop when the condition is evaluate as false?

      As the condition evaluate to false in while loop, the control comes outside of the loop and execute any below statements.

       

      5. How can we make our loop dynamic where the loop will run the number of times the user wants?

      We can use a variable to make our loop dynamic where the loop will run the number of time the user wants.

       

      6. Name the loop that can be used to execute a group of statements as long as the user wants?

      Do-while loop

       

      7. Why do we use semicolon in an expression?

      We used semicolon (;) in an expression to terminate the statements.

       

      8. Which expression is executed before entering the for loop?

      The initialization expression is executed before entering the for loop.

       

      9. Can we use more than one statement in an expression of for loop?

      Yes, we use more than one statement in an expression of for loop.

       

      10. How can we check the divisibility of a number in programing?

      We can check the divisibility of a number in programing using modulus (%) operation and double equal operation (==).

       

      11. What is the use of break statements?

      Break statement is used to come out of loop before its specific time.

       

      12. Write a C program to check whether a number is prime or not.

       

      #include<stdio.h>

      int main()

      {

       int no, I, flag=0;

      printf(“Enter a number:\n”);

      scanf(“%d”, &n);

      for(i=2;i<=no/2;i++)

      {

         if (no%2==0)

          {

       flag =1;

       break;

         }

      }

      if (flag ==0 && no !=1)

         printf(“%d is a prime number”, no);

      else

      printf(“%d is not a  prime number”,no);

      return 0:

      )

       

      13. Write a C program to display the capital letters of English alphabet using loop.

      #include<stdio.h>

      int main()

      {

       int i;

      for(i=0; i<26; i++)

        {

       Printf(“%c”, i+65);

        }

      Return 0;

      }

       

      14. Write a C program to display the even numbers form 1 to 100.

      #include<stdio.h>

      int main()

      {

      int i;

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

        {

       if (i%2==0)

        {

       Printf(“%d”,i);

        }

      }

      Return 0;

      }

      Table of Content