Вы находитесь на странице: 1из 9

1. There are 100 pills and 1 out of them is defected. the defected pill is lighter than the others.

if ur friend needs to b given a pill now, how wud u pick 1 nondefective pill in only 1 weigh. DIvide the pills into 2 halves...put them on the wighing baalance..which ever side is lighter has the defective pill..so give u r frnd a pill for the other lot. 2) There are 10 jars each containing candies. only 1 jar contains defected candies which weigh 9gms each and the rest weigh 10 gms each. u have to pick out any no. of candies of the jars and in a single weigh u havta tell which jar contains the defected candies take increasing amt of candies from the jars.. like 1 from 1st 2 from sec and so on... now measure them togather... from the diff of expected and actual u can find the defected jar.. for eg if first one is then the diff will b 1 gm for 2nd will b 2gms..and so on 3)There are 3 baskets. 1 contains apples only, 1 contains oranges only and the 3rd 1 contains a mix of apples and oranges. Each basket has a label on it which is incorrect as in if the basket contains only apples it mite have label of oranges. u hv to pick 1 fruit out of any 1 basket and correct all the labels Assummption held in solving this problem is that all the labels MUST be wrong (i.e. there is no chance of a basket having the correct label). Pick up a fruit from the jar labelled 'mixed'. If you get an apple, then label this jar with 'apples' and the jar who has the label 'apples' previously should be labelled 'oranges' while the third jar (previously labelled oranges) should be labelled 'mixed'. Similar logic can be applied if an orange is picked up from the jar labelled mixed. pick one fruit from the jar which leveled as mixed if it is apple then jar which have level apple can not aontain mix because in that case third jar will be right level so it is not accepted, it means jar which have level apple is containing orange and rest is mixed jar. In same way u can apply for orange. 4)There are 9 people in a party and each person shakes hand with every other person. wat's the no. of handshakes. each person will handshake wid 8 other person... so no of handshake is 9*8-9(for repitation) i.e 9*7=63 answer is 36.... 9*8....divide by 2 to exclude repetition!!! the simple funda is left the highest term and add all the othr terms i.e left 9 in this problem add all 8+7+6+5+4+3+2+1=36 5)How to arrange 24 balls in 6 rows so that each row has 5 balls? 6)How to weight an aeroplane from a distance?

7)There are 3 bulbs inside a room with opaque walls. There are 3 switches outside the room corresponding to each bulb. There is a door. You can enter the room only once. You can switch on/off the switches any number of times. Find which switch belong to which bulb turn on 1 switch for 5 min.. then turn on the second... go inside the room.. one wud b hot (first one) 2nd burning remaining is the 3rd 8)There is an island. You are on another land. There is a person on the island whom you have to save. The length of water body from each side is 10m. You have a ladders of only 9m. How will you save him? 9) There is a flat table of any size. You can place a coin on them. The one who places the coin last wins the game. How will you ensure that you win the game

Given a string, print all anagrams which are unique (these anagrams may not have any meaning) Input: Input consists of string whose unique anagrams are to be displayed Output: The program displays all the unique anagrams of the given string Sample Input: Enter the string: aab Sample Output: Anagrams for abc are: aab aba baa #include<stdio.h> #include<string.h> int length; void anagram(char *str,int size) { char tempStr[20]; if(size > 2) { strcpy(tempStr,str); for(int i=1;i <= size;i++) { anagram(str,size-1); strcpy(str,tempStr); str[length-size] = str[length-size] + str[length-size+i] - (str[length-size+i] = str[lengthsize]); } } else { printf("\n%s",str);

str[length-1] = str[length-1] + str[length-2] - (str[length-2] = str[length-1]); printf("\n%s",str); } } void main() { char str[20]; int size; printf("Enter any String : "); scanf("%s",str); length = size = strlen(str); anagram(str,size); printf("\n"); }

How do you reverse the words in a string? "My name is Amit Agarwal" to "Agarwal Amit is name My" One solution is to first reverse each word in the string and then reverse the whole string "This is Amit" Using strtok with space a delimiter we can get individual words "This" "is" and "Amit" Now reverse each to "ishT" "si" timA" Now bring all to "ishT si timA" Reverse the entire string to get "Amit is This"

Write a small C program, which while compiling takes another programfrom input terminal, and on running gives the result for the secondprogram. (NOTE: The key is, think UNIX). Suppose, the program is 1.c Then, while compiling $ cc -o 1 1.c int main() { printf("Hello World\n"); } ^D $ ./1

Hello World $ program 1. //prog1.c main() { system("cc -o prog2 prog2.c"); system("./prog2"); } //prog2.c main() { printf("Program 2 running\n"); } How to find the no of rows and columns in a 2-d array step no. 1. the sizeof operator gives the total size of the memory given allocated to our array. by comparing it with the std. size of the data type total number of elements can be found. step no. 2 know suppose the array is a[10][5] if you find size of a[0] you will get 5. by dividing the total number of elements found in step 1. with this info. you will get the other demension.

How to write a c code which prints its own code ... and without using any filehandling ! the solution is perfectly of ... char*f="char*f=%c%s%c;main() {printf(f,34,f,34,10);}%c"; main(){printf(f,34,f,34,10);} the number 34 is ascii value of " and also 10 is for line feed there fore and the last f is considered the string ... u get the same code printed on screen

This code is for generating prime number series until infinity... int l;main(O){for(;2-l?O:printf("%8d",O);l=O%--l?l:++O);}

Can any of you write a program to find the biggest of three integers without using 1. conditional operator 2. any of the mathematical comparision 3. any of the bitwise operators if((int)a/b) { if((int)a/c) return a; } else { if((int)b/c) return b;

} return c; sum=0,a=2; sum=a++ + ++a + a++; printf("%d",sum); output is 9 ..how tell me i am confused First the middle 'a' gets incremented being pre increment. So now its 3. Then the other two a's are post increment. So first their values are used in the addition operation and gets incremented. Thus 3+3+3 = 9. The final value of a is 5. How to swap the nibbles in a byte in C? (i.e 0xAF to 0xFA.) a=a>>4|a<<4; "7 raised to 5 = 16807, For which next power of 7 you will get 16807 as it's last digits ?" i.e. we need find "n" in, 2 raised to n=____16807 where, "____16807" could be any number...ending with 16087. this question was asked to me during the 2nd stage of microsoft's competition code4bill. i had the program.... but it was not showing....the required result... please try.... make a loop that keep s on multiplying 7 to those last digits required , after multiflication drop those extra digits i.e let 111 be the required last digits 111 * 10 =1110 .now drop the extra one(lhs) make ur variable =110 . check whether the value is same as 111 . if not increment a counter and loop again. long int num=7; int i=2; while(1) { num=(num*=7)%100000; printf("%d - %ld\n", i++, num); if(num==16807) getch(); } The powers of 7 that have 16807 as last digits are 5,505,1005,1505,2005,2505 and so on. Add 500. Thats the key !!

Swapping without 3 variable


{ int a,b; b=(a+b)-(a=b); } /* this program prints the numbers in spiral order */

/* LOGIC : there are two varables needed -- n and the row inside the current nxn square (i called this "level") for each row printed out: if n is even: if level is zero, print out a row of numbers else recurse then print out the last number of the row else if level is n, print out a row of numbers else print the first number and recurse */ #include "stdio.h" void print(); void print_row(int n, int level) { int i; if( n%2==0 ) { if( level == 0 ) { for( i=n*n-1; i>=n*n-n; i-- ) printf( "\t%d", i ); } else { print_row(n-1,level-1); printf( "\t%d", (n*n-n-level) ); } } else { if( level == n-1 ) { for( i=n*n-n; i<n*n; i++ ) printf( "\t%d", i ); } else { printf( "\t%d", (n-1)*(n-1)+level ); print_row(n-1,level); } } } void draw_spiral(int n) { int i; for( i=0; i<n; i++) { print_row(n,i); printf("\n"); } } int main(int argc, char* argv[]) { int n; printf("\n:Enter the value of n"); scanf("%d",&n); draw_spiral( n );

print(); return 0; } void print() { printf("\n:this program is ending\n"); } u got it, though yours spiral in opposite direction.. here is my version spiraling other way.. void print(int n,int line) { int i,j; if(line==1) /* to print first line*/ for( i=n*n-n+1;i<=n*n;i++) printf("%2d ",i); else { printf("%2d ",n*n-n-line+2); /* print first element*/ if((n-2)>=(line-1)) /* Recursion*/ { print(n-2,line-1); printf("%2d ",(n-2)*(n-2)+line-1); } else /* to print last line*/ { for(i=(n-1)*(n-1),j=1;j<=(n-1);j++,i--) printf("%2d ",i); } } } void print_spiral(int n) { int i; for(i=1;i<=n;i++,printf("\n")) print(n,i); } int main() { clrscr(); print_spiral(7); getch(); } void print_row (int n, int r) { . // n-> the number for which the spiral has to be printed. . // r-> row being printed .. varies from 0 to 1 . . if (n%2 == 0) .{ . . print_row (n-1, r); . . cout << (n-1)*(n-1) + 1 + level; . . return; .} . else

.{ . . if (r == 0) ..{ . . . for (int j=n*n-n+1; j<=n*n; j++) ...{ . . . . cout << j; ...} . . . return; ..} . . if (r == n-1) ..{ . . . for (int j = n*n-n+1-r; j > n*n-n+1-r-n; j --;) . . . . cout << j; . . . return; ..} .. . . cout << n*n-n+1-r; . . print_row (n-1, r-1); .} } int main () { . cout << "Enter the number to print the spiral for : " ; . cin >> n; . for (int r = 0; r < n; r ++) .{ . . print_row (n, r); . . cout << endl; .} }

Given the values of two nodes in a *binary search tree*, write a c program to find the lowest common ancestor. You may assume that both values already exist in the tree. The function prototype is as follows: int FindLowestCommonAncestor(node* root,int value1,int value) 20 /\ 8 22 /\ 4 12 /\ 10 14 I/P : 4 and 14 O/P : 8 (Here the common ancestors of 4 and 14, are {8,20}. Of {8,20}, the lowest one is 8).

----------------------------------------------------------------------------------------------------------You are given a singly link-list such that each node of this list is also a head of another link list of the same type. So, how does one flatten the linked-list struct node { void *data; /* could be anything */ struct node *next; struct node *down; };
Write a C code to print the sourcecode itself as the output.... programs those print their source code is called quine. #include <stdio.h> int main() { char c; FILE *fp; fp = fopen(__FILE__, "r"); while(EOF != (c = getc(fp))) { printf("%c", c); } return 1; }

u r given d pointer to d last node of the doubly linked list. Reach to d Nth node frm d start. We will keep two pointers last and first. last pointer will point to the last node and first pointer will point to n th node from last. We will increment first and last pointer till node->prev !=NULL. When this happens, the first node will point to the first element in the list and last node will point to nth node from begining.

Give a one-line C expression to test whether a number is a power of 2. (No loops allowed) x ^ (x-1) == 0

How to find common elements from two link list of size 'n' and 'm' in O(n+m) time complexity. Assume they are NOT sorted.. How to find common ancestor of two given nodes in a binary tree(not Binary Search Tree) head node is given for that tree..

Вам также может понравиться