ICSE Computer Science Java Programs | IT Developer
IT Developer

Java Programs - Solved 2020 ICSE Computer Science Paper



Share with a Friend

Solved 2020 ICSE Computer Science Paper

Class 10 - ICSE Computer Science Solved Papers

Class Overload Program - ICSE 2020 Computer Science

Design a class to overload a function sum() as follows:
(i) int sum(int a, int b) to calculate and return the sum of all the even numbers in the range a to b.
Sample Input:
a = 14
b = 16
Sample Output:
Sum = 30
(ii) double sum(double n) to calculate and return the product of the following series:
Product = 1.0 × 1.2 × 1.4 × … × N
(iii) int sum(int n) to calculate and return the sum of only odd digits of the number N.
Sample Input:
N = 43961
Sample Output:
Sum = 13
Write the main() method to create an object and invoke the above methods.

import java.io.*; class Overload{ public int sum(int a, int b){ int s = 0; for(int i = a; i <= b; i++){ if(i % 2 == 0) s += i; } return s; } public double sum(double n){ double p = 1.0; for(double i = 1.0; i <= n; i += 0.2) p *= i; return p; } public int sum(int n){ int s = 0; for(int i = n; i != 0; i /= 10){ if((i % 10) % 2 == 1) s += i % 10; } return s; } public static void main(String args[])throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Overload obj = new Overload(); System.out.print("a = "); int a = Integer.parseInt(br.readLine()); System.out.print("b = "); int b = Integer.parseInt(br.readLine()); int result = obj.sum(a, b); System.out.println("Sum of the range: " + result); System.out.print("n = "); double n = Double.parseDouble(br.readLine()); double product = obj.sum(n); System.out.println("Product: " + product); System.out.print("n = "); int num = Integer.parseInt(br.readLine()); result = obj.sum(num); System.out.println("Sum of odd digits: " + result); } }

Output

 
 OUTPUT : 
a = 14
b = 16
Sum of the range: 30
n = 5
Product: 2.710780430399311E8
n = 43961
Sum of odd digits: 13