Showing posts with label C interview Question. Show all posts
Showing posts with label C interview Question. Show all posts
Thursday, 25 July 2019
Tuesday, 18 June 2019
Sunday, 16 June 2019
Saturday, 15 June 2019
Thursday, 13 June 2019
Wednesday, 12 June 2019
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
Tuesday, 4 June 2019
Sunday, 2 June 2019
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
Implementing ternary operator without any conditional statement
June 02, 2019C interview Question, Implementing ternary operator without any conditional statement, Tricky Interview QuestionNo comments
How to implement a ternary operator in C without using conditional statements.
In the following condition : a ? b : c
If a is true, b will be executed.
Otherwise, c will be executed.
We can assume a, b and c as values.
We can code the equation as :
Result = (!!a)*b + (!a)*c
In above equation, is a is true, result will be b.
Otherwise, the result will be c.
// C code to implement ternary operator
#include<stdio.h>
// Function to implement ternary operator without
// conditional statements
int ternaryOperator(int a, int b, int c)
{
// If a is true, we return (1 * b) + (!1 * c) i.e. b
// If a is false, we return (!1 * b) + (1 * c) i.e. c
return ((!!a) * b + (!a) * c);
}
// Driver code
int main()
{
int a = 0, b = 10, c = 20;
// Function call to output b or c depending on a
printf("%d",ternaryOperator(a, b, c));
return 0;
}
Output:
20
Conditionally assign a value without using conditional and arithmetic operators
June 02, 2019C interview Question, Conditionally assign a value without using conditional and arithmetic operators, Tricky Interview QuestionNo comments
Asked in: Google Interview
Given 4 integers a, b, y, and x, where x can only either 0 and 1 only. The ask is as follows:
If 'x' is 0,
Assign value 'a' to variable 'y'
Else (If 'x' is 1)
Assign value 'b' to variable 'y'.
Note: – You are not allowed to use any conditional operator (including the ternary operator) or any arithmetic operator ( +, -, *, /).
Examples :
Input : a = 5 , b = 10, x = 1
Output : y = 10
Input : a = 5, b = 10 , x = 0
Output : y = 5
An Idea is to simply store both 'a' and 'b'
in an array at 0th and 1st index respectively.
Then store value to 'y' by taking 'x' as the index.
Below is implementation
// C program to assign value to y according
// to value of x
#include<stdio.h>
// Function to assign value to y according
// to value of x
int assignValue(int a, int b, int x)
{
int y;
int arr[2];
// Store both values in an array
// value 'a' at 0th index
arr[0] = a;
// Value 'b' at 1th index
arr[1] = b;
// Assign value to 'y' taking 'x' as index
y = arr[x];
return y;
}
int main()
{
int a = 5;
int b = 10;
int x = 0;
printf("Value assigned to 'y' is %d", assignValue(a, b, x);
return 0;
}
Output :
Value assigned to 'y' is 5
If 'x' is 0,
Assign value 'a' to variable 'y'
Else (If 'x' is 1)
Assign value 'b' to variable 'y'.
Note: – You are not allowed to use any conditional operator (including the ternary operator) or any arithmetic operator ( +, -, *, /).
Examples :
Input : a = 5 , b = 10, x = 1
Output : y = 10
Input : a = 5, b = 10 , x = 0
Output : y = 5
An Idea is to simply store both 'a' and 'b'
in an array at 0th and 1st index respectively.
Then store value to 'y' by taking 'x' as the index.
Below is implementation
// C program to assign value to y according
// to value of x
#include<stdio.h>
// Function to assign value to y according
// to value of x
int assignValue(int a, int b, int x)
{
int y;
int arr[2];
// Store both values in an array
// value 'a' at 0th index
arr[0] = a;
// Value 'b' at 1th index
arr[1] = b;
// Assign value to 'y' taking 'x' as index
y = arr[x];
return y;
}
int main()
{
int a = 5;
int b = 10;
int x = 0;
printf("Value assigned to 'y' is %d", assignValue(a, b, x);
return 0;
}
Output :
Value assigned to 'y' is 5
Saturday, 1 June 2019
C Program to convert Number in Characters
Number in characters conversion: In c language, we can easily convert the number in characters by the help of loop and switch case. In this program, we are taking input from the user and iterating this number until it is 0. While iteration, we are dividing it by 10 and the remainder is passed in switch case to get the word for the number.
Let's see the c program to convert the number in characters.
#include<stdio.h>
#include<stdlib.h>
int main(){
long int n,sum=0,r;
system("cls");
printf("enter the number=");
scanf("%ld",&n);
while(n>0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
n=sum;
while(n>0)
{
r=n%10;
switch(r)
{
case 1:
printf("one ");
break;
case 2:
printf("two ");
break;
case 3:
printf("three ");
break;
case 4:
printf("four ");
break;
case 5:
printf("five ");
break;
case 6:
printf("six ");
break;
case 7:
printf("seven ");
break;
case 8:
printf("eight ");
break;
case 9:
printf("nine ");
break;
case 0:
printf("zero ");
break;
default:
printf("tttt");
break;
}
n=n/10;
}
return 0;
}
Output:
enter the number=4321
four three two one
C Program to generate Fibonacci Triangle
In this program, we are getting input from the user for the limit for the fibonacci triangle, and printing the Fibonacci series for the given number of times (limit).
Let's see the c example to generate Fibonacci triangle.
#include<stdio.h>
#include<stdlib.h>
int main(){
int a=0,b=1,i,c,n,j;
system("cls");
printf("Enter the limit:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
a=0;
b=1;
printf("%d\t",b);
for(j=1;j<i;j++)
{
c=a+b;
printf("%d\t",c);
a=b;
b=c;
}
printf("\n");
}
return 0;
}
Output:
Enter the limit:9
1
1 1
1 1 2
1 1 2 3
1 1 2 3 5
1 1 2 3 5 8
1 1 2 3 5 8 13
1 1 2 3 5 8 13 21
1 1 2 3 5 8 13 21 34
Enter the limit:5
1
1 1
1 1 2
1 1 2 3
1 1 2 3 5
C Program to print Number Triangle
Like the alphabet triangle,
we can write the c program to print the number triangle. The number triangle can be printed in different ways.
Let's see the c example to print number triangle.
#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j,k,l,n;
system("cls");
printf("enter the range=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("%d",k);
}
for(l=i-1;l>=1;l--)
{
printf("%d",l);
}
printf("\n");
}
return 0;
}
Output:
enter the range= 4
1
121
12321
1234321
enter the range= 7
1
121
12321
1234321
123454321
12345654321
1234567654321
C Program to print Alphabet Triangle
There are different triangles that can be printed. Triangles can be generated by alphabets or numbers.
In this c program, we are going to print alphabet triangles.
Let's see the c example to print alphabet triangle.
#include<stdio.h>
#include<stdlib.h>
int main(){
int ch=65;
int i,j,k,m;
system("cls");
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
printf(" ");
for(k=1;k<=i;k++)
printf("%c",ch++);
ch--;
for(m=1;m<i;m++)
printf("%c",--ch);
printf("\n");
ch=65;
}
return 0;
}
Output:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
C Program without main() function
We can write c program without using the main() function.
To do so, we need to use the #define preprocessor directive.
Let's see a simple program to print "hello" without main() function.
#include<stdio.h>
#define start main
void start() {
printf("Hello");
}
Output:
Hello








