Embedded C Interview Questions with Answers | Part 4
Square of Number Without Using * Operator in C
#include <stdio.h>
#include "stdafx.h"
#include "conio.h"
int square(int x)
{
int i = (x-1);
int orgx = x;
while(i--)
{
x=x+orgx;
}
return x;
}
#define SQR(x) {int i = x; \
int res = 0; \
while(i--){ \
res = res + x;} \
x=res;}
int getSquare(int x)
{
int i = x;
int res = 0;
while(i--)
{
res=res+x;
}
return res;
}
int main()
{
printf("Hello, World!\n");
int a = 7;
printf("%d\n",square(a));
printf("%d\n",getSquare(a));
SQR(a);
printf("%d\n",a);
getch();
}
Output:
Hello, World!
49
49
49
#include "stdafx.h"
#include "conio.h"
int square(int x)
{
int i = (x-1);
int orgx = x;
while(i--)
{
x=x+orgx;
}
return x;
}
#define SQR(x) {int i = x; \
int res = 0; \
while(i--){ \
res = res + x;} \
x=res;}
int getSquare(int x)
{
int i = x;
int res = 0;
while(i--)
{
res=res+x;
}
return res;
}
int main()
{
printf("Hello, World!\n");
int a = 7;
printf("%d\n",square(a));
printf("%d\n",getSquare(a));
SQR(a);
printf("%d\n",a);
getch();
}
Output:
Hello, World!
49
49
49
Divide the Number With 9 Without Using / * % Operator in C
#include "stdafx.h"
#include <conio.h>
#include <stdio.h>
int checkDivby9(int x)
{
int status = 0, ans = 0;
int divby8;
if(x < 9)
status = 0;
while(x >= 72)
{
x = x - 72;
ans = ans + 8;
}
divby8 = x >> 3;
//printf("%d\n",divby8);
//printf("%d\n",x);
if(divby8 == (x-(divby8 << 3)))
{
status = 1;
ans = ans + divby8;
}
//printf("%d\n",ans);
return status;
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("Hello, World!\n");
int a;
a = 36;
if(checkDivby9(a))
printf("%d is divisible by 9\n", a);
a = 80;
if(checkDivby9(a))
printf("%d is divisible by 9\n", a);
a = 90;
if(checkDivby9(a))
printf("%d is divisible by 9\n", a);
a = 108;
if(checkDivby9(a))
printf("%d is divisible by 9\n", a);
a = 4500;
if(checkDivby9(a))
printf("%d is divisible by 9\n", a);
getch();
return 0;
}
OutPut:
Hello, World!
36 is divisible by 9
90 is divisible by 9
108 is divisible by 9
4500 is divisible by 9
#include <conio.h>
#include <stdio.h>
int checkDivby9(int x)
{
int status = 0, ans = 0;
int divby8;
if(x < 9)
status = 0;
while(x >= 72)
{
x = x - 72;
ans = ans + 8;
}
divby8 = x >> 3;
//printf("%d\n",divby8);
//printf("%d\n",x);
if(divby8 == (x-(divby8 << 3)))
{
status = 1;
ans = ans + divby8;
}
//printf("%d\n",ans);
return status;
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("Hello, World!\n");
int a;
a = 36;
if(checkDivby9(a))
printf("%d is divisible by 9\n", a);
a = 80;
if(checkDivby9(a))
printf("%d is divisible by 9\n", a);
a = 90;
if(checkDivby9(a))
printf("%d is divisible by 9\n", a);
a = 108;
if(checkDivby9(a))
printf("%d is divisible by 9\n", a);
a = 4500;
if(checkDivby9(a))
printf("%d is divisible by 9\n", a);
getch();
return 0;
}
OutPut:
Hello, World!
36 is divisible by 9
90 is divisible by 9
108 is divisible by 9
4500 is divisible by 9
How to Check Weather Number is Power of 2 | Optimize Method
It is very much easy to check the power of 2. If you observe the pattern for the different numbers.
For example:
3 = 11
4 = 100
7 = 111
8 = 1000
15 = 1111
16 = 10000
31 = 11111
32 = 100000
Observe that both numbers carefully. You can see if you do anding of both the numbers then result will be zero.
Example 3 & 4 = 0.
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "string.h"
#include "stdlib.h"
int _tmain(int argc, _TCHAR* argv[])
{
int chPow2 = 30;
if((chPow2 & (chPow2 - 1)) == 0)
printf("Number: %d is power of 2", chPow2);
else
printf("Number: %d is not power of 2", chPow2);
getch();
return 0;
}
For example:
3 = 11
4 = 100
7 = 111
8 = 1000
15 = 1111
16 = 10000
31 = 11111
32 = 100000
Observe that both numbers carefully. You can see if you do anding of both the numbers then result will be zero.
Example 3 & 4 = 0.
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "string.h"
#include "stdlib.h"
int _tmain(int argc, _TCHAR* argv[])
{
int chPow2 = 30;
if((chPow2 & (chPow2 - 1)) == 0)
printf("Number: %d is power of 2", chPow2);
else
printf("Number: %d is not power of 2", chPow2);
getch();
return 0;
}
Post a Comment