Mindtree Programming Questions with Solutions
1. C Program to check if two given matrices are identical
#include <stdio.h>
#define N 4
// This function returns 1 if A[][] and B[][] are identical
// otherwise returns 0
int areSame(int A[][N], int B[][N])
{
int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
if (A[i][j] != B[i][j])
return 0;
return 1;
}
int main()
{
int A[N][N] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
int B[N][N] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
if (areSame(A, B))
printf("Matrices are identical");
else
printf("Matrices are not identical");
return 0;
}
2.Print a given matrix in spiral form
#define R 4
#define C 5
void spiralOfMatrix(int enrow, int encol, int arr1[R][C])
{
int i, rowind = 0, colind = 0;
while (rowind < enrow && colind < encol)
{
for (i = colind; i < encol; ++i)
{
printf("%d ", arr1[rowind][i]);
}
rowind++;
for (i = rowind; i < enrow; ++i)
{
printf("%d ", arr1[i][encol-1]);
}
encol--;
if ( rowind < enrow)
{
for (i = encol-1; i >= colind; --i)
{
printf("%d ", arr1[enrow-1][i]);
}
enrow--;
}
if (colind < encol)
{
for (i = enrow-1; i >= rowind; --i)
{
printf("%d ", arr1[i][colind]);
}
colind++;
}
}
}
int main()
{
int i,j;
int arr1[R][C] = { {1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20}
};
//------------- print original array ------------------
printf("The given array in matrix form is : \n");
for(i = 0; i < R; i++)
{
for (j=0;j<C;j++)
{
printf("%d ", arr1[i][j]);
}
printf("\n");
}
//------------------------------------------------------
printf("The spiral form of above matrix is: \n");
spiralOfMatrix(R, C, arr1);
return 0;
}
3.Given an n-by-n matrix of 0’s and 1’s where all 1’s in each row come before all 0’s, find the most efficient way to return the row with the maximum number of 0’s.
#include <stdio.h>
#include <stdlib.h>
#define COL 4
#define ROW 4
int main()
{
int arr[ROW][COL]= {
{1,1,1,1},
{1,1,0,0},
{1,0,0,0},
{1,1,0,0},
};
int rownum;
int i = 0, j = COL-1;
while(i<ROW && j>0)
{
if(arr[i][j]==0)
{
j--;
rownum=i;}
else
i++;
}
printf("%d",rownum);
return 0;
}
4.A Pythagorean triplet is a set of three integers a, b and c such that a2 + b2 = c2. Given a limit, generate all Pythagorean Triples with values smaller than given limit.
#include <stdio.h>
int main()
{
int LegOne, LegTwo, Hypotenuse;
Hypotenuse = 0;
while ( Hypotenuse < 50 )
{
LegTwo = 1;
while ( LegTwo < 50 )
{
LegOne = 1;
while ( LegOne < 50 )
{
if ( LegOne*LegOne + LegTwo*LegTwo == Hypotenuse*Hypotenuse && LegOne < LegTwo )
{
printf("\n\t\t %4d,%4d,%4d", LegOne,LegTwo,Hypotenuse);
}
LegOne++;
}
LegTwo++;
}
Hypotenuse++;
}
printf("\n");
}
5.C Program to find lcm of 3 numbers
#include<stdio.h>
int lcm(int,int);
int main(){
int a,b,c,l,k;
printf("Enter any three positive integers ");
scanf("%d%d%d",&a,&b,&c);
if(a<b)
l = lcm(a,b);
else
l = lcm(b,a);
if(l>c)
k= lcm(l,c);
else
k= lcm(c,l);
printf("LCM of two integers is %d",k);
return 0;
}
int lcm(int a,int b){
int temp = a;
while(1){
if(temp % b == 0 && temp % a == 0)
break;
temp++;
}
return temp;
}
6.Remove all Vowels from a String using Pointers concept?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
int check_vowel(char);
int main()
{
char string[100], *temp, *pointer, ch, *start;
printf("Enter a string\n");
gets(string);
temp = string;
pointer = (char*)malloc(100);
if( pointer == NULL )
{
printf("Unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
start = pointer;
while(*temp)
{
ch = *temp;
if ( !check_vowel(ch) )
{
*pointer = ch;
pointer++;
}
temp++;
}
*pointer = '\0';
pointer = start;
strcpy(string, pointer); /* If you wish to convert original string */
free(pointer);
printf("String after removing vowel is \"%s\"\n", string);
return 0;
}
int check_vowel(char a)
{
if ( a >= 'A' && a <= 'Z' )
a = a + 'a' - 'A';
if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return TRUE;
return FALSE;
}
7.Write a program to return a sorted array from two unsorted array?
#include<stdio.h>
int main()
{
int arr1 [10], arr2 [10], arr3 [20];
int i, n1, n2, m, index=0;
printf("\n Enter the number of elements in array 1: ");
scanf("%d", &n1);
printf("\n\n Enter the Elements of the first array");
printf("\n ****************************");
for(i=0;i<n1;i++)
{
scanf ("%d",&arr1[i]);
}
printf("\n Enter the number of elements in array 2: ");
scanf ("%d", &n2 );
printf("\n\n Enter the Elements of the second array");
printf("\n ****************************");
for(i=0;i<n2;i++)
{
scanf ("%d", &arr2[i]);
m = n1+n2;
}
for(i=0;i<n1;i++)
{
arr3[index]=arr1[i];
index++;
}
for(i=0;i<n2;i++)
{
arr3[index]=arr2[i];
index++;
}
printf ("\n\n The merged array is");
printf ("\n ******************** " );
for(i=0;i<m;i++)
{
printf("\t\n Arr[%d] = %d", i, arr3[i]);
}
return 0;
}
8.To print the trapezium pattern?
1*2*3*4*17*18*19*20
5*6*7*14*15*16
8*9*12*13
10*11
#include<stdio.h>
int main(){
int n=4,num=1,i=1,space=0,k=1,number=n;
for(i=0;i<n;i++)
{
for(int j=1;j<=space;j++)
{
printf(" -");
}
for(int m=1;m<2*n-space;m++)
{
if(m%2==0)
printf(“%s”,”*”);
else
printf(“%d”,num++);
}
printf(“%s”,”*”);
for(int l=1;l<2*n-space;l++)
{
if(l%2==0)
printf(“%s”,”*”);
else
{
printf(“%d”,k+number*number);
k++;
}
}
number--;
space=space+2;
printf(“\n”);
}
return 0;
}
9.Print the following Pattern and get the OutPut?
N=5
Output
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
#include<stdio.h>
int main()
{
int i,j,n,count=0,k=0;
printf(“Enter N”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
count=k;
for(j=1;j<=i;j++)
{
if(i%2==0)
{
printf(“%d”,count+i);
count=count-1;
if(j!=i)printf(“*”);
k++;
}
else
{
count=count+1;
printf(“%d”,count);
if(j!=i)printf(“*”);
k++;
}
}
printf(“\n”);
}
return 0;
}
10.Programming Pattern to Print 2*N Number of rows for input Pattern?
3
44
555
6666
555
44
3
#include<stdio.h>
int main()
{
int n=4,num=n-1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
printf("%d",num);
num++;
printf("\n");
}
num--;
for(int i=n;i>=1;i--)
{
for(int j=1;j<=i;j++)
printf("%d",num);
num--;
printf("\n");
}
return 0;
}
11.Print the following Pattern and get the output to match test cases?
To print the pattern like
for n=3
the program should print
1 1 1 2
3 2 2 2
3 3 3 4
#include<stdio.h>
int main()
{
int n=3,c=n-1;
for(int i=1;i<=n;i++)
{
if(i%2==0)
printf("%d",c++);
for(int j=1;j<=n;j++)
{
printf("%d",i);
}
if(i%2!=0)
printf("%d",c++);
printf("\n");
}
return 0;
}
12.Print the following pattern-
C, Java, C++ Program to print 1*2*3*10*11*12 4*5*8*9 6*7 Pattern
1*2*3*10*11*12
4*5*8*9
6*7
#include <stdio.h>
void pattern(int);
int main()
{
int n;
scanf("%d", &n);
pattern(n);
return 0;
}
void pattern(int n)
{
int i, j, k, s;
int a = 1;
int b = n*n + 1;
for (i = n; i >= 1; i--)
{
for (s = 0; s < n - i; s++)
printf(" ");
for (j = 0; j < i; j++)
printf("%d*", a++);
for (k = 0; k < i - 1; k++)
printf("%d*", b++);
printf("%d\n", b); // last b should without *
b -= 2*(i - 1);
}
}
13.C program to find HCF and LCM
#include <stdio.h>
int main() {
int a, b, x, y, t, gcd, lcm;
printf("Enter two integers\n");
scanf("%d%d", &x, &y);
a = x;
b = y;
while (b != 0) {
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x*y)/gcd;
printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
return 0;
}
13.C program to find HCF and LCM
#include <stdio.h>
int main() {
int a, b, x, y, t, gcd, lcm;
printf("Enter two integers\n");
scanf("%d%d", &x, &y);
a = x;
b = y;
while (b != 0) {
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x*y)/gcd;
printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
return 0;
}
Comments
Post a Comment