Page 573 - ComputerScience_Class_11
P. 573

57                      System.out.println(n + " is not a Smith Number.");
                   58              }
                   59              else

                   60                  System.out.println(n + " is not a Smith Number.");
                   61          }    //end of main
                   62      }     //end of class



                 The output of the preceding program is as follows:

                       BlueJ: Terminal Window - Java
                   Options

                  Checking whether a number is a Smith Number or not
                  Enter a number = 666
                  666 is a Smith Number.


                   Program 6     Write a program to input a single-dimensional array and print the sum of its odd digits at
                                 even positions.

                   1       import java.util.*;
                   2       class odd

                   3       {          //start of class
                   4           public static void main(String args[])
                   5           {         //start of main

                   6               int n,s=0;
                   7               Scanner sc=new Scanner(System.in);

                   8               System.out.println("enter the size of the array");
                   9               n=sc.nextInt();
                   10              int a[]=new int[n];
                   11              System.out.println("enter the array elements");

                   12              for(int i=0;i<n;i++)
                   13              {

                   14                  a[i]=sc.nextInt();
                   15              }
                   16              for(int i=0;i<n;i=i+2)//taking only the even positions
                   17              {

                   18                  if(a[i]%2!=0)//checking if the digit at that position is odd
                   19                  {

                   20                      s=s+a[i];
                   21                  }




                                                                                                Internal Assessment  571
   568   569   570   571   572   573   574   575   576   577   578