Here is the part 2 of Coding problems for technical interview with answers in C programming language.
Click here to see coding problems for technical interview Part -1
1. GCD of two numbers.
#include <stdio.h>
int main()
{
int num1, num2, i, gcd;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
for(i=1; i <= num1 && i <= num2; ++i)
{
if(num1%i==0 && num2%i==0)
gcd = i;
}
printf("G.C.D of %d and %d is %d", num1, num2, gcd);
return 0;
}
2. Abundant number / Excessive number.
#include<stdio.h>
int main()
{
int n, temp;
scanf("%d",&n);
int sum = 0;
for(int i = 1; i < n; i++)
{
if(n % i == 0)
{
sum = sum + i;
}
}
if(n < sum)
printf("It is an Abundant Number ");
else
printf("It is not an Abundant Number");
return 0;
}
3. Find the number of 3s.
#include<stdio.h>
int count_3s( int );
int main()
{
int n;
printf( "Enter a number: " );
scanf("%d", &n);
printf("The number of threes is %d\n", count_3s(n) );
return 0;
}
int count_3s(int n)
{
int c = 0;
int rem;
while(n > 0) {
rem = n % 10;
n /= 10;
if(rem == 3)
c++;
}
return c;
}
4. Replace digit
#include <stdio.h>
int digit_replace(int num, int num1, int num2) {
int res=0, d=1;
int rem;
while(num) {
rem = num%10;
if(rem == num1)
res = res + num2 * d;
else
res = res + rem * d;
d *= 10;
num /= 10;
}
printf("After replacing: %d", res);
return 0;
}
int main(int argc, char const *argv[]) {
int num, num1, num2;
printf("\nEnter the number: ");
scanf("%d", &num);
printf("\nEnter the digit to be replaced: ");
scanf("%d", &num1);
printf("\nEnter the digit to be replaced with: ");
scanf("%d", &num2);
digit_replace(num, num1, num2);
return 0;
}
6. Reverse a number
#include <stdio.h>
int main()
{
int num,a;
printf("Enter the number:");
scanf("%d",&num);
while(num!=0)
{
a=num%10;
printf("%d",a);
num=num/10;
}
return 0;
}
0 Comments