Posts

Showing posts with the label Java

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:

Program to simulate a menu driven calculator

Image
Program: import java.util.*; class calculator { public static void main(String args[]) { int a,b,n; Scanner sc=new Scanner(System.in); System.out.println("1. Addition"); System.out.println("2. Subtraction"); System.out.println("3. Multiplication"); System.out.println("4. Division"); System.out.println("5. Modulus"); System.out.println("Enter your choice"); n=sc.nextInt(); System.out.println("Enter two numbers"); a=sc.nextInt(); b=sc.nextInt(); switch(n) { case 1: System.out.println("Result = "+(a+b)); break; case 2: System.out.println("Result = "+(a-b)); break; case 3: System.out.println("Result = "+(a*b)); break; case 4: System.out.println("Result = "+(a/b)); break; case 5: System.out.println("Result = "+(a%b)); break; default: System.out.pr...

Program to display Human face using applet

Image
PROGRAM: import java.awt.*; import java.applet.*; /* <applet CODE="Face.class" WIDTH=200 HEIGHT=200> </applet> */ public class Face extends Applet { public void paint(Graphics g) { g.drawOval(40,40,120,150); //head g.drawOval(57,75,30,20); //left eye g.drawOval(110,75,30,20); //right eye g.fillOval(68,81,10,10); //pupil (left) g.fillOval(121,81,10,10); // pupil( right) g.drawOval(85,100,30,30); //nose g.fillArc(60,125,80,40,180,180); // mouth g.drawOval(25,92,15,30); // left ear g.drawOval(160,92,15,30); // right ear } } } OUTPUT:

program to count frequency of a given Letter in a String

Image
Program: import java.util.*; import java.lang.*; class count { public static void main(String[] args) { int n,k=0,i; String str = ""; Scanner sc = new Scanner(System.in); System.out.println("Enter String: "); str = sc.nextLine(); n = str.length(); System.out.println("Enter character whose occurences to count: "); char ch=sc.next().charAt(0); char c[] = new char[n]; c = str.toCharArray(); for(i=0;i<n;i++) { if(c[i]==ch) k++; } System.out.println("Total occurences = "+k+" times."); } Output:

Bubble sort

Image
WAP to Sort the elements of an array in ascending order using bubble sort Program:     import java.util.Scanner;     class Ascending2    {         public static void main(String[] args)         {             int n, temp;             Scanner s = new Scanner(System.in);             System.out.print("Enter no. of elements you want in array:");             n = s.nextInt();             int a[] = new int[n];             System.out.println("Enter all the elements:");             f...

Upper Triangular Pattern

Image
WAP to print Upper Triangular Pattern Program: import java.util.*; class Pattern {  public static void main(String args[])  {   int i,j,n;   char k='A';    Scanner sc=new Scanner(System.in);    System.out.println("Enter the number of rows");    n=sc.nextInt();    System.out.print("\n");     for(i=0;i<n;i++)           {             for(j=0;j<=i;j++)           {                           System.out.print("\t"+k);                    k++;           }    ...

Upper triangle number pattern

Image
WAP to print upper triangle number pattern Program: import java.util.*; class Pattern {  public static void main(String args[])  {   int i,j,k=1,n;    Scanner sc=new Scanner(System.in);    System.out.println("Enter the number of rows");    n=sc.nextInt();    System.out.print("\n");     for(i=0;i<n;i++)           {             for(j=0;i>=j;j++)           {                    System.out.print(k);                    k++;           }              System.out.pri...