From 3ee2a9c0ba62cdac5632164c8f85efb883975c3d Mon Sep 17 00:00:00 2001 From: cool-nerdy <33223406+cool-nerdy@users.noreply.github.com> Date: Mon, 30 Oct 2017 18:04:08 +0530 Subject: [PATCH] Add files via upload linear search, stack operation and prime number programs added --- Linear.cpp | 34 +++++++++++ prime.cpp | 25 ++++++++ stackoperations.C | 151 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 210 insertions(+) create mode 100644 Linear.cpp create mode 100644 prime.cpp create mode 100644 stackoperations.C diff --git a/Linear.cpp b/Linear.cpp new file mode 100644 index 0000000..2e96b5c --- /dev/null +++ b/Linear.cpp @@ -0,0 +1,34 @@ + +#include +#include +int main() +{ + int arr[10], i, num, n, c=0, pos; + cout<<"Enter the array size : "; + cin>>n; + cout<<"Enter Array Elements : "; + for(i=0; i>arr[i]; + } + cout<<"Enter the number to be search : "; + cin>>num; + for(i=0; i +int main() +{ + int n, i, flag = 0; + + printf("Enter a positive integer: "); + scanf("%d",&n); + + for(i=2; i<=n/2; ++i) + { + // condition for nonprime number + if(n%i==0) + { + flag=1; + break; + } + } + + if (flag==0) + printf("%d is a prime number.",n); + else + printf("%d is not a prime number.",n); + + return 0; +} diff --git a/stackoperations.C b/stackoperations.C new file mode 100644 index 0000000..ff83186 --- /dev/null +++ b/stackoperations.C @@ -0,0 +1,151 @@ + + +Program: +#include +#include +void main() +{ +int st[100],ch,top=-1,size,n,i; +clrscr(); +printf("Enter the size of stack:"); +scanf("%d",&size); +do +{ +printf("\nChoose an option:"); +printf("\n1.Push"); +printf("\n2.Pop"); +printf("\n3.Peek"); +printf("\n4.Display"); +printf("\n5.Exit\n"); +scanf("%d",&ch); +switch(ch) +{ +case 1: + { + printf("\nTo Push:\n"); + if(top==size-1) + printf("\nStack Full\n"); + else + { + printf("\nEnter the element:\n"); + scanf("%d",&n); + top=top+1; + st[top]=n; + printf("\nOperation Successful..!!\n"); + } + break; + } +case 2: + { + printf("\nTo pop\n"); + if(top==-1) + printf("\nStack Empty\n"); + else + { + printf("\nElement Deleted:%d\n",st[top]); + top=top-1; + printf("\nOperation Successful...!!\n"); + } + break; + } +case 3: + { + printf("\nTo Peek\n"); + if(top==-1) + printf("\nStack Empty\n") ; + else + { + printf("Element is:%d",st[top]); + } + break; + } +case 4: + { + printf("\nTo Display\n"); + if(top==-1) + printf("\nStack Empty\n"); + else + { + printf("\nElements of stack are:\n"); + for(i=0;i<=top;i++) + printf("%d\n",st[i]); + printf("\n"); + } + break; + } +case 5: + { + printf("\nExit\n"); + break; + } +default: + printf("\nOOPS!! Wrong value\n"); + } +}while(ch!=5); + +getch(); +} + + +Output +Choose an option: +1.Push +2.Pop +3.Peek +4.Display +5.Exit +1 + +Enter the Element +23 +Operation Successful...!! + + +Choose an option: +1.Push +2.Pop +3.Peek +4.Display +5.Exit +3 + +To Peek + +Element is:23 + + +Choose an option: +1.Push +2.Pop +3.Peek +4.Display +5.Exit +2 + +Element Deleted :23 + +Operation Successful...!! + +Choose an option: +1.Push +2.Pop +3.Peek +4.Display +5.Exit +4 + +To Display + +Stack Empty + +Choose an option: +1.Push +2.Pop +3.Peek +4.Display +5.Exit +5 + +Exit + +