Monday, 4 February 2013

Loop - class 9

Points to remember :
1. break statement is used to break the loop.
2. continue sends the control to the update expression.
3. for loop is used for fixed iterations.That is where you know the final limit.
4. In while loop, condition is tested before execution.
5. The do while loop executes the statements atleast once even if the condition is false.
6. Infinite loop : control does not reach the test condition.
7. Empty loop : loop does not contain any statement in the body.


1. What will be the output for the following programs:
i)
            for(a=0;a<30;a++)
           {
                   if(a==5)
                   break;
                   System.out.print(+a);
          }

ii)
      for(x=1;x<=5;x++)
      {
           if(x==2)
          continue;
          if(x==4)
          continue;
System.out.println(x);
      }
iv)
        int x=0;
       while(x<=10)
        {
               x++;
             if(x==4)
              break;
             System.out.println(x);
        }
2. Differentiate : while and do while.
3.Write a program to enter a number and print the sum of odd numbers and even numbers for the first n natural numbers.                                                                     [ICSE]
4.
      i)         12+22+32+42+…………+n2
      ii)     x2/2+x3/3+………xn/n

1. Fibonacci series:
 0,1,1,2,3,5,8.............................
2. Prime number:
    The divisors are one and the number itself.
    e.g : 5 : divisors 1 and 5
3. Perfect number :
     The sum of the divisors(excluding the number) equal to the number itself.
    e.g : 6
       divisors are 1+2+3=6
4. Armstrong number:
    e.g :
         153=13+53+33
               =1+125+27
               =153
5. Palindrome number:
  e.g:
        272: reverse the number 272.