Remove repeated words


write a program in java to remove the repeated strings(words) from the input sentence.

import java.io.*;
class repeatwords
{
     public static void main(String args[]) throws IOException
    {
     InputStreamReader read = new InputStreamReader(System.in);
    BufferedReader x=new BufferedReader (read);
    System.out.println("enter any word");
    String s=x.readLine();
    s=s+" ";
    int l=s.length();
    String ans="";
    char ch1,ch2;
    for(int i=0;i<l-1;i++)
    {
        ch1=s.charAt(i);
        ch2=s.charAt(i+1);
        if(ch1!=ch2)
        {
            ans=ans+ch1;
        }
    }
    System.out.println("word after removing repeated characters="+ans);
}
}

Output:

enter any word
javaaaaaa
word after removing repeated characters=java

Time in words


write a program to convert time in numbers to time in words.

import java.io.*;
public class timeinwords
{
    
     public static void main(String args[]) throws IOException
    {
     InputStreamReader read = new InputStreamReader(System.in);
    BufferedReader x=new BufferedReader (read);
     System.out.println("enter hours");
     int h=Integer.parseInt(x.readLine());
      System.out.println("enter minutes");
     int m=Integer.parseInt(x.readLine());
     if((h>=1&&h<=12)&&(m>=0&&m<=59))
     {
         String word[]={"","one ","two ","three ","four ","five ","six ","seven ","eight ","nine ","ten "," eleven "," twelwe "," thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","twenty one","twenty two","twenty three","twenty four","twenty five","twenty six","twenty seven","twenty eight","twenty nine"};
         String plu,a;
         if(m==1||m==59)
         plu="minute";
         else
         plu="minutes";
         if(h==12)
         a=word[1];
         else
         a=word[h+1];
          System.out.print("output: "\n+h+":"+m+"-----");
          if(m==0)
            System.out.println(word[h]+"o'clock");
            else  if(m==15)
            System.out.println("quarter past"+word[h]);
     else  if(m==30)
            System.out.println("half past"+word[h]);
            else  if(m==45)
            System.out.println("quarter to"+a);
            else  if(m<30)
            System.out.println(word[m]+" "+plu+" past+words[h] ");
            else
            System.out.println(word[60-m]+" "+plu+" to "+a);
        }
        else 
        System.out.println("invalid input!");
    }
}

output:

enter hours
10
enter minutes
55
output:
10:55—–five minutes to eleven

Prime Triplets


import java.util.*;
class primeTriplets
{ 
    boolean isPrime(int n) //funton for checking prime
        {
            int count=0;
            for(int i=1; i<=n; i++)
                {
                    if(n%i == 0)
                        count++;
                }
            if(count == 2)
                return true;
             else
                return false;
        }
        
public static void main(String args[]) 
        {
            Scanner sc=new Scanner(System.in);
            System.out.println("Enter the lower and upper range:");
            int m=sc.nextInt();
            int n=sc.nextInt();
            primeTriplets pt=new primeTriplets();
            int prime=0;
            //pt.get();
            if(m>0 && n> 0  && m<n)
            {
                System.out.println("Prime Triplets:");
                for(int i=m;i<n;i++)
                {
                    if(pt.isPrime(i)==true && pt.isPrime(i+2)==true && pt.isPrime(i+6))
                    {System.out.print(i+" " + (i+2) +" " +(i+6)+"\n");
                    prime++;
                }
                    else if(pt.isPrime(i)==true && pt.isPrime(i+4)==true && pt.isPrime(i+6))
                  {  System.out.print(i+" " + (i+4) +" " +(i+6)+"\n");
                      prime++;
                    }
                    
                    else
                    {
                    }
                }
                
            }
            else
            System.out.println("invalid");
            System.out.println("Total prime triplets are:"+prime);
        }
}

Output:

Enter the lower and upper range:
2
17
Prime Triplets:
5 7 11
7 11 13
11 13 17
13 17 19
Total prime triplets are :  4

Decimal to Binary conversion (without array)


Write a program to convert decimal into binary without using array and display the number of 1’sand 0’s.

import java.util.Scanner;
class bintoDec
{
public static void main(String args[])
{
int n, a, count0 = 0, count1=0;
String s = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter decimal number ");
n = sc.nextInt();
while(n>0)
{
a = n%2;
if(a == 1)
{
count1++;
}
else
count0++;
s = s+" "+a;
n = n/2;

}
System.out.println("Binary number: "+s);
System.out.println("No. of 1s: "+count1);
System.out.println("No. of 0 s: "+count0);
}
}

Output:

Enter decimal number
2
Binary number: 0 1
No. of 1s: 1
No. of 0 s: 1
Enter decimal number
6
Binary number: 0 1 1
No. of 1s: 2
No. of 0 s: 1
Enter decimal number
16
Binary number: 0 0 0 0 1
No. of 1s: 1
No. of 0 s: 4
Enter decimal number

Decimal to Binary conversion (using array)


Conversion of decimal to binary using array

import java.io.*;
import java.util.*;

class binaryArray
{
// function to convert decimal to binary
static void decToBinary(int n)
{
// array to store binary number
int[] binaryNum = new int[1000];

// counter for binary array
int i = 0;
while (n > 0)
{
// storing remainder in binary array
binaryNum[i] = n % 2;
n = n / 2;
i++;
}

// printing binary array in reverse order
for (int j = i - 1; j >= 0; j--)
System.out.print(binaryNum[j]);
}

// driver program
public static void main (String[] args)
{ Scanner sc=new Scanner(System.in);
int n ;
System.out.println("Enter a decimal value:");
n=sc.nextInt();
decToBinary(n);
}
}

Output:

Enter a decimal value:
7
111

Evil Number/Odious Number in java


An evil number is a non-negative number that has an even number of 1s in its binary expansion. (Binary Expansion – is representation of a number in the binary numeral system or base-2 numeral system which represents numeric values using two different symbols: typically 0 (zero) and 1 (one)).

Odious Numbers: Numbers that are not Evil are called Odious Numbers. Given a number, the task is to check if it is Evil Number or Odious Numbers.

import java.util.*;
class EvilNumber 
{
  String toBinary(int n) // Function to convert a number to Binary
  {
   int r;
   String s=""; //variable for storing the result
   char dig[]={'0','1'}; //array storing the digits (as characters) in a binary number system
   while(n>0)
   {
       r=n%2; //finding remainder by dividing the number by 2
       s=dig[r]+s; //adding the remainder to the result and reversing at the same time
       n=n/2;
    }
    return s;
  }


  int countOne(String s) // Function to count no of 1’s in binary number
  {   
      int c = 0, l = s.length();
      char ch;
      for(int i=0; i<l; i++)
      {
          ch=s.charAt(i);
          if(ch=='1')
          {
              c++;
          }

      }
      return c;
  }
  public static void main(String args[])
  {
      EvilNumber ob = new EvilNumber();
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter a positive number : ");
      int n = sc.nextInt();
      String bin = ob.toBinary(n);
      System.out.println("Binary Equivalent = "+bin);
      int x = ob.countOne(bin);
      System.out.println("Number of Ones = "+x);
      if(x%2==0)
        System.out.println(n+" is an Evil Number.");
      else
        System.out.println(n+" is  an Odious Number.");
  }
}

Output:

Enter a positive number : 23
Binary Equivalent = 10111
Number of Ones = 4
23 is an Evil Number.


Enter a positive number : 21
Binary Equivalent = 10101
Number of Ones = 3
21 is an Odious Number.

Smith number


A Smith Number is a composite number whose sum of digits is equal to the sum of digits of its prime factors obtained as a result of prime factorization (excluding 1). The first few such numbers are 4, 22, 27, 58, 85, 94, 121 ………………..

Examples:

1. Enter a Number : 85
Sum of Digit = 13
Sum of Prime Factor = 13
It is a Smith Number

2. Enter a Number : 666
Sum of Digit = 18
Sum of Prime Factor = 18
It is a Smith Number

3. 999

Enter a Number : 999
Sum of Digit = 27
Sum of Prime Factor = 19
It is Not a Smith Number

Write a program to input a number and display whether the number is a Smith number or not.

import java.util.*;
public class SmithNumbers
{

//Extracting digits and find the sum of the digits
int sumDigit(int n)
{
int s=0;
while(n>0)
{
s=s+n%10;
n=n/10;
}
return s;
}

int sumPrimeFact(int n)
{
int i=2, sum=0;
while(n>1)
{
if(n%i==0)
{
sum=sum+sumDigit(i);
n=n/i;
}
else
{
do{
i++;
}while(!IsPrime(i));
}
}
return sum;
}
boolean IsPrime(int k)
{
boolean b=true;
int d=2;
while(d<Math.sqrt(k))
{
if(k%d==0)
{
b=false;
}
d++;
}
return b;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
SmithNumbers ob=new SmithNumbers();
System.out.print(“Enter a Number : “);
int n=sc.nextInt();
int a=ob.sumDigit(n);
int b=ob.sumPrimeFact(n);
System.out.println(“Sum of Digit = “+a);
System.out.println(“Sum of Prime Factor = “+b);
if(a==b)
System.out.print(“It is a Smith Number”);
else
System.out.print(“It is Not a Smith Number”);
}
}

Conversion of octal to decimal number


import java.util.*;
public class Octal2Decimal{

//method definition to convert octal numbers into decimal

public static int getDecimal(int octal){
int decimal = 0;
//Declaring variable to use in power
int ct = 0;
while(true){
if(octal == 0){
break;
} else {
int temp = octal % 10;
decimal += temp*Math.pow(8, ct); //
octal = octal/10;
ct++;
}
}
return decimal;
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println(“Enter an octal number:”);
int num=sc.nextInt();
Octal2Decimal od=new Octal2Decimal();

System.out.println(“Decimal of given octal is: “+ od.getDecimal(num));
}
}

ICSE-Computer Applications- October 2018


                                  Section A (40 Marks)
                                Answer all questions.

Question 1                                                                                                                              [10]

  1. Declare a single dimensional array of 10 integer values.
  2. Convert the following while loop to the corresponding for loop.
    int m=5, n=10;

while(n>=1)   {
System.out.println(m*n);
n–;
}

  1. Convert the following segment into an equivalent do loop:

int x,c;

for(x=10, c=20;c>=20;c=c-2)

x++:

  1. What is the final value of ctr when the iteration process given below, executes?

int ctr=0;
for(int j=1;j<=5;j+=2)
++ctr;

  1. If, array[]={1,4,5,6,3,32,23,90}; i) What is array.length?  ii) What is array[10]?

Question 2                                                                                                                                [10]

  1. Which keyword used to acquire the properties of one class to another class?
  2. Here is a skeleton definition of a class
    class Sample {
    int i;
    char c;
    float f;
    } Implement the constructor.
  3. Write a program to check whether the given number is palindrome or not.
  4. Differentiate between public and private modifiers for members of a class.
  5. What type of inheritance does Java here?

Question 3                                                                                                                                [10]

  1. Write a program to print Fibonacci series i.e., 0 1 1 2 3 5 8…..The number of terms required, is to be passed as parameter.
  2. What will be the result stored in x after evaluating the following expression?
    int x =42;
    x+ = (x++ ) + (++x) +x;
  3. What will be the output of the following code?

int m=3;

int n=15;

for(int i=1;i<5;i++)

m++;

–n;

System.out.println(“m=”+m);

System.out.println(“n=”+n);

  1. Analyze the following program segment and determine how many times the loop will be executed and what will be the output of the program segment?
    int p=200;

while(true)

{

if(p<100)

break;

p=p-40;

}

System.out.println(p);

  1. The following is a segment of a program

x=1; y=1;
if(n>0)  {

x=x+1;
y=y+1;}

What will be the value of x and y, if n assumes a value  i)1  ii) 0 ?

Question 4                                                                                                                                [10]

  1. Give two differences between the switch statement and if else statement.
  2. Write two difference between linear search and binary search.
  3. Given that in x[][]={{2,4,6},{3,5,7}}, what will be the value of x[1][1] and x[0][2]?
  4. State the total size in bytes, of the arrays a[4] of char data type and p[6] of float data type.
  5. If int x[]={4,3,7,8,9,10}; What are the values of p and q?
    i) p= x.length     ii)   q=x[2] +x[5] * x[1]

   

 

         Section B (60 Marks)
Attempt any four questions from this Section.

      The answers in this Section should consist of the Programs in either BlueJ

environment or any program environment with Java as the base.

Each program should be written using Variable descriptions/Mnemonic Codes
such that the logic of the program is clearly depicted.
Flow-Charts and Algorithms are not required.

Question 5                                                                                                                              [15]
Designa class to overload a function polygon() as follows:
i)          void polygon(int n, char ch)       : with one integer argument and one character type argument that draws a filled square of side n using the character stored in ch.

  1. ii) void polygon(int x, int y) : with two integer arguments that draws a filled rectangle of length x and breadth y using the symbol ‘@’.

iii)         void polygon()              : with no argument that draws a filled triangle shown below.

Example:

  1. i) Input value of n=2, ch=’O’

Output: OO
OO

  1. ii) Input value of x=2,y=5

Output

@@@@@

@@@@@

 

iii) Output

*

* *

* * *

 
Question 6                                                                                                                              [15]

Write  a menu driven program  to accept a number and check and display whether it is a prime number or not OR an automorphic number or not.

  1. a) Prime number : A number is said to be a prime number if it is divisible only by 1 and itself and not by any other number. Example; 3,5,7,11,13..
  2. b) Automorphic number : An automorphic is the number which is contained in the last digit of the square. Example : 25 is an automorphic number as its square is 625 and 25 is present as the last two digits.

 

Question 7                                                                                                                              [15]

Special words are those words which start and end with the same letter.
Examples:  EXISTENCE, COMIC, WINDOW
Palindrome words are those words which read the same from left to right and vice-versa.

Examples: MALAYALAM, MADAM, LEVEL, ROTATOR

All palindromes are special words, but all special words are not palindromes.

Write a program to accept a word check and print whether the word is a palindrome or only special word.

Question 8                                                                                                                              [15]
Write a program that encodes a word into Piglatin. To translate a word into a Piglatin word, convert the word into upper case and then place the first vowel of the original word as the start of the new word along with the remaining alphabets. The alphabets present before the vowel being shifted towards the end following by  “YZ”.

Sample input : Flowers     Sample Output: OWERSFLYZ
Sample input : Olympics            Sample Output : OLYMPICSYZ

Question 9                                                                                                                             

 1)  Write a program to initialize the given data in an array and find the minimum and maximum    values along with the sum of the given elements.                                                                                                                             [5]

Numbers: 2 5 4 1 3

Output: Minimum value  : 1
Maximum value=5

Sum of the elements=15

 

2) Write a program to accept 15 integers from the keyboard, assuming that no integer entered is a zero. Perform selection sort on the integers and then print them in ascending order.        [10]

 

Question 10                                                                                                                            [15] Define a class and store the given city names in a single dimensional array. Sort these names in alphabetical order using the bubble sort technique only.

Input:  Calcutta, Agra, Mumbai, Bangalore, Delhi

Output: Agra, Bangalore, Calcutta, Delhi, Mumbai

 

.

Armstrong number using recursive method (Q7- Model QP)


Model Question Paper -ISC Computer Science – Oct 2018

Question 7

An Armstrong number is such that the sum of the cube of the digits of the number is the number itself. Example 153= 13+53+33
Design  a class Arm to perform the given task. Some of the members of the class are given below:

Class name                              : Arm
Data member/Instance variables         :
no                                            : integer to store the number
sum                                          : integer to store the sum of the cube of the digits
Member methods:
Arm(….)                                 : to initialize member data
no, and assign 0 to sum.
long fnPower(int a, int b)        : to calculate a^b using recursive function
void fnPerform()                     : to verify and print whether the member data is an Armstrong number or not.
Specify the class Arm, giving the details of the above member data and methods. Also define the main() to create an object and call the relevant methods accordingly to enable the task

Program

import java.util.*;
class Arm
{
int no, sum;
Arm(int n1)
{
no=n1;
sum=0;
}

long fnPower(int a, int b)
{
if(b==0)
return 1;
else
return a*fnPower(a,b-1);
} // recursive ()

void fnPerform()
{
int an=no,r;
long sum1=(long) sum;
while(an>0)
{
r=an%10;
an=an/10;
sum1=sum1+fnPower(r,3);                                // recursive fnPower() invoked here
}
if(no==sum1)
System.out.println(“Armstrong number”);
else
System.out.println(“Not an Armstrong number”);
}

public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print(“Enter the number:”);
int num=sc.nextInt();
Arm a1=new Arm(num);
a1.fnPerform();
}
}

Output:
Enter the number: 153
Armstrong number

Enter the number: 135
Not an Armstrong number

Model Question Paper June 2018-ISC Computer Science


Model Question Paper ISC Computer Science 2018

PART – I (20 Marks)
Answer all questions.
While answering questions in this Part, indicate briefly your working and
reasoning, wherever required.

Question 1

  1. Write the distributive laws. Prove anyone using truth table.
  2. Simplify the expression using Boolean laws
    F1=(a+c’).(a’+b’+c’)
  3. Find the complement of :
    F 2 =ab’ + a’ + a’b+b’
  1. Verify using truth table ( ~p ^ q) V (p^q) = q.
  2. Write the dual of F3 = (p +1).(q’ + 0)

Question 2

  1. An array D [-2…10][3…8] contains double type elements. If the base address is 4110, find the address of D[4][5], when the array is stored in Column Major Wise
  2. I) Name the keyword it is used for memory allocation.                                          
  3. Write the syntax of a character input.
  4. If (~P => Q) then write its:
  5. Inverse ii)         Converse
  6. Differentiate between a method and a constructor of a class.
  7. State De Morgan’s laws. Verify anyone using a truth table                                         

Question 3

  1. Given the following method

void fnM(int p)

{

int RM[]={2,4,6,8,10};

for( int j=1; j<=RM.length-1; j++)

{

RM[j-1] = RM[j-1] +RM[j];

RM[j] = RM[j-1] *2;

}

System.out.print(“OUTCOME=” +RM[p]);

}

  • What will be the output if p=1?
  • What will be the output if p=4?
  • Which token finds the size of the array?

                                                     PART II (50 Marks)

          Answer six questions in this part, choosing two questions from Section A,
                              two from Section B and two from Section C.
SECTION – A
                                               Answer any two questions

Question 4

  • Given the Boolean function : F(A,B,C,D) =Ʃ(0,2,3,6,7,8,10,12,13,14,15)

  • Reduce the above expression by using 4 variable Karnaugh map, showing
    the various groups (i.e., octal, quads, pairs).
  • Draw the logic gate diagram for the reduced expression. Assume that the
    variables and their complements are available as inputs.
  • Given the Boolean function : F(P, Q,R,S) =Π (0, 1, 2, 4, 5, 6, 8, 10)
  • Reduce the above expression by using 4 variable Karnaugh map, showing
    the various groups (i.e., octal, quads, pairs).
  • Draw the logic gate diagram for the reduced expression. Assume that the
    variables and their complements are available as inputs.

Question 5

  • Given the Boolean function: F(A,B,C,D)= Π (0,1,2,3,5,7,8,9,10,11)
    Reduce  the above expression by using 4-variable K map, showing the various groups (i.e., octet, quads and pairs)
  • Given the Boolean function : F(A,B,C,D) =ABC’D’ +A’BC’D’ + A’BC’D + ABC’D+ A’BCD+ABCD
    Reduce the above expression by using 4 variable K map showing the various groups ( octet, quads and pairs).

Question 6

A school intends to select candidates for an inter-school essay competition as per the criteria given below:

The student has participated in an earlier competition and is very creative.
OR
The student is very creative and has excellent general awareness, but has not participated in any competition earlier.

 OR

The student has excellent general awareness and has won prize in an inter-house competition.

The inputs are:

INPUTS
A                     participated in a competition earlier
B                      is very creative
C                     won prize in an inter-house competition
D                     has excellent general awareness
(in all the above cases 1 indicates yes and 0 indicates no for all cases)

Output: X [1 indicates yes, 0 indicates no for all cases]
Draw the truth table for the inputs and outputs given above and write the POS expression for X(A,B,C,D).

  • Write the idempotence law and verify using truth table.
  • Convert the following Boolean expression into its Canonical POS form:            F(A,B,C) = (B+C’).(A’+B)

                                            SECTION – B

                             Answer any two questions.

Each program should be written in such a way that it clearly depicts the logic of the problem. This can be achieved by using mnemonic names and comments in the program.
(Flowcharts and Algorithms are
not required.)
The programs must be written in Java.

Question 7

A Special is a number in which the sum of the factorial of its digits is equal                  to the number. Example  1!+4!+5! =145. Thus 145 is a special number.
Design a class Special to check if the number is a Special number or not. Some of the members of the class are given below:

Class Name                :                                   Special
Data member/Instance Variables:   n :          integer  to store the number
Member functions:
Special()                                   : constructor to initialize data members with legal initial values
void accept()                            : to accept the number
int factorial(int x)                       : returns the factorial of a number using recursive technique.
boolean isSpecial()                   : checks for the special number by invoking the function factorial() and returns true if Special, otherwise returns false.
void display()                            : to display the result with an appropriate message.

Specify the class Special, giving the details of the above member data and methods only. Define the main() function to create an object and call the member function accordingly to enable the task.

Question 8                                                                                                                              [10]

A beam number is a number in which the sum of the square of the digits of the number is larger than the number itself.

For example  :  N= 25 Sum of square of digits = 22 + 52 = 4 +25 = 29
Number 25 is a Beam Number
For example  : N = 34 Sum  of square of digits = 32+ 42  = 9 + 16 = 25
Number 34 is Not a Beam Number
Design a class BEAM to check if a given number is a beam number.

Class Name                                        : BEAM
Data Member/Instance Variables    :
int m                                                     : integer
Member Functions:
BEAM()                                               : Constructor to initialize member data to null
void fnGet( int mm)                               : To input value for the member data
int fnSqSum (int d)                                : To return the sum of the square of the digits of the argument
For example : d= 69
Final method return 62 + 9 2 = 36 +81 = 117
boolean isBeam()                                  : To  verify whether the member data is a Beam Number or not as per the criteria given above.
Specify the class
BEAM, giving details of the above member data and methods only. Also define the main() to create an object and call the other methods accordingly to enable the task.

Question 9                                                                                                                              [10]

Write a class called Numbers with following specifications:

ArmStrong Number = 407= 43+03+73

ArmStrongLike Numbers = 165033 = 163+503+333

Class Name                                        : Numbers
Data Member/Instance Variables    :
number                                     : long type
Member Functions:
boolean isArmStrong()              : checks whether the number is Armstrong or not.
boolean isArmStrongLike()                   : checks whether the number is ArmstrongLike numbers or not.
void genArmStrongNos()                      : generates Armstrong number

void genArmStrongLikeNo()                : generates Armstrong number Like

Specify the class Numbers, giving the details of the above member data and methods only. Define the main() function to create an object and call the member function accordingly to enable the task.

SECTION – C

Answer any two questions.

Each program should be written in such a way that it clearly depicts the logic of the problem. This can be achieved by using mnemonic names and comments in the program.
(Flowcharts and Algorithms are
not required.)
The programs must be written in Java.

Question 10                                                                                                                [5]

Show the output of the following code;

public class BreakDemo

{

   public static void main(String[] args)

   {

      for (int i = 1; i <= 10; i++)

      {

         if (i == 5)

         {

            break;   

         }

    System.out.print(i + ” “);

      }

  System.out.println(“Loop is over.”);

   }

          }

 Question 11                                                                                                                [5]

The following function is a part of some class. Assume n is a positive integer. Answer the given questions along with dry run/working.

int unknown(int n)

{

int I, K;

if(n%2==0)

{
I=n/2;

            K=1;

}

else

{

K=n;

n–;

I= n/2;

}

while(I >0)

{

K=K*I*n;

I–;

n–;

}

return k;

}

  • What will be returned by unknown(5) ?
  • What will be returned by unknown(7) ?

Question 12                                                                                                                [5]

Briefly describe static and instance member with example.

 

 

ICSE Programs


Welcome you to new blog especially for the ISC and ICSE computer applications subject. Dear friends, you can access the blog and try out the programs that be given in this blog. Hope this blog can be a part of your success in ISC & ICSE board exams.