Computer Programs Class 11
Computer Programs Class 11
Computer Programs Class 11
0 Index
1
1.1 Exception Handling
Create a program using Exception Handling to catch Arithmetic Exception such as dividing by
zero.
import java.util.*;
class Program1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter no.");
int n=sc.nextInt();
try {
int divideByZero = n / 0;
System.out.println("Performing division by zero...");
}
catch (ArithmeticException e) {
System.out.println("ArithmeticException => " + e.getMessage());
}
}
}
Output:
2
1.2 Customized Exceptions/User Created Exceptions
Create a program in which user created exceptions are raised and caught.
Output:
3
1.3 2-D Arrays
Create a program in Java to add two matrices and print the output.
import java.util.*;
class Program3
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter no. of rows in matrices");
int m=sc.nextInt();
System.out.println("Enter no. of columns in matrices");
int n=sc.nextInt();
int a[][]= new int[m][n];
int b[][]= new int[m][n];
System.out.println("MATRIX 1");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.println("Enter element "+(j+1)+" of row "+(i+1));
a[i][j]=sc.nextInt();
}
}
System.out.println("MATRIX 2");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.println("Enter element "+(j+1)+" of row "+(i+1));
b[i][j]=sc.nextInt();
}
}
System.out.println("Performing addition....");
int c[][]=new int[m][n];
for(int i=0;i<m;i++)
{
4
for(int j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
System.out.println("Addition performed...Printing in matrix form...");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
Output:
5
1.4 Call by Value
Create a program demonstrating passing of values using Call by Value.
class Program4
{
int data=50;
}
}
Output:
6
1.5 Call by Reference
Create a program demonstrating passing of values using Call by Reference.
class Program5
{
int data=50;
void change(Program5 op)
{
op.data=op.data+100;
}
}
}
Output:
7
1.6 Patterns
Create a pattern program printing in the following form:
55555
4444
333
22
1
import java.util.*;
class Program6
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows");
int n=sc.nextInt();
for(int i=n;i>=1;i--)
{
for(int c=i;c>=1;c--)
{
System.out.print(i);
}
System.out.println();
}
}
}
Output:
8
1.7 Switch Case
Create a program in Java demonstrating the use of switch case, giving the user the option to
choose pattern programs.
import java.util.*;
class Program7
{
public static void main(String args[])
{
int n,i,c;
Scanner sc=new Scanner(System.in);
System.out.println("Hello user! Welcome to our interactive pattern program");
System.out.println("What is your name?");
String name=sc.next();
System.out.println("Hi "+name);
System.out.println("Press 1 for asterisk pattern");
System.out.println("Press 2 for number pattern");
System.out.println("Press 3 for different number pattern");
int opt=sc.nextInt();
switch(opt)
{
case 1:
System.out.println("Enter no. of rows");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
for(c=1;c<=i;c++)
{
System.out.print("*");
}
System.out.println();
}
break;
case 2:
System.out.println("Enter no. of rows");
9
n=sc.nextInt();
for(i=1;i<=n;i++)
{
for(c=1;c<=i;c++)
{
System.out.print(i%10);
}
System.out.println();
}
break;
case 3:
System.out.println("Enter no. of rows");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
for(c=1;c<=i;c++)
{
System.out.print(c%10);
}
System.out.println();
}
break;
default:
System.out.println("Invalid choice");
}
}
}
10
Output:
11
1.8 Loops- For, While, Do While
Create a program to display the table of 5 using for, while, and do while loops.
class Program8
{
public static void main(String args[])
{
System.out.println("The table of 5 is");
for(int i=1;i<=10;i++)
{
System.out.println(5*i);
}
System.out.println("This was printed using for loop");
System.out.println();
System.out.println("The table of 5 is");
int j=1;
while(j<=10)
{
System.out.println(5*j);
j++;
}
System.out.println("This was printed using while loop");
System.out.println();
System.out.println("The table of 5 is");
j=1;
do
{
System.out.println(5*j);
j++;
}
while(j<=10);
System.out.println("This was printed using do while loop");
}
}
12
Output:
13
1.9 Selection sort
Create a program in Java to sort an array input by the user using selection sort.
import java.util.*;
class Program9
{
public static void main(String args[])
{
int t=0;
int minVal=0;
int smallIndex=0;
Scanner sc=new Scanner(System.in);
int a[]=new int[5];
for(int i=0;i<=4;i++)
{
System.out.println("Enter value");
a[i]=sc.nextInt();
}
for(int i=0;i<4;i++)
{
minVal=a[i];
smallIndex=i;
for(int j=i+1;j<5;j++)
{
if(a[j]<minVal)
{
smallIndex=j;
minVal=a[j];
}
}
if(smallIndex!=i)
{
t=a[smallIndex];
a[smallIndex]=a[i];
a[i]=t;
}
}
14
for(int i=0;i<=4;i++)
{
System.out.println(a[i]);
}
}
}
Output:
15
1.10 Bubble sort
Create a program in Java to sort an array using bubble sort.
import java.util.*;
class Program10
{
public static void main(String args[])
{
int t=0;
Scanner sc=new Scanner(System.in);
int a[]=new int[5];
for(int i=0;i<=4;i++)
{
System.out.println("Enter value");
a[i]=sc.nextInt();
}
for(int i=0;i<4;i++)
{
for(int j=i+1;j<=4;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(int i=0;i<=4;i++)
{
System.out.println(a[i]);
}
}
}
16
Output:
17
1.11 Binary Search
Create a program in Java to input an array from the user and find a particular element using
binary search.
import java.util.*;
class Program11
{
public static void main(String args[])
{
int t=0;
int flag=0;
int beg;
int end;
int mid;
Scanner sc=new Scanner (System.in);
System.out.println("Enter no. of terms");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter the terms");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Enter the search no.");
int s=sc.nextInt();
beg=0;
end=n-1;
mid=(beg+end)/2;
while(beg<=end)
{
t=mid;
if((a[beg]==s||a[end]==s)||a[mid]==s)
{
System.out.println("Search no. found");
flag=0;
break;
}
18
else
{
if(s>a[mid])
{
beg=mid;
mid=(beg+end)/2;
flag=1;
if(mid==t)
{
break;
}
}
else
{
end=mid;
mid=(beg+end)/2;
flag=1;
if(mid==t)
{
break;
}
}
}
}
if(flag==1)
{
System.out.println("Search no. not found");
}
}
}
19
Output:
20
1.12 Linear Search
Create a program in Java to input an array from the user and find a particular element using
binary search.
import java.util.*;
class Program12
{
public static void main(String args[])
{
int flag=1;
Scanner sc=new Scanner(System.in);
int a[]=new int[5];
for(int i=0;i<=4;i++)
{
System.out.println("Enter value");
a[i]=sc.nextInt();
}
System.out.println("Enter search value");
int n=sc.nextInt();
for(int i=0;i<=4;i++)
{
if(a[i]==n)
{
System.out.println("Element found at index : "+i);
flag=1;
break;
}
else
{
flag=0;
}
}
if(flag==0)
{
System.out.println("Element not found");
}
}
21
}
Output:
22
1.13 Recursion
Create a program in java to add the squares of the first ten consecutive numbers and print the
output using recursion.
class Program13
{
public static void main(String args[])
{
int x=1;
System.out.println(sum(x));
}
public static int sum(int n)
{
int s=0;
if (n>10)
return s;
else
{
s=s+(n*n);
return s+sum(n+1);
}
}
}
Output:
23
1.14 Constructor Overloading
Create a program to calculate the area of a rectangle demonstrating constructor overloading.
import java.util.*;
class Program14
{
int length,breadth; //length and breadth of rectangle respectively
Program14(int l, int b)
{
length=l;
breadth=b;
}
Program14(Program14 ob)
{
length=ob.length;
breadth=ob.breadth;
}
24
}
Output:
25
1.15 Function Overloading
Create a program in Java demonstrating Function Overloading, with different series being
printed.
import java.util.*;
class Program15
{
public static void series(int x, int n)
{
double s=0;
for(int i=1;i<=n;i++)
{
s=s+Math.pow(x,i);
}
System.out.println(s);
}
public static void series(int p)
{
for(int i=1;i<=p;i++)
{
System.out.println(((i*i*i)-1));
}
26
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of terms for series 1");
int n=sc.nextInt();
System.out.println("Enter the number for series 1");
int x=sc.nextInt();
System.out.println("Enter the no. of terms for series 2");
int p=sc.nextInt();
System.out.println("SERIES 1");
series(x,n);
System.out.println("SERIES 2");
series(p);
System.out.println("SERIES 3");
series();
}
}
Output:
27
1.16 This Keyword
Create a program in Java using this keyword to display the roll no., name, and fee of two
students.
class Program16
{
int rollno;
String name;
float fee;
Program16(int rollno,String name,float fee)
{
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display()
{
System.out.println(rollno+" "+name+" "+fee);
}
public static void main(String args[]){
Program16 s1=new Program16(111,"Tahira",5000f);
Program16 s2=new Program16(112,"Xyz",6000f);
s1.display();
s2.display();
}
}
Output:
28
1.17 Break and Continue Statements
Create a program in Java checking whether a number is prime or not using break and continue
statements.
import java.util.*;
class Program17
{
public static void main(String args[])
{
int flag=1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter no.");
int n=sc.nextInt();
for(int i=2;i<n;i++)
{
if(n%i==0)
{
flag=0;
break;
}
else
{
flag=1;
continue;
}
}
if(n==1)
{
System.out.println("Unique");
}
if(flag==1)
{
System.out.println("Prime");
}
if(flag==0)
{
System.out.println("Not prime");
29
}
}
}
Output:
30
1.18 Nested if
Create a program in Java demonstrating the concept of Nested if, through if-inside-else case.
import java.util.*;
class Program18
{
public static void main(String args[])
{
double t=0;
double tt=0;
double sur=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter your salary per annum");
int s=sc.nextInt();
if(s<=50000)
{
tt=0;
}
else
{
if(s>50000&&s<=150000)
{
t=(10*s)/100;
}
else
{
if(s>150000&&s<=300000)
{
t=(15*s)/100;
}
else
{
if(s>300000&&s<=500000)
{
t=(20*s)/100;
}
else
31
{
if(s>500000)
{
t=(25*s)/100;
}
}
}
}
System.out.println("Your income tax is "+t);
}
}
Output:
32
1.19 Sum of Sequences
Create a program printing the sum of series S=1+x+x2 +...+xn.
import java.util.*;
class Program19
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter x");
double x=sc.nextDouble();
System.out.println("Enter n");
double n=sc.nextDouble();
double s=0;
for(int i=0; i<=n; i++)
{
s=s+Math.pow(x,i);
}
System.out.println("Sum: "+s);
}
}
Output:
33
1.20 Sum of Sequences
Create a program printing the sum of the series S=1+1/1!+1/2!+1/3!+...
import java.util.*;
class Program20
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no. of numbers");
int n=sc.nextInt();
double s=1;
for(int i=1; i<=n; i++)
{
s=s+(1/fact(i));
}
System.out.println("Sum: "+s);
}
public static int fact(int n)
{
int f=1;
for(int i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
}
Output:
34