Sunteți pe pagina 1din 13

Solution of nagarro placement questions(programming) Ankit Bharthan(BVCOE) 1.

Take an array of 100 elements and fill it with the no 1 to 100 such that one no should skip and one no should repeated. Find the no which is repeated and which is skipped. #include<stdio.h> void check(int a[]); int main() { int a[100],i; for(i=0;i<100;i++){ a[i]=i; } a[68]=13; printf("done"); check(a); }

void check(int a[]){ int b[100],c=1,k=0; for(int i=0;i<100;i++){ b[i]=0; } for(int i=0;i<100;i++){ if(b[a[i]]==c){ b[a[i]]++; } else { b[a[i]]=c; }

Solution of nagarro placement questions(programming) Ankit Bharthan(BVCOE) } while(b[k]==1){ k++; } for(int g=0;g<100;g++){ if(b[g]==0) printf("value replaced is %d\n",g); } printf("repeated value is %d\n",k); }

2. find the all possible combination of digits ranging 1 to 9 whose sum is 10, no digit shud be repeated in any combination. 1234 127 136 145 19 235 28 37 46 #include<stdio.h> int main() { int a,b,c,d; for(a=1;a<=9;a++) { for(b=a+1;b<=9;b++) { if((a+b)==10 ) { printf("%d%d\n",a,b); } else { for(c=b+1;c<=9;c++) {

Solution of nagarro placement questions(programming) Ankit Bharthan(BVCOE) if(((a+b+c)==10)) { printf("%d%d%d\n",a,b,c); } else { for(d=c+1;d<=9;d++) { if(((a+b+c+d)==10) ){ printf("%d%d%d%d\n",a,b,c,d); } } } } } } } return 0; } 3. Write a program to find out the combination of an element of each array gives a result 0. For example: array 1: {2,1,4,7} array 1: {3,-3,-8,0} array 1: {-1,-4,-7,6}

Output: pairs = {2,-8,6} {1,3,-4} {4,-3,-1} {7,0,-7}

Solution of nagarro placement questions(programming) Ankit Bharthan(BVCOE) #include<iostream.h> int main() { int arr1[]={2,1,4,7}; int arr2[]={3,-3,-8,0}; int arr3[]={-1,-4,-7,6}; for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ for(int k=0;k<4;k++){ if(arr1[i]+arr2[j]+arr3[k]==0) cout<<"{"<<arr1[i]<<","<<arr2[j]<<","<<arr3[k]<<"}"<<endl; } } } } 4. Write a program to check whether 2 strings given by the user are anagram strings or not. For example: str1: Are you engineer. str2: You are engineer. Output: yes str1: Am i fine. str2: I'm fine. Output: No str1: Am i fine. str2: I am fire. Output: No #include<stdio.h> #include<string.h>

Solution of nagarro placement questions(programming) Ankit Bharthan(BVCOE) int anagram(char *str1,char *str2); int main(){ char *str1="are you engineer"; char *str2="you are engineer"; int a=anagram(str1,str2); if(a==1){ printf("yes"); } else { printf("no"); } return 0; } int anagram(char *str1,char *str2){ int i,temp; int count1[256]={0}; int count2[256]={0};

if(strlen(str1)!=strlen(str2)) return 0;

for(i=0;i<strlen(str1);i++){ temp=(int)str1[i]; count1[temp]++; }

for(i=0;i<strlen(str2);i++){ temp=(int)str2[i];

Solution of nagarro placement questions(programming) Ankit Bharthan(BVCOE) count2[temp]++; }

for(i=97;i<=122;i++){ if(count1[i]!=count2[i]){ return 0; } }

return 1; }

5. Write a program to get the first non repeating alphabet from the given string by the user For example: string = abcba Output : c string = abcdecbae Output : d string =naveen Output : a #include<stdio.h> #include<string.h> int main() { char *str="abcdecbae"; int a; for(int i=0;i<strlen(str);i++){

Solution of nagarro placement questions(programming) Ankit Bharthan(BVCOE) a=0; for(int j=0;j<strlen(str);j++){ if(str[i]==str[j]) { a++; // if there is more than one occurance value of a>1 } } if(a==1){ //a is incremented once when i==j printf("%c",str[i]); break; // to end loop after first occurance } } } 6. A string of charater is given.Find the highest occurance of a character and display that character. eg.: INPUT: AEGBCNAVNEETGUPTAEDAGPE OUTPUT: E or I,J(if equal occurance) #include<stdio.h> #include<string.h> int main() { char *str="AEGBCNAVNEETGUPTAJJJJJJJEDAGPEMMMMMMM"; int count[256]={0}; int temp; for(int i=0;i<strlen(str);i++){ temp=(int)str[i]; count[temp]++; } temp=0;

Solution of nagarro placement questions(programming) Ankit Bharthan(BVCOE) int max[10]={0}; int k=0; for(int i=65;i<122;i++){ if(count[i]>=temp){ if(count[i]==temp) k++; temp=count[i]; max[k]=i; } } for(int i=0;i<10;i++){ if(max!=0) printf("%c",max[i]); } }

7. Given a matrix; u hv to print elements on spiral form eg. 1 2 3 4 5678 9 10 11 12 13 14 15 16 output : 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 #include <stdio.h> #define R 3 #define C 6

void spiralPrint(int m, int n, int a[R][C]) {

Solution of nagarro placement questions(programming) Ankit Bharthan(BVCOE) int i, k = 0, l = 0;

/* k - starting row index m - ending row index l - starting column index n - ending column index i - iterator */

while (k < m && l < n) { /* Print the first row from the remaining rows */ for (i = l; i < n; ++i) { printf("%d ", a[k][i]); } k++;

/* Print the last column from the remaining columns */ for (i = k; i < m; ++i) { printf("%d ", a[i][n-1]); } n--;

/* Print the last row from the remaining rows */ if ( k < m){ for (i = n-1; i >= l; --i) { printf("%d ", a[m-1][i]);

Solution of nagarro placement questions(programming) Ankit Bharthan(BVCOE) } m--; }

/* Print the first column from the remaining columns */ for (i = m-1; i >= k; --i) { printf("%d ", a[i][l]); }

l++; } }

int main() { int a[R][C] = { {1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18} };

spiralPrint(R, C, a); return 0; }

Solution of nagarro placement questions(programming) Ankit Bharthan(BVCOE) 8. // sorting string arrays import java.util.*;

class ankit { public static void main(String args[]) { String arr[]={ "ball green1","ball green2","ball green3","ball red1", "ball red3","ball red2","ball blue1"}; String temp=" "; java.util.Arrays.sort(arr);

for(int i=0;i<arr.length;i++) { System.out.println(" "+arr[i]+" "); } } }

9. /*Algorithm..

1. Reverse whole sentence first. 2. Reverse each word individually. Input: complex is fun Output: fun is complex All the reversing happens in-place. */

#include<string.h> #include<stdio.h>

Solution of nagarro placement questions(programming) Ankit Bharthan(BVCOE) void rev(char *l, char *r);

int main(int argc, char *argv[]) { char buf[] = "the world will go on forever"; char *end, *x, *y;

// Reverse the whole sentence first.. for(end=buf; *end; end++); rev(buf,end-1);

// Now swap each word within sentence... x = buf-1; y = buf;

while(x++ < end) { if(*x == '\0' || *x == ' ') { rev(y,x-1); y = x+1; } }

// Now print the final string.... printf("%s\n",buf);

Solution of nagarro placement questions(programming) Ankit Bharthan(BVCOE) return(0); }

// Function to reverse a string in place... void rev(char *l,char *r) { char t; while(l < r) { t = *l; *l++ = *r; *r-- = t; } }

S-ar putea să vă placă și