Skip to main content

Posts

FACTORIAL Factorial A farmer used to grow the same crop every year. This year because of better water availability, he decides to grow different crops. He doesn't know how to arrange those crops in the field. Assume the number of distinct crops he wants to grow is N. Can you help the farmer to find the total number of ways N distinct crops can be arranged in the field? Note : Formulate a RECURSIVE Algorithm Function name: int nWays(int n); Input Format: Input consists of a single integer N. Output Format: Refer the sample output for specification. Sample Input: 5 Sample Output: 120 ways #include int nWays(int n) { if(n>=1) return(n*nWays(n-1)); else return 1; } int main() { int N; scanf("%d",&N); printf("%d ways",nWays(N)); return 0; }
NUMBER OF BITS TO BE FLIPPED TO CONVERT A TO B Number of bits to be flipped to convert A to B Given two numbers A and B, write a program to find the number of bits to be flipped to convert A to B.Using EX-OR(^) and bitwise AND(&) operator. Input Format: Input consists of 2 integers --- A , B Output Format: Output is an integer that corresponds to the number of bits to be flipped to convert A to B. Refer sample input and output for formatting specifications. Sample Input 1 : 2 4 Sample Output 1 : 2 Explanation for Sample 1: A=2 B=4 Binary representaion of 2 = 00000010 Binary representaion of 4 = 00000100 To convert A to B, 2 bits needed to be shifted.(00000010) #include int bitcount(int); int main() { int A,B,c,y; scanf("%d",&A); scanf("%d",&B); c=A^B; y=bitcount(c); printf("%d",y); return 0; } int bitcount(int x) { int i; for(i=0;x!=0;x>>=1) if(x&01) i++; return i; ...
SMALLEST NUMBER Smallest Number Write a program to find the smallest of two numbers.Using EX-OR(^) and bitwise AND(&) operator. Input Format: First line of input is an integer that corresponds to the first number. Second line of input is an integer that corresponds to the second number. Output Format: Output consists of an integer that corresponds to the smallest of the two numbers. Refer sample input and output for formatting specifications. Sample Input 1: 15 5 Sample Output 1: 5 Sample Input 1: 25 64 Sample Output 1: 25 #include int main() { int x,y; scanf("%d%d",&x,&y); printf("%d",(y^((x^y) & -(x
TOTAL SET BITS Total Set Bits Given the number N, write a program to count the total set bits in all the numbers from 1 to N.Using Right Shift (>>) and Bitwise AND(&) operators. Input Format: Input consists of an integer that corresponds to the value of N. Output Format: Output consists of an integer that corresponds to the number of set bits from 1 to N. Sample Input 1 : 3 Sample Output 1: 4 Sample Input 2: 6 Sample Output 2: 9 Explanation for Sample 1: N = 3 No of 1's in 1 = 1 (00000001) No of 1's in 2 = 1 (00000010) No of 1's in 3 = 2 (00000011) Total 1's = 1+1+2 = 4 #include int main() { unsigned int c,n,i,r=0; scanf("%d",&n); for(i=1;i >=1) { p+=c&1; } r+=p; } printf("%d",r); return 0; }
Carrot Plantation After calculating the yield from each plant John found that too much water flow to the carrot plants has reduced the yield from carrot plants. So he puts the meshes before and after each carrot plant position. Symbol of each vegetable is given below P = Potato O = Onion C = Carrot R = Raddish Help John in writing a program for making safety precautions for stopping more amount of water flow by '#' symbol before and after the Carrot plant position. Assume the maximum length of the string to be 100. Input format: The input is the string. Output format: The output is the string which includes the # symbol before and after 'C'. Sample Input 1: RRORPCOPCP Sample Output 1: RRORP#C#OP#C#P Sample Input 2: PCCOP Sample Output 2: P#C#C#OP #include int main() { char s1[20],s2[50]; scanf("%s",s1); char *p1=s1; char *p2=s2; while(*p1 != '\0') { if(*p1 != 'C') { *p2=(*p1); ...
Crop Rotation Crop rotation is the practice of growing a series of dissimilar or different types of crops in the same area in sequenced seasons. It is done so that the soil of farms is not used for only one set of nutrients. It helps in reducing soil erosion and increases soil fertility and crop yield. In crop rotation technique, there are four strategies. Out of those, 1) Two field system 2) Three field system are popularly followed. Two Field System: Under a two-field rotation, half the land was planted in a year, while the other half lay fallow. Then, in the next year, the two fields were reversed. Three-field system: Dividing available lands into three parts. One section was planted in the autumn with rye or winter wheat, followed by spring oats or barley; the second section grew crops such as peas, lentils, or beans; and the third field was left fallow. The three fields were rotated in this manner so that every three years, a field would rest and...
Weed Removal A farmer removes the weeds from the square field in the reverse spiral format. Given the weed allignment in the field, determine the order in which he removes the weeds. Input Format: The first line of input consists of a number corresponding to the square field. From the second line, the input consists of values in the square matrix. Output Format: The order of the weed in which it is removed. Sample Input: 4 12 32 34 13 23 15 55 21 21 14 16 51 66 23 34 32 Sample Output: 12 23 21 66 23 14 15 32 34 55 16 34 32 51 21 13 #include #include int main() { int i,j,m,*p; scanf("%d",&m); p=(int*)malloc(m*m*sizeof(int)); for(i=0;i =0;i--) { printf(" %d ",*(p+i*m+j)); } } j++; } return 0; }