Showing posts with label Switch Case. Show all posts
Showing posts with label Switch Case. Show all posts

WAP of stack using structure and that needs to perform push & pop operation

/*WAP of stack using structure and that needs to perform push & pop operation*/
#include<iostream.h>
#include<conio.h>
//using namespace std;
void stack();
void push(int);
int pop();
struct stack
{
//private:
int arr[5],top;
//public:
}s;
void stack()
{
s.top=-1;
}
void push(int v)
{
if(s.top==4)
cout<<"Stack is full"<<endl;
else
{
s.arr[++s.top]=v;
cout<<"Data pushed successfully"<<endl;
}
}
int pop()
{
if(s.top==-1)
{
cout<<"Stack is empty"<<endl;
return NULL;
}
else
return s.arr[s.top--];
}
main()
{
///stack s;
int ch,k;
stack();
cout<<"1 for push\n2for pop";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Variable";
cin>>k;
push(k);
break;
case 2:
cout<<pop();
break;
}
getch();
}

WAP of stack that perform push,pop and peep operation using functions

/*stack-push,pop and peep*/
#include<stdio.h>
#include<conio.h>
int top=-1;
const int size=10;
int a[10];
void push();
void pop();
void peep();
void display();
void main()
{
int n;
char ch='y';
clrscr();
while(ch=='y')
{
printf("\n1.push\n2.pop\n3.peep\n4.display\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
peep();
break;
}
case 4:
{
display();
break;
}
default:
{
printf("Invalid choice....");
break;
}
}
printf("\nDo you want to continue?");
fflush(stdin);
scanf("%c",&ch);
}
getch();
}
void push()
{
int item;
if(top>=size)
{
printf("\nStack overflow.....\n");
}
else
{
top+=1;
printf("Enter element:");
scanf("%d",&item);
a[top]=item;

}
}
void pop()
{
int item;
if(top<0)
{
printf("\nStack underflow....\n");
}
else
{
item=a[top];
top-=1;
printf("\nValue deleted is:%d",item);
}

}
void display()
{
int i;
if(top<0)
{
printf("\nStack underflow......");
}
else
{
for(i=0;i<=top;i++)
{
printf("\n%d",a[i]);
}
}
}
void peep()
{
int item;
if(top<0)
{
printf("\nStack underflow......");
}
else
{
item=a[top];
printf("the top most element is %d",item);
}
}


write a menu driven program to add,subtract,multiply,divide two int digits

/*write a menu driven program to add,subtract,multiply,divide two int digits*/

#include<stdio.h>
#include<conio.h>

void main()

{
int n1,n2,c,value;
clrscr();
printf("enter two int:\n");
scanf("%d %d",&n1,&n2);
printf("\n1.Adition\n2.Subsraction\n3.Multification\n4.Division\n");
printf("\nEntre Your Choice:");
scanf("%d",&c);
switch(c)
{
case 1:
value=n1+n2;
printf("\nValue:%d",value);
break;
case 2:
value=n1-n2;
printf("\nValue:%d",value);
break;
case 3:
value=n1*n2;
printf("\nValue:%d",value);
break;
case 4:
value=n1/n2;
printf("\nValue:%d",value);
break;
default:
printf("\nWrong Choice");
break;
}
getch();
}

write a menu driven program for x^2,x^3,x^4,x^5 of integer x

/*write a menu driven program for x^2,x^3,x^4,x^5 of integer x*/
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()

{
 long int x,c,ans;
 clrscr();
 printf("enter the value of x:");
 scanf("%ld",&x);
 printf("\n1.x^2\n\n2.x^3\n\n3.x^4\n\n4.x^5");
 printf("\n\nenter your choice:");
 scanf("%ld",&c);
 switch(c)
  {
   case 1:ans=pow(x,2);
    printf("\nans:%ld",ans);
    break;
   case 2:ans=pow(x,3);
    printf("\nans:%ld",ans);
    break;
   case 3:ans=pow(x,4);
    printf("\nans:%ld",ans);
    break;
   case 4:ans=pow(x,5);
    printf("\nans:%ld",ans);
    break;
   default:
    printf("\ninvalid choice");
  }
 getch();
}

write a menu driven program to find out x^2,x^3,x^4, x^5 if x is a number, entre through keyboard

/*write a menu driven program to find out x^2,x^3,x^4,
x^5 if x is a number, entre through keyboard*/

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()

{
	long int x,c,ans;
	clrscr();
	printf("Enter Value of x:");
	scanf("%ld",&x);
	printf("\n\n1.x^2\n2.x^3\n3.x^4\n4.x^5\n");
	printf("\nEnter Your Choice:");
	scanf("%ld",&c);
	switch(c)
	{
		case 1:
			ans=pow(x,2);
			printf("\nans:%ld",ans);
			break;
		case 2:
			ans=pow(x,3);
			printf("\nans:%ld",ans);
			break;
		case 3:
			ans=pow(x,4);
			printf("\nans:%ld",ans);
			break;
		case 4:
			ans=pow(x,5);
			printf("\nans:%ld",ans);
			break;
		default:
			printf("\nInvalid Choice");
			break;
	}
	getch();
}

Wap to find sum of first ODD or EVEn numbers using SWITCH CASE

/*write a c program to add first 10 odd or even numbers*/

#include<stdio.h>
#include<conio.h>

void main()

{
 int n,c,sum=0;
 clrscr();
 printf("1.sum of first 10 odd numbers");
 printf("\n2.sum of first 10 even numbers");
 printf("\nenter your choice:");
 scanf("%d",&c);
 switch(c)
 {
 case 1:
  for(n=1;n<=20;n=n+2)
  {
   sum=sum+n;
  }
  printf("sum:%d",sum);
 break;
 case 2:
  for(n=2;n<=20;n=n+2)
  {
   sum=sum+n;
  }
  printf("sum:%d",sum);
 break;
 }
 getch();
}