Wipro Coding questions – set 1
1) Find the distinct elements in a given array. (Assume size of an array n<=20)
Sample Input:
- 9 = size of an array
- 2 3 4 5 6 1 2 3 4 = array elements
Sample Output:
- 2 3 4 5 6 1
Program:
// C program to print all distinct elements in a given array
#include
void distict_elements(int a[], int n);
int main()
{
int size_array, i, arr[20];
// Get the array size
scanf(“%d”, &size_array)
// Get the array elements
for(i=0; i<size_array; i++)
{
scanf(“%d”, &arr[i]);
}
// C program to print all distinct elements in a given array
#include
void distict_elements(int a[], int n);
int main()
{
int size_array, i, arr[20];
// Get the array size
scanf(“%d”, &size_array)
// Get the array elements
for(i=0; i<size_array; i++)
{
scanf(“%d”, &arr[i]);
}
// Function call to print the distinct elements in an array
distict_elements(arr, size_array);
return 0;
}
void distict_elements(int a[], int n)
{
int i, j;
// Pick all elements one by one
for (i=0; i<n; i++)
{
// Check if the picked element is already printed
for (j=0; j<i; j++)
{
if (a[i] == a[j])
break;
}
distict_elements(arr, size_array);
return 0;
}
void distict_elements(int a[], int n)
{
int i, j;
// Pick all elements one by one
for (i=0; i<n; i++)
{
// Check if the picked element is already printed
for (j=0; j<i; j++)
{
if (a[i] == a[j])
break;
}
// If not printed earlier, then print it
if (i == j)
printf(“%d “, a[i]);
}
}
if (i == j)
printf(“%d “, a[i]);
}
}
2) Program to sort array in ascending & descending order.
Input:
5
8 6 9 2 7
Output:
2 6 7 8 9
9 8 7 6 2
5
8 6 9 2 7
Output:
2 6 7 8 9
9 8 7 6 2
Program:
// C program to sort the given array elements in ascending and descending order
#include
int main(void)
{
int arr[10], i=0, j=0, size, temp;
// Get the size of an array
scanf (“%d”, &size);
// Get the array elements as an input
for (i = 0; i <size; i++)
{
scanf (“%d”, &arr[i]);
}
// Sorting elements in ascending order
for (j=0 ; j<(size-1) ; j++)
{
for (i=0 ; i<(size-1) ; i++)
{
if (arr[i+1] < arr[i])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
// Print the elements from index value 0 to (size-1) –> ascending order
for (i=0 ; i
// C program to sort the given array elements in ascending and descending order
#include
int main(void)
{
int arr[10], i=0, j=0, size, temp;
// Get the size of an array
scanf (“%d”, &size);
// Get the array elements as an input
for (i = 0; i <size; i++)
{
scanf (“%d”, &arr[i]);
}
// Sorting elements in ascending order
for (j=0 ; j<(size-1) ; j++)
{
for (i=0 ; i<(size-1) ; i++)
{
if (arr[i+1] < arr[i])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
// Print the elements from index value 0 to (size-1) –> ascending order
for (i=0 ; i
{ printf (“%d “, arr[i]);
}
printf(“\n”);
}
printf(“\n”);
// Print the elements from the index value (size-1) to 0 –> descending order
for (i=size-1; i>=0 ; i–)
{
printf (“%d “, arr[i]);
}
return 0;
}
for (i=size-1; i>=0 ; i–)
{
printf (“%d “, arr[i]);
}
return 0;
}
3) Sort first half of an array in ascending and second half in descending order.
Example 1:
Input:
8
2 4 7 9 3 1 6 8
Output:
1 2 3 4 9 8 7 6
Example 2:
Input:
6
1 2 3 4 5 6
Output:
1 2 3 6 5 4
Input:
8
2 4 7 9 3 1 6 8
Output:
1 2 3 4 9 8 7 6
Example 2:
Input:
6
1 2 3 4 5 6
Output:
1 2 3 6 5 4
Algorithm:
i) Sort the given array.
ii) Run a loop up to half the length of the array and print the elements of the sorted array.
iii) Run a loop from the last index of the array to the middle of the array and print the elements in reverse order.
ii) Run a loop up to half the length of the array and print the elements of the sorted array.
iii) Run a loop from the last index of the array to the middle of the array and print the elements in reverse order.
Program:
#include
void sorting_elements(int arr[], int n);
void display(int arr[], int n);
int main()
{
int size, arr[20], i;
scanf(“%d”, &size);
for(i=0; i<size; i++)
{
scanf(“%d”, &arr[i]);
}
display(arr, size);
return 0;
}
// Sort the elements in the ascending order
void sorting_elements(int arr[], int n)
{
int i,j,temp;
for (j=0 ; j<(n-1) ; j++)
{
for (i=0 ; i<(n-1) ; i++)
{
if (arr[i+1] < arr[i])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
}
// Display the sorted elements
void display(int arr[], int n)
{
sorting_elements(arr, n);
int i, j
#include
void sorting_elements(int arr[], int n);
void display(int arr[], int n);
int main()
{
int size, arr[20], i;
scanf(“%d”, &size);
for(i=0; i<size; i++)
{
scanf(“%d”, &arr[i]);
}
display(arr, size);
return 0;
}
// Sort the elements in the ascending order
void sorting_elements(int arr[], int n)
{
int i,j,temp;
for (j=0 ; j<(n-1) ; j++)
{
for (i=0 ; i<(n-1) ; i++)
{
if (arr[i+1] < arr[i])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
}
// Display the sorted elements
void display(int arr[], int n)
{
sorting_elements(arr, n);
int i, j
// Print the first half as such (i.e. from index 0 to midlle)
for (i=0; i<n/2; i++)
{
printf(“%d “, arr[i]);
}
for (i=0; i<n/2; i++)
{
printf(“%d “, arr[i]);
}
// Print the second half in the reverse order (i.e. from n-1 to midlle)
for (j=n-1; j>=n/2; j–)
{
printf(“%d “, arr[j]);
}
}
for (j=n-1; j>=n/2; j–)
{
printf(“%d “, arr[j]);
}
}
4) Print the below pattern
Input:
3 4
Output:
3
44
555
6666
555
44
3
3 4
Output:
3
44
555
6666
555
44
3
Input :
4 4
Output:
4
55
666
7777
666
55
4
4 4
Output:
4
55
666
7777
666
55
4
Program:
#include
int main()
{
int i,j,s,N,count=0;
scanf(“%d%d”,&s,&N);
for(i=s;count<4;count++)
{
for(j=0;j<count+1;j++)
printf(“%d”,i);
printf(“\n”);
i=i+1;
}
for(i=s+N-2;count>0;count–)
{
for(j=0;j<count-1;j++)
printf(“%d”,i);
printf(“\n”);
i=i-1;
}
return 0;
}
#include
int main()
{
int i,j,s,N,count=0;
scanf(“%d%d”,&s,&N);
for(i=s;count<4;count++)
{
for(j=0;j<count+1;j++)
printf(“%d”,i);
printf(“\n”);
i=i+1;
}
for(i=s+N-2;count>0;count–)
{
for(j=0;j<count-1;j++)
printf(“%d”,i);
printf(“\n”);
i=i-1;
}
return 0;
}
5) Print the following pattern
Input :
3
Output:
1
2*2
3*3*3
3*3*3
2*2
1
3
Output:
1
2*2
3*3*3
3*3*3
2*2
1
Input :
4
Output:
1
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
1
4
Output:
1
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
1
Program:
#include
int main()
{
int i,j,k,N,count=0;
scanf(“%d”,&N);
for(i=1;i<=N;i++)
{
k=1;
for(j=0;j<i;j++)
{
printf(“%d”,i);
if(k<i)
{
printf(“*”);
k=k+1;
}
}
printf(“\n”);
}
for(i=N;i>0;i–)
{
k=1;
for(j=0;j<i;j++)
{
printf(“%d”,i);
if(k<i)
{
printf(“*”);
k=k+1;
}
}
printf(“\n”);
}
return 0;
}
#include
int main()
{
int i,j,k,N,count=0;
scanf(“%d”,&N);
for(i=1;i<=N;i++)
{
k=1;
for(j=0;j<i;j++)
{
printf(“%d”,i);
if(k<i)
{
printf(“*”);
k=k+1;
}
}
printf(“\n”);
}
for(i=N;i>0;i–)
{
k=1;
for(j=0;j<i;j++)
{
printf(“%d”,i);
if(k<i)
{
printf(“*”);
k=k+1;
}
}
printf(“\n”);
}
return 0;
}
6) Print the below pattern
Input:
4
Output:
1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1
4
Output:
1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1
Program:
#include
int main() {
int i,j,count=1,n;
printf(“Enter a number\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(j<i)
printf(“%d*”,count++);
else
printf(“%d”,count++);
} printf(“\n”);
}
count=count-n;
for(i=n;i>=1;i–)
{ for(j=1;j<=i;j++)
{
if(j<i)
printf(“%d*”,count++);
else
printf(“%d”,count++);
}
count=(count+1)-2*i;
printf(“\n”);
}
return 0;
}
#include
int main() {
int i,j,count=1,n;
printf(“Enter a number\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(j<i)
printf(“%d*”,count++);
else
printf(“%d”,count++);
} printf(“\n”);
}
count=count-n;
for(i=n;i>=1;i–)
{ for(j=1;j<=i;j++)
{
if(j<i)
printf(“%d*”,count++);
else
printf(“%d”,count++);
}
count=(count+1)-2*i;
printf(“\n”);
}
return 0;
}
Wipro Coding questions – set 2
7) Print the following pattern
Input:
3 4
Output:
3
44
555
6666
6666
555
44
3
3 4
Output:
3
44
555
6666
6666
555
44
3
Program:
#include
int main()
{
int i,j,s,N,count=0;
scanf(“%d%d”,&s,&N);
for(i=s;count<4;count++)
{
for(j=0;j<count+1;j++)
printf(“%d”,i);
printf(“\n”);
i=i+1;
}
for(i=s+N-2;count>0;count–)
{
for(j=0;j<count-1;j++)
printf(“%d”,i);
printf(“\n”);
i=i-1;
}
return 0;
}
#include
int main()
{
int i,j,s,N,count=0;
scanf(“%d%d”,&s,&N);
for(i=s;count<4;count++)
{
for(j=0;j<count+1;j++)
printf(“%d”,i);
printf(“\n”);
i=i+1;
}
for(i=s+N-2;count>0;count–)
{
for(j=0;j<count-1;j++)
printf(“%d”,i);
printf(“\n”);
i=i-1;
}
return 0;
}
8) Print the below pattern
Input:
5
Output:
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
5
Output:
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
Program:
#include
int main()
{
int i,j,k,l=1,N,d,r,count=0;
scanf(“%d”,&N);
for(i=1;i<=N;i++)
{
k=1;
d=i%2;
r=l+i-1;
for(j=0;j<i;j++)
{
#include
int main()
{
int i,j,k,l=1,N,d,r,count=0;
scanf(“%d”,&N);
for(i=1;i<=N;i++)
{
k=1;
d=i%2;
r=l+i-1;
for(j=0;j<i;j++)
{
if(d==0)
{
printf(“%d”,r);
r–;
if(k<i)
{
printf(“*”);
k=k+1;
}
l++;
continue;
}
printf(“%d”,l);
l++;
if(k<i)
{
printf(“*”);
k=k+1;
}
}
printf(“\n”);
}
return 0;
}
{
printf(“%d”,r);
r–;
if(k<i)
{
printf(“*”);
k=k+1;
}
l++;
continue;
}
printf(“%d”,l);
l++;
if(k<i)
{
printf(“*”);
k=k+1;
}
}
printf(“\n”);
}
return 0;
}
9) Print the below pattern
Input:
4
Output:
1*2*3*4*17*18*19*20
– -5*6*7*14*15*16
– – – -8*9*12*13
– – – – – -10*11
4
Output:
1*2*3*4*17*18*19*20
– -5*6*7*14*15*16
– – – -8*9*12*13
– – – – – -10*11
Program:
#include
void pattern(int);
int main()
{
int n;
scanf(“%d”, &n);
pattern(n);
return 0;
}
void pattern(int n)
{
int i, j, k, s, a = 1,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);
}
}
#include
void pattern(int);
int main()
{
int n;
scanf(“%d”, &n);
pattern(n);
return 0;
}
void pattern(int n)
{
int i, j, k, s, a = 1,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);
}
}
10) Print the below pattern
Input:
3
Output:
3 3 3
3 1 3
3 2 3
3 3 3
3
Output:
3 3 3
3 1 3
3 2 3
3 3 3
Program:
#include
int main()
{
int i, j, n, c=1;
scanf(“%d”, &n);
for(i=1; i<=n+1; i++)
{
for(j=1; j<=n; j++)
{
if(i!=1 && j==n-1)
{
printf(“%d “, c);
c++;
}
else
printf(“%d “, n);
}
printf(“\n”);
}
return 0;
}
#include
int main()
{
int i, j, n, c=1;
scanf(“%d”, &n);
for(i=1; i<=n+1; i++)
{
for(j=1; j<=n; j++)
{
if(i!=1 && j==n-1)
{
printf(“%d “, c);
c++;
}
else
printf(“%d “, n);
}
printf(“\n”);
}
return 0;
}
11) Paranthesis checker: Check whether the given expression is valid or not(only parenthesis symbol).
Test Case: 1
Input: “(( ))”
Output: Valid
Test Case: 2
Input: “()(“
Output: Invalid
Input: “(( ))”
Output: Valid
Test Case: 2
Input: “()(“
Output: Invalid
Program:
#include
#include
#include
int top = -1; char stack[100];
void push(char);
void pop();
void find_top();
void main()
{
int i;
char a[100];
scanf(“%s”, &a);
for (i = 0; a[i] != ‘\0’; i++)
{
if (a[i] == ‘(‘)
push(a[i]);
else if (a[i] == ‘)’)
pop();
}
find_top();
}
// to push elements in stack
void push(char a)
{
top++;
stack[top] = a;
}
// to pop elements from stack
void pop()
{
if (top == -1)
{
printf(“Invalid”);
exit(0);
}
else
top–;
}
// to find top element of stack
#include
#include
#include
int top = -1; char stack[100];
void push(char);
void pop();
void find_top();
void main()
{
int i;
char a[100];
scanf(“%s”, &a);
for (i = 0; a[i] != ‘\0’; i++)
{
if (a[i] == ‘(‘)
push(a[i]);
else if (a[i] == ‘)’)
pop();
}
find_top();
}
// to push elements in stack
void push(char a)
{
top++;
stack[top] = a;
}
// to pop elements from stack
void pop()
{
if (top == -1)
{
printf(“Invalid”);
exit(0);
}
else
top–;
}
// to find top element of stack
void find_top()
{
if (top == -1)
printf(“Valid”);
else
printf(“Invalid”);
}
{
if (top == -1)
printf(“Valid”);
else
printf(“Invalid”);
}
12) Print the transpose of a Matrix:
#include
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf(“Enter rows and columns of matrix: “);
scanf(“%d %d”, &r, &c);
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf(“Enter rows and columns of matrix: “);
scanf(“%d %d”, &r, &c);
// Storing elements of the matrix
printf(“\nEnter elements of matrix:\n”);
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf(“Enter element a%d%d: “,i+1, j+1);
scanf(“%d”, &a[i][j]);
}
// Displaying the matrix a[][] */
printf(“\nEntered Matrix: \n”);
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf(“%d “, a[i][j]);
if (j == c-1)
printf(“\n\n”);
}
// Finding the transpose of matrix a
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}
// Displaying the transpose of matrix a
printf(“\nTranspose of Matrix:\n”);
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf(“%d “,transpose[i][j]);
if(j==r-1)
printf(“\n\n”);
}
return 0;
}
printf(“\nEnter elements of matrix:\n”);
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf(“Enter element a%d%d: “,i+1, j+1);
scanf(“%d”, &a[i][j]);
}
// Displaying the matrix a[][] */
printf(“\nEntered Matrix: \n”);
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf(“%d “, a[i][j]);
if (j == c-1)
printf(“\n\n”);
}
// Finding the transpose of matrix a
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}
// Displaying the transpose of matrix a
printf(“\nTranspose of Matrix:\n”);
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf(“%d “,transpose[i][j]);
if(j==r-1)
printf(“\n\n”);
}
return 0;
}
13) Matrix Addition:
Program:
#include
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf(“Enter number of rows (between 1 and 100): “);
scanf(“%d”, &r);
printf(“Enter number of columns (between 1 and 100): “);
scanf(“%d”, &c);
printf(“\nEnter elements of 1st matrix:\n”);
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf(“Enter element a%d%d: “,i+1,j+1);
scanf(“%d”,&a[i][j]);
}
printf(“Enter elements of 2nd matrix:\n”);
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf(“Enter element a%d%d: “,i+1, j+1);
scanf(“%d”, &b[i][j]);
}
// Adding Two matrices
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}
// Displaying the result
printf(“\nSum of two matrix is: \n\n”);
for(i=0;i<r;++i)
{ for(j=0;j<c;++j)
{
printf(“%d “,sum[i][j]);
if(j==c-1)
{
printf(“\n\n”);
}
}
return 0;
}
#include
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf(“Enter number of rows (between 1 and 100): “);
scanf(“%d”, &r);
printf(“Enter number of columns (between 1 and 100): “);
scanf(“%d”, &c);
printf(“\nEnter elements of 1st matrix:\n”);
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf(“Enter element a%d%d: “,i+1,j+1);
scanf(“%d”,&a[i][j]);
}
printf(“Enter elements of 2nd matrix:\n”);
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf(“Enter element a%d%d: “,i+1, j+1);
scanf(“%d”, &b[i][j]);
}
// Adding Two matrices
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}
// Displaying the result
printf(“\nSum of two matrix is: \n\n”);
for(i=0;i<r;++i)
{ for(j=0;j<c;++j)
{
printf(“%d “,sum[i][j]);
if(j==c-1)
{
printf(“\n\n”);
}
}
return 0;
}
- Get link
- X
- Other Apps
Labels:
Wipro Coding questions – set 1Wipro coding questions | Wipro Programming questions (Automata) -
- Get link
- X
- Other Apps
I have found that this site is very informative, interesting and very well written. keep up the nice high quality writing Planning and Administering Microsoft Azure for SAP Workloads AZ-120
ReplyDelete