
ISC & ICSE Computer Science – Crash Course 2nd SEM Board Exams


Program to perform binary search, values input by the user using Scanner class. Binary Search can do only in sorted arrays. So a method for Sorting bubble Sort technique , then in the sorted array key is searched by BinarySearch method.
Usually we do by initializing the array value. Here each process defined in user defined methods.
import java.util.*;
public class Userdefined
{
static void BubbleSort(int num[]) //Sorting
{
int len=num.length;
int i,j, temp;
for(i=0;i<len;i++)
{
for( j=0;j<len-i-1;j++)
{
if(num[j]>num[j+1])
{
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
// System.out.println("Sorted Elements:");
for(int k=0;k<len;k++)
System.out.print(num[k]+"\t");
System.out.println();
}
static void BinSearch(int num[],int k) //Binary Search
{
int l=0,u=num.length-1;
int m=0, found=0;
while(l<=u)
{
m= (l+u)/2;
if( k > num[m])
l =m+1;
else if( k< num[m])
u=m-1;
else
{
found =1;
break;
}
}
if(found==1)
System.out.println(" Element Present at"+" "+ (m+1) +" " +"after sorting");
else
System.out.println("Not Present");
}
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.println( "Enter the total number of elements in the array:");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println( "Enter the elements:");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Entered Elements");
for(int i=0;i<n;i++)
{
System.out.print(a[i]+"\t");
}
System.out.println();
System.out.println("Elements should be sorted before Binary Search .....\n ..Sorted Elements....");
BubbleSort(a); //invoking BubbleSort ()
System.out.println("Binary Search \n");
System.out.println("Enter element to search");
int ky=sc.nextInt();
BinSearch(a, ky); // invoking Binary search method
}
}
Output:
Enter the total number of elements in the array:
5
Enter the elements:
54
12
78
1
6
Entered Elements
54 12 78 1 6
Elements should be sorted before Binary Search …..
…Sorted Elements….
1 6 12 54 78
Binary Search
Enter element to search
12
Element Present at 3 after sorting
DEar FrndZ , those who wants to know about Functions /Methods in JAva PRogramminG … JUst have a view on the below video…in easy simple way explained
We are happy to share the news on launching Online Tuitions 1 to 1 mode. Focusing on Board Classes (10th, 12th ) & Pre Board Classes (9th , 11th) also. Grab your opportunity to score 90+ in Board Exams…. Place your seat now !!! @ compogeniusacademy@gmail.com Limited Seats. We are having limited seats for Online […]

Dear Students, It is a simple Q & A session of 10 questions. related to Methods and simple concepts. You can answer in the comment box !!!
u Cn eXpLR !!! & comment
https://rhythmhoney.wordpress.com/
https://sunmarkgroups.wordpress.com/
harvestfarmfresh.com – buy online natural products
Write a program to accept a sentence and check whether it is terminated by ‘.’ or ‘? ‘ or ‘!’ . Display the words using default delimiter (space) and the first character of each word should be converted into Upper Case.
Sample Input :
converting first character.
Sample Output:
converting Converting
first First
character Character
import java.util.*;
class strDec
{
public static String firstLetter(String wrd)
{
String res="";char ch2=' ';
int l=wrd.length();
for(int i=1;i<l;i++)
{
char ch1=wrd.charAt(i);
ch2=wrd.charAt(0);
if(Character.isLowerCase(ch2))
{
ch2 =Character.toUpperCase(ch2);
}
//System.out.println(ch1);
res=res +ch1;
}
res=ch2+res;
return res;
//System.out.println(res);
}
public static void main(String args[])
{ strDec strd=new strDec();
String str;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence:");
str=sc.nextLine();
int l=str.length();
char c=str.charAt(l-1);
if(c=='.'||c=='?'||c=='!')
{
StringTokenizer st=new StringTokenizer( str, " .?!");
int n=st.countTokens();
String wrd[]=new String [n];
String ch[]=new String[n];
for(int i=0;i<n;i++){
wrd[i]=st.nextToken();
ch[i]=firstLetter(wrd[i]);
System.out.println(wrd[i] + " "+ch[i]+" " );
}
}
else
{
System.out.println("Enter a sentence terminated with . or ? or ! :");
}
}
}
Output:
Enter a sentence:
the sky is the limit.
the The
sky Sky
is Is
the The
limit Limit
Write a program to declare a matrix A[][]of order ( M*N) where ‘M’ is the number of rows and ‘N’ is the number of columns such that the value of “M’ must be greater than 0 and less than 10 and the value of ‘N’ must be greater than 2 and less than 6. Allow the user to input digits (0-7) only at each location, such that each row represents an octal number.
Example:
231(decimal equivalent of 1st row=153 i.e. 2*8² +3*8¹ +1*8⁰) 405(decimal equivalent of 1st row=261 i.e. 4*8¹ +0*8¹ +5*8⁰) 156(decimal equivalent of 1st row=110 i.e. 1*8² +5*8¹ +6*8⁰) Perform the following tasks on the matrix:
Example 1: Input: M=1 N=3 ENTER ELEMENTS FOR ROW 1: 1 4 4 Output: FILLED MATRIX DECIMAL EQUIVALENT 1 4 4 100 Example 2: Input: M=3 N=4 ENTER ELEMENTS FOR ROW 1: 1 1 3 7 ENTER ELEMENTS FOR ROW 2: 2 1 0 6 ENTER ELEMENTS FOR ROW 3: 0 2 4 5 Output: FILLED MATRIX DECIMAL EQUIVALENT 1 1 3 7 607 2 1 0 6 1094 0 2 4 5 165 Example 3: Input: ENTER ROW SIZE M=3 ENTER COLUMN SIZE N=9 Output:OUT OF RANGE
import java.util.*;
class mat
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter no.of rows M ");
int m=sc.nextInt();
System.out.println("Enter no.of rows N ");
int n= sc.nextInt();
int flag=0,pow=0;
int mat[][]=new int[m][n];
if((m>0 && m< 10 )&&(n>2 && n<6)) //M >0 AND <10 && N>2 AND <6
{
for(int i=0;i<m;i++)
{System.out.print("Enter elements for row "+(i+1)+":");
for(int j=0;j<n;j++)
{
mat[i][j]=sc.nextInt();
if(mat[i][j] < 0 || mat[i][j]>7) //ALLOW THE USER TO INPUT ONLY FROM 0 TO 7 (OCTAL)
{
flag=1;
System.out.println("INPUT DIGITS (0 -7) ONLY AT EACH LOCATION");
System.exit(1);
}
}
}
}
if(flag==0)
{
System.out.println("FILLED MATRIX \t DECIMAL EQUIVALENT");
for(int i=0;i<m;i++)
{
int r=0,sum=0;
for(int j=n-1;j>=0;j--)
{
pow=((int)Math.pow(8,r++));
sum=sum+(mat[i][j]*pow);
}
for(int k=0;k<n;k++)
{
System.out.print(mat[i][k]+ " " );
}
System.out.print("\t\t \t" +sum);
System.out.println();
}
}
else
System.out.println("Enter valid range");
}
}
OUTPUT:
Enter no.of rows M
3
Enter no.of rows N
3
Enter elements for row 1:2 3 1
Enter elements for row 2:4 0 5
Enter elements for row 3:1 5 6
FILLED MATRIX DECIMAL EQUIVALENT
2 3 1 153
4 0 5 261
1 5 6 110
import java.util.*;
class permutationDigits
{
int count = 0;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a digit: ");
int a= sc.nextInt();
String s=String.valueOf(a); //converting int into string
System.out.println("The Anagrams are : ");
compute("",s);
System.out.println("Total Number of Anagrams = "+count);
}
void compute(String s1, String s2)
{
if(s2.length()<=1)
{
count++;
System.out.println(s1+s2);
}
else
{
for(int i=0; i<s2.length(); i++)
{
String a = s2.substring(i, i+1);
String b = s2.substring(0, i);
String c = s2.substring(i+1);
compute(s1+a, b+c);
}
}
}
public static void main(String args[])
{
permutationDigits ob=new permutationDigits();
ob.input();
}
}
Output:
Enter a digit: 234
The Anagrams are :
234
243
324
342
423
432
Total Number of Anagrams = 6
import java.util.*;
class anagramsWord
{
int count = 0;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word : ");
String s = sc.next();
System.out.println("The Anagrams are : ");
compute("",s);
System.out.println("Total NO. of Anagrams = "+count);
}
void compute(String s1, String s2)
{
if(s2.length()<=1)
{
count++; // no of combination words
System.out.println(s1+s2);
}
else
{
for(int i=0; i<s2.length(); i++)
{
String a = s2.substring(i, i+1);
String b = s2.substring(0, i);
String c = s2.substring(i+1);
compute(s1+a, b+c); // recursive method
}
}
}
public static void main(String args[])throws Exception
{
anagramsWord ob=new anagramsWord();
ob.input();
}
}
Output:
Enter a word : TOP
The Anagrams are :
TOP
TPO
OTP
OPT
PTO
POT
Total NO. of Anagrams = 6
An Evil number is a positive whole number which has even number of 1’s in its binary equivalent. Eg: Binary equivalent of 9 is 1001, contains even number of 1’s.
Design a program to accept a positive whole number and find the binary equivalent of the number and count the number of 1’s in it and display whether it is an Evil number or not.
Eg:
INPUT : 15
BINARY EQUIVALENT : 1111
NO. OF 1’S : 4
OUTPUT : EVIL NUMBER
import java.util.Scanner;
class bintoDec
{
int n, a, count1=0;
String s = "";
void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter decimal number ");
n = sc.nextInt();
System.out.println("INPUT : "+n);
}
void calculate()
{
input();
while(n>0)
{
a = n%2;
if(a == 1)
{
count1++;
}
s = a+" "+s;
n = n/2;
}
System.out.println("BINARY EQUIVALENT:" +s);
System.out.println("NO. OF 1's " +count1);
}
void count()
{
calculate();
if( count1 %2 ==0)
System.out.println("\n EVIL NUMBER");
else
System.out.println("\nNOT AN EVIL NUMBER");
}
public static void main(String args[])
{
bintoDec bin=new bintoDec();
bin.count();
}
}
Enter decimal number
24
INPUT : 24
BINARY EQUIVALENT:1 1 0 0 0
NO. OF 1’s 2
EVIL NUMBER
Enter decimal number
25
INPUT : 25
BINARY EQUIVALENT:1 1 0 0 1
NO. OF 1’s 3
NOT AN EVIL NUMBER
ICSE 10th Computer Applications -Chapter wise – 1st Chapter
How to map in KMap ? – Part 3
How to map values in KMAP ? – PArt 2
How to draw a KMap ? Part 1
Datatypes- Primitive and non primitive datatypes
what is rvalue and lvalue ?
Write a program to check whether the given number is prime number or not, if it is prime number display the factorial.
import java.util.*;
public class primeFactorial
{
int i,m,flag,n;
primeFactorial()
{
n=0;
m=0;
flag=0;
}
public void input()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter the number:");
n=sc.nextInt();
}
public void calculate()
{
input(); // input()method invocation
m=n/2;
if(n==0||n==1)
{
System.out.println(n+" is not prime number");
}
else
{
for(i=2;i<=m;i++)
{
if(n%i==0)
{
flag=1; //status variable, if any divisor found, flag=1
break;
}
} // end of for loop
if(flag==0)
{
System.out.println(n+" is prime number");
fact(n);
}
else
{
System.out.println(n+" is not prime number");
}
}//end of outer else
}
public void fact(int num1)
{
int n2=num1,fact=1;
for(int j=1;j<=n2;j++)
fact=fact *j;
System.out.println("Factorial " + fact);
}
public static void main(String args[])
{
primeFactorial pN=new primeFactorial();
pN.calculate(); // calculate() method call
}
}
Output:
Enter the number:
5
5 is prime number
Factorial 120
Enter the number:
4
4 is not prime number
Prime Number : – a number which is divisible by 1 and itself (i.e it has only two factors).
0 & 1 are non prime numbers.
Eg: 5 ===> 5 * 1 = 5 (1 , 5 are the two factors – 5 is Prime no.)
15 =====> 15 * 1= 15 , 5 * 3= 15
Factors of 15 are 1, 3 and 5. (3 factors – 15 is not a Prime no.)
——————————————————————————————————————–
Write a Java program to check whether the given number is
PRIME or NOT.
To understand the program, here a simple explanation:
for loop variable starts with 2 (i =2 ) because 1 is a factor of every number (prime and non prime). We only have to loop through 2 to half of given number , because no number is divisible by more than its half.
(Divisibilty concept a % b == 0 , to find the divisor. If any divisor finds, the
value of flag updates as 1 and immediately exit from the loop by the break statement. )
If the flag =1 means it is not a prime number
import java.util.*;
public class primeNumberProgram
{
public static void main(String args[])
{
int i,m=0,flag=0,n;
Scanner sc = new Scanner (System.in);
System.out.println("Enter the number:");
n=sc.nextInt();
m=n/2;
if(n==0||n==1)
{
System.out.println(n+" is not prime number");
}
else
{
for(i=2;i<=m;i++)
{
if(n%i==0)
{
flag=1; //status variable, if any divisor found, flag=1
break;
}
} // end of for loop
if(flag==0)
{
System.out.println(n+" is prime number");
}
else
{
System.out.println(n+" is not prime number");
}
}//end of outer else
}// main
}//class
Output:
Enter the number:
25
25 is not prime number
Enter the number:
23
23 is prime number
Enter the number:
19
19 is prime number
m=n/2, because to find factors of a number,
eg: 10 can be written as
1 * 10 =10
2 * 5 = 10
can either write as
5 * 2 = 10
10 * 1= 10
From the above example, you can understand by finding the factors from 1 to n is same as 1 to n/2.
till half of the n value is required. (m =n/2)
Reduce Boolean Expression using truth table and algebraic method
Find the complement of the given using Demorgan’s law
Kindly Like, Share and Subscribe !!!
Support me!!!
Logic gates and Principle of Duality has explained in the above video.
Have a look !!!