C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++ tutorials and pdf

int main() { int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n"); scanf("%d",&n);

printf("Enter %d integers\n", n);

for ( c = 0 ; c < n ; c++ ) scanf("%d",&array[c]);

printf("Enter value to find\n"); scanf("%d",&search);

first = 0; last = n - 1; middle = (first+last)/2;

while( first <= last ) { if ( array[middle] < search ) first = middle + 1; else if ( array[middle] == search ) { printf("%d found at location %d.\n", search, middle+1); break; } else last = middle - 1;

middle = (first + last)/2; } if ( first > last ) printf("Not found! %d is not present in the list.\n", search);

#include #include int key,A[5],m,i,lb,ub; int binary(key,A[],lb,ub) { if(lb>ub) { printf("\n Search failed"); return; } m=lb+ub/2; if(key==A[m]) { printf("\n Match Found and value is %d",key); return; } elseif(key<A[m]) { ub=m-1; binary(key,A[],lb,ub); } else { lb=m+1; binary(key,A[],lb,ub); } } void main() { clrscr(); for(i=0;i<5;i++) { printf("\n Enter the value in array:\n"); scanf("%d",&A[i]) } printf("\n Enter the value of item to search:\n"); scanf("%d",&key); lb=A[0]; ub=A[5]; binary(key,A[],lb,ub); getch(); }

Given a number.You have to find the binary equivalent of that number first. Your task is to rearrange the 0's and 1's in the binary equivalent found to all the combinations and print all the numbers which form a co-prime with the digit entered.Print nil if no co-prime is found.

This seems to go infinite unless specified differently, but I may very much be wrong, here is what I am thinking though,

if (m > n || m < 0 ) return 0;

Unless you want to allow the array to contain negative numbers, in that case I assume one would have to put in an algorithm to initiate the smallest number in the array to a variable, then insert that one instead of 0 in the above if statement, one way would be to insert below inside the for loop when scanning for a[i]:

// once there is exactly 2 elements in the array a[], then initialize min variable as the smallest number if (i == 1) min = a[0]; // Now one can compare each iteration to see if a number in the array a[] is smaller if (a[i] < min) min = a[i];

So after this, one can use the code above, now in this way:

if (m > n || m < min ) return 0;

But hey, I am actually very very new to this, so please let me know if this even works at all, I am currently learning how to implement binary search, so hopefully my thoughts here are in the ball park at least :)

linear search starts from one value, like the smallest, then iterates through each value to see if it finds what it is searching for, so for example, if looking for the number 19 from 0-40, linear search would go: 1, 2, 3, 4, 5 etc til it reached 19

binary search instead, splits the value in 2, until it finds it, so it will do: is the number in the middle of 0-40 ? (20, in other words) No, then is the value more than, or less than 20? If more, then check the middle between 20-40 If less, check 0-20 then repeat

So in this case, that would give us: 10 (0-20) 15 (10-20) 17(or 18) (15-20) 19, found it!

The reason this is efficient is that it makes it way faster to search through numbers When the sample size is small, it might not make a difference, but when searching through really big numbers, it will show

For example, find 19000 out of 0-1000000 (one milion) 500000 (0-1000000) 250000 (0-500000) 125000 (0-250000) 62500 (0-125000) 31250 (0-62500) 15625 (0-31250) 23437 (15625-31250) 19531 (15625-23437) 17578 (15625-19531) 18555 (17578-19531) 19043 (18555-19531) 18799 (18555-19043) 18921 (18799-19043) 18982 (18921-19043) 19012 (18982-19043) 18997 (18982-19012) 19004 (18997-19012) 19000 (18997-19004), Found it!

So in this example it takes 19 steps out of a 1000000 (Milion) numbers With linear search it would have taken 19000, in this case I think that out of a 1000000000 (Bilion) numbers, it would only require 4-5 more steps to 19.

So basically, it stays pretty quick even if the numbers increase dramatically.

Here is my new programming repositry..you can visit to get latest interview programs along with theirs solution..https://github.com/harsit007/Programming_Made_Easy_With_Harsit

since the array index starts with zero. so, a[o]=1, a[1]=2, a[2]=3, a[3]=4. If your printing 'i', it is an index number. then for element 3 the position or index number is 2

## 54 comments:

not working

Hello friend ur program is working sucessfully. Thanks for it.

cud u put some comments 2 make it easier 2 undrstand

thanx bro!!! u got a good hand in C!!!!!!!!

please put some comments

thanks

hey i need a program for graphical representation of binary search in c

hey can i get a program of queue in c using graphic.c

Good Job

it saves our time ....... thanx

lucky guy

only works if input array is already sorted

seocndly ..for m<a[mid]){ u=mid-1; the loop will go infinte

sorry it wont go infinite..

I think l < u would be correct

It is not working......

can show us it's algorithm as well as flowchart.

good work

for searching,array must be sorted

its realy gud thanx dear

Could always the input be in ascending order?

nice work bro

thank you very much. i feel very easy in using 'c' in this blog. so simple and power

prasad..

simple huffman algorithm

#include

#include

#include

void main()

{

struct huff

{

long int freq,code;

char data[10];

}h[50];

long int n,n1=100,i,j,n2=90,temp;

char tempc[10];

clrscr();

printf("\t\t\tHUFFMAN ALGORITHM\n\n");

printf("Enter the number of character");

scanf("%ld",&n);

for(i=0;i<n;i++)

{

printf("Enter the character::");

scanf("%s",h[i].data);

printf("Enter the frequency for character %s::", h[i].data);

scanf("%ld",&h[i].freq);

}

for(i=0;i<n-1;i++)

{

for(j=1;j<n;j++)

{

if(h[i].freq<h[j].freq)

{

temp=h[i].freq;

h[i].freq=h[j].freq;

h[j].freq=temp;

strcpy(tempc,h[i].data);

strcpy(h[i].data,h[j].data);

strcpy(h[j].data,tempc);

}

}

}

h[0].code=10;

h[1].code=11;

for(i=2;i<n;i++)

{

if(i%2==0)

{

h[i].code=h[i-2].code+n1;

n1=n1*10;

}

else

{

h[i].code=h[i-2].code+n2;

n2=n2*10;

}

}

printf("Huffman code");

printf("\nchar\tfreq\tcode");

for(i=0;i<n;i++)

{

printf("\n%s\t%ld\t%ld\n",h[i].data,h[i].freq,h[i].code);

}

getch();

}

cud u pls do d programs fo findin hcf & lcm ?

good one.....

Yup it should always be in ascending order.

thanx ....but pls put commands....sir

so many closed braces but only one open brace...how ca it work!!!

Thankzzzz....!!!!!

Itzz really very useful 2 meeee......!!!!!!

you try again

Thank you dude its working thanks a lot

good

#include

int main(){

int a[10],i,n,m,c=0,l,u,mid;

int j;

printf("Enter the size of an array: ");

scanf("%d",&n);

printf("Enter the elements in ascending order: ");

for(i=0;i<n;i++){

scanf("%d",&a[i]);

}

printf("Enter the number to be search: ");

scanf("%d",&m);

for(j=0;j<n;j++)

{

if(m==a[j])

{

c=1;

}

}

if(c==0)

printf("The number is not found.");

else

printf("The number is found.");

return 0;

}

what is the diff between linear search and binary search

#include

int main()

{

int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");

scanf("%d",&n);

printf("Enter %d integers\n", n);

for ( c = 0 ; c < n ; c++ )

scanf("%d",&array[c]);

printf("Enter value to find\n");

scanf("%d",&search);

first = 0;

last = n - 1;

middle = (first+last)/2;

while( first <= last )

{

if ( array[middle] < search )

first = middle + 1;

else if ( array[middle] == search )

{

printf("%d found at location %d.\n", search, middle+1);

break;

}

else

last = middle - 1;

middle = (first + last)/2;

}

if ( first > last )

printf("Not found! %d is not present in the list.\n", search);

return 0;

}

its realy gud thanks

i am getting it a postion before. ex 1 2 3 4 and i choose to search 3 it show the postion of 3 as 2. help :)

can i get the logic of the program

#include

#include

int key,A[5],m,i,lb,ub;

int binary(key,A[],lb,ub)

{

if(lb>ub)

{

printf("\n Search failed");

return;

}

m=lb+ub/2;

if(key==A[m])

{

printf("\n Match Found and value is %d",key);

return;

}

elseif(key<A[m])

{

ub=m-1;

binary(key,A[],lb,ub);

}

else

{

lb=m+1;

binary(key,A[],lb,ub);

}

}

void main()

{

clrscr();

for(i=0;i<5;i++)

{

printf("\n Enter the value in array:\n");

scanf("%d",&A[i])

}

printf("\n Enter the value of item to search:\n");

scanf("%d",&key);

lb=A[0];

ub=A[5];

binary(key,A[],lb,ub);

getch();

}

plz give me the ans for this question

Given a number.You have to find the binary equivalent of that number first. Your task is to rearrange the 0's and 1's in the binary equivalent found to all the combinations and print all the numbers which form a co-prime with the digit entered.Print nil if no co-prime is found.

This seems to go infinite unless specified differently, but I may very much be wrong, here is what I am thinking though,

if (m > n || m < 0 )

return 0;

Unless you want to allow the array to contain negative numbers, in that case I assume one would have to put in an algorithm to initiate the smallest number in the array to a variable, then insert that one instead of 0 in the above if statement, one way would be to insert below inside the for loop when scanning for a[i]:

// once there is exactly 2 elements in the array a[], then initialize min variable as the smallest number

if (i == 1)

min = a[0];

// Now one can compare each iteration to see if a number in the array a[] is smaller

if (a[i] < min)

min = a[i];

So after this, one can use the code above, now in this way:

if (m > n || m < min )

return 0;

But hey, I am actually very very new to this, so please let me know if this even works at all, I am currently learning how to implement binary search, so hopefully my thoughts here are in the ball park at least :)

linear search starts from one value, like the smallest, then iterates through each value to see if it finds what it is searching for, so for example, if looking for the number 19 from 0-40, linear search would go:

1, 2, 3, 4, 5 etc til it reached 19

binary search instead, splits the value in 2, until it finds it, so it will do:

is the number in the middle of 0-40 ? (20, in other words)

No, then is the value more than, or less than 20?

If more, then check the middle between 20-40

If less, check 0-20

then repeat

So in this case, that would give us:

10 (0-20)

15 (10-20)

17(or 18) (15-20)

19, found it!

The reason this is efficient is that it makes it way faster to search through numbers

When the sample size is small, it might not make a difference, but when searching through really big numbers, it will show

For example, find 19000 out of 0-1000000 (one milion)

500000 (0-1000000)

250000 (0-500000)

125000 (0-250000)

62500 (0-125000)

31250 (0-62500)

15625 (0-31250)

23437 (15625-31250)

19531 (15625-23437)

17578 (15625-19531)

18555 (17578-19531)

19043 (18555-19531)

18799 (18555-19043)

18921 (18799-19043)

18982 (18921-19043)

19012 (18982-19043)

18997 (18982-19012)

19004 (18997-19012)

19000 (18997-19004), Found it!

So in this example it takes 19 steps out of a 1000000 (Milion) numbers

With linear search it would have taken 19000, in this case

I think that out of a 1000000000 (Bilion) numbers, it would only require 4-5 more steps to 19.

So basically, it stays pretty quick even if the numbers increase dramatically.

not working for odd num of array element

ex a[15]={1,3,5,7,8,10,12,13,20,34,42,56,61,71,85}

for above array it not working

Define a binary search.illustrate the binary search algorithm with graphical representation

Please any body to can answer fast

any one can explain me this program please

Here is my new programming repositry..you can visit to get latest interview programs

along with theirs solution..https://github.com/harsit007/Programming_Made_Easy_With_Harsit

since the array a[i] is declared as integer, you need to give the array size with in its rage. That's it

since the array index starts with zero. so, a[o]=1, a[1]=2, a[2]=3, a[3]=4. If your printing 'i', it is an index number. then for element 3 the position or index number is 2

ll the brackets ain't closed correctly!will this work?

venkatareddy dodda how can i reach i want to talk

u

Post a Comment