Page 178 - CA_Blue( J )_Class10
P. 178

19.  What is an infinite loop? Using a while loop, give an example.
                Ans.  A loop that executes a set of instructions endlessly due to wrong logic in test condition is called an infinite loop. Example:
                    int i;
                    i=2;
                    while(i>=1)
                    {
                          System.out.println(i);
                          i++;
                    }
                 20.  Define null loop.
               Ans.  If code is missing in the body of the loop, then it is known as a null loop.
                    Example:
                    int i=5;
                    while(i>=1)
                    {
                    }
                 21.  Write a program to input any 10 numbers (including positive and negative).
                     Perform the following tasks:
                    (a) Sum of the positive and negative numbers
                    (b) Count the number of odd numbers and even numbers
                Ans.  import java.util.*;
                    class iterative1
                    {
                        public static void main()
                        {
                             Scanner sc = new Scanner(System.in);
                             int pos_sum = 0, odd_c = 0, neg_sum = 0, even_c = 0,n,i;
                             System.out.println("Enter 10 numbers");
                            for (i = 1; i <= 10; i++)
                            {   n = sc.nextInt();
                                if (n >= 0)
                                {    pos_sum += n;    }
                                else
                                {    neg_sum += n;    }
                                if(n%2==0)
                                {    even_c++;    }
                                else
                                {    odd_c++;    }
                           }
                            System.out.println("Positive Sum = " + pos_sum);
                            System.out.println("Negative Sum = " + neg_sum);
                            System.out.println("Even Count = " + even_c);
                            System.out.println("Odd Count = " + odd_c);
                        }
                    }
                 22.  Write a program to enter "n" numbers and check whether they are divisible by 4 or not. If divisible then perform the following
                    tasks:
                    (a) Display all the numbers ending with the digit 4.   (b)  Count those numbers ending with 8.

                Ans.  import java.util.*;
                    class iterative2
                    {
                        public static void main() {
                             Scanner sc = new Scanner(System.in);
                            int n,no, c = 0,i;
                             System.out.println("Enter a number : ");
                            n=sc.nextInt();
                            for (i = 1; i <= n; i++)
                            {

                176176  Touchpad Computer Applications-X
   173   174   175   176   177   178   179   180   181   182   183