Sunday, 30 June 2019
Saturday, 29 June 2019
Friday, 28 June 2019
Monday, 24 June 2019
Aptitude Hack#121(24-6-19)
Question:
Free notebooks were distributed equally among children of a class. The number of notebooks each child got was one-eighth of the number of children. Had the number of children been half, each child would have got 16 notebooks. Total how many notebooks were distributed?
A. 256
B. 432
C. 512
D. 640
E. None of these
Free notebooks were distributed equally among children of a class. The number of notebooks each child got was one-eighth of the number of children. Had the number of children been half, each child would have got 16 notebooks. Total how many notebooks were distributed?
A. 256
B. 432
C. 512
D. 640
E. None of these
Sunday, 23 June 2019
Saturday, 22 June 2019
Friday, 21 June 2019
Thursday, 20 June 2019
Wednesday, 19 June 2019
Anagram or not.
Question:
Approach:
Step 1: Check Both the String For Equal Length.
if equal then continue, else print Not Anagram.
Step 2: Take A Array for All the Alphabets
And Calculate the no of times that present in the both String
Step 3: Comparing the Array if equal then Anagram
Else not Anagram.
Solution:
#include<stdio.h>
int main()
{
char a[10],b[10];
int i,j,k,m,n,z[26]={0},y[26]={0},flag,c,d;
printf("enter the 2 string");
scanf("%s %s",a,b);
if(strlen(a)!=strlen(b))
printf("not an anagram");
else
{
k=0;
while(k!=26)
{
n=1;
m=1;
for(i=0;i<strlen(a);i++)
{
c=(int)a[i]-97;
d=(int)b[i]-97;
if(k==d)
{
y[k]=m++;
}
if(k==c)
{
z[k]=n++;
}
}
k++;
}
for(j=0;j<26;j++)
{
if(z[j]!=y[j]){
printf("Not anagram");
return 0;
}
}
printf("Anagram");
}
return 0;
}
Approach:
Step 1: Check Both the String For Equal Length.
if equal then continue, else print Not Anagram.
Step 2: Take A Array for All the Alphabets
And Calculate the no of times that present in the both String
Step 3: Comparing the Array if equal then Anagram
Else not Anagram.
Solution:
#include<stdio.h>
int main()
{
char a[10],b[10];
int i,j,k,m,n,z[26]={0},y[26]={0},flag,c,d;
printf("enter the 2 string");
scanf("%s %s",a,b);
if(strlen(a)!=strlen(b))
printf("not an anagram");
else
{
k=0;
while(k!=26)
{
n=1;
m=1;
for(i=0;i<strlen(a);i++)
{
c=(int)a[i]-97;
d=(int)b[i]-97;
if(k==d)
{
y[k]=m++;
}
if(k==c)
{
z[k]=n++;
}
}
k++;
}
for(j=0;j<26;j++)
{
if(z[j]!=y[j]){
printf("Not anagram");
return 0;
}
}
printf("Anagram");
}
return 0;
}
//Credits Rakshita.
Tuesday, 18 June 2019
Monday, 17 June 2019
Sunday, 16 June 2019
Saturday, 15 June 2019
Friday, 14 June 2019
Thursday, 13 June 2019
Wednesday, 12 June 2019
Tricky Interview#1 Solution
Question:
W A C Program That Prints All The vowels Given In String Without Using IF and Break Statements In program?
Solution:
//Credits Gaurav and Swathi H I
#include<stdio.h>
#include<stdlib.h>
main(){
char str[10];
scanf("%s",str);
int i=0;
while(str[i]!='\0'){
(str[i]=='a'|str[i]=='e'|str[i]=='i'|str[i]=='o'|str[i]=='u')? printf("%c",str[i]):printf("");
i++;
}
}
Alternative Solution:
//JavaAbhigyan
#include<stdio.h>
#include<stdlib.h>
main()
{
char str[10],c;
scanf("%s",str);
int i=0;
while((c=str[i])!='\0')
{
switch(c)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c",c);
}
i++;
}
}
Tuesday, 11 June 2019
Aptitude Hack#108(11-6-19)
Question:
A drinks machine offers three selections - Tea, Coffee or Random but the machine has been wired up wrongly so that each button does not give what it claims. If each drink costs 50p, how much minimum money do you have to put into the machine to work out which button gives which selection?
(A) 5 rupees
(B) 10 rupees
(C) 25 rupees
(D) Infinity
A drinks machine offers three selections - Tea, Coffee or Random but the machine has been wired up wrongly so that each button does not give what it claims. If each drink costs 50p, how much minimum money do you have to put into the machine to work out which button gives which selection?
(A) 5 rupees
(B) 10 rupees
(C) 25 rupees
(D) Infinity
Monday, 10 June 2019
Tricky Interview#6
Question:
WAC PROGRAM THAT GIVES THE SIZE OF STRING WITHOUT USING ANY FUNCTION(PRE DEFINED OR USER DEFINED), NO LOOPS AND NO RECURSION.
Solution:
#include<stdio.h>
void main()
{
int a[100],n;
printf("Enter the String\n");
scanf("%s",a);
n=printf("%s\n",a);
printf("%d",n-1);
}
WAC PROGRAM THAT GIVES THE SIZE OF STRING WITHOUT USING ANY FUNCTION(PRE DEFINED OR USER DEFINED), NO LOOPS AND NO RECURSION.
Solution:
#include<stdio.h>
void main()
{
int a[100],n;
printf("Enter the String\n");
scanf("%s",a);
n=printf("%s\n",a);
printf("%d",n-1);
}
Tricky Interview#5
Question:
How would you write a C program to print 1 to 100 without loop, recursion, or goto?
Solution:
#include<stdio.h>
void hundred() { static int i=1; printf("%d\n",i++); }
void twenty(){ hundred(),hundred(),hundred(),hundred(),hundred(); }
void four() { twenty(),twenty(),twenty(),twenty(),twenty(); }
int main()
{
four(),four(),four(),four();
}
How would you write a C program to print 1 to 100 without loop, recursion, or goto?
Solution:
#include<stdio.h>
void hundred() { static int i=1; printf("%d\n",i++); }
void twenty(){ hundred(),hundred(),hundred(),hundred(),hundred(); }
void four() { twenty(),twenty(),twenty(),twenty(),twenty(); }
int main()
{
four(),four(),four(),four();
}
Sunday, 9 June 2019
Aptitude Hack#106(9-6-19)
Question:
Mr. Das decided to walk down the escalator of a mall. He found that if he walks down
26 steps, he requires 30seconds to reach the bottom. However, if he steps down 34
stair He would only require 18 seconds to get to the bottom. If the time is measured
from the moment the top step begins to descend to the time he steps off the last step
at the bottom, find out the height of the stairway insteps?
(A)46
(B)35
(C) 56
(D)55
Mr. Das decided to walk down the escalator of a mall. He found that if he walks down
26 steps, he requires 30seconds to reach the bottom. However, if he steps down 34
stair He would only require 18 seconds to get to the bottom. If the time is measured
from the moment the top step begins to descend to the time he steps off the last step
at the bottom, find out the height of the stairway insteps?
(A)46
(B)35
(C) 56
(D)55
Saturday, 8 June 2019
Friday, 7 June 2019
Thursday, 6 June 2019
Wednesday, 5 June 2019
Aptitude Hack#102(5-6-19)
Question:
Ram and Hari started from A and B, towards B and A at 6.00 am and 7.00 am respectively. They meet each other at 9.00 am and continued towards their respective destinations. Ram reaching B turns back and catches up with Hari before Hari reaches A at 11.00 am. At what time will Hari reach A.
(a) 7.00 pm
(b) 4.00 pm
(c) 5.00 pm
(d) 6.00 pm
Ram and Hari started from A and B, towards B and A at 6.00 am and 7.00 am respectively. They meet each other at 9.00 am and continued towards their respective destinations. Ram reaching B turns back and catches up with Hari before Hari reaches A at 11.00 am. At what time will Hari reach A.
(a) 7.00 pm
(b) 4.00 pm
(c) 5.00 pm
(d) 6.00 pm
Tuesday, 4 June 2019
Monday, 3 June 2019
Sunday, 2 June 2019
Program for Shell Sort in C
Shell short is an improved and efficient version of the insertion sort.
In this algorithm, we sort the pair of elements that are far apart by gap h.
The process is repeated by reducing h until it becomes 1.
Shell Sort |
Algorithm
Following is the algorithm for shell sort.
Step 1 − Initialize the value of h
Step 2 − Divide the list into smaller
sub-list of equal interval h
Step 3 − Sort these sub-lists using insertion sort
Step 4 − Repeat until the complete list is sorted
Program for Shell Sort in C
#include<stdio.h>
void sort(int a[],int n)
{
int gap,i,j,temp;
for(gap=n/2;gap>0;gap/=2)
{
for(i=gap;i<n;i+=1)
{
temp=a[i];
for(j=i;j>=gap&&a[j-gap]>temp;j-=gap)
a[j]=a[j-gap];
a[j]=temp;
}
}
}
int main()
{
int a[20],i,n;
printf("Enter number of elements:");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;++i)
{
scanf("%d",&a[i]);
} sort(a,n);
printf("\nArray after shell sort:\n");
for(i=0;i<n;++i)
printf("%d ",a[i]);
return 0;
}
Output
Enter number of elements:5
Enter array elements:
56 7 2 9 12
Array after shell sort:
2 7 9 12 56
C program to print the truth table for XY+Z
June 02, 2019C interview Question, C program to print the truth table for XY+Z, Tricky Interview QuestionNo comments
C program to print the truth table for XY+Z
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
clrscr(); //to clear the screen
printf(“XtYtZtXY+Z”);
for(x=0;x<=1;++x)
for(y=0;y<=1;++y)
for(z=0;z<=1;++z)
{
if(x*y+z==2)
printf(“nn%dt%dt%dt1”,x,y,z);
else
printf(“nn%dt%dt%dt%d”,x,y,z,x*y+z);
}
}
OUTPUT:
Print “javaabhigyan” with empty main() in C
June 02, 2019C interview Question, Print “javaabhigyan” with empty main() in C, Tricky Interview QuestionNo comments
Write a program that prints “javaabhigyan” with empty main() function.You are not allowed to write anything in main().
1.) One way of doing this is to apply GCC constructor attribute to a function so that it executes before main()
#include <stdio.h>
/* Apply the constructor attribute to myStartupFun()
so that it is executed before main() */
void myStartupFun(void) __attribute__((constructor));
/* implementation of myStartupFun */
void myStartupFun(void)
{
printf("javaabhigyan");
}
int main()
{
}
Output:
javaabhigyan
2.) In linux, just override the default definition of _start() function so that it would work as a custom startup code. See this article to understand more.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
}
// _start() function
void _start(void)
{
printf("javaabhigyan");
// Call main() function
int var = main();
exit(var);
}
Now compile this by following command
gcc -nostartfiles -o file file.c
Output:
javaabhigyan
Compute average of two numbers without overflow
June 02, 2019C interview Question, Compute average of two numbers without overflow, Tricky Interview QuestionNo comments
Given two numbers, a and b. Compute the average of the two numbers.
The well know formula (a + b) / 2 may fail at the following case :
If, a = b = (2^31) – 1; i.e. INT_MAX.
Now, (a+b) will cause overflow and hence formula (a + b) / 2 wont work
Improved Formula that does not cause overflow :
Average = (a / 2) + (b / 2) + (((a % 2) + (b % 2)) / 2)
Below is the implementation :
// C code to compute average of two numbers
#include <stdio.h>
#define INT_MAX 2147483647
// Function to compute average of two numbers
int compute_average(int a, int b)
{
return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2);
}
int main()
{
// Assigning maximum integer value
int a = INT_MAX, b = INT_MAX;
// Average of two equal numbers is the same number
printf("Actual average : %d\n",INT_MAX);
// Function to get the average of 2 numbers
printf("Computed average : %d",compute_average(a, b));
return 0;
}
Output:
Actual average: 2147483647
Computed average: 2147483647