Page 181 - CA_Blue( J )_Class10
P. 181

28.  Write a program to print 1st n Fibonacci numbers.
                        Example: say n = 10;
                        Result: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
                   Ans. import java.util.*;
                      class fibonacci
                      {
                          public static void main () {
                               Scanner sc = new Scanner (System.in);
                               int i, a = 0, b = 1, c, n;
                               System.out.println("Enter the number of terms :" );
                              n = sc.nextInt();
                               System.out.println("The Fibonacci : ");
                              if(n==1)
                                  System.out.print(a);
                              else
                              if(n==2)
                                   System.out.print(a + " , " + b);
                              else
                             {    System.out.print(a + " , " + b);
                             for(i=3;i<=n;i++)
                             {   c=a+b;
                                  System.out.print(" , " + c);
                                 a=b;
                                 b=c;
                             }
                           }
                         }
                      }
                    29.  Write a program to input n numbers and print whether the number is a prime number.
                   Ans. import java.util.*;
                      class series4
                      {
                          public static void main () {
                               Scanner sc = new Scanner (System.in);
                               int i, c=0, p=1, n;
                               System.out.println("Enter a number :");
                               n=sc.nextInt();
                               for(i=1;i<=n;i++){
                               if(n%i==0)
                               {   c++;   }
                               }
                               if(c==2)
                                   System.out.println(n+ "Prime Number");
                               else
                                   System.out.println(n+ "Not Prime Number");
                               }
                      }
                    30.  Write a program to input a number and check whether it is a Neon number or not. If a number where the sum of digits of
                       square of the number is equal to the number, then it is called a Neon number. Example: 9 = 9 * 9 = 81 = 8 + 1 = 9
                   Ans. import java.util.*;
                       class series5
                      {
                          public static void main ()
                          {
                              Scanner sc = new Scanner (System.in);
                              int s = 0, r, n, t;
                              System.out.println("Enter a number:");
                              n=sc.nextInt();


                                                                                                                       179
                                                                                              Iterative constructs in Java  179
   176   177   178   179   180   181   182   183   184   185   186