Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: July 2014

Wednesday, July 30, 2014

ISC COMPUTER SCIENCE PRACTICAL Solved ~ 2004



The computer department of the Agency of International Espionage is trying to decode intercepted messages.
The agency's spies have determined that the enemy encodes messages by first converting all characters to
their ASCII values and then reversing the string. For example, consider A_z ( the underscore is just to hightlight the space).
The ASCII values of A, <space>, z are 65,32,122 respectively.
Concatenate them to get 6532122, then reverse this to get 2212356 as the coded message.
Write a program which reads a coded message and decodes it.
It will contain only alphabets (A......Z, and a........z) and spaces.
ASCII values of A.......Z are 65........90 and those of a.....z are 97......122.
Test your program for the following data and some random data.


SAMPLE DATA:
INPUT:
Encoded Message: 2312179862310199501872379231018117927
OUTPUT:
The Decoded Message: Have a Nice Day

The idea behind the program is to store the Encoded message in a String.
Reverse the string and extract the individual characters and convert it 
into its corresponding integer values.
So, if input is 2212356 we will reverse it to get
6532122
Now the integer values 6,5,3,2,1,2,2 will be stored in some array;
To get the integer value of the character '6' (say) we need to subtract 48 from it.
since 54 is the ascii value of '6'. thus 54-48 = 6.
This has been achived by the following code
 public void reverseString() {
        int j = 0;
        for ( int i = Decryptmsg.length()-1; i >= 0; i--) {
              codeValues[j++] = Decryptmsg.charAt(i)-48; // for extracting the integer value at the i-th position from the String msg in
            }                                          // in reverse order
        }  
Now the idea is to extrac the ascii values from the array and print the 
corresponding character
For example, we need to extrac 65, 32 and 122 from the array.
For this note that the ascii values of characters ranges from 65 to 90
and 97 to 122 and one extra value of 32 for the space.
So we will take the first 3 values from the integer, 6 5 and 2 and convert
it into decimal number by 6*10*10+5*10+2=652
but this is outside our range, so we need to take only 6 and 5 and convert it
to get 65. then print the corresponding character. For this we have used the code

while ( i < Decryptmsg.length() ) {
              if (i < Decryptmsg.length() && i+1 < Decryptmsg.length() && i+2 < Decryptmsg.length())
                     tmp = codeValues[i]*10*10+codeValues[i+1]*10+codeValues[i+2];
              else
                     tmp = codeValues[i]*10+codeValues[i+1];
              if (tmp >= 97 && tmp <=122) {
                     System.out.print((char)tmp); // prints the character for the corresponding ascii
                     i +=3;
                     }
              else {
                     tmp = codeValues[i]*10+codeValues[i+1];
                     System.out.print((char)tmp); // prints the character for the corresponding ascii
                     if(tmp==32)
                           System.out.print("");
                     i +=2;
                     }
              }
Below is the link of solved ISC Computer Science Practical Paper year wise.


JAVA CODE

import java.util.*;
import java.io.*;
public class ISC2004Q1 {
    public static void main(String[] args) throws IOException
        {
        String msg;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
        System.out.println(" Enter the sentence (Encoded Message):");
        msg = br.readLine();   
        Decrypt ob = new Decrypt(msg);
        ob.reverseString();
        System.out.println("OUTPUT The Decoded Message: ");
        ob.decryptCode();       
        }
    }
class Decrypt {
    Decrypt(String msg) {
        Decryptmsg = msg;
        codeValues = new int[Decryptmsg.length()];
        }
    public void reverseString() {
        int j = 0;
        for ( int i = Decryptmsg.length()-1; i >= 0; i--) {
              codeValues[j++] = Decryptmsg.charAt(i)-48; // for extracting the integer value at the i-th position from the String msg in
            }                                          // in reverse order
        }   
    public void decryptCode() {
       int tmp,i=0;
        while ( i < Decryptmsg.length() ) {
              if (i < Decryptmsg.length() && i+1 < Decryptmsg.length() && i+2 < Decryptmsg.length())
                     tmp = codeValues[i]*10*10+codeValues[i+1]*10+codeValues[i+2];
              else
                     tmp = codeValues[i]*10+codeValues[i+1];
              if (tmp >= 97 && tmp <=122) {
                     System.out.print((char)tmp); // prints the character for the corresponding ascii
                     i +=3;
                     }
              else {
                     tmp = codeValues[i]*10+codeValues[i+1];
                     System.out.print((char)tmp); // prints the character for the corresponding ascii
                     if(tmp==32)
                           System.out.print("");
                     i +=2;
                     }
              }
       }   
    private String Decryptmsg;   
    private int[] codeValues;
    }  

Monday, July 28, 2014

ISC COMPUTER SCIENCE PRACTICAL 2014~ COMPOSITE MAGIC

/*
A Composite Magic number is a positive integer which is composite as well as a magic number.
Composite number : A composite number is a number which has more than 2 factors.
For example : 10
Factors are : 1,2,5,10.
Magic number : A magic number is a number in which the eventual sum of the
digits is equal to 1.
For example:- 28 = 2+ 8 =10 = 1+0=1
Accept 2 positive integers m and n, where m is less than n as user input. Display the number of
composite magic integers that are in the range between m and n (both inclusive) and output them
along with the frequency, in the format specified below.
Test your program with the sample data and some random data:

Example 1:
INPUT:
m=10
n=100
OUTPUT:
THE COMPOSITE MAGIC INTEGERS ARE :
10,28,46,55,64,82,91,100
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS :8

Example 2:INPUT:
m=1200
n=1300
OUTPUT:
THE COMPOSITE MAGIC INTEGERS ARE :
1207,1216,1225,1234,1243,1252,1261,1270,1288
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS :9

Example 3:
INPUT:
m=120
n=99
OUTPUT:
INVALID INPUT
*/

/* ISC COMPUTER SCIENCE PRACTICALS SOLVED, 2014 */
import java.util.*;
public class CompositeMagic {
    public static void main(String[] args) {
        int m,n;
        Scanner in = new Scanner(System.in);
        System.out.println("INPUT THE VALUE OF m & n:");
        m = in.nextInt();
        n = in.nextInt();
        while( m > n || n < 1 || m < 1) {
            System.out.println("INVALID CHOICE:");
            m = in.nextInt();
            n = in.nextInt();
            }
        CheckCompositeMagic cm = new CheckCompositeMagic(m,n);
        System.out.println("THE COMPOSITE MAGIC NUMBERS ARE:");
        cm.checkComposite();
        System.out.println("FREQUENCY OF COMPOSITE MAGIC INTEGERS IS:"+cm.fCount());
        System.out.println();
        }
    }
class CheckCompositeMagic {
    CheckCompositeMagic (int m, int n) {
        M = m; N = n;
        freqCompositeMagic = 0;
        }
    public int fCount() {
        return     freqCompositeMagic;
        }
    public void checkComposite() {
        for ( int i = M; i <= N ;i++) {
            flag = false;
            for ( int j = 2; j <= Math.sqrt(i); j++) {
                if( i%j == 0) {
                    flag = true;
                    break;
                    }
                }
            if(flag == true) {
                 checkMagic(i);
                 if(fg == true)   
                     System.out.println(i);
                 }   
            }
        }    
    private void checkMagic(int mg) {
        fg = false;
        int tmp,rem;
        tmp = mg;
        sum = 0;
        while( tmp > 0) {
            rem = tmp % 10;
            sum += rem;
            tmp = tmp/10;
            }
        if( sum == 1 ) {
            freqCompositeMagic++;
            fg = true;
            }   
        else {
            if(sum < 10)
                fg = false;
            else
                checkMagic(sum);
            }   
           
        }   
    private int M;
    private int N;   
    private int freqCompositeMagic;
    private int sum;
    private boolean flag,fg;
    }   

ISC Computer Science Practical 2014 solved



/*
Question 3.
Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’ or ‘!’ only. Any
other character may be ignored. The words may be separated by more than one blank space and are
in UPPER CASE.
Perform the following tasks:
a) Accept a sentence and reduce all the extra blank space between 2 words to a single blank space.
b) Accept a word from the user which is a part of the sentence along with its position number
and delete the word and display the sentence.

Test your program for the following data and some random data.:

Example 1
INPUT: A   MORNING WALK IS A BLESSING FOR   THE    WHOLE DAY.
WORD TO BE DELETED : IS
WORD POSITION IN THE SENTENCE : 4
OUTPUT: A MORNING WALK A BLESSING FOR THE WHOLE DAY.

Example 2
INPUT: AS YOU SOW, SO SO YOU REAP.
WORD TO BE DELETED : SO
WORD POSITION IN THE SENTENCE : 4
OUTPUT: AS YOU SOW, SO YOU REAP.

Example 3
INPUT: STUDY WELL ##.
OUTPUT: INVALID INPUT.
/*

/* ISC COMPUTER SCIENCE PRACTICAL~2014 */
import java.util.*;
import java.io.*;
public class SpaceWord {
    public static void main(String[] args) throws IOException
        {
        String msg;
        String wordDelete;
        int pos;
        Scanner in = new Scanner(System.in);
        System.out.println("WORD POSITION IN THE SENTENCE:");
        pos = in.nextInt();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
        System.out.println(" Enter the sentence:");
        msg = br.readLine();
        br.close();
        OutputString os = new OutputString(msg);
        if(os.validInvalid() == false)
            System.out.println("INVALID INPUT");
        else    {
            os.extraSpaceDeletion();
            os.wordDeletion(pos);
            }
               
        }
    }
class OutputString {
    OutputString(String mg) {
        msg = mg;
        lengthMsg = msg.length();
        outputmsg = "";
        }
    public boolean validInvalid() {
        return (msg.charAt(lengthMsg-1) == '.'  ? true : msg.charAt(lengthMsg-1) == '!' ? true : msg.charAt(lengthMsg-1) == '?' ? true : false);
        }   
    public void extraSpaceDeletion() {
        for ( int i = 0; i < lengthMsg; i++) {
            if(msg.charAt(i) != ' ') {
                outputmsg += msg.charAt(i);
                }
            else {
                outputmsg += msg.charAt(i);
                while(msg.charAt(i) == ' ' && i < lengthMsg)
                    i++;
                i--;   
                }   
            }
        System.out.println();   
        }   
    public void wordDeletion(int pos) {
        int tmp = 0;
        for ( int i = 0; i < outputmsg.length(); i++) {
            if(outputmsg.charAt(i) == ' ')
                tmp++;
            if(tmp == pos-1) {
                while(outputmsg.charAt(i+1) != ' ' && i < outputmsg.length())
                    i++;
                }   
            else
            System.out.print(outputmsg.charAt(i));   
            }
        System.out.println();
        }   
    private String msg;
    private String outputmsg;
    private int lengthMsg;
    }       

Sunday, July 27, 2014

CBSE ~ COMPUTER SCIENCE ~ CLASS XI ~ CHAPTER 3


CBSE ~ COMPUTER SCIENCE ~ CLASS XI ~ CHAPTER 3 #Python




1. Create following Variables
i) “mystring” to contain “hello‟
ii) “myfloat‟ to contain “2.5‟
iii) “myint‟ to contain “10‟
Answer: mystring = 'hello'
myfloat = 2.5
myint = 10

2. Write the value justification
i) 2*(3+4) ii) 2*3+4 iii) 2+3*4
Answer: 14,10,14

3. What is the type of the following result:
i) 1+2.0+3
Answer: float, adding/subtracting/multiplying/dividing an integer with a float type results in float.

4. Which of the following is the valid variable name:
i) global ii) 99flag iii) sum iv) an$wer
Answer: sum

5. True of False
i) Character Data type values should be delimited by using the single quote.
ii) None is one of the data type in python
iii) The += operator is used to add the right hand side value to the left hand side
variable.
iv) The data type double is not a valid python data type.
v) Python does not have any keywords
vi) The equal to condition is written by using the == operator

Answer: False,True,False,True,False,True

6. Check all syntactically correct statements
a) Which input statements are correct
i) a = raw_input ( )
ii) a = raw_input (“enter a number”)
iii) a = raw_imput (enter your name)
Answer: Only iii) is incorrect

b) Which print statements are correct?
i) _print “9” + “9”
ii) _print int(“nine”)
iii) _print 9+9
iv) print 9
Answer: iv)

c) Which are correct arithmetical operations?
i) a = 1*2
ii) 2 = 1+1
iii) 5 + 6 = y
iv) Seven = 3 * 4
Answer: i) and iv)

d) Which are correct type conversions?
i) int (7.0+0.1)
ii) str (1.2 * 3.4)
iii) float (“77”+“.0”)
iv) str ( 9 / 0 )
Answer: i) and ii)

e) Which operations result in 8?
i) 65 // 8
ii) 17 % 9
iii) 2 * * 4
iv) 64 * * 0.5

Answer: 8,8,16,8.0

f) Which lines are commented?
i) “””This is a comment”””
ii) # This is a comment
iii) // this is a comment
iv) '''This is a comment'''

Answer: i) & ii)

g) Find the matching pairs of expressions and values.
i) 1023 boolean
ii) None int
iii) [2, 4, 8, 16] tuple
iv) True list
v) 17.54 str
vi) ('Roger', 1952) NoneType
vii) “my fat cat” float

Answer: 1023-int, None-NoneType, [2,4,8,16]-list,True-boolean, 17.54-float, ('Roger', 1952)-tuple,”my fat cat”-str

7. MCQ
i) The __________ data type allows only True/False values [bool]

ii) If the value of a = 20 and b = 20, then a+=b will assign ________ to a [40]

iii) The ____________ operator is used to find out if division of two number
yields any remainder [%]

8. When will following statement in interpreter result into error:
>>> B+4

Answer: when B is not defined before hand or B+4 is not being assigned to any other variable

9. How can we change the value of 6*1-2 to -6 from 4?

Answer: 6*(1-2)

10. Is python case sensitive?

Answer: YES

11. What does 'immutable' mean; which data type in python are immutable.

Answer: Integer & Long , Float/floating point , Complex