Showing posts with label Functions. Show all posts
Showing posts with label Functions. Show all posts

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


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

WAP to find power if any number which is given by user without MATH.H header file


#include<conio.h>
#include<stdio.h>
int power(int x,int y);
void main( )
{
 int x, y;
 int pow;

 clrscr();
 printf("\nEnter two numbers: -");
 scanf("%d %d",&x,&y);

 pow=power(x,y);
 printf("\n%d to the power %d=%d",x,y,pow);

 getch();
 }

 int power(int x,int y)
 {
  int i;

  int p=1;
  for(i=1;i<=y;i++)

   p=p*x;
   return (p);

}

wap to find prime factors of a given positive int number (make function "factor" to find prime factor)


/*wap to find prime factors of a given positive int num(make function "factor"
to find prime factor)*/

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

void factor(int n);
void main()
{
	int n;
	clrscr();
	printf("Enter any int to find its Prime Factors:\n");
	scanf("%d",&n);
	factor(n);
	getch();
}
void factor(int n)
{
	int a,i,j,c=0;
	printf("prime factor\n");
	for(i=2;i<=n;i++)
	{
		c=0;
		if(n%i==0)
		{
			a=i;
			for(j=2;j<i;j++)
			{
				if(a%j==0)
				{c++;}
			}
				if(c==0)
				{printf("%d\n",a);}
		}
	}
}

WAP to find factorial using recursive of function

#include<conio.h>
#include<stdio.h>
int fact(int);

void main()
{
int n,f;
clrscr();

printf("\nEnter the number :-");
scanf("%d",&n);

f=fact(n);

printf("\nThe Factorial=%d",f);

getch();

}

int fact(int n)
{
 int f=1;
 if(n==1)
 {
 return(f);

 }
 else
 {
 f=n*fact(n-1);
 return(f);
 }
 }

Wap to find Binnary Number of any Decimal Number using functions

#include<conio.h>
#include<stdio.h>
int binary(int);
void main()
{

int num;
clrscr();

printf("\nEnter the number");
scanf("%d",&num);

binary(num);

getch();

}

int binary(int num)
{
 int r;
 r=num%2;
 num=num/2;
 if(num==0)
 {
  printf("\nThe binary euivalent is %d",r);

 }
 else
 {
 binary(num);
 printf("%d",r);
 return(r);
}
}