Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
c Programming Logics-II

c Programming Logics-II

Ratings: (0)|Views: 82|Likes:
Published by PRASATH.R

More info:

Published by: PRASATH.R on Mar 05, 2011
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





[Q001] Write a program (W.A.P.) in C to SWAP the contents of 3 variables without usingthe temporary (or extra) variables.
 /* Swapping 3 numbers without using extra variable */
#include< stdio.h >#include< conio.h >void Swap(int *a,int *b,int *c){*a = *a + *b + *c;*b = *a - (*b + *c);*c = *a - (*b + *c);*a = *a - (*b + *c);}int main(){int x=1,y=2,z=3;clrscr();printf("BEFORE SWAPPING : %d %d %d\n",x,y,z);Swap(&x,&y,&z);printf("AFTER SWAPPING : %d %d %d",x,y,z);return(0);}
/* End of Main */
[Q002] W.A.P. in C to find the Fifth root of the sum of the squares of the first 100 ODDnumbers only.
/* To find the Fifth root of the sum of the squares of the first 100 ODD numbersONLY */ 
#include< stdio.h >#include< conio.h >#include< math.h >int main(void){long i,oddnum=1,sqrnum,sum=0;for (i=1; i<=100; i++){sqrnum=oddnum * oddnum;
// Square the ODD number  
// Add Square value to the sum 
// Get the next ODD number  
}printf("\nThe result is : %ld,%.2f",sum,pow((double)sum,(1.0/5.0)));return(0);}
/* End of Main */
[Q003] W.A.P. in C to multiply any two numbers without using * (asterisk)[Hint : Use BITWISE OPERATORS]
/* Multiplication of two numbers using BITWISE OPERATORS ONLY */ 
#include< stdio.h >int main(){
long int i,n,mul,mul2,count,temp,a,b,sum,carry,res,tot;printf("\nEnter any 2 numbers : ");scanf("%ld %ld",&mul,&n);mul2=temp=mul;for (i=2; i<=n; i++){temp=mul;count=32;res=1;tot=sum=carry=0;while (count--){a=temp & 0x1;b=mul2 & 0x1;if ((a^b==1) && (carry==1)){sum=(a^b)^carry;carry=(a^b)&carry;}else{sum=a^b|carry;carry=a&b;}temp=temp>>1;mul2=mul2>>1;tot+=res*sum;res=res*2;}mul2=tot;}printf("\n%3ld * %3ld = %3ld",mul,i-1,tot);getch();return(0);}
/* End of Main */
[Q004] W.A.P. in C to check whether given number x is equal to the value 2 POWER i or something, where i>=0 using BITWISE operators ONLY. [Hint : Check whether the givennumber x is equal to the value 2 POWER i or something using BITWISE operatorsONLY]
/* Check whether the given number x is equal to the value 2 power i or not usingBITWISE operators ONLY */ 
#include< stdio.h >#include< conio.h >int main(void){long x;printf("Enter a number : ");scanf("%ld",&x);if ((x & 0x1) == 0x0)
// Checking the Least significant bit
printf("The given number %ld is EQUAL to the value 2 POWER something",x);elseprintf("The given number %ld is NOT EQUAL to the value 2 POWER something",x);getch();return(0);}
/* End of Main */
[Q005] W.A.P. in C to maintain 2 STACKS within a SINGLE ARRAY and the values of onestack should not overwrite the values of another stack.
/* Maintaining TWO STACKS within a SINGLE ARRAY */
#include< stdio.h > #include< conio.h > #define MAX 10int stack[MAX],top1,top2;void init(){top1=-1;top2=10;}void Push1(int item){stack[++top1]=item;}void Push2(int item){stack[--top2]=item;}int Pop1(){return stack[top1--];}int Pop2(){return stack[top2++];}void Display(){int i;if(top1==-1)printf("\nStack1 : Empty");else{printf("\nContent of Stack1 :\n");for(i=0;i<=top1;i++)printf("%d\t",stack[i]);}if(top2==10)printf("\nStack2 : Empty");else{printf("\nStack2 contains:\n");for(i=MAX-1;i>=top2;i--)printf("%d\t",stack[i]);}}int main()

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->