W.A.P. C++

WAP | CPP | Programs in C++ | C++ Solutions

Snehil Khanor's Binary Log
Showing posts with label numbers. Show all posts
Showing posts with label numbers. Show all posts

Write a program to Implement a Linked List in C++


The user is provided with the following options..

To insert an item at begining
To insert an item at end
To insert an item at position
To traverse the list
To delete first item
To delete last item
To delete item from position
To count no. of items in list


Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<conio.h>
  2. #include<stdio.h>
  3. #include<alloc.h>
  4. #include<process.h>
  5. struct node
  6. { int info;
  7. struct node *next;
  8. };
  9. typedef struct node sn;
  10. void insertatbeg(sn **,int);
  11. void insertatend(sn **,int);
  12. void insertatpos(sn **,int,int);
  13. void traverse(sn *);
  14. int delfirst(sn *);
  15. int dellast(sn *);
  16. int delpos(sn *,int);
  17. int count(sn *);
  18. //Coding by: Snehil Khanor
  19. //http://WapCPP.blogspot.com
  20. void main()
  21. { sn *p;
  22. int n,i,l;
  23. char ch;
  24. clrscr();
  25. p=NULL;
  26. do
  27. { printf("\n-----select an operation-----");
  28. printf("\n1->To insert an item at begining<-");
  29. printf("\n2->To insert an item at end<-");
  30. printf("\n3->To insert an item at position<-");
  31. printf("\n4->To traverse the list<-");
  32. printf("\n5->To delete first item<-");
  33. printf("\n6->To delete last item<-");
  34. printf("\n7->To delete item from position<-");
  35. printf("\n8->To count no. of items in list<-");
  36. printf("\n9->Exit<-");
  37. printf("\n->Enter your choice:");
  38. scanf("%d",&n);
  39. switch(n)
  40. { case 1:printf("\n->Enter the item to insert");
  41. scanf("%d",&i);
  42. insertatbeg(&p,i);
  43. break;
  44. case 2:printf("\n->Enter the item to insert");
  45. scanf("%d",&i);
  46. insertatend(&p,i);
  47. break;
  48. case 3:printf("\n->Enter the item to insert");
  49. scanf("%d",&i);
  50. printf("\n->Enter the position");
  51. scanf("%d",&l);
  52. insertatpos(&p,i,l);
  53. break;
  54. case 4:traverse(p);
  55. break;
  56. case 5:i=delfirst(p);
  57. printf("\nItem deleted is %d.",i);
  58. break;
  59. case 6:i=dellast(p);
  60. printf("\nItem deleted is %d.",i);
  61. break;
  62. case 7:printf("\n->Enter the position");
  63. scanf("%d",&l);
  64. i=delpos(p,l);
  65. printf("\nItem deleted is %d.",i);
  66. break;
  67. case 8:i=count(p);
  68. printf("\nNumber of nodes in list are %d.",i);
  69. break;
  70. case 9:exit(0);
  71. break;
  72. default:printf("\n->You have entered wrong choice");
  73. }
  74. printf("\n->Do you want to continue/exit(y/n)");
  75. fflush(stdin);
  76. scanf("%c",&ch);
  77. } while((ch=='y')||(ch=='Y'));
  78. getch();
  79. }
  80. int count(sn *p)
  81. { sn *temp;
  82. int c=1;
  83. temp=p;
  84. while((temp->next)!=NULL)
  85. { c++;
  86. temp=temp->next;
  87. }
  88. return c;
  89. }
  90. void insertatbeg(sn **p,int item)
  91. { sn *ptr;
  92. ptr=(sn *)malloc(sizeof(sn));
  93. ptr->info=item;
  94. if(*p==NULL)
  95. ptr->next=NULL;
  96. else
  97. ptr->next=*p;
  98. *p=ptr;
  99. }
  100. void insertatend(sn **p,int item)
  101. { sn *ptr,*temp;
  102. ptr=(sn *)malloc(sizeof(sn));
  103. ptr->info=item;
  104. ptr->next=NULL;
  105. if(*p==NULL)
  106. *p=ptr;
  107. else
  108. { temp=*p;
  109. while((temp->next)!=NULL)
  110. temp=temp->next;
  111. temp->next=ptr;
  112. }
  113. }
  114. void insertatpos(sn **p,int item,int loc)
  115. { sn *ptr,*temp,*s;
  116. int i,c;
  117. temp=*p;
  118. ptr=(sn *)malloc(sizeof(sn));
  119. ptr->info=item;
  120. c=count(*p);
  121. if(loc>c)
  122. printf("\nList is short,Item can't inserted");
  123. else
  124. {
  125. for(i=0;i<loc-1;i++)
  126. { s=temp;
  127. temp=temp->next;
  128. }
  129. ptr->next=s->next;
  130. s->next=ptr;
  131. }
  132. }
  133. //Coding by: Snehil Khanor
  134. //http://WapCPP.blogspot.com
  135. void traverse(sn *p)
  136. { sn *temp;
  137. if(p==NULL)
  138. printf("\nList is empty.");
  139. else
  140. {
  141. temp=p;
  142. while((temp->next)!=NULL)
  143. { printf("\nno.=%d",temp->info);
  144. temp=temp->next;
  145. }
  146. printf("\nno.=%d",temp->info);
  147. }
  148. }
  149. int delfirst(sn *p)
  150. { int item;
  151. sn *temp;
  152. if(p==NULL)
  153. { printf("\nList is empty");
  154. return 0;
  155. }
  156. else
  157. { temp=p;
  158. p=p->next;
  159. item=temp->info;
  160. free(temp);
  161. return item;
  162. }
  163. }
  164. int dellast(sn *p)
  165. { int item;
  166. sn *temp,*s;
  167. if(p==NULL)
  168. { printf("\nList is empty");
  169. return 0;
  170. }
  171. else
  172. { temp=p;
  173. while((temp->next)!=NULL)
  174. { s=temp;
  175. temp=temp->next;
  176. }
  177. item=temp->info;
  178. s->next=NULL;
  179. free(temp);
  180. return item;
  181. }
  182. }
  183. int delpos(sn *p,int loc)
  184. { int item,i,c;
  185. sn *temp,*s;
  186. c=count(p);
  187. if(p==NULL)
  188. { printf("\nList is empty");
  189. return 0;
  190. }
  191. else
  192. { if(loc>c)
  193. { printf("\nItem is not in list.");
  194. return 0;
  195. }
  196. else
  197. { temp=p;
  198. for(i=0;i<loc-1;i++)
  199. { s=temp;
  200. temp=temp->next;
  201. }
  202. item=temp->info;
  203. s->next=temp->next;
  204. free(temp);
  205. return item;
  206. }
  207. }
  208. }

Write a program to sort elements of array using Selection Sorting


Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 
  2. #include<conio.h> 
  3. void main() 
  4. int a[100],n,i,j,temp,loc,min; 
  5. clrscr(); 
  6. printf("\How many elements:\n"); 
  7. scanf("%d",&n); 
  8. printf("Enter the element of array\n"); 
  9. for(i=0;i<=n-1;i++) 
  10. scanf("%d",&a[i]); 
  11. min=a[0]; 
  12. //Coding by: Snehil Khanor  
  13. //http://WapCPP.blogspot.com  
  14. for(i=0;i<=n-1;i++) 
  15. min=a[i]; 
  16. loc=i; 
  17. for(j=i+1;j<=n-1;j++) 
  18. if(a[j]<min) 
  19. min=a[j]; 
  20. loc=j; 
  21. if(loc!=1) 
  22. temp=a[i]; 
  23. a[i]=a[loc]; 
  24. a[loc]=temp; 
  25.  
  26. printf("The number after selection sorting are:\n"); 
  27. for(i=0;i<=n-1;i++) 
  28. printf("%d\n",a[i]); 
  29. getch(); 

Write a program to sort elements of array using Quick Sort


Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 
  2. #include<conio.h> 
  3. #define max 100 
  4. int a[max],n,i,l,h; 
  5. void main() 
  6. void input(void); 
  7. input(); 
  8. getch(); 
  9.  
  10. void input(void) 
  11. void output(int a[],int n); 
  12. void quick_sort(int a[],int l,int h); 
  13. printf("How many elements in the array : "); 
  14. scanf("%d",&n); 
  15. printf("\n"); 
  16. printf("Enter the elemennts : \n"); 
  17. for(i=0;i<=n-1;i++) 
  18. scanf("%d",&a[i]); 
  19. l=0; 
  20. h=n-1; 
  21. quick_sort(a,l,h); 
  22. printf("Sorted Array :\n "); 
  23. output(a,n); 
  24.  
  25. void quick_sort(int a[],int l, int h) 
  26. int temp,key,low,high; 
  27. low=l; 
  28. high=h; 
  29. key=a[(low+high)/2]; 
  30. do 
  31. while(key>a[low]) 
  32. low++; 
  33. while(key<a[high]) 
  34. high--; 
  35. if(low<=high) 
  36. temp=a[low]; 
  37. a[low++]=a[high]; 
  38. a[high--]=temp; 
  39. //Coding by: Snehil Khanor 
  40. //http://WapCPP.blogspot.com 
  41. } while(low<=high); 
  42. if(l<high) 
  43. quick_sort(a,l,high); 
  44. if(low<h) 
  45. quick_sort(a,low,h); 
  46. void output(int a[],int n) 
  47. for(i=0;i<=n-1;i++) 
  48. printf("%d\n",a[i]); 
  49. }

Write a program to sort elements of array using Insert Sorting


Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 
  2. main() 
  3. int a[100],n,k,i,j,temp; 
  4. printf("How many elements: \n"); 
  5. scanf("%d",&n); 
  6. printf("Enter the element of array: \n"); 
  7. for(i=0;i<=n-1;i++) 
  8. scanf("%d",&a[i]); 
  9. for(k=1;k<=n-1;k++) 
  10. temp=a[k]; 
  11. j=k-1; 
  12. //Coding by: Snehil Khanor 
  13. //http://WapCPP.blogspot.com 
  14. while((temp<a[j])&&(j>=0)) 
  15. a[j+1]=a[j]; 
  16. j=j-1; 
  17. a[j+1]=temp; 
  18. printf("Elements of array after sorting: \n"); 
  19. for(i=0;i<=n-1;i++) 
  20. printf("%d\n",a[i]); 
  21. getch();

write a program to calculate sum of all even numbers upto n


A number is taken from user and sum of all even numbers upto that number is calculated.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int n,sum=0;
  7. cout<<"Please enter a number: ";
  8. cin>>n;
  9. for (int i=1;i<=n;i++)
  10. //Coding by: Snehil Khanor
  11. //http://WapCPP.blogspot.com
  12. {
  13. if(i%2==0)
  14. sum=sum+i;
  15. else
  16. continue;
  17. }
  18. cout<<sum;
  19. getch();
  20. }

write a program to calculate sum of all odd numbers upto n


A number n is taken from user and sum of all odd numbers upto n is calculated.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int n,sum=0;
  7. cout<<"Please enter a number: ";
  8. cin>>n;
  9. for (int i=1;i<=n;i++)
  10. //Coding by: Snehil Khanor
  11. //http://WapCPP.blogspot.com
  12. {
  13. if(i%2!=0)
  14. sum=sum+i;
  15. else
  16. continue;
  17. }
  18. cout<<sum;
  19. getch();
  20. }

write a program to find sum of n numbers


A number n is taken from user.
Sum of all natural numbers upto n is displayed.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int n,sum=0;
  7. cout<<"Please enter a number: ";
  8. cin>>n;
  9. //Coding by: Snehil Khanor
  10. //http://WapCPP.blogspot.com
  11. for (int i=1;i<=n;i++)
  12. sum=sum+i;
  13. cout<<sum;
  14. getch();
  15. }

write a program to check whether entered number is prime or not


Prime number: A natural number which has exactly two distinct natural number divisors: 1 and itself.

A number is taken from user and is checked whether prime number or not.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int n,m;
  7. cout<<"Please enter a number: ";
  8. cin>>n;
  9. for (int i=2;i<=(n-1);i++)
  10. {
  11. if(n%i==0)
  12. //Coding by: Snehil Khanor
  13. //http://WapCPP.blogspot.com
  14. {
  15. cout<<"Not a prime";
  16. break;
  17. }
  18. else
  19. {
  20. cout<<"Prime";
  21. break;
  22. }
  23. }
  24. getch();
  25. }

write a program to display fibonacci series upto n terms


Fibonacci series:The first two Fibonacci numbers are 0 and 1, and each remaining number is the sum of the previous two
e.g.: 0 1 1 2 3 5 8 13 21 ....
A number n is taken from user and the series is displayed upto nth term.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a=0,b=1,c=0,n;
  7. cout<<"Enter the number of terms you wanna see: ";
  8. cin>>n;
  9. cout<<a<<" "<<b<<" ";
  10. for(int i=1;i<=n-2;i++)
  11. {
  12. c=a+b;
  13. a=b;
  14. b=c;
  15. //Coding by: Snehil Khanor
  16. //http://WapCPP.blogspot.com
  17. cout<<c<<" ";
  18. }
  19. getch();
  20. }

write a program to display numbers from 10 to 1


numbers from 10 to 1 are displayed...
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. for(int i=10;i>0;i--)
  7. //Coding by: Snehil Khanor
  8. //http://WapCPP.blogspot.com
  9. { cout<<i;
  10. cout<<"\n";
  11. }
  12. getch();
  13. }

write a program to display numbers upto even numbers upto 50


Even numbers upto 50 are displayed.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. for(int i=1;i<=50;i++)
  7. //Coding by: Snehil Khanor
  8. //http://WapCPP.blogspot.com
  9. {if(i%2==0)
  10. cout<<i;
  11. cout<<"\n";
  12. }
  13. getch();
  14. }

write a program to display numbers upto n


A number n is taken from the user and then numbers from 1 to n is displayed.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int lim;
  7. cout<<"Please enter Limit: ";
  8. cin>>lim;
  9. //Coding by: Snehil Khanor
  10. //http://WapCPP.blogspot.com
  11. for(int i=1;i<=lim;i++)
  12. { cout<<i;
  13. cout<<"\n";
  14. }
  15. getch();
  16. }

Write a program to find average of three numbers


Three numbers are taken(Input) from the user and avg of these three nos. is calculated(Output).
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include <iostream.h>
  2. #include <conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a,b,c,d,avg;
  7. cout<<"enter a no. ";
  8. cin>>a;
  9. cout<<"enter another no. ";
  10. cin>>b;
  11. cout<<"enter another nos ";
  12. cin>>c;
  13. //Coding by: Snehil Khanor
  14. //http://WapCPP.blogspot.com
  15. d=a+b;
  16. avg=d/3;
  17. cout<<"The average is "<<s;
  18. getch();
  19. }

Search

About WAP C++

Here you'll find a wide range of programs' solution ranging from beginer level to advanced level.
All programs here are made, compiled and tested by me..and are running absolutely fine.. still if you find any bug in any program do let me know :)

Followers

Subscribe via email

Enter your email address:

Delivered by FeedBurner

my Binary Log



eXTReMe Tracker