Guest User

ICSE Specimen Paper 2020 - Question 6.

a guest
Oct 29th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. /**
  2.  * Question 6 of the ICSE Specimen Paper 2020.
  3.  * Specimen Paper: http://www.cisce.org/pdf/ICSE-Class-X-Specimen-Question-Papers-2020/Computer%20Applications_Specimen_2020.pdf
  4.  */
  5. public class Question_6
  6. {
  7.     int Sum(int A, int B)
  8.     {
  9.         int i;
  10.         int sum = 0;
  11.         for(i=A;i<=B;i++)
  12.         {
  13.             if(i%2 == 0)
  14.                 sum += i;
  15.         }
  16.        
  17.         return sum;
  18.     }
  19.    
  20.     double Sum(double N)
  21.     {
  22.         double i = 1.0;
  23.         double prod = 1;
  24.         while(i<=N)
  25.         {
  26.             prod *= i;
  27.             i += 0.2;
  28.         }
  29.        
  30.         return prod;
  31.     }
  32.    
  33.     int Sum(int N)
  34.     {
  35.         String num = Integer.toString(N);
  36.         int sum = 0;
  37.         int i, digit;
  38.         char x;
  39.         for(i=0;i<num.length();i++)
  40.         {
  41.             x = num.charAt(i);
  42.             digit = Character.getNumericValue(x);
  43.             if(digit % 2 != 0)
  44.             {
  45.                 sum += digit;
  46.             }
  47.         }
  48.        
  49.         return sum;
  50.     }
  51.    
  52.     public static void main(String[] args)
  53.     {
  54.         Question_6 s1 = new Question_6(); //Creating an object to invoke the functions.
  55.        
  56.         int a = s1.Sum(4, 16); //Expected Output - 4 + 6 + 8 + 10 + 12 + 14 + 16 = 70.
  57.         System.out.println(a);
  58.        
  59.         double b = s1.Sum(2.0); //Expected Output - 1.0 x 1.2 x 1.4 x 1.6 x 1.8 x 2.0 = 9.6768.
  60.         System.out.println(b);
  61.        
  62.         int c = s1.Sum(43961); //Expected Output - 3 + 9 + 1 = 13.
  63.         System.out.println(c);
  64.     }
  65. }
Add Comment
Please, Sign In to add comment