Infosys Coding Interview Questions

Infosys Coding Interview Questions 2022

Prepare for Infosys Interview with the latest Infosys Coding Questions. On this page, we have provided Infosys Coding Interview Questions for Freshers.

coding questions asked in infosys

Infosys Coding Questions 2022

Question 1: Write a Program to Swap Two Numbers.

# Swap two variables in Python
a=int(input(“Enter value : “))
b=int(input(“Enter value : “))
print(“Before swapping a :”,a)
print(“Before swapping b :”,b)
#logic to swap using third variable 
temp=a
a=b
b=temp
print(“After swapping a becomes :”,a)
print(“After swapping b becomes :”,b)

Question 2: Write a Program to Swap Two Numbers without using the Third Variable.

a=int(input(“Enter value : “))
b=int(input(“Enter value : “))

print(“Before swapping a :”,a)
print(“Before swapping b :”,b)
#logic to swap without using third variable 
a=a+b 
b=a-b
a=a-b


print(“After swapping a becomes :”,a)
print(“After swapping b becomes :”,b)

Question 3: Write a Program to Convert Decimal Number to Binary Number.

// Decimal to binary conversion using an Array in C
#include<stdio.h>

void convert(int num)
{
// creating an array to store binary equivalent
int binaryArray[32];

// using i to store binary bit at given array position
int i = 0;
while (num > 0) {

// resultant remainder is stored at given array position
binaryArray[i] = num % 2;
num = num / 2;
i++;
}

// printing binary array in reverse order
for (int j = i - 1; j >= 0; j--)
printf("%d",binaryArray[j]);
}

int main()
{
int n = 11;
convert(n);
return 0;
}

Find More Solutions at: C Program to Concert Decimal to Binary

Question 4: Write a Program to Convert Decimal Number to Octal Number.

// C Program to convert Decimal to Octal using Array
#include<stdio.h>

void convert(int num)
{
// creating an array to store octal equivalent
int octalArray[32];

// using i to store octal bit at given array position
int i = 0;
while (num > 0) {

// resultant remainder is stored at given array position
octalArray[i] = num % 8;
num = num / 8;
i++;
}

// printing octal array in reverse order
for (int j = i - 1; j >= 0; j--)
printf("%d",octalArray[j]);
}

int main()
{
int n = 148;
convert(n);
return 0;
}

Find More Solutions at: C Program for Decimal to Octal Conversion

Question 5: Write a Program to Convert Decimal Number to Hexadecimal Number.

#include <stdio.h>

void convert (int num)
{
// creating a char array to store hexadecimal equivalent
char hexa[100];

// using i to store hexadecimal bit at given array position
int i = 0;
while (num != 0)
{
int rem = 0;


rem = num % 16;

// check if rem < 10 : Digits : 0 - 9
// ascii 0 : 48
if (rem < 10) { hexa[i] = rem + 48; i++; } 
// else positional values : A - F 
// rem value will be > 10, adding 55 will result : A - F 
// ascii A : 65, B : 66 ..... F : 70 
else
{
hexa[i] = rem + 55;
i++;
}
num = num / 16;
}
// printing hexadecimal array in reverse order
printf ("Hexadecimal:");
for (int j = i- 1; j >= 0; j--)
printf ("%c" , hexa[j]);
}

int main ()
{
int decimal = 1457;
convert (decimal);
return 0;
}

Find More Solutions at: C Program for Decimal to Hexadecimal Conversion

Question 6: Write a Program to Convert Octal Number to Binary Number.

#include<stdio.h>
#include<math.h>

void convert(int octal)
{
int i = 0, decimal = 0;

//converting octal to decimal
while (octal!=0)
{
int digit = octal % 10;
decimal += digit * pow(8, i);

octal /= 10;
i++;
}

printf("Decimal Value: %d\n",decimal);

long long binary = 0;
int rem;
i = 1;

// converting decimal to binary here
while(decimal!=0)
{
rem = decimal % 2;
decimal /= 2;
binary += rem * i;

// moving to next position ex: units -> tens
i *= 10;
}

printf("Binary Value: %d",binary);
}

int main()
{
int octal;

printf("Octal Value: "); 
scanf("%d", &octal);

convert(octal);

return 0;
}

Find More Solutions at: C Program for Octal to Binary Conversion

Question 7: Write a Program to Convert Octal Number to Decimal Number.

#include<stdio.h>
#include<math.h>

// function to convert octal to decimal
int convert(long long num)
{
int i = 0, decimal = 0;

// will only work for bases 2 - 10
int base = 8;

//converting octal to decimal
while (num!=0)
{
int digit = num % 10;
decimal += digit * pow(base, i);

num /= 10;
i++;
}
return decimal;
}

//main program
int main()
{
// long used rather than int to store large values
long long octal;

printf("Enter Octal Number: ");
scanf("%lld", &octal);

printf("Decimal: %lld", convert(octal));

return 0;
}

Find More Solutions at C Program for Octal to Decimal Conversion

Question 8: Write a Program to find out the Spiral Traversal of a Matrix.

#include <stdio.h>
#define r 4
#define c 4

int main()
{ 
int a[4][4] = { { 1, 2, 3, 4 },

{ 5, 6, 7, 8 },

{ 9, 10, 11, 12 },

{ 13, 14, 15, 16 } };

int i, left = 0, right = c-1, top = 0, bottom = r-1;

while (left <= right && top <= bottom) {

/* Print the first row
from the remaining rows */
for (i = left; i <= right; ++i) {
printf("%d ", a[top][i]);
}
top++;

/* Print the last column
from the remaining columns */
for (i = top; i <= bottom; ++i) {
printf("%d ", a[i][right]);
}
right--;

/* Print the last row from
the remaining rows */
if (top <= bottom) { for (i = right; i >= left; --i) {
printf("%d ", a[bottom][i]);
}
bottom--;
}

/* Print the first column from
the remaining columns */
if (left <= right) { for (i = bottom; i >= top; --i) {
printf("%d ", a[i][left]);
}
left++;
}
}

return 0;
}

Find More Solutions at C Program to Find the Spiral Traversal of a Matrix

Question 9: Write a Program to Rotate a Matrix by 90 degrees.

#include <stdio.h>

void swap(int *x, int *y){
int temp = *x;
*x = *y;
*y= temp;
}
int main(){

int n=4;
int mat[4][4]= { { 1, 2, 3, 4 },{ 5, 6, 7, 8 },{ 9, 10, 11, 12 },{ 13, 14, 15, 16 } };

//Tranposing the matrix
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++)
swap(&mat[i][j], &mat[j][i]);
}

//Reversing each row of the matrix
for(int i=0; i<n; i++){
for(int j=0; j<n/2; j++){
swap(&mat[i][j], &mat[i][n-j-1]);
}
}

//Print the matrix
printf("Rotated Matrix :\n");
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
printf("%d ",mat[i][j]);
}
printf("\n");
}
}

Find More Solutions at C Program to rotate a matrix by 90 degrees.

Question 10: Write a Program to Find a Specific Pair in a Matrix

#include <stdio.h>
#include <limits.h>
#define N 5

int findMaxValue(int mat[][N])
{
int maxValue = INT_MIN;

for (int a = 0; a < N - 1; a++)
for (int b = 0; b < N - 1; b++)
for (int d = a + 1; d < N; d++)
for (int e = b + 1; e < N; e++)
if (maxValue < (mat[d][e] - mat[a][b]))
maxValue = mat[d][e] - mat[a][b];

return maxValue;
}

int main()
{
int mat[N][N] = {
{ 1, 2, -1, -4, -20 },
{ -8, -3, 4, 2, 1 },
{ 3, 8, 6, 1, 3 },
{ -4, -1, 1, 7, -6 },
{ 0, -4, 10, -5, 1 }
};
printf("Maximum Value is %d", findMaxValue(mat));

return 0;
}

Find More Solutions at C Program to Find a Specific Pair in a Matrix

More Infosys Coding Questions:-

  • Write a program for swapping of two arrays.
  • Write a program for swapping of two strings.
  • Write a program to convert the string from upper case to lower case.
  • Write a program to convert the string from lower case to upper case.
  • Write a program to delete all consonants from a given string.
  • Write a program to count the different types of characters in given string.
  • Write a program to sort the characters of a string.  
  • Write a program for addition of two matrices.
  • Write a program for subtraction  of two matrices.
  • Write a program for multiplication of two matrices.
  • Write a program to find out the sum of diagonal element of a matrix.

3 comments on “Infosys Coding Interview Questions”