MIND TREE
1.
1 1 1 1 2
3 2 2 2 2
3 3 3 3 4
5 4 4 4 4
5 5 5 5 6
7 6 6 6 6
7 7 7 7 8
9 8 8 8 8
9 9 9 9 10
#include <stdio.h>
int main()
{
int i,j;
int n = 9;
for(i=1;i<=n;i++)
{
for(j=1;j<=5;j++)
{
if(i%2==0)
{
if(j==1)
{
printf(" %d",i+1);
}
else
printf(" %d",i);
}
else
{
if(j==5)
{
printf(" %d",i+1);
break;
}
else
{
printf(" %d",i);
}
}
}
printf("\n");
}
return 0;
}
2.
input :
abc cba
1
ab aa
-1
#include<stdio.h>
#include<string.h>
int test(char*str1,char *str2)
{
int ctr1=0,ctr2=0,i,j;
for(j=0;str1[j]!=0;j++)
{
for(i=0;i<strlen(str1);i++)
if(str1[i]==str1[0])
ctr1++;
for(i=0;i<strlen(str2);i++)
if(str2[i]==str1[0])
ctr2++;
if(ctr1==ctr2)
{
j++;
continue;
}
else
return -1;
}
return 1;
}
int main() {
char*s1="ab";
char*s2="aa";
printf("%d",test(s1,s2));
}
input :
abc cba
1
ab aa
-1
3.Sub String occurance in another string with out case sensitive Mind Tree
public class GFG {
static int stringOccuranceCount(String pat, String txt) {
int M = pat.length();
int N = txt.length();
int res = 0;
String pat1=pat.toUpperCase();
String txt1=txt.toUpperCase();
/* A loop to slide pat[] one by one */
for (int i = 0; i <= N - M; i++) {
/* For current index i, check for
pattern match */
int j;
for (j = 0; j < M; j++) {
if (txt1.charAt(i + j) != pat1.charAt(j)) {
break;
}
}
// if pat[0...M-1] = txt[i, i+1, ...i+M-1]
if (j == M) {
res++;
j = 0;
}
}
return res;
}
4. input : 3 2 0 1
output: 2 3 1 0
#include<stdio.h>
int main()
{
int n;
int a[20], res[20];
printf("Enter n: ");
scanf("%d", &n);
printf("Enter array elements: ");
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for(int i = 0; i < n; i++)
{
res[a[i]] = i;
}
for(int i = 0; i < n; i++)
{
printf("%d ", res[i]);
}
return 0;
}
5.* C Program - Delete Vowels from String */
#include<stdio.h>
int findAlt(int arr[],int len)
{
int i,j,tmp;
/*
for(i=1,j=0;i<len;i+=2,j++)
arr[j]=ar[i];
len=j-1;
*/
for(i=0;i<len-1;i++)
{
for(j=i+1;j<len;j++)
{
if(*(arr+i)>*(arr+j))
{
tmp=*(arr+i);
*(arr+i)=*(arr+j);
*(arr+j)=tmp;
}
}
}
}
int main()
{
int no[]={4,2,5,3,7},i;
findAlt(no,5);
for(i=0;i<5;i+=2)
printf("%d",no[i]);
}
7. Print the below pattern
Output:
1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1
1 1 1 1 2
3 2 2 2 2
3 3 3 3 4
5 4 4 4 4
5 5 5 5 6
7 6 6 6 6
7 7 7 7 8
9 8 8 8 8
9 9 9 9 10
#include <stdio.h>
int main()
{
int i,j;
int n = 9;
for(i=1;i<=n;i++)
{
for(j=1;j<=5;j++)
{
if(i%2==0)
{
if(j==1)
{
printf(" %d",i+1);
}
else
printf(" %d",i);
}
else
{
if(j==5)
{
printf(" %d",i+1);
break;
}
else
{
printf(" %d",i);
}
}
}
printf("\n");
}
return 0;
}
2.
input :
abc cba
1
ab aa
-1
#include<stdio.h>
#include<string.h>
int test(char*str1,char *str2)
{
int ctr1=0,ctr2=0,i,j;
for(j=0;str1[j]!=0;j++)
{
for(i=0;i<strlen(str1);i++)
if(str1[i]==str1[0])
ctr1++;
for(i=0;i<strlen(str2);i++)
if(str2[i]==str1[0])
ctr2++;
if(ctr1==ctr2)
{
j++;
continue;
}
else
return -1;
}
return 1;
}
int main() {
char*s1="ab";
char*s2="aa";
printf("%d",test(s1,s2));
}
input :
abc cba
1
ab aa
-1
3.Sub String occurance in another string with out case sensitive Mind Tree
public class GFG {
static int stringOccuranceCount(String pat, String txt) {
int M = pat.length();
int N = txt.length();
int res = 0;
String pat1=pat.toUpperCase();
String txt1=txt.toUpperCase();
/* A loop to slide pat[] one by one */
for (int i = 0; i <= N - M; i++) {
/* For current index i, check for
pattern match */
int j;
for (j = 0; j < M; j++) {
if (txt1.charAt(i + j) != pat1.charAt(j)) {
break;
}
}
// if pat[0...M-1] = txt[i, i+1, ...i+M-1]
if (j == M) {
res++;
j = 0;
}
}
return res;
}
4. input : 3 2 0 1
output: 2 3 1 0
#include<stdio.h>
int main()
{
int n;
int a[20], res[20];
printf("Enter n: ");
scanf("%d", &n);
printf("Enter array elements: ");
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for(int i = 0; i < n; i++)
{
res[a[i]] = i;
}
for(int i = 0; i < n; i++)
{
printf("%d ", res[i]);
}
return 0;
}
5.* C Program - Delete Vowels from String */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str[20];
int len, i, j;
printf("Enter a string : ");
gets(str);
len=strlen(str);
for(i=0; i<len; i++)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
str[i]=='o' || str[i]=='u' || str[i]=='A' ||
str[i]=='E' || str[i]=='I' || str[i]=='O' ||
str[i]=='U')
{
for(j=i; j<len; j++)
{
str[j]=str[j+1];
}
len--;
}
}
printf("After deleting the vowels, the string will be : %s",str);
getch();
}
5.o/p:
1
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
1
#include<stdio.h>
int main() {
int i,j,n=4;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
if(j!=i)
printf("*");
}
printf("\n");
}
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
if(j!=i)
printf("*");
}
printf("\n");
}
}
6. write an algorithm to figure out the max no of calls required special function to obtain an array ascending order i/p teat case 1) 5 1 3 11 size 4 o/p 23 test case2 ) 5,1,2,3,8,0,12 7 o/p 5039 #include<stdio.h> int fact(int n) { int f=1,i; for(i=1;i<=n;i++) { f=f*i; } return f; } int maxOperations(int*list,int len) { return fact(len)-1; } int main() { int no[]={5,1,2,3,8,0,12}; printf("%d\n\n",maxOperations(no,7)); }
7.sort elements and display alternative
int findAlt(int arr[],int len)
{
int i,j,tmp;
/*
for(i=1,j=0;i<len;i+=2,j++)
arr[j]=ar[i];
len=j-1;
*/
for(i=0;i<len-1;i++)
{
for(j=i+1;j<len;j++)
{
if(*(arr+i)>*(arr+j))
{
tmp=*(arr+i);
*(arr+i)=*(arr+j);
*(arr+j)=tmp;
}
}
}
}
int main()
{
int no[]={4,2,5,3,7},i;
findAlt(no,5);
for(i=0;i<5;i+=2)
printf("%d",no[i]);
}
7. Print the below pattern
Output:
1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1
import java.io.*;
public class Pattern
{
public static void main(String args[]) {
int i,j,k=1,n=4;
for (i=1;i<=n;i++) {
for (j=1;j<=i;j++) {
if(j<i)
System.out.print(k++ +"*");
else
System.out.print(k++);
}
System.out.println();
}
k=k-1;
for (i=n;i>=1;i--) {
for (j=1;j<=i;j++) {
if(j<i)
System.out.print(k-- +"*");
else
System.out.print(k--);
}
System.out.println();
}
}
}
8. find distict elements in an array
i/p
90,91,1,2,90,3,4,3,91
o/p
90 91 1 2 3 4
#include<stdio.h>
int* distinctElementArray(int *arr, int len)
{
// Pick all elements one by one
for (int i=0; i<len; i++)
{
// Check if the picked element is already printed
int j;
for (j=0; j<i; j++)
if (arr[i] == arr[j])
break;
// If not printed earlier, then print it
if (i == j)
printf("%d ",arr[i]);
}
return &arr[0];
}
int main()
{
int arr[] = {90,91,1,2,90,3,4,3,91};
int n = sizeof(arr)/sizeof(arr[0]);
distinctElementArray(arr, n);
return 0;
}
i/p
90,91,1,2,90,3,4,3,91
o/p
90 91 1 2 3 4
#include<stdio.h>
int* distinctElementArray(int *arr, int len)
{
// Pick all elements one by one
for (int i=0; i<len; i++)
{
// Check if the picked element is already printed
int j;
for (j=0; j<i; j++)
if (arr[i] == arr[j])
break;
// If not printed earlier, then print it
if (i == j)
printf("%d ",arr[i]);
}
return &arr[0];
}
int main()
{
int arr[] = {90,91,1,2,90,3,4,3,91};
int n = sizeof(arr)/sizeof(arr[0]);
distinctElementArray(arr, n);
return 0;
}
find distict elements in an array
#include<stdio.h>
int* distinctElementArray(int *arr, int len)
{
int i,j,k=0;
int *ar;
ar=(int*)malloc(sizeof(int)*len);
for (i=0; i<len; i++)
{
for (j=0; j<i; j++)
if (arr[i] == arr[j])
break;
if (i == j )
ar[k++]=arr[i];
}
return &ar[0];
}
int main()
{
int *ret,i;
int arr[] = {90,91,1,2,90,3,4,9};
int n = sizeof(arr)/sizeof(arr[0]);
clrscr();
ret=distinctElementArray(arr, n);
for(i=0;i<5;i++)
printf("%d ",ret[i]);
return 0;
}
i/p
90,91,1,2,90,3,4,3,91
o/p
90 91 1 2 3 4
----------------------------------------------------------------------------------------------------------------------------------
pattern
o/p
1*2*3*4
9*10*11*12
13*14*15*16
5*6*7*8
#include<stdio.h>
void print(int n)
{
int m = 1;
int i,j;
for( i=1; i<=n-1; i++)
{
if(i!=2)
{
for(j=1; j<=n; j++){
if(j!=n)
printf("%d*", m);
else
printf("%d", m);
m++;
}
printf( "\n");
}else{
m = m+n;
for(j=1; j<=n; j++){
if(j!=n)
printf("%d*", m);
else
printf("%d", m);
m++;
}
printf("\n");
}
}
for(i=n+1 ;i<=(n+n);i++){
if(i!=n+n)
printf("%d*", i);
else
printf("%d", i);
}
printf("\n");
}
int main() {
print(4);
}
--------------------------------------------------------------------------
sort elements and display alternative
#include<stdio.h>
int findAlt(int arr[],int len)
{
int i,j,tmp;
/*
for(i=1,j=0;i<len;i+=2,j++)
arr[j]=ar[i];
len=j-1;
*/
for(i=0;i<len-1;i++)
{
for(j=i+1;j<len;j++)
{
if(*(arr+i)>*(arr+j))
{
tmp=*(arr+i);
*(arr+i)=*(arr+j);
*(arr+j)=tmp;
}
}
}
}
int main()
{
int no[]={4,2,5,3,7},i;
findAlt(no,5);
for(i=0;i<5;i+=2)
printf("%d",no[i]);
}
--------------------------------------------------------------
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;
}
-----------------------------------------------------------------------------------------------------------
#include<stdio.h>
int* distinctElementArray(int *arr, int len)
{
int i,j,k=0;
int *ar;
ar=(int*)malloc(sizeof(int)*len);
for (i=0; i<len; i++)
{
for (j=0; j<i; j++)
if (arr[i] == arr[j])
break;
if (i == j )
ar[k++]=arr[i];
}
return &ar[0];
}
int main()
{
int *ret,i;
int arr[] = {90,91,1,2,90,3,4,9};
int n = sizeof(arr)/sizeof(arr[0]);
clrscr();
ret=distinctElementArray(arr, n);
for(i=0;i<5;i++)
printf("%d ",ret[i]);
return 0;
}
i/p
90,91,1,2,90,3,4,3,91
o/p
90 91 1 2 3 4
----------------------------------------------------------------------------------------------------------------------------------
pattern
o/p
1*2*3*4
9*10*11*12
13*14*15*16
5*6*7*8
#include<stdio.h>
void print(int n)
{
int m = 1;
int i,j;
for( i=1; i<=n-1; i++)
{
if(i!=2)
{
for(j=1; j<=n; j++){
if(j!=n)
printf("%d*", m);
else
printf("%d", m);
m++;
}
printf( "\n");
}else{
m = m+n;
for(j=1; j<=n; j++){
if(j!=n)
printf("%d*", m);
else
printf("%d", m);
m++;
}
printf("\n");
}
}
for(i=n+1 ;i<=(n+n);i++){
if(i!=n+n)
printf("%d*", i);
else
printf("%d", i);
}
printf("\n");
}
int main() {
print(4);
}
--------------------------------------------------------------------------
sort elements and display alternative
#include<stdio.h>
int findAlt(int arr[],int len)
{
int i,j,tmp;
/*
for(i=1,j=0;i<len;i+=2,j++)
arr[j]=ar[i];
len=j-1;
*/
for(i=0;i<len-1;i++)
{
for(j=i+1;j<len;j++)
{
if(*(arr+i)>*(arr+j))
{
tmp=*(arr+i);
*(arr+i)=*(arr+j);
*(arr+j)=tmp;
}
}
}
}
int main()
{
int no[]={4,2,5,3,7},i;
findAlt(no,5);
for(i=0;i<5;i+=2)
printf("%d",no[i]);
}
--------------------------------------------------------------
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;
}
-----------------------------------------------------------------------------------------------------------
Comments
Post a Comment