Showing posts with label program. Show all posts
Showing posts with label program. Show all posts

Monday, 23 January 2017

MATLAB PROGRAM TO EVALUATE QUADRATIC EQUATION(Ax^2+Bx+C=0)

prompt = 'Enter A'
A= input(prompt) % Read A from keyboard
prompt = 'Enter B'
B= input(prompt) % Read B from keyboard
prompt = 'Enter C'
C= input(prompt) % Read C from keyboard


quat=[A,B,C]      
   
x=roots(quat)  % roots is a function to evaluate polynomial

Sunday, 7 October 2012

Two unrelated programs uses shared memory

First program


#include<stdio.h>
#include<sys/shm.h>
#include<string.h>
int main()
{

int id=shmget(17,50,0666|IPC_CREAT);


if(id>=0)
{
printf("\nShared memory created with id:%d\n",id);
}
char *adr=shmat(id,NULL,0);
if(adr>=0)
printf("Memory attached\n");
printf("Enter a string\n");
char str[10];

scanf("%s",str);
strncpy(adr,str,sizeof(str));



}



Second program

#include<stdio.h>
#include<sys/shm.h>
#include<string.h>
int main()
{

int id=shmget(17,0,0666|IPC_CREAT);
char str[10];

if(id>=0)
{
printf("\nShared memory with id:%d\n is accessed",id);
}
char *adr=shmat(id,NULL,0);
if(adr>=0)
printf("\nMemory attached\n");
printf("\nThe string is\n%s",adr);
}


Sample output:

cc first.c
./a.out

Shared memory created with id:360454
Memory attached
Enter a string
Hello

cc second.c
./a.out

Shared memory with id:360454
 is accessed
Memory attached

The string is
hello


Friday, 3 February 2012

Binary to Decimal converter- 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

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

multiply macro num1
mov ax,0001
mov dh,02
mov dl,00
l10:
cmp dl,num1
jz l9
mul dh
inc dl
jmp l10
l9:
endm

data segment
num dw ?
num1 db 0

array db 10 dup(0)

cr equ 0dh
msg1 db "Enter a binary number$"
msg2 db 0ah,0dh,"Decimal Equivalant$"

msg4 db 0ah,"$"
data ends

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


printmsg msg1
lea si,array
mov bx,si

printmsg msg4

l2:mov ah,01h
int 21h
cmp al,cr
je l1
sub al,30h
mov [si],al
inc si
inc cl
jmp l2

l1:mov bx,0000
mov si,offset array

l5:

cmp cl,00
jne l6
jmp l7
l6:

multiply cl
mov dh,[si]
mul dh
add bx,ax
inc si
dec cl
jmp l5

l7:
printmsg msg4
printmsg msg2

printmsg msg4
mov ax,bx
mov dh,02
 div dh
mov ah,00
calcnum ax

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






Sunday, 29 January 2012

Check a number is Armstrong or not-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
n dw ?
p dw 00
rem dw ?
z dw ?

cr db,0dh,"$"
msg1 db "Enter a number(Ex:0123)$"
msg2 db 0ah,0dh,"Armstrong$"
msg3 db 0ah,0dh,"Not Armstrong$"
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
mov ah,00
sub al,30h
mov dx,1000
mul dx
mov n,ax
mov ah,01h
int 21h
mov ah,00
sub al,30h
mov dh,100
mul dh
add ax,n
mov n,ax
mov ah,01h
int 21h
sub al,30h
mov ah,00
mov dh,10
mul dh
add ax,n
mov n,ax
mov ah,01
int 21h
sub al,30h
mov ah,00
add ax,n

mov n,ax
mov z,ax
mov cl,04

l2:
mov ax,n
mov dh,10
div dh
mov rem,al
mov al,ah
mov ch,ah
mov ah,00
mul ch
mul ch
add ax,p
mov p,ax
mov ax,rem
mov n,ax
dec cl
cmp cl,00
jne l2


mov dx,p
cmp z,dx
je l3
l4:
printmsg msg3
jmp skip
l3:
printmsg msg2








skip:mov ah,04h
int 21h
code ends
end start



Masm program to find prime numbers upto a limit

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 num,ah
aam
mov dh,al
printnum ah
printnum dh
mov al,num
aam
mov dh,al
printnum ah
printnum dh
endm

data segment
num dw ?
i dw 02
j dw ?
n dw ?

msg1 db "Enter a limit$"
msg2 db 0ah,0dh,"Prime nos:$"
msg3 db 0ah,0dh,"$"
data ends

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

printmsg msg1

mov dh,02
mov ah,01h
int 21h
mov ah,00
sub al,30h

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

add ax,n
inc ax
mov n,ax

printmsg msg2


mov ah,00
mov cx,n
l3:
cmp cx,i
je l1

mov j,02
mov ax,i
mov dh,02
div dh
mov bl,al
l6:cmp j,bl
jnc l2
l7:mov ax,i
mov dl,j
div dl
cmp ah,00
je l9
inc j
jmp l6

l2:
jz l7
printmsg msg3
calcnum i
l9:inc i
jmp l3

l1:mov ah,04h
int 21h
code ends
end start

Factorial of a number-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

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

data segment
num dw  ?
msg1 db "Enter a number$"
msg2 db 0ah,0dh,"Factorial:$"
msg3 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 cl,al
mov ch,00
mov ax,0001


l1:
mul cx

dec cl
cmp cl,01
jne l1


mov bx,ax
printmsg msg2
calcnum bx


mov ah,04h
int 21h
code ends
end start

Sunday, 7 August 2011

C++ program to generate pascal's triangle without using array

Hai friends,
Here i give   c++ program to generate Pascal's  triangle. Many people use 2D array to generate Pascal's triangle.The use of 2D array may be complex. Use the following program.Try this simple code.

Pascal's triangle for the limit 6

1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1



C++ Program to generate Pascal's Triangle


#include<iostream>
using namespace std;
#include<conio.h>
int fact(int);
int main()
{
     int t,r,p=1,f,k,n,c,i,j;
     cout<<"Enter the limit \n";
     cin>>n;
     for(i=1;i<=n;i++,p++)
     {
                     k=0;
     for(j=0;j<=i;j++,k++)
     {
              t=fact(p);
     r=fact(p-k);
     f=fact(k);
      c=t/(f*r);
     cout<<c<<'\t';
     }
     cout<<'\n';
     }
     getch();
     }
     int fact(int x)
     {
         int z,f=1;
         if(x==0)
         return(1);
         else
         for(z=1;z<=x;z++)
         f=f*z;
         return(f);
         } 



Sample Output:
Enter the limit
5

1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Twitter Delicious Facebook Digg Stumbleupon Favorites More