Thursday, 30 November 2017

GAUSS SEIDEL METHOD USING C PROGRAM

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int n,i,j,k,s=1;
printf("enter number of variales");
scanf("%d",&n);
float a[n][n];
float  b[n];
float x[n]={0},var,itr,temp,temp1;
printf("enter approx");
scanf("%f",&itr);
for(i=0;i<n;i++)
{
printf("enter element at b[%d]",i);
scanf("%f",&b[i]);

for(j=0;j<n;j++)
{
printf("enter element at a[%d][%d]",i,j);
scanf("%f",&a[i][j]);
}
}
do
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
{
var+=a[i][j]*x[j];
}
}
temp1=(1/a[i][i])*(b[i]-var);
var=0;
printf("initial %f\n",x[i]);
temp=fabs(temp1-x[i]);
x[i]=temp1;
temp1=0;
}
}while((temp>itr));

for(i=0;i<n;i++)
{
printf(" x%d=%f\t",i,x[i]);
}

return 0;
}

GAUSS JACOBI METHOD USING C PROGRAM

//gauss jacobi
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int n,i,j,k,s=1;
printf("enter number of variales");
scanf("%d",&n);
float a[n][n];
float  b[n];
float x[n]={0},x1[n]={0},var,itr,temp,temp1;
printf("enter approx");
scanf("%f",&itr);
for(i=0;i<n;i++)
{
printf("enter element at b[%d]",i);
scanf("%f",&b[i]);

for(j=0;j<n;j++)
{
printf("enter element at a[%d][%d]",i,j);
scanf("%f",&a[i][j]);
}
}
do
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
{
var+=a[i][j]*x1[j];
}
}
x[i]=(1/a[i][i])*(b[i]-var);
var=0;
printf("initial %f\n",x[i]);
temp=fabs(x[i]-x1[i]);
}
for(i=0;i<n;i++)
{
x1[i]=x[i];
}
}while((temp>itr));
for(i=0;i<n;i++)
{
printf(" x%d=%f\t",i,x[i]);
}

return 0;
}

EULER MODIFIED METHOD USING C PROGRAM

#include<stdio.h>
#include<math.h>
float fun(float x,float y)
{
float f;
f=x*x+y;
return f;
}
int main()
{
float diff,y0,y,x0,x1,h,xn,y1,y2;
printf("enter x0,y0,h,xn\n");
scanf("%f%f%f%f",&x0,&y0,&h,&xn);
do
{
  y=y0+h*(fun(x0,y0));
  x1=x0+h;
  do
  {
  y2=y;
  y1=y0+(h/2)*(fun(x0,y0)+fun(x1,y));
  y=y1;
  diff=y1-y2;
  //printf("%f\n",y1-y2);
  }while(fabs(diff)>0.000001);
  y0=y1;
  x0=x0+h;
  printf("x=%f \t y=%f\n",x0,y1);
}while(x0<xn);
return 0;

}

RANGE KUTTA METHOD USING C PROGRAM

#include<stdio.h>
#include<math.h>
float fun(float x,float y)
{
float f;
f=x+(y*y);
return f;
}
int main()
{
float x0,y0,xn,h,y,k1,k2,k3,k4,k,x;
printf("enter x0,y0,h,xn\n");
scanf("%f%f%f%f",&x,&y,&h,&xn);
do
{
k1=h*fun(x,y);
k2=h*fun(x+h/2,y+(k1/2));
k3=h*fun(x+h/2,y+(k2/2));
k4=h*fun(x+h,y+k3);
k=(1/6)*(k1+2*(k2+k3)+k4);
y0=y+k;
printf("%f\n",y);
x=x+h;
y=y0;
}while(x<xn);

return 0;

}

C PROGRAM FOR NON RECURSIVE PREORDER TRAVERSAL

#include<stdio.h>
#include<stdlib.h>
struct Tnode{
struct Tnode*pleft;
struct Tnode*pright;char ch;
};
struct Tnode *stack[50];
int top;
void push(struct Tnode* ch1);
struct Tnode* pop();
struct Tnode*root;
struct Tnode*create_node(char c);
void create();
void insert(char c);
void print();
//main function
int main(){
top=-1;root=NULL;
create();
printf("\n");
print();
return 0;}

struct Tnode*create_node(char c){
struct Tnode*pnew=(struct Tnode*)malloc(sizeof(struct Tnode));
if(pnew){
pnew->ch=c;
pnew->pleft=NULL;
pnew->pright=NULL;}
return pnew;}
void insert(char c){
struct Tnode *pnew,*ptrav,*pa;
pnew=create_node(c);
if(!pnew) printf("no memeory\n");
else{
if(!root)
root=pnew;
else{
pa=root;ptrav=root;
while(ptrav){
pa=ptrav;
if(c<(ptrav->ch)){
ptrav=ptrav->pleft;}
else{
ptrav=ptrav->pright;}}
if(c<(pa->ch)){
pa->pleft=pnew;}
else{
pa->pright=pnew;}}return;}}
void create(){
char k,f;
int i,n;
printf("enter number of characters\n");scanf("%d",&n);
for(i=0;i<n;i++) {
scanf("%c",&f);
scanf("%c",&k);
insert(k);
}}
void push(struct Tnode *ch1){
top++;
stack[top]=ch1;
return;
}
struct Tnode *pop(){
struct Tnode* k;
if(top<0){
k=NULL;return k ;}
else
{

k=stack[top];
top--;
return k;
}
}
void print(){
char l;
int done=0;
struct Tnode *ptemp=NULL,*ptr=root;
push(ptr);
if(!root){printf("empty tree\n");
}else{
printf("NON Recursive:\n");
while(top>=0){
// ptemp=ptr;
          ptr=pop();
printf("%c",ptr->ch);
 if((ptr->pright)!=NULL){
ptemp=ptr->pright;
// printf("%c",ptr->ch);
push(ptemp);}
if((ptr->pleft)!=NULL){
ptemp=ptr->pleft;
// printf("%c",ptr->ch);
push(ptemp);
}
}
}}

POLYNOMIAL ADDITION USING LINKED LIST IN C

#include <stdio.h>
#include <stdlib.h>
struct Node{
int co,po;
struct Node*plink;
};
struct head{
struct Node*pfirst;
};
struct head *pl1,*pl2,*pl3;
void createlist(struct head*);
struct Node* create();
struct Node* mem(int,int);
void disp(struct head *);
void insert(struct head*,struct Node*);
void Add(struct head*,struct head*,struct head*);
int main() {
pl1=(struct head*)malloc(sizeof(struct head));
pl1->pfirst=NULL;
pl2=(struct head*)malloc(sizeof(struct head));
pl2->pfirst=NULL;
pl3=(struct head*)malloc(sizeof(struct head));
pl3->pfirst=NULL;
if(pl1&&pl2&&pl3){
printf("  enter poly exp1:\n");
createlist(pl1);
printf("  enter poly exp2:\n");
createlist(pl2);
printf("poly 1:\n");
disp(pl1);
printf("\n");
printf("poly 2:\n");
disp(pl2);
printf("\n");
Add(pl1,pl2,pl3);
printf("poly exp after adding is\n");
disp(pl3);

}
return 0;
}
struct Node* create(){
struct Node* ptemp=(struct Node*)malloc(sizeof(struct Node));
if(!ptemp) return NULL;
else{
printf("enter Coefficient,power\n");scanf("%d%d",&(ptemp->co),&(ptemp->po));ptemp->plink=NULL;return ptemp;
}
}
void createlist(struct head*ptr){struct Node* ptemp=NULL;
int i,n=0;
printf("enter highest power of expression\n");
scanf("%d",&n);for(i=0;i<=n;i++)
{
ptemp=create();
insert(ptr,ptemp);
}
return;}
void insert(struct head*ptr1,struct Node*p1)
{
if(!p1){
printf("no memory \n");
return;
}
else{
struct Node *ptemp1=NULL;
if(!(ptr1->pfirst)){
ptr1->pfirst=p1;
}
else
{
ptemp1=ptr1->pfirst;
while(ptemp1->plink)
ptemp1=ptemp1->plink;
ptemp1->plink=p1;
}
return;
}
}
struct Node* mem(int c,int p){
struct Node* ptr=(struct Node*)malloc(sizeof(struct Node));
if(!ptr)
return NULL;
else{
ptr->co=c;
ptr->po=p;ptr->plink=NULL;return ptr;
}}
void disp(struct head*ptr2){
struct Node*ptemp=NULL;
if(!ptr2->pfirst){
printf("empty polynomial\n");return;
}
else{
int i=0;
ptemp=ptr2->pfirst;
do{
if(((ptemp->co)>0)&&i!=0){
printf("+");
}
printf("%dx^%d",ptemp->co,ptemp->po);
ptemp=ptemp->plink;
i++;
}while(ptemp);
}
return;
}
void Add(struct head*pt1,struct head*pt2,struct head*pt3){
struct Node *ptl1=NULL,*ptl2=NULL,*ptl3=NULL;
ptl1=pt1->pfirst; ptl2=pt2->pfirst;
int a;
while((ptl1)&&(ptl2)){
if((ptl1->po)>(ptl2->po)){
ptl3=mem(ptl1->po,ptl1->co);
ptl1=ptl1->plink;
insert(pt3,ptl3);
}
else if((ptl2->po)>(ptl1->po)){
ptl3=mem(ptl2->po,ptl2->co);
ptl2=ptl2->plink;
insert(pt3,ptl3);
}
else{
a=(ptl1->co)+(ptl2->co);
ptl3=mem(a,ptl1->po);
ptl1=ptl1->plink; ptl2=ptl2->plink;insert(pt3,ptl3);
}
}
while(ptl1){
ptl3=mem(ptl1->po,ptl1->co);
ptl1=ptl1->plink;
insert(pt3,ptl3);
}
while(ptl2){
ptl3=mem(ptl2->po,ptl2->co);
ptl2=ptl2->plink;
insert(pt3,ptl3);
}
return;
} 🔜


HEAP SORT USING C PROGRAM

# include<stdio.h>
#define Max 50
void heapup(int arr[],int,int);
void heap(int arr[],int n);
int main(){
int Heap[Max],n,i,j;
printf("enter number of elements in array\n");
scanf("%d",&n);
printf("enter  elements of array\n");
for(i=1;i<=n;i++)
{
scanf("%d",&Heap[i]);
heapup(Heap,i,n);
}

heap(Heap,n);
for(i=1;i<=n;i++){
printf("%d\t",Heap[i]);
}
return 0;
}
void heapup(int arr[],int i,int n){
int temp=arr[i],t,j=2*i;
while(j<=n){
if((j<n)&&(arr[j]<arr[j+1]))
j=j+1;
if(temp>arr[j])
break;
arr[j/2]=arr[j];
j=2*j;
}
arr[j/2]=temp;
}
void heap(int arr[],int n){
int t,k=n;
do{
t=arr[k];
arr[k]=arr[1];
arr[1]=t;
k--;
heapup(arr,1,k);
}while(k>0);
}

NEWTON BACKWARD INTERPOLATION USING C PROGRAM

#include<stdio.h>
int main()
{
int n;
printf("enter size");
scanf("%d",&n);
float ax[n];
float ay[n];
int i,j;
for(i=0;i<n;i++)
{
printf("enter ax[%d]\n",i);
scanf("%f",&ax[i]);
printf("enter ay[%d]\n",i);
scanf("%f",&ay[i]);
}
float d;
printf("enter intermediate value of x");
scanf("%f",&d);
float y=1,p;
p=(d-ax[n-1])/(ax[1]-ax[0]);
float a[n][n];
for(i=0;i<n;i++)
{
a[i][0]=ay[i];
}
for(i=1;i<n;i++)
{
for(j=0;j<n;j++)
{
a[j][i]=a[j+1][i-1]-a[j][i-1];
}
}
float g=ay[n-1];
for(i=1;i<n;i++)
{
y=(y*(p+i-1))/i;
g=g+(y*a[n-i-1][i]);
}
printf("value of y=%f ",g);
return 0;
}

C PROGRAM FOR NON RECURSIVE POST ORDER TRAVERSAL

#include<stdio.h>
#include<stdlib.h>
struct Tnode{
struct Tnode*pleft;
struct Tnode*pright;char ch;
};
struct Tnode *stack[50];
struct Tnode *stack1[50];
int top,top1;
void push(struct Tnode* ch1);
struct Tnode* pop();
struct Tnode*root;
struct Tnode*create_node(char c);
void create();
void insert(char c);
void print();
void push1(struct Tnode *ch1);
struct Tnode *pop1();
//main function
int main(){
top=-1;root=NULL;top1=-1;
create();
printf("\n");
print();
return 0;}//main end

struct Tnode*create_node(char c){
struct Tnode*pnew=(struct Tnode*)malloc(sizeof(struct Tnode));
if(pnew){
pnew->ch=c;
pnew->pleft=NULL;
pnew->pright=NULL;}
return pnew;}
void insert(char c){
struct Tnode *pnew,*ptrav,*pa;
pnew=create_node(c);
if(!pnew) printf("no memeory\n");
else{
if(!root)
root=pnew;
else{
pa=root;ptrav=root;
while(ptrav){
pa=ptrav;
if(c<(ptrav->ch)){
ptrav=ptrav->pleft;}
else{
ptrav=ptrav->pright;}}
if(c<(pa->ch)){
pa->pleft=pnew;}
else{
pa->pright=pnew;}}return;}}//inserrt end

void create(){
char k,f;
int i,n;
printf("enter number of characters\n");scanf("%d",&n);
for(i=0;i<n;i++) {
scanf("%c",&f);
scanf("%c",&k);
insert(k);
}}//creaate end

void push(struct Tnode *ch1){
top++;
stack[top]=ch1;
return;
}//push end

struct Tnode *pop(){
struct Tnode* k;
if(top<0){
k=NULL;return k ;}
else
{

k=stack[top];
top--;
return k;
}
}//pop end

void push1(struct Tnode *ch1){//thi function is for stack1 operations
top1++;
stack1[top1]=ch1;
return;
}//push1 end

struct Tnode *pop1(){
struct Tnode* k;
if(top1<0){
k=NULL;return k ;}
else
{

k=stack1[top1];
top1--;
return k;
}
}//pop1 end

//printing non recursively using print function
void print(){
char l;
int done=0;
struct Tnode *ptemp=NULL,*ptr=root;
push(ptr);
if(!root){printf("empty tree\n");
}else{
printf("NON Recursive:\n");
while(top>=0){
          ptr=pop();
push1(ptr);
if((ptr->pleft)!=NULL){
ptemp=ptr->pleft;
push(ptemp);}
 if((ptr->pright)!=NULL){
ptemp=ptr->pright;
push(ptemp);}
}
}
while(top1>=0){
ptemp=pop1();
printf("%c",ptemp->ch);
}
}//end print

FERMATS LITTLE THEOREM

import java.math.*; import java.io.*; import java.util.Scanner; public class Main { public static void main(String[] args) {    Sca...