The Programming Project

Friday, October 1, 2021

ICSE JAVA PROGRAMS 2019: SECTION B QUESTION 8 SOLVED

 

ICSE JAVA PROGRAMS 2019: SECTION B QUESTION 8

Programming for school students ( upper to middle/secondary school)

Write a program to input s sentence and convert it into uppercase and display the total number of words starting with letter 'A'.

Example:

INPUT: Advancement and application of Information Technology are ever changing.

OUTPUT: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER CHANGING.

Number of word(s)starting with A is/are: 4




JAVA CODE

import java.util.Scanner;
public class ISCJava{
       public static void main(String[] args) {
       String sentence;
       int counterStartingA = 0;
       Scanner in = new Scanner(System.in);
       System.out.println("Enter your sentence:");
       sentence = in.nextLine();
       System.out.println();
       System.out.println("OUTPUT:");
       System.out.println("ORIGINAL STATEMENT:");
       System.out.println(sentence);
       System.out.println();
       System.out.println("AFTER CHANGING TO UPPER CASE:");
       for(int i =0isentence.length();i++) {
              char c = sentence.charAt(i);
              if(c >= 'a' && c <= 'z') {
                     c -= 32;
                     System.out.print(c);
              }
              else
              System.out.print(c);   
       }
       //counting number of words staring with A
       System.out.println();
       for(int i =0isentence.length(); i++) 
              if (sentence.charAt(i) == 'A' || sentence.charAt(i) == 'a') {
                     if(i==0)
                            counterStartingA++; 
                     else if(sentence.charAt(i-1) == ' ')
                            counterStartingA++;
              }
       System.out.println("Number of word(s)starting with A is/are: "+counterStartingA);
       in.close();
       }
}


PYTHON CODE

counterStartingA = 0
sentence = str(input("Enter the sentence: "))
print("OUTPUT:")
print("ORIGINAL STATEMENT:")
print(sentence)
print("AFTER CHANGING TO UPPER CASE:")
for i in range(len(sentence)):
    c = (sentence[i])
    if (c >= 'a' and c <= 'z'):
        t = ord(c) -32
        print(chr(t),end="")
    else:
        print(c,end="")
for i in range(len(sentence)):
    if sentence[i] == 'A' or sentence[i] == 'a':
        if i == 0:
            counterStartingA +=1
        elif sentence[i-1] == ' ':
            counterStartingA +=1
print()
print("Number of word(s)starting with A is/are:",counterStartingA)

ICSE JAVA PROGRAMS 2019: SECTION B QUESTION 7 SOLVED

ICSE JAVA PROGRAMS 2019: SECTION B QUESTION 7

Programming for school students ( upper to middle/secondary school)

Design a class to overload a function series() as follows:

(a) void series (int x, int n) - To display the sum of the series given below:

x+x2+x3+........+xn

(b) void series (int p) - To display the following series:

0, 7, 26, 63, ........ upto p terms

(c) void series () - To display the sum of the series given below

&frac12 + 1⁄3 +&frac14 +.......+1⁄10


import java.util.Scanner;
public class ISCJava{
       public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       Series obj = new Series();
       int choice;
       System.out.println("Enter your choice(1/2/3):");
       choice = in.nextInt();
       switch(choice){
              case 1:
              int xn;
              System.out.println("Enter the value of x:");
              x = in.nextInt();
              System.out.println("Enter the value of n:");
              n = in.nextInt();
              obj.series(x,n);
              break;
              case 2:
              int p;
              System.out.println("Enter the value of p:");
              p = in.nextInt();
              obj.series(p);
              break;
              case 3:
              obj.series();
              break;
              default:
              System.out.println("Wrong Choice:");
       }
       in.close();
       }
}
class Series {
       public void series(int xint n) {
              for (int i = 1; i<= n ;i++)
                     sumOfSeries += Math.pow((double)x,(double)i);
              System.out.println("Sum of the series is:"+sumOfSeries);
       }
       public void series(int p) {
              System.out.println("The series is");
              for(int i = 1; i<= p; i++)
                     if (i != p)
                            System.out.print((int)Math.pow(i, 3)-1+",");
                     else
                            System.out.println((int)Math.pow(i, 3)-1);       
       }
       public void series() {
              for (int i = 2; i<= 10 ;i++)
                     sumOfSeries += (1.0/i);
              System.out.println("Sum of the series is:"+sumOfSeries);
       }
       Series() {
              this.sumOfSeries = 0.0;
       }
       private double sumOfSeries;
}
       
PYTHON CODE
Note: Function overloading is not available in Python.

class Series:
    def seriesOne(self,x,n):
        for i in range(n):
            i +=1
            self.__sumOfSeries += x**i
        print("Sum of the series is:",self.__sumOfSeries)
    def seriesTwo(self,p):
        for i in range(p):
            i +=1
            if i<p:
                print(i**3-1end =",")
            else:
                print(i**3-1end =" ")
    def seriesThree(self):
        for i in range(9):
            i +=2
            self.__sumOfSeries += (1.0/(i+1))
        print("Sum of the series is:",self.__sumOfSeries)
    def __init__(self):
       self.__sumOfSeries = 0
    __sumOfSeries = float(0)
# main program    
obj = Series()
try:
    N = int(input("Enter your choice(1/2/3):"))
    if N > 3:
        print("Your choice in invalid")   
    elif N==1:
        x = int(input("Enter the value of x:"))
        n = int(input("Enter the value of n:"))
        obj.seriesOne(x,n)
    elif N==2:
        p = int(input("Enter the value of p:"))
        obj.seriesTwo(p
    else:
        obj.seriesThree()    
except ValueError as e:
    print("Your choice is invalid")

Thursday, September 30, 2021

Python program to enter 10 numbers in list and how many of them are ending with 5: Python

PYTHON PROGRASM FOR BEGINNERS

SCHOOL LEVEL PROGRAMMING

Write a Python program to enter 10 numbers in list and how many of them are ending with 5. Display all the numbers ending with 5 and the total of such numbers. If there are no numbers ending with 5 display "NIL".

Both Java and Python codes are given below.

In this program you will learn:

i. Taking input

ii. Adding numbers to list using append() method

iii. for loop

iv. using boolean variables

v. using if statements

PYTHON CODE

#Python program to enter 10 numbers in list and how many of them are ending with 5

numberList = []
for i in range(10):
    print("Enter the element at the position: ",i+1)
    numberList.append(int(input("Enter the value:")))
counter = 0
flagEndingFive = False
print("Numbers ending with 5 is/are:")
for i in numberList:
    if i%10 == 5:
        counter +=1
        flagEndingFive = True
        print(i)
if flagEndingFive == False:
    print("NIL")
print("Total number of numbers ending with 5 = ",counter)


JAVA CODE

import java.util.Scanner;
public class NumbersEndingFive{
       public static void main(String[] args) {
        int[] numberList;
        boolean flagEndingFive = false;
        int counter = 0;
        Scanner in = new Scanner(System.in);
        numberList = new int[10];
        for(int i = 0i10i++){
               System.out.println("Enter the element at the position:"+(i+1));
               numberList[i] = in.nextInt();
       }
       System.out.println("Numbers ending with 5 is/are:");
       for(int i = 0i10i++) 
              if(numberList[i]%10 == 5) {
                     flagEndingFive = true;
                     counter++;
                     System.out.println(numberList[i]);
              }
       if(!flagEndingFive)
              System.out.println("NIL");
       System.out.println("Total number of numbers ending with 5 = "+counter);
       in.close();
       }
}

ISC COMPUTER SCIENCE PRACTICAL SPECIMEN PAPER 2021: QUESTION 3 MOBIUS FUNCTION

Question 3
A MOBIUS function M(N) returns the value -1 or 0 or 1 for a natural number (N) by the following conditions are defined :
When,
M ( N ) = 1 if N=1.
M ( N ) = 0 if any prime factor of N is contained more than once.
M ( N ) = ( -1 )P if N is the product of ‘P’ distinct prime factors.

Write a program to accept a positive natural number (N) and display the MOBIUS result with proper message. Design your program which will enable the output in the format given below:

Sample 1
INPUT:78
OUTPUT: 78 = 2 x 3 x 13
NUMBER OF DISTINCT PRIME FACTORS = 3
M(78) = -1

Sample 2
INPUT:34
OUTPUT: 34 = 2 x 17
NUMBER OF DISTINCT PRIME FACTORS = 2
M(34) = 1

Sample 3
INPUT:12
OUTPUT: 12 = 2 x 2 x 3
DUPLICATE PRIME FACTORS
M(12) = 0

Sample 4
INPUT:1
OUTPUT: 1 = 1
NO PRIME FACTORS
M(1) = 1

ISC COMPUTER SCIENCE PRACTICAL SPECIMEN PAPER 2021

ENTER A POSITIVE INTEGER: