/* http://native-code.blogspot.com */ #include<stdio.h> #include<conio.h> #include<malloc.h> typedef struct link { int data; struct link *next; } node; node *ins_beg(node*, int); int prime_node_sum(node*); void display(node*); int isprime(int); void main() { int k,ch,n,p; node *head=NULL; while(1) { printf("/* http://native-code.blogspot.com */"); printf("\n\n1: Insert\n2: Sum of prime no\n3: Display\n4: EXIT\n"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter the value for new node:\n"); scanf("%d", &n); head=ins_beg(head,n); break; case 2: p=prime_node_sum(head); printf("Sum of prime no is %d\n",p); break; case 3: display(head); printf("\n"); break; case 4: exit(0); default: printf("Wrong choice.\n"); } } } node* ins_beg(node *h, int n) { node *temp, *r=h; temp=(node *)malloc(sizeof(node)); temp->data=n; temp->next=NULL; if(h==NULL) { h=temp; } else { temp->next=h; h=temp; } return(h); } void display(node *h) { node *r=h; while(r!=NULL) { printf("%d-->",r->data); r=r->next; } printf("NULL"); } int prime_node_sum(node* h) { node* r=h; int d,f,s=0; while(r!=NULL) { d=r->data; f=isprime(d); if(f==0) s+=d; r=r->next; } return(s); } int isprime(int d) { int i,f=0; for(i=2;i<=d/2;i++) { if(d%i==0) { f=1; break; } } return(f); } /* http://native-code.blogspot.com */
Home
» C
» data-structure
» Malloc
» prime-number
» single-linked-list
» How to calculate the sum of all prime numbers in a single link list?
Post a Comment