Showing posts with label Interview. Show all posts
Showing posts with label Interview. Show all posts
Saturday, 25 May 2019
Tuesday, 14 May 2019
The Interview Question Today#27(26-3-19)(Size of structure)
May 14, 2019C interview Question, C Question Group 0, Interview, Size of structure, Technical RoundNo comments
Question:
Write a c program to find the size of structure without using sizeof operator?
Solution:
C program:
#include<stdio.h>
struct XYZ{
int x;
float y;
char z;
};
int main(){
int sz = (int) (((struct XYZ *)0) + 1);
printf("Size of Struct= %d",sz);
return 0;
}
Write a c program to find the size of structure without using sizeof operator?
Solution:
C program:
#include<stdio.h>
struct XYZ{
int x;
float y;
char z;
};
int main(){
int sz = (int) (((struct XYZ *)0) + 1);
printf("Size of Struct= %d",sz);
return 0;
}
Output:
Size of Struct= 12
Friday, 12 April 2019
Integer Promotions in C
Integer Promotions in C
Some data types like char, short int take less number of bytes than int, these data types are automatically promoted to int or unsigned int when an operation is performed on them. This is called integer promotion. For example, no arithmetic calculation happens on smaller types like char, short and enum. They are first converted to int or unsigned int, and then arithmetic is done on them. If an int can represent all values of the original type, the value is converted to an int. Otherwise, it is converted to an unsigned int.
For example see the following program:
#include <stdio.h>
int main()
{
char a = 30, b = 40, c = 10;
char d = (a * b) / c;
printf ("%d ", d);
return 0;
}
Output:
120
At first look, the expression (a*b)/c seems to cause arithmetic overflow because signed characters can have values only from -128 to 127 (in most of the C compilers), and the value of subexpression ‘(a*b)’ is 1200 which is greater than 128. But integer promotion happens here in arithmetic done on char types and we get the appropriate result without any overflow.
Some data types like char, short int take less number of bytes than int, these data types are automatically promoted to int or unsigned int when an operation is performed on them. This is called integer promotion. For example, no arithmetic calculation happens on smaller types like char, short and enum. They are first converted to int or unsigned int, and then arithmetic is done on them. If an int can represent all values of the original type, the value is converted to an int. Otherwise, it is converted to an unsigned int.
For example see the following program:
#include <stdio.h>
int main()
{
char a = 30, b = 40, c = 10;
char d = (a * b) / c;
printf ("%d ", d);
return 0;
}
Output:
120
At first look, the expression (a*b)/c seems to cause arithmetic overflow because signed characters can have values only from -128 to 127 (in most of the C compilers), and the value of subexpression ‘(a*b)’ is 1200 which is greater than 128. But integer promotion happens here in arithmetic done on char types and we get the appropriate result without any overflow.
Thursday, 11 April 2019
Tricky Interview #1
Question:
W A C Program That Prints All The vowels Given In String Without Using IF and Break Statements In program?
Submit Ur Answers (Follow The Link)
Link: https://docs.google.com/forms/d/e/1FAIpQLSeBXalNTB5cLX8dC8BlFmzGcItV52GBiUkqaaCM2q_bpaDnKg/viewform?usp=sf_link
W A C Program That Prints All The vowels Given In String Without Using IF and Break Statements In program?
Submit Ur Answers (Follow The Link)
Link: https://docs.google.com/forms/d/e/1FAIpQLSeBXalNTB5cLX8dC8BlFmzGcItV52GBiUkqaaCM2q_bpaDnKg/viewform?usp=sf_link
Wednesday, 3 April 2019
Thursday, 28 March 2019
Print a long int in C using putchar() only
Print a long int in C using putchar() only
C program:
#include <stdio.h>
void print(long n)
{
/* If the number is smaller than 0, put a - sign and change number to positive */
if (n < 0) {
putchar('-');
n = -n;
}
if (n/10)
print(n/10);
// Print the last digit
putchar(n%10 + '0');
}
int main()
{
long int n = 12045;
print(n);
return 0;
}
Output:
12045
Print characters without using format specifiers
Question:
C program to print characters without using format specifiers
C Program:
// Prints characters without format specifiers
#include <stdio.h>
int main()
{
//ASCII Values
printf("\x41 \n");
printf("\x4d \n");
printf("\x49 \n");
printf("\x54");
return 0;
}
Output:
A
M
I
T
Round off
Write a one-line C function to round floating point numbers
Algorithm: roundNo(num)1. If num is positive then add 0.5.
2. Else subtract 0.5.
3. Typecast the result to int and return.
2. Else subtract 0.5.
3. Typecast the result to int and return.
Example:num = 1.67, (int) num + 0.5 = (int)2.17 = 2
C program:
/* Program for rounding floating point numbers */
# include<stdio.h>
int roundNo(float num)
{
return num < 0 ? num - 0.5 : num + 0.5;
}
int main()
{
printf("%d", roundNo(-1.777));
getchar();
return 0;
}
Print “Even” or “Odd” without using conditional statement
Question:
Print “Even” or “Odd” without using a conditional statement
C program:
#include<stdio.h>
int main()
{
int no;
printf("Enter a no: ");
scanf("%d", &no);
(no & 1 && printf("odd"))|| printf("even");
return 0;
}
Output:
Enter a no: 45
odd
C to convert a number to a string
Question:
What is the best way in C to convert a number to a string?
Solution: Use sprintf() function.
C program:
#include<stdio.h>
int main()
{
char result[50];
float num = 23.34;
sprintf(result, "%f", num);
printf("\n The string for the num is %s",
result);
}
Output:
The string for the num is 23.340000
Program for Sum the digits of a given number
Program for Sum the digits of a given number
Examples :
Input : n = 687
Output : 21
C program:
# include<stdio.h>
/* Function to get sum of digits */
int getSum(int n)
{
int sum;
/* Single line that calculates sum */
for (sum = 0; n > 0; sum += n % 10, n /= 10);
return sum;
}
int main()
{
int n = 687;
printf(" %d ", getSum(n));
return 0;
}
Output:
21
To find sum of two numbers without using any operator
Write a program to find the sum of positive integers without using any operator.
C program:
#include<stdio.h>
int add(int x, int y)
{
return printf("%*c%*c", x, ' ', y, ' ');
}
int main()
{
printf("Sum = %d", add(3, 4));
return 0;
}
Output:
Sum = 7
Activity Selection Problem
Activity Selection Problem
1) Sort the activities according to their finishing time
2) Select the first activity from the sorted array and print it.
3) Do following for remaining activities in the sorted array.
a) If the start time of this activity is greater than or equal to the finish time of previously selected activity then select this activity and print it.
2) Select the first activity from the sorted array and print it.
3) Do following for remaining activities in the sorted array.
a) If the start time of this activity is greater than or equal to the finish time of previously selected activity then select this activity and print it.
Example:
Consider the following 3 activities sorted by finish time.
Input:
start[] = {10, 12, 20};
finish[] = {20, 25, 30};
Output:
A person can perform at most two activities. The
maximum set of activities that can be executed
is {0, 2}
C program:
#include<stdio.h>
/* Prints a maximum set of activities that can be done by a single person, one at a time.*/
// n --> Total number of activities
// s[] --> An array that contains
//start time of all activities
//start time of all activities
// f[] --> An array that contains
//finish time of all activities
//finish time of all activities
void printMaxActivities(int s[], int f[], int n)
{
int i, j;
printf ("Following activities are selected n ");
i = 0;
printf("%d ", i);
for (j = 1; j < n; j++)
{
// If this activity has start time greater than or
// equal to the finish time of previously selected
// activity, then select it
if (s[j] >= f[i])
{
printf ("%d ", j);
i = j;
}
}
}
int main()
{
int s[] = {1, 3, 0, 5, 8, 5};
int f[] = {2, 4, 6, 7, 9, 9};
int n = sizeof(s)/sizeof(s[0]);
printMaxActivities(s, f, n);
return 0;
}
Output:
Following activities are selected n 0 1 3 4
Minimum number of Coins
Find the Minimum number of Coins
1) Initialize result as empty.
2) find the largest denomination that is
smaller than V.
3) Add found denomination to result.
Subtract the value of found denomination from V.
4) If V becomes 0, then print result.
Else repeat steps 2 and 3
for the new value of V
Example:
Input: V = 70
Output: 2
We need a 50 Rs note and a 20 Rs note.
C program:
#include <stdio.h>
#define COINS 9
#define MAX 20
// All denominations of Indian Currency
int coins[COINS] = { 1, 2, 5, 10, 20, 50, 100, 200, 2000 };
void findMin(int cost)
{
int coinList[MAX] = { 0 };
int i, k = 0;
for (i = COINS - 1; i >= 0; i--) {
while (cost >= coins[i]) {
cost -= coins[i];
coinList[k++] = coins[i];
}
}
for (i = 0; i < k; i++) {
printf("%d ", coinList[i]); }
return;
}
int main(void)
{
int n;
printf("Enter the Rupees u want Change\n");
scanf("%d",&n);
printf("Following is minimal number of change for %d: ", n);
findMin(n);
return 0;
}
Output:
Enter the Rupees u want Change
1276
Following is minimal number of change for 1276: 200 200 200 200 200 200 50 20 5 1







