Showing posts with label programs. Show all posts
Showing posts with label programs. Show all posts

Saturday, 20 October 2012

Accept n numbers through one program send it to the server program check their for prime numbers send only the prime numbers to client-Java Socket program

tcpserver1.java

import java.io.*;
import java.net.*;
class tcpserver1
{
public static void main(String args[])throws Exception
{
ServerSocket ss = new ServerSocket(6856);
Socket cs=ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(cs.getInputStream()));
PrintStream ps = new PrintStream(cs.getOutputStream());
int[] a=new int[15];
int[] b= new int [15];
int k=0,n,i,j,f=0;
n=Integer.parseInt(br.readLine());
for(i=0;i<n;i++)
a[i]=Integer.parseInt(br.readLine());
for(i=0;i<n;i++)
{
for(j=2;j<a[i];j++)
{
if(a[i]%j==0)
{
f=1;
break;
}
}
if(f==0)
{
if(a[i]!=1)
{
b[k]=a[i];
k++;
}
}
f=0;
}
ps.println(k);
for(i=0;i<k;i++)
ps.println(b[i]);

}
}

tcpclient1.java

import java.io.*;
import java.net.*;
class tcpclient1
{
public static void main(String args[])throws Exception
{
Socket cs = new Socket("localhost",6856);

BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new InputStreamReader(cs.getInputStream()));
PrintStream ps = new PrintStream(cs.getOutputStream());
int[] a=new int[15];
int[] b= new int [15];
int k,n,i;
System.out.println("Enter the limit");
n=Integer.parseInt(kb.readLine());
ps.println(n);
System.out.println("Enter the Numbers");
for(i=0;i<n;i++)
a[i]=Integer.parseInt(kb.readLine());
for(i=0;i<n;i++)
ps.println(a[i]);
k=Integer.parseInt(br.readLine());
for(i=0;i<k;i++)
b[i]=Integer.parseInt(br.readLine());
System.out.println("Prime nos\n");
for(i=0;i<k;i++)
System.out.println(b[i]);

}
}

Communication between two unrelated process using messege queue

MSGQ1.c 

#include<stdio.h>
#include<sys/msg.h>
#include<string.h>
#include<stdlib.h>
struct msgbuf
{
long mtype;
char mtext[30];
}ptr;
int main()
{
int i=0;
int id=msgget(52,0666|IPC_CREAT);
if(id>=0)
printf("Msg Q created sucessfully\n");
else
printf("Not created\n");
printf("Enter the msg type:\n");
scanf("%ld",&(ptr.mtype));

printf("Enter the msg\n");
scanf("%s",ptr.mtext);
int p=msgsnd(id,&ptr,(sizeof(ptr.mtext)),0);
if(p>=0)
printf("Msg send sucessfully\n");
else
printf("Msg not send\n");
}




MSGQ2.C



#include<stdio.h>
#include<sys/msg.h>
struct msgbuf
{
long mtype;
char mtext[30];
}ptr;
int main()
{

int id=msgget(52,0666|IPC_CREAT);
if(id>=0)
printf("Success\n");
printf("Enter the msgtype\n");
scanf("%ld",&ptr.mtype);

int p=msgrcv(id,&ptr,30,(ptr.mtype),0);
if(p>=0)
{
printf("Msg read sucessfully\n");
printf("Msg is:%s\n",ptr.mtext);
}
else
printf("Error\n");
}


Sample output


msgq1.c

Enter the msg type:
2
Enter the msg
kkk
Msg send sucessfully


msgq2.c

Success
Enter the msgtype
2
Msg read sucessfully
Msg is:kkk





Wednesday, 2 May 2012

Sorting of matrix elements-C++ program


#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
    int a[20][20],n,b[20],m,i,j,k=0,t;
    cout<<"Enter the order\n";
    cin>>m>>n;
    cout<<"Enter the elements\n";
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
    {
    cin>>a[i][j];
    b[k++]=a[i][j];
     }
     }
     for(i=0;i<(m*n);i++)
     {
     t=b[i];
     j=i;
     while(j>0 && b[j-1]>=t)
     {
               b[j]=b[j-1];
               j--;
               }
               b[j]=t;
               }
             
               cout<<"\n";
               cout<<"Before sorting\n";
               for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
   
    cout<<a[i][j]<<"\t";
    cout<<"\n";
}
               cout<<"After sorting\n";
               k=0;
    for(i=0;i<m;i++)
   
    for(j=0;j<n;j++)
   
    a[i][j]=b[k++];
   
   
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
   
    cout<<a[i][j]<<"\t";

cout<<"\n";
}

    getch();
}





Sample Output
Enter the order
 3 3
9
8
7
4
5
2
1
3
6
Before sorting
9   8   7
4   5   2
1   3   6
After sorting
1   2   3
4   5   6
7   8   9



No third variables and pointers-C++ swapping program


#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
    int a,b;
    cout<<"Enter two number:\t";
    cin>>a>>b;
    cout<<"\nBefore swapping:\t";
    cout<<a<<"\t"<<b;
   
    a=a+b;
    b=a-b;
    a=a-b;
    cout<<"\nAfter swapping:\t";
    cout<<"\t"<<a<<"\t"<<b;
    getch();
}

Wednesday, 7 March 2012

Masm program to check a given year is leap year or not


printmsg macro msg
mov ah,09h
lea dx,msg
int 21h
endm

printnum macro nu
mov dl,nu
add dl,30h
mov ah,02h
int 21h
endm

calcnum macro regx
mov ax,regx
mov bl,100
div bl
mov dh,ah
aam
mov bl,al
printnum ah
printnum bl
mov al,dh
aam
mov bl,al
printnum ah
printnum bl

endm

data segment
n db,0000
n1 db ,0000
msg1 db 0ah,0dh,"Enter a year$"
msg2 db 0ah,0dh,"Leap year$"
msg3 db 0ah,0dh,"Not leap year$"
msg4 db 0ah,0dh,"$"
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax

printmsg msg1
mov ah,01h
int 21h
sub al,30h
mov ah,00
mov cx,1000
mul cx
mov n,ax

mov ah,01h
int 21h
sub al,30h
mov ah,00
mov cx,100
mul cx
add ax,n
mov n,ax

mov ah,01h
int 21h
sub al,30h
mov ah,00
mov cl,10
mul cl
add ax,n
mov n,ax

mov ah,01h
int 21h
sub al,30h
mov ah,00
add ax,n
mov n,ax

mov cx,0004
mov dx,0000
mov ax,n
div cx

cmp dx,0000
je t1
jmp t3
t1:
mov cx,0100
mov dx,0000
mov ax,n
div cx

cmp dx,0000
jne t2
jmp t3
t2:
printmsg msg2
jmp st1
t3:
mov ax,n
mov bx,400
div bx
cmp dx,0000
je t2

printmsg msg3



st1:
mov ah,4ch
int 21h
code ends
end start

Monday, 6 February 2012

Masm program to sort n numbers


printmsg macro msg
mov ah,09h
lea dx,msg
int 21h
endm

printnum macro reg
mov dl,reg
add dl,30h
mov ah,02h
int 21h
endm

calcnum macro regx
mov ax,regx
mov dh,100
div dh
mov n,ah
aam
mov dh,al
printnum ah
printnum dh
mov al,n
aam
mov dh,al
printnum ah
printnum dh
endm


data segment
num db 0
array db 50 dup(0)
n db 0



cr equ 0dh
msg0 db "Enter a limit(Ex:05)$"
msg1 db "Enter the nos(Ex:06)$"
msg2 db 0ah,0dh,"After sorting$"
msg4 db 0ah,"$"
msg5 db 0ah,0dh,"Before sorting$"
data ends

code segment
assume cs:code,ds:data

start:
mov ax,data
mov ds,ax
mov cl,00
printmsg msg0
mov ah,01h
int 21h
sub al,30h
mov n,al
mov ah,00
mov dh,10
mul dh
mov n,ax
mov ah,01h
int 21h
sub al,30h
mov ah,00
add ax,n

mov cl,ax
mov num,cl
printmsg msg4
printmsg msg1
lea si,array
l1:

mov ah,01h
int 21h
sub al,30h
mov ah,00
mov ch,10
mul ch
mov n,ax
mov ah,01h
int 21h
sub al,30h
mov ah,00
add ax,n
mov [si],ax

inc si
dec cl
printmsg msg4
cmp cl,00
jne l1



printmsg msg4
mov cl,num
mov ch,00
printmsg msg5
printmsg msg5
lea si,array
t1:
printmsg msg4
cmp cl,ch
je t2

mov ah,00
mov al,[si]
calcnum ax

inc ch
inc si
jmp t1
t2:
printmsg msg4
mov cl,num
mov bh,00
mov bl,00
mov dl,num
dec dl
lea si,array
i1:

cmp bh,cl
je stop
mov di,si
inc di
mov bl,bh
inc bl
j1:
cmp bl,cl
je jstop
mov ch,[si]
cmp ch,[di]
jc k1
xchg ch,[di]
mov [si],ch
k1:
inc bl
inc di
jmp j1
jstop:
inc si
inc bh
jmp i1
stop:


mov cl,num
lea si,array
printmsg msg4
printmsg msg2
q1:
printmsg msg4
mov ah,00
mov al,[si]
calcnum ax
dec cl
inc si
cmp cl,00
jne q1
mov ah,4ch
int 21h
code ends
end start




Count number of words in a sentence-Masm program


printmsg macro msg
mov ah,09h
lea dx,msg
int 21h
endm

printnum macro reg
mov dl,reg
add dl,30h
mov ah,02h
int 21h
endm

data segment

str db 40 dup(0)

cr equ 0dh
lf equ 0ah
msg1 db "Enter a string:$"
msg4 db 0ah,0dh,"$"
msg5 db 0ah,0dh,"No: of words$"
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
lea si,str
mov cl,00

printmsg msg1
printmsg msg4
mov ch,'$'
mov [si],ch
l1:
mov ah,01h
int 21h
cmp al,32
je v1
jmp v2
v1:inc cl
jmp v2
v2:cmp al,cr
je p3
sub al,30h
mov [si],al
inc si
mov [si],ch
jmp l1

p3:
mov si,offset str
mov ch,[si]
cmp ch,'$'
je d1
inc cl
jmp d2
d1:mov cl,00



d2:printmsg msg4
printmsg msg5
printmsg msg4
printnum cl




mov ah,4ch
int 21h
code ends
end start



Twitter Delicious Facebook Digg Stumbleupon Favorites More