The Programming Project: SCHOOL
Showing posts with label SCHOOL. Show all posts
Showing posts with label SCHOOL. Show all posts

Saturday, October 2, 2021

ICSE JAVA PROGRAMS 2019: SECTION B QUESTION 9 SOLVED

 

ICSE JAVA PROGRAMS 2019: SECTION B QUESTION 9

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

A tech number has even number of digits. If the number is split in two equal halves, then the square of the sum of these halves is equal to the number itself. Write a program to generate and print all four digits tech numbers.



JAVA CODE

public class ISCJava{
       public static void main(String[] args) {
       final int j = 2;
       int counter;
       int lastTwoDigit;
       int firstTwoDigit;
       int temp;
       forint i = 1000 ; i < 10000 ; i++) { // looping through all 4-digits
              counter = 0;
              lastTwoDigit = 0;
              firstTwoDigit = 0;
              temp = i;
              // extracting the last two digit
              while ( counter < j) {
                     lastTwoDigit += (temp%10)*(int)Math.pow(10,counter);
                     counter++;
                     temp /= 10;
                     }
              // extracting the first two digit
              counter = 0;
              while ( counter < j) {
                     firstTwoDigit += (temp%10)*(int)Math.pow(10,counter);
                     counter++;
                     temp /= 10;
                     }
              ifi ==  (int)Math.pow(firstTwoDigit+lastTwoDigit,2))
                     System.out.println(i+" is a tech number.");
              }
       }
}

PYHTON CODE
#PROGRAM TO PRINT ALL 4 DIGITS TECH NUMBER
j = 2
for i in range(9000): #total number of 4 digits number are 9000
    counter = 0
    lastTwoDigit = 0
    firstTwoDigit = 0
    temp = i+1000
    numb = temp
    #extracting the last two digit
    while counter < j:
        lastTwoDigit += int(temp%10*int((10**counter)))
        temp /= 10
        temp = int(temp)
        counter +=1
    #extracting the last two digit
    counter = 0
    while counter < j:
        firstTwoDigit += int(temp%10*int((10**counter)))
        temp /= 10
        temp = int(temp)
        counter +=1
    #print(firstTwoDigit,"last",lastTwoDigit,numb)
    if numb == int((firstTwoDigit+lastTwoDigit)**2):
        print(numb," is a tech number.")

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)

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();
       }
}

Friday, September 24, 2021

COMPOUND INTEREST CACULATOR : PYTHON PROGRAMMING


If the principal is P and the rate of compound interest is r% per annum and the interest is compounded k times in a year, then the amount after n years is given by the formula


If k = 1, the interest is payable yearly, for k =2 the interest is payable half-yearly and for k=4 the interest is payable quarterly. Below is the program which uses above formula to calculate the final amount.

PYTHON CODE



principal = float(input("Enter the principal value:"))
rateOfInterest = float(input("Enter the rate of interest:"))
numberOfMonths = int((input("Enter the period in months:")))
frequency =int(input("Enter the number of times the the interest is compunded (yearly=1, half-yearly=2, quarterly =4,..):"))
finalAmount = 0.0
finalAmount += principal*(1+(rateOfInterest/frequency)/100)**(frequency*(numberOfMonths/12))
print("The final amount is"round(finalAmount,2))