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
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;
}
-----------------------------------------------------------------------------------------------------------
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;
}
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;
}
---------------------------------------------------------------------------------------------
#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
------------------------------------------------------------------------------------------------------------------------
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");
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
44
555
6666
6666
555
44
3
#include<stdio.h>
void incrementPatternPrint(int s,int n)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
printf("%d",s);
s++;
printf("\n");
}
s--;
for(int i=n;i>=1;i--)
{
for(int j=1;j<=i;j++)
printf("%d",s);
s--;
printf("\n");
}
}
int main()
{
incrementPatternPrint(3,4);
}
---------------------------------------------------------------------------------------------------------------
write an algorithm to figure out the max no of calls required specoial function to obtain an array seending 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
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));
}
----------------------------------------------------
MINDTREE OLD Q"S
-------------------------
#include <stdio.h>
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;
}