Posts

Servlet Program to Print Today’s Date and Time using refresh header

Simple Java Servlet Program to Print Date and Time using refresh header (Time updates automatically) Code: import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.Date; public class servlet2 extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException { rsp.setIntHeader("Refresh", 1); rsp.setContentType("text/html"); PrintWriter out = rsp.getWriter(); Date now = new Date(); out.println("<html>"); out.println("<head><title> Time Check </title></head>"); out.println("<body>"); out.println("<p text-align=auto>Current Time: " + now + "</p>"); out.println("</body></html>"); } }

Java program to implement Word Count using Map-Reduce in Hadoop Cluster

Simple Java program to implement Word Count using Map-Reduce in Hadoop Cluster Code: import java.io.IOException; import java.util.StringTokenizer;  import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;   public class wc {     public static class TokenizerMapper     extends Mapper<Object, Text, Text, IntWritable>{   private final static IntWritable one = new IntWritable(1); private Text word = new Text();   public void map(Object key, Text value, Context context                 ) throws IOException, InterruptedException {   StringTokenizer ...

C program to implement Doubly Circular Linked List Data Structure

Simple C program to implement Doubly Circular Linked List Data Structure along with following operations: Insertion at end Insertion at front Insert after a value Insert before a value Delete at end Delete at front Delete after a value Delete before a value Delete element itself Delete the whole list Display linked list Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> struct node{ int data; struct node *next; struct node *prev; }; struct node *head,*tail; void insertend(){ int val; printf("\nEnter the value to be Entered: "); scanf("%d",&val); struct node *head2; head2=head; struct node *temp; temp=(struct node*)malloc(sizeof(struct node)); temp->data=val; temp->next=NULL; temp->prev=NULL; if(head==NULL){ head=temp; temp->next=head; temp->prev=head; } else{ while(head2->next!=head){ head2=head2->next; } temp->prev=head2; temp->next=head; head2->next=temp; tail=temp; head-...

Java program to implement Apriori Algorithm

Simple Java program to implement Apriori Algorithm Code: package apriori; import com.sun.xml.internal.ws.util.StringUtils; import java.util.*; import java.lang.Object; public class Apriori { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Number Of Transactions:"); int n = sc.nextInt(); System.out.println("Enter Minumum Limit:"); int min = sc.nextInt(); sc.nextLine(); ArrayList<Set<String>> aray = new ArrayList<>(); Set<String> list = new HashSet<>(); Set<String> list1= new HashSet<>(); Set<String> list2,list3,list4; System.out.println("Enter Item id saperated by space per transaction"); for(int i=0;i<n;i++){ System.out.println("Entere Item Id included in transaction-"+(i+1)); String tempp =sc.nextLine(); Se...

Java program to check the given string is palindrome or not without using Reverse String

Simple Java program to check the given string is palindrome or not without using Reverse String Code: import java.util.Scanner; class p84{public static void main(String args[]){ Scanner sc = new Scanner(System.in); String s = sc.nextLine(); boolean palindrome=true; int j=(s.length()-1); for(int i=0;i<(s.length()/2);i++){ if(s.charAt(i)!=s.charAt(j)){palindrome=false;} j--; } if(palindrome){System.out.println("palindrome");} else{System.out.println("Not palindrome");} }}

Java program to count words starting with capital letters in given line

Simple Java program to count words starting with capital letters in given line Code: import java.util.Scanner; class p83{public static void main(String args[]){ Scanner sc=new Scanner(System.in); String s; System.out.println("Enter a line:"); s=sc.nextLine(); int count=0; for(String str: s.split(" ")) { if(str.charAt(0)>=65 && str.charAt(0)<=90) { count++; } } System.out.println("total number of words start with capital letters are :"+count); }}

Java program to calculate occurrence of consonants and vowels in given line

Simple Java program to calculate the number of consonants and vowels present in given line Code: import java.util.Scanner; class p82{public static void main(String args[]){ String s = new String(); int co=0,cs=0; Scanner sc = new Scanner(System.in); s=sc.nextLine(); for(int i=0;i<s.length();i++){ char c = s.charAt(i); if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U'){co++;} else if(!(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U') && ((c>=65 && c<=90) || (c>=97 && c<=122))){cs++;} } System.out.println("Vowels: "+co); System.out.println("Consonant: "+cs); }}

Java program to implement concept of Abstract Class

Simple Java program to implement concept of Abstract Class Code: abstract class phone{ void rearcamera(){System.out.println("Capturing from rear camera");} void rflesh(){System.out.println("Capturing from rear camera");} void frontcamera(){System.out.println("Capturing from front camera");} abstract void fflesh(); } class model extends phone { void fflesh(){System.out.println("No front flesh available");} } class abstracte{ public static void main(String arg[]){ model e70 = new model(); e70.rearcamera();e70.rflesh();e70.frontcamera();e70.fflesh(); } }

Java program to implement use of Final Keyword on Class, Method and Variable

Image
Simple Java program to use Final Keyword on following items: Variable Class Method Code: class a{ final int a=5; void setfinala(int x) { a=x; } final void finalmethod() { System.out.println("Final method."); } } class a1 extends a{ void finalmethod() { System.out.println("Final method in extended class."); } } final class b{ } class c extends b{ } class p33{ public static void main(String args[]){ a obj = new a(); a.setfinala(3); } } Note: The program won't run because of error and that error is output of our program.

Java program to implement concept of Inner Class

Simple Java program to implement use of Inner Class Code: class Outer{ private int data=30; class Inner{ void msg(){System.out.println("data is "+data);} } } class innersample{ public static void main(String args[]){ Outer obj=new Outer(); Outer.Inner in=obj.new Inner(); in.msg(); } }

Java program to check palindrome number using recursion

Simple Java program to check whether the number is palindrome or not by using Recursion. Code: import java.util.Scanner; class xyz{ int pd(int i,int j,int[] a){ while(i<j){ if(a[i]==a[j]){i++;j--;pd(i,j,a);} else{return 0;} } return 1; } } class palindrome{ public static void main(String args[]){ int x,i=0; System.out.println("Enter integer: "); Scanner sc = new Scanner(System.in); x=sc.nextInt(); int a[]=new int[10]; while(x!=0){ a[i]=x%10; x=x/10; i++; } xyz obj = new xyz(); if(obj.pd(0,i-1,a)==1){System.out.println("palindrome");} else{System.out.println("Not Palindrome");} } }

Java program to implement patterns

Simple Java program to implement the following pyramid pattern. 1 2 2 3 3 3 4 4 4 4    and 4 3 3 2 2 2 1 1 1 1 Code: public class pattern{ public static void main(String srgs[]){ System.out.println("first pattern"); int a=4; for(int i=1;i<=4;i++){ System.out.println(" "); for(int j=1;j<=i;j++){System.out.print(" " +i);} } System.out.println(" "); System.out.println("Second pattern"); for(int i=4;i>=1;i--){ System.out.println(" "); for(int j=1;j<=i;j++){System.out.print(" " +i);} } }} Si...

C program to implement Graph data structure with Depth First Search and Breadth First Search

Simple C program to implement Graph data structure with following operations: Depth First Search(DFS) Breadth First Search(BFS) Code: #include<stdio.h> int q[20],top=-1,front=-1,rear=-1,a[20][20],vis[20],stack[20]; int delete(); void add(int item); void bfs(ints,int n); void dfs(ints,int n); void push(int item); int pop(); void main() { intn,i,s,ch,j; char c,dummy; printf("ENTER THE NUMBER VERTICES "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 ",i,j); scanf("%d",&a[i][j]); } } printf("THE ADJACENCY MATRIX IS\n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf(" %d",a[i][j]); } printf("\n"); } do { for(i=1;i<=n;i++) vis[i]=0; printf("\nMENU"); printf("\n1.B.F.S"); printf("\n2.D.F.S"); printf("\nENTER YOUR CHOICE"); scanf("%d",&ch); p...

C program to implement Tree data structure and its operations

Simple C program to implement Tree data structure having following operations: Insert a value Inorder Display Preorder Display Postorder Display Search An Element Delete An Element Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> struct node{ int data; struct node *left; struct node *right; }; struct node *root; struct node *newnode(){ int val; printf("\nEnter the value to be inserted:"); scanf("%d",&val); struct node *temp; temp=(struct node*)malloc(sizeof(struct node)); temp->left=NULL; temp->right=NULL; temp->data=val; return temp; } void insert(struct node *root, struct node *root2) { if (root2->data <= root->data) { if (root->left == NULL) root->left = root2; else insert(root->left, root2); } if (root2->data > root->data) { if (root->right == NULL) root->right = root2; else i...

C program to multiply two matrices and create Single Result Matrix

Simple C program to do multiplication of given matrices Code: #include<conio.h> #include<stdio.h> void main() { clrscr(); int a[3][3],b[3][3],c[3][3],i,j,k; printf("Enter the matrix values of A:"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Enter the element number A%d%d:",i,j); scanf("%d",&a[i][j]); } } printf("Enter the matrix values of B:"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Enter the element number B%d%d:",i,j); scanf("%d",&b[i][j]); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=0; for(k=0;k<3;k++) { c[i][j]=c[i][j]+a[i][k] *b[k][j]; } } } Printf(“Result matrix: “); for(i=0;i<3;i++) { printf("\n"); for(j=0;j<3;j++) { printf("%d",c[i][j]); } } getch(); }

C program to add two matrices into single Matrix

Simple C program to do addition of given matrices Code: #include<stdio.h> #include<conio.h> void main() { clrscr(); int a[4][4],b[4][4],c[4][4]; int i,j,k,m,n,p,q; printf("\nenter no. of rows for matrix A:"); scanf("%d",&m); printf("\nenter no. of coloums for matrix A:"); scanf("%d",&n); printf("\nenter no. of rows for matrix B:"); scanf("%d",&p); printf("\nenter no. of coloums for matrix B:"); scanf("%d",&q); printf("\nenter array A:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } printf("\n"); } printf("\nenter array B:\n"); for(i=0;i<p;i++) { for(j=0;j<q;j++) { scanf("%d",&b[i][j]); } printf("\n"); } if((m==p)&&(n==q)) { printf("\naddition of array is:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[i][j]=a[i][j]+b[i][j]; printf("%d ",c[i][j]); } p...

C program to reverse the elements of an array

Simple C program to reverse elements of an array Code: #include<stdio.h> #include<conio.h> void main() { int n,a[10],i,j,e; clrscr(); printf("enter limit : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter number of a[%d] : ",i); scanf("%d",&a[i]); } for(i=0,j=n-1;i<n/2;i++,j--) { e=a[i]; a[i]=a[j]; a[j]=e; } Printf(“Reversed string: “); for(i=0;i<n;i++) { printf("\n %d ",a[i]); } getch(); }

C program to merge two arrays into single array

Simple C program to merge two arrays into a single array Code: #include<stdio.h> #include<conio.h> int main(){ int a[50],b[50],c[100],i,j=0,n,m; clrscr(); printf("Enter the length of first array: "); scanf("%d",&n); printf("Enter the length of second array: "); scanf("%d",&m); printf("Enter the element of FIRST array: "); for (i=0;i<n;i++){ scanf("%d",&a[i]);} printf("Enter the element of second array: "); for(i=0;i<m;i++){ scanf("%d",&b[i]);} for (i=0;i<(n+m);i++){ if(i>n-1){ c[i]=b[j]; j++; } else{ c[i]=a[i]; } } Printf(“The final Array: ”); for (i=0;i<(m+n);i++){ printf("\n%d",c[i]); } getch(); }

C program to find an element from Array

Simple C program to find an element from Array Code: #include<stdio.h> #include<conio.h> void main() { int a[20],i,x,n; clrscr(); printf(“How many elements?”); scanf(“%d”,&n); printf(“Enter array elements:”); for(i=0;i<n;++i) scanf(“%d”,&a[i]); printf(“nEnter element to search:”); scanf(“%d”,&x); for(i=0;i<n;++i) if(a[i]==x) break; if(i<n) printf(“Element found at index %d”,i); else printf(“Element not found”); getch(); }

C program to find sum of numbers of an array

Simple C program to find sum of numbers of an array Code: #include<stdio.h> #include<conio.h> void main() { clrscr(); int a[50]; int l,i; int sum=0; printf("\n enter length of array:"); scanf("%d",&l); printf("\n enter array:"); for(i=0;i<l;i++) { scanf("%d",&a[i]); sum+=a[i]; } printf("\nsum of array elements is:%d",sum); getch(); }