Page 179 - CA_Blue( J )_Class10
P. 179

System.out.print("Enter a number : ");
                                  no = sc.nextInt();
                                  if (no % 4 == 0)
                                  {  if (no % 10 == 4)
                                          System.out.println(no + " ends with the digit 4");
                                      if (no % 10 == 8)
                                          c++;
                                  }
                              }
                               System.out.println("Count of numbers ending with 8: " + c);
                          }
                      }
                    23.  Write a program to input a number and print whether the number is a palindrome number or not. A palindrome number is a
                       number that is equal to the reverse of the number.
                        Ex. 121, 56765, 151
                   Ans. import java.util.*;
                      class palindrome
                      {
                          public static void main() {
                               Scanner sc = new Scanner(System.in);
                              int n,r,t,rev=0;
                               System.out.println("Enter a number : ");
                              n=sc.nextInt();
                              t=n;
                              while(t>0)
                              {    r=t%10;
                                   rev=rev*10+r;
                                   t=t/10;
                              }
                              if(n==rev)
                                        System.out.println(n + " is palindrome Number.");
                              else
                                        System.out.println(n + " is not palindrome Number.");
                          }
                      }
                    24.  Write a program to input 20 numbers and print all the Armstrong numbers. An Armstrong number is a number that is equal to
                       the sum of cubes of its digits. 1, 153, 370, 371, 407 are some examples.
                   Ans.  import java.util.*;
                      class armstrong
                      {
                          public static void main ()
                          {
                              Scanner sc = new Scanner (System.in);
                              int n,r,t,i,sum;
                              for (i=1; i<=20; i++)
                              {
                                  System.out.println("Enter a number: ");
                                  n=sc.nextInt();
                                  t=n;
                                  sum=0;
                                  while(t>0)
                                  {
                                      r=t%10;
                                      sum=sum+r*r*r;
                                      t=t/10;
                                  }
                                  if(n==sum)
                                      System.out.println(n + "is Armstrong Number.");
                              }


                                                                                                                       177
                                                                                              Iterative constructs in Java  177
   174   175   176   177   178   179   180   181   182   183   184