Showing posts with label C Programs. Show all posts
Showing posts with label C Programs. 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 c program to swap three variable a,b&c using forth variable

/*write a c program to swap three variable a,b&c using forth variable*/

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

void main()
{
int a,b,c,d;
clrscr();
printf("enter value of a:");
scanf("%d",&a);
printf("enter value of b:");
scanf("%d",&b);
printf("enter value of c:");
scanf("%d",&c);
printf("\n values of variables a,b&c before swapping:\n\t%d %d %d",a,b,c);
d=a;
a=b;
b=c;
c=d;
printf("\n values of variables a,b&c after swapping:\n\t%d %d %d",a,b,c);
getch();
}

Wap to swap two values without using third variable

/*Wap to swap two values without using third variable*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 2 value");
scanf("%d %d",&a,&b);
printf("before swapping\na=%d\nb=%d\n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("after swapping \na=%d,\nb=%d",a,b);
getch();
}

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();
}

WAP of LINKED lIST insert front , print , delet front in single linked list

/*WAP of LINKED lIST insert front , print , delet front in single linked list*/
#include<stdio.h>
#include<conio.h>

void insert_front_sl();
void print_sl();
void insert_end_sl();

struct node
{
	int data;
	struct node *link;
};
struct node *header;

void main()
{
	clrscr();
	
	header = (struct node *) malloc(sizeof(struct node));
	header->data = NULL;
	header->link = NULL;
	
	insert_front_sl();
	insert_front_sl();
	print_sl();
	insert_end_sl();
	insert_end_sl();
	print_sl();
	
	getch();
}

void insert_front_sl()
{
	struct node *temp,*ptr;
	int X;
	temp = (struct node *) malloc(sizeof(struct node));
	
	if(temp == NULL)
	{
		printf("Memory Insufficient");
	}
	else
	{
		ptr = header->link;
		
		printf("\nEnter the value to insert at front: ");
		scanf("%d", &X);
		temp->data = X;
		temp->link = ptr;
		header->link = temp;
	}
}

void print_sl()
{
	struct node *ptr;
	ptr = header;
	
	while(ptr->link != NULL)
	{
		ptr = ptr->link;
		printf("%d ",ptr->data);
	}
}

void insert_end_sl()
{
	struct node *temp,*ptr;
	int X;
	temp = (struct node *) malloc(sizeof(struct node));
	
	if(temp == NULL)
	{
		printf("Memory Insufficient");
	}
	else
	{
		ptr = header;
		while(ptr->link != NULL)
		{
			ptr = ptr->link;
		}
		
		printf("\nEnter the value to insert at end: ");
		scanf("%d", &X);
		temp->data = X;
		temp->link = NULL;
		ptr->link = temp;
	}
}


WAP to illustrate the use of time.h header file

/*WAP to illustrate the use of time.h header file*/
#include <stdio.h>
#include <time.h>

main(){


	time_t time_val;
	struct tm *tp;
	char buffer[100];
	
	time_val = time(NULL);
	printf("Time_Value: %li\n",time_val);

	tp = localtime(&time_val);

	printf("Hours:%d, Minutes:%d, Seconds:%d\n",
	        tp->tm_hour, tp->tm_min, tp->tm_sec);

	printf("asctime()==%s\n",asctime(tp));

	strftime(buffer, 99, "%A %B %d %Y at %I:%M:%S%p", tp);
	printf("%s\n",buffer);

}

WAP to write code of fake torjan virus using structure

/*WAP to write code of fake torjan virus using structure*/
#include<stdio.h>
#include<conio.h>
#include<string.h>

struct date
{
    int day,month,year;
    char path[100];
}d;

int main()
{
  printf("Enter Day");
  scanf("%d",&d.day);

  printf("Enter Month");
  scanf("%d",&d.month);

  printf("Enter Year");
  scanf("%d",&d.year);

  printf("Your Date is successfully saved\n\n\tDate\t%d/%d/%d",d.day,d.month,d.year);

  printf("Enter Path of file");
  scannf("%s",&d.path);

  getch();
  return(0);
}

WAP to find transport of of given 3x3 matrix

/*WAP to find transport of of given 3x3 matrix*/
#include<stdio.h>
#include<conio.h>
void main()
{
	int a[3][3],i,j;
	clrscr();
	printf("Enter Elements for A Matrix:");
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			printf("\n\nElements[%d][%d]:",i,j);
			scanf("%d",&a[i][j]);
		}
	}
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			printf("%d\t",a[i][j]);
		}
		printf("\n");
	}
	printf("\nTranspose of Matrix:\n");
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			printf("%d\t",a[j][i]);
		}
		printf("\n");
	}
	getch();
}

write a c programme to calculate area of triangle

/*write a c programme to calculate area of triangle*/
#include<stdio.h>
#include<conio.h>
void main()
{
	float b,l,area;
	clrscr();
	printf("enter the value of b:");
	scanf("%f",&b);
	printf("enter the value of l:");
	scanf("%f",&l);
	area=0.5*b*l;
	printf("area of triangle: %.2f",area);
	getch();

}

WAP to add two number or find the sum of two number using UDF functions with return value

/*WAP to add two number or find the sum of two number using UDF functions with return value*/
#include<stdio.h>
#include<conio.h>

int addint(int,int);

void main()
{
 int num1,num2,ans;
 clrscr();

 scanf("%d %d",&num1,&num2);
 ans=addint(num1,num2);
 printf("%d",ans);

 getch();
}

int addint(int n1, int n2)
{
 return(n1+n2);
}

Wap to print line or draw line after text without using graphics


/*Wap to print line or draw line after text without using graphics*/
#include<stdio.h>
#include<conio.h>

void drawline();

void main()
{
 clrscr();

 drawline();

 printf("SMIT");

 drawline();

 getch();
}

void drawline()
{
 int i;
 printf("\n");
 for(i=0;i<10;i++)
  printf("-");
 printf("\n");

}

WAP to illustrate the use of CONTINUE and BREAK statement using IF Else

/*WAP to illustrate the use of CONTINUE and BREAK statement using IF Else*/
#include<stdio.h>
#include<conio.h>

void main()

{
 int x=5,y=7,z=8;
 clrscr();
 for(;50<100;x++,y++)
 {
 while(y>=x)
 {
 printf("\n%d%d%d",x,y,z);
 y--;
 break;
 }
 if(x==5)
  break;
 else
  continue;
 }
 getch();
}

WAP to count how many number of vovels in the given string and print on output screen

/*WAP to count how many number of vovels in the given string and print on output screen*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 int count=0,i;
 char a[25],b[25];
 clrscr();
 printf("Enter a strting:");
 gets(a);
 for(i=0;a[i]!='\0';i++)
 {
  if(a[i]=='a' || a[i]=='i' || a[i]=='o' || a[i]=='u' || a[i]=='e')
  {
   strcpy(&b[i],&a[i]);
   count+=1;
  }
  else
  {
   printf("%c",a[i]);
  }

 }
  printf("\n Total vovels: %d",count);

 getch();
}

wap to find revrse of a given number (using wile loop) & check whether the given number is pelindrom or not

/*wap to find revrse of a given no.(using wile loop) & check whether the given
no. is pelindrom or not*/

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

void main()

{
 int n,rev=0,a,m;
 clrscr();

 printf("enter value of n:");
 scanf("%d",&n);

 m=n;

 while(n!=0)
 {
  a=n%10;
  n=n/10;
  rev=rev*10+a;
 }

 printf("\nreverse number:%d",rev);

 if(rev==m)
        { printf("\nThe Number is Pelindrom");    }
 else
        { printf("\nThe Number is Not Pelindrom"); }

 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 c program to add first 10 successive integer

/*write a c program to add first 10 successive integer*/

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

void main()

{
 int x,n,i,ans=1;
 clrscr();
 printf("enter value of x:");
 scanf("%d",&x);
 printf("enter value of n:");
 scanf("%d",&n);
 for(i=1;i<=n;i++)
 {
  ans=ans+x;
 }
 printf("ans:%d",ans);
 getch();
}

WAP to illustrate the equal to and not equal to operator with if else

/*WAP to illustrate the equal to and not equal to operator with if else*/
#include<stdio.h>
#include<conio.h>

void main()

{
 int x,y;
 clrscr();
 printf("enter the value of x & y:\n");
 scanf("%d %d",&x,&y);

 if(x==0&&y==0)
  {
   printf("the point is origin");
  }
 else if(x==0&&y!=0)
  {
   printf("the point is on y-axis");
  }
 else if(y==0&&x!=0)
  {
   printf("the point is on x-axis");
  }
 else
   printf("the point is not on any axis");
 getch();
}

wap to find sum of first n terms using recursive function


/*wap to find sum of first n terms using recursive function*/

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

int addition(int sum);
void main()
{
 int n,sum;
 clrscr();
 printf("enter value of n:");
 scanf("%d",&n);
 sum=addition(n);
 printf("sum of first %d numbers:%d",n,sum);
 getch();
}

int addition(int n)
{
 int sum=0;
 if(n==1)
        { return(1);          }
 else
        { sum=n+addition(n-1);
   return(sum);        }
}

wap using recursive function to generatr n terms and print the sum of following series for N terms.. 1+4+9+16+25+36+......+n=sum


/*wap using recursive function to generatr n terms and print the sum of
following series for N terms.. 1+4+9+16+25+36+......+n=sum*/

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

int addition(int n);

void main()

{
 int n,sum;
 clrscr();
 printf("enter value of n:");
 scanf("%d",&n);
 sum=addition(n);
 printf("%d",sum);
 getch();
}

int addition(int n)

{
 int sum;

 if(n==1)
        { return(1);          }
 else
        {
   return(pow(n,2)+addition(n-1));        }
}