You are on page 1of 3

Find val which sum of any elements in array

Sol
#include <stdio.h>
void sumoftwo(int a[],int n);
int main()
{
int val;
int a[7]={5,7,1,2,8,4,3};
scanf("%d",&val);
sumoftwo(a,val);

return 0;
}
void sumoftwo(int a[],int n)
{
int i,j;
for(i=0;i<7;i++)
{
for(j=i+1;j<n;j++)
{
if((a[i]+a[j])==n)
{
printf("a[%d]+a[%d]=%d+%d=%d\n",i,j,a[i],a[j],n);
}
}
}
}

2. insetion elements in to linked list


Sol
Insertion from star to end :-
#include <stdio.h>
#include <stdlib.h>
void insert(int val);
void display();
struct node
{
int data;
struct node* link;
};
struct node* head;
void insert(int val)
{
struct node*d;
if(head==NULL)
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->data=val;
temp->link=head;
head=temp;
d=temp;
}
else{
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->link=NULL;
temp->data=val;
d->link=temp;
d=temp;
}
}

void display()
{
while(head!=NULL)
{
printf("link data=%d\n",head->data);
head=head->link;
}
}
int main()
{
head=NULL;
insert(10);
insert(20);
insert(30);
insert(40);
display();
}

Insertion from end start:-

#include <stdio.h>
#include <stdlib.h>
void insert(int val);
void display();
struct node
{
int data;
struct node* link;
};
struct node* head;
void insert(int val)
{
struct node*d;
if(head==NULL)
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->data=val;
temp->link=head;
head=temp;
d=temp;
}
else{
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->link=head;
temp->data=val;
head=temp;
}
}
void display()
{
while(head!=NULL)
{
printf("link data=%d\n",head->data);
head=head->link;
}
}
int main()
{
head=NULL;
insert(10);
insert(20);
insert(30);
insert(40);
display();
}

You might also like