Wednesday 30 January 2013

WAP To find the GCD (greatest common divisor) of two given integers


/* Write C programs that use both recursive and non-recursive functions

To find the GCD (greatest common divisor) of two given integers.*/

#include<stdio.h>

#include<conio.h>

#include<math.h>

unsigned int GcdRecursive(unsigned m, unsigned n);

unsigned int GcdNonRecursive(unsigned p,unsigned q);

int main(void)

{

int a,b,iGcd;

clrscr();

printf(“Enter the two numbers whose GCD is to be found: “);

scanf(“%d%d”,&a,&b);

printf(“GCD of %d and %d Using Recursive Function is %d\n”,a,b,GcdRecursive(a,b));

printf(“GCD of %d and %d Using Non-Recursive Function is %d\n”,a,b,GcdNonRecursive(a,b));

getch();

}

/* Recursive Function*/

unsigned int GcdRecursive(unsigned m, unsigned n)

{

if(n>m)

return GcdRecursive(n,m);

if(n==0)

return m;

else

return GcdRecursive(n,m%n);

}

/* Non-Recursive Function*/

unsigned int GcdNonRecursive(unsigned p,unsigned q)

{

unsigned remainder;

remainder = p-(p/q*q);

if(remainder==0)

return q;

else

GcdRecursive(q,remainder);

}

WAP TO FIND THE LENGTH OF ANY STRING


void main ()
{
char ch [20];
int l;
clrscr ();
printf ("Enter String: ");
gets (ch);
l=strlen(ch);
printf ("Length of string is %d",l);
getch ();
}

WAP TO FIND THE FACTORIAL OF THE NUMBER (1X2X3X4)



void main ()
{
int fact=1,no;
clrscr();
printf ("Enter any number: ");
scanf ("%d",&no);
do
{
fact=fact*no;
no--;
}
while (no>0);
printf ("\nFactorial is %d",fact);
getch ();
}

WAP TO FIND THAT NUMBER IS PRIME OR NOT (7,11,13,17,19 ETC.)



void main ()
{
int no,i=2;
clrscr ();
printf ("Enter Number: ");
scanf ("%d",&no);
while (i<=no)
{
if (no%i==0)
break;
i++;
}
if (i==no)
printf ("Number is Prime");
else
printf ("Number is not Prime");
getch ();
}

WAP TO FIND THAT NUMBER IS PALANDROM OR NOT (121=121)


#include
void main ()
{
int no,r,res,temp=0;
clrscr ();
printf ("Enter Number: ");
scanf ("%d",&no);
r=res=0;
temp=no;
while (no>0)
{
r=no%10;
no=no/10;
res=(res*10)+r;
}
if (temp==res)
printf("Number is Palandrom");
else
printf("Number is not Palandrom");
getch ();
}

WAP TO FIND STRING WITHIN A STRING


void main ()
{
char *k="Borland International", *g, *p;
clrscr ();
printf ("Enter string to find: ");
gets (g);
p=strstr(k,g);
printf ("%s",p);
getch ();
}

WAP TO FIND SIZE OF ANY VARIABLE


void main()
{
int a;
float b;
double c;
char ch;
long d;
char nm[10];
clrscr();
printf("\nInt size is \t:%d", sizeof (a));
printf("\nFloat size is \t:%d", sizeof (b));
printf("\nDouble size is \t:%d", sizeof (c));
printf("\nChar size is \t:%d", sizeof (ch));
printf("\nLong size is \t:%d", sizeof (d));
printf("\nString size is \t:%d", sizeof (nm));
getch ();
}

WAP TO FIND OUT YEAR IS LEAP OR NOT (IF-ELSE)


void main ()
{
int a;
clrscr ();
printf ("Enter the Year: ");
scanf("%d",&a);
if (a%4==0)
{
printf ("\nYear is Leap");
}
else
{
printf("\nYear is not Leap");
}
getch ();
}

WAP TO FIND OUT TOTAL SALARY WITH (IF-ELSE)


void main ()
{
long int sal,hra,ta,ts;
clrscr ();
printf ("Enter Salary: ");
scanf("%ld",&sal);
if (sal>=5000)
{
hra=sal*.10;
ta=sal*.07;
}
else
{
hra=sal*.08;
ta=sal*.05;
}
ts=sal+hra+ta;
printf ("\n\nTotal Salary is Rs.%ld", ts);
getch ();
}

WAP TO FIND OUT TOTAL MARKS OF THREE SUBJECTS

void main ()
{
int m1,m2,m3,tm;
clrscr ();
printf ("Enter M1: ");
scanf ("%d",&m1);
printf ("Enter M2: ");
scanf ("%d",&m2);
printf ("Enter M3: ");
scanf ("%d",&m3);
tm=m1+m2+m3;
printf ("\nTotal Marks are %d",tm);
getch ();
}

WAP TO FIND OUT TOTAL MARKS & PERCENTAGE OF THREE SUBJECTS


void main ()
{
int m1,m2,m3;
float tm,per;
clrscr ();
printf ("Enter M1: ");
scanf ("%d",&m1);
printf ("Enter M2: ");
scanf ("%d",&m2);
printf ("Enter M3: ");
scanf ("%d",&m3);
tm=m1+m2+m3;
per=(tm/300*100);
printf ("\nTotal Marks are %.2f",tm);
printf ("\nPercentage is %.2f",per);
getch ();
}

WAP TO FIND OUT TOTAL BILL WITH DISCOUNT ACCORDING TO CONDITIONS (TERNARY OPERATORS)


void main ()
{
int b,dis,n;
clrscr ();
printf ("Enter Bill: ");
scanf ("%d",&b);
dis=(b<500)?(b*.10):(b>=500 &&amp; b<1000)?(b*.15):(b>=1000 && b<=2000)?(b*.20):(b*.25);
n=b-dis;
printf ("Net Bill is %d",n);
getch();
}

WAP TO FIND OUT SQUARE ROOT OF ANY NUMBER


#include
#include
void main ()
{
int no, a;
clrscr ();
printf ("Enter Number : ");
scanf ("%d",&no);
a=sqrt(no);
printf ("\nResult is %d", a);
getch ();
}

Output

WAP TO FIND OUT QUARDRATIC EQUATION (D=B2-4AC)


void main ()
{
int a,b,c,d;
clrscr ();
printf ("Enter A: ");
scanf ("%d",&a);
printf ("Enter B: ");
scanf ("%d",&b);
printf ("Enter C: ");
scanf ("%d",&c);
d= (b*b)-(4*a*c);
printf ("\nAnswer is %d",d);
getch ();
}

WAP TO FIND OUT POWER OF ANY NUMBER


#include
#include
void main ()
{
double no,r,res;
clrscr ();
printf ("Enter Number : ");
scanf ("%lf",&no);
printf ("Enter raised : ");
scanf ("%lf",&r);
res=pow(no,r);
printf ("\nResult is %.2lf", res);
getch ();
}

Output

WAP TO PRINT SERIES FROM 1 TO 10 AND SKIP 5 & 7


void main ()
{
int a;
clrscr ();
for (a=1;a<=10;a++)
{
if (a==5 || a==7)
continue;
printf ("%d\n",a);
}
getch ();
}


WAP TO FIND OUT POSITIVE OR NEGATIVE (IF-ELSE)


void main ()
{
int a;
clrscr ();
printf ("Enter the value of A: ");
scanf("%d",&a);
if (a>=0)
{
printf ("Number is Positive");
}
else
{
printf("Number is Negative");
}
getch ();
}

WAP TO FIND OUT EVEN OR ODD NUMBER (IF-ELSE)


void main ()
{
int a;
clrscr ();
printf ("Enter the value of A: ");
scanf("%d",&a);
if (a%2==0)
{
printf ("\nNumber is Even");
}
else
{
printf("\nNumber is Odd");
}
getch ();
}

WAP TO FIND OUT BIGGER NUMBER FROM TWO NUMBERS (TERNARY OPERATORS)



void main ()
{
int a,b;
clrscr ();
printf ("Enter the value of A: ");
scanf("%d",&a);
printf ("Enter the value of B: ");
scanf ("%d",&b);
(a>b)? printf ("A is Big"):printf("B is Big");
getch ();
}

WAP TO PRINT SERIES FROM 1 TO 10 & FIND ITS SQUARE AND CUBE


void main ()
{
int a=1,sqr=0,cube=0;
clrscr ();
while (a<=10)
{
sqr=pow(a,2);
cube=pow(a,3);
printf ("%d\t %d\t %d\n",a,sqr,cube);
a++;
}
getch ();
}


WAP TO PRINT SERIES FROM 1 TO 10 AND BREAK ON 5


void main ()
{
int a;
clrscr ();
for (a=1;a<=10;a++)
{
if (a==5)
break;
printf ("%d\n",a);
}
getch ();
}

WAP TO PRINT ODD NUMBERS FROM 1 TO 20


void main ()
{
int a;
clrscr ();
a=1;
while (a<=20)
{
if (a%2==1)
printf ("\n%d",a);
a++;
}
getch ();
}

WAP TO FIND OUT BIGGER NUMBER (IF-ELSE)


void main ()
{
int a,b;
clrscr ();
printf ("Enter the value of A: ");
scanf("%d",&a);
printf ("Enter the value of B: ");
scanf ("%d",&b);
if (a>b)
{
printf ("A is Greater");
}
else
{
printf("B is Greater");
}
getch ();
}