Posts

Program to implement sequential search algorithm

Image
Program: import java.util.*; class LinearSearch { public static void main(String args[]) { int c, n, search, array[]; Scanner in = new Scanner(System.in); System.out.println("Enter number of elements"); n = in.nextInt(); array = new int[n]; System.out.println("Enter " + n + " integers"); for (c = 0; c < n; c++) array[c] = in.nextInt(); System.out.println("Enter value to find"); search = in.nextInt(); for (c = 0; c < n; c++) { if (array[c] == search) /* Searching element is present */ { System.out.println(search + " is present at location " + (c + 1) + "."); break; } } if (c == n) /* Searching element is absent */ System.out.println(search + " is not present in array."); } } Output:

Program to draw shape of rectangle, circle and oval with different color

Image
Program: import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; /* <applet code="colordraw.class" HEIGHT=1000 WIDTH=1000> </applet> */ public class colordraw extends Applet { public void paint(Graphics g) { g.setColor(Color.green); g.drawRect(20,20,800,600); g.setColor(Color.yellow); g.drawOval(70,70,500,500); g.setColor(Color.blue); g.drawOval(120,120,50,90); } } Output:

Program to compute sum 1 to N numbers using recursion

Image
Program: import java.util.*; public class Sum { public static void main(String args[]) { System.out.print("Enter any number:"); Scanner sc = new Scanner(System.in); int number = sc.nextInt(); int sum = sumN(number); System.out.println("Sum of " + number + " Numbers using Recursion is : " + sum); } public static int sumN(int num) { int sum; if(num==1) return(1); else sum=num + sumN(num-1); return sum; } Output:

Program to add two matrix of size m*n

Image
Program: import java.util.*; class Mat { public static void main(String args[]) { int i,j,m,n; Scanner sc=new Scanner(System.in); System.out.println("enter no of rows & column"); m=sc.nextInt(); n=sc.nextInt(); int a[][]=new int [m][n]; int b[][]=new int [m][n]; int c[][]=new int [m][n]; System.out.println("Enter the first matrix"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { a[i][j]=sc.nextInt(); } } System.out.println("Enter the second matrix"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { b[i][j]=sc.nextInt(); } } System.out.println("first matrix is=:"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { System.out.print("\t"+ a[i][j]); } System.out.println(); } System.out.println("second matrix is=:"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { System.out.print("\t"+ b[i][j]); } ...

Program to calculate permutation and combination

Image
Program: import java.util.*; class Pattern { public static void main (String[] args) { int nCr,nPr,n,r; Scanner sc=new Scanner(System.in); System.out.println("Enter the value of n and r"); n=sc.nextInt(); r=sc.nextInt(); nPr=fact(n)/fact(n-r); nCr=fact(n)/(fact(r)*fact(n-r)); System.out.println("nCr= "+nCr); System.out.println("nPr= "+nPr); } static int fact(int n) { int f=1; if(n==0) return 1; else { for(int i=1;i<=n;i++) f=f*i; return f; } } } Output:

Program to find the multiplication table of entered number

Image
Program: import java.util.*; class Table {public static void main(String args[]) { int n; Scanner sc=new Scanner(System.in); System.out.print("Enter the number : "); n=sc.nextInt(); for(int i=1;i<=10;i++) { System.out.println(n+"*"+i+"="+(i*n)); } } } Output:

Program to reverse the number & calculate the sum of its digit

Image
Program: import java.util.*; class Reverse {public static void main(String args[]) { int n,sum=0,r,rev=0; Scanner sc=new Scanner(System.in); System.out.println("Enter the number : "); n=sc.nextInt(); while(n!=0) { r=n%10; sum=sum+r; rev=rev*10+r; n=n/10; } System.out.println("Reverse of the number is: "+rev); System.out.println("Sum of the digits is : "+sum); } } Output: