Você está na página 1de 28

1.

Pattern 1: (Mirror Pattern)

Program to print the following pattern #include<stdio.h>


1 int main( )
2*2 {
3*3*3 int rows;
4*4*4*4 scanf(“%d”,&rows);
4*4*4*4 MirrorPattern(rows);
3*3*3 return 0;
2*2 }
1 void MirrorPattern(int n)
{
// Write Your code here

return;
}

#include<stdio.h>
ffor(i=n;i>=1;i--) //Prints second half of
int main( ) {
the pattern
int rows;
{
scanf("%d",&rows);
for(j=1;j<i;j++) // Prints the numbers with * in
MirrorPattern(rows);
ith Line
return 0; }
{
void MirrorPattern(int n) {
printf("%d*",i);
// Write Your code here
}
int i , j ;
printf("%d",i);
for(i=1;i<=n;i++) // Prints first half of the
printf("\n"); }
pattern
return;
{
}
for(j=1;j<i;j++)
{
printf("%d*",i); // Prints the numbers with * in
ith Line
}
printf("%d",i); //Prints the last number
without * ith Line
printf("\n");
}
2. Pattern 2:

Program to print the following pattern #include<stdio.h>


1 int main( )
2*2 {
3*3*3 int rows;
4*4*4*4 scanf(“%d”,&rows);
3*3*3 MirrorPattern(rows);
2*2 return 0;
1 }
void MirrorPattern(int n)
{
// Write Your code here

return;
}
#include<stdio.h>
ffor(i=n-1;i>=1;i--) //Prints second half of
int main( ) {
the pattern
int rows;
{
scanf("%d",&rows);
for(j=1;j<i;j++) // Prints the numbers with * in
MirrorPattern(rows);
ith Line
return 0; }
{
void MirrorPattern(int n) {
printf("%d*",i);
// Write Your code here
}
int i , j ;
printf("%d",i);
for(i=1;i<=n;i++) // Prints first half of the
printf("\n"); }
pattern
return;
{
}
for(j=1;j<i;j++)
{
printf("%d*",i); // Prints the numbers with * in
ith Line
}
printf("%d",i); //Prints the last number
without * ith Line
printf("\n");
}
3. Pattern 3: (Mirror Pattern)
Program to print the following pattern #include<stdio.h>
1 int main( )
2*3 {
Pattern(4);
4*5*6
return 0;
7*8*9*10 }
7*8*9*10 void Pattern(int n)
4*5*6
{
// Write Your code here
2*3
1 return;
}

#include<stdio.h>
int main( ) {
Pattern(4);
return 0; }
void Pattern(int n)
{ // Write Your code here
int i, j, k=1, m;
for (i=1;i<=n;i++) // Prints first half of the pattern
{
for (j=1;j<i;j++) //To print i-1 numbers in ith row
{
printf("%d*",k++); // Prints the numbers with * in ith Line
}
printf("%d",k++); //Prints the last number without * in ith line
printf("\n"); } //end of loop i. First half pattern printed
k=k-n; //Update k value for second half.
m=k; // To use ith row k value in i-1th row
for (i=n;i>=1;i--) //Prints second half
{
for (j=1;j<i;j++) {
printf("%d*",k++); }
printf("%d",k++); //Prints the last number without * ith Line
printf(" \n"); k=m-i+1; //Update k for the next row
m=k; // To use ith row k value in i-1th row
} return ;
}
4. Pattern 4:

Program to print the following pattern #include<stdio.h>


with initial value 2 and n=3 int main( )
2 {
3*3 int rows;
4*4*4 scanf("%d",&rows);
3*3
Pattern(rows);
return 0;
2
}
void Pattern(int n)
{
// Write Your code here

return;
}

#include<stdio.h>
int main( )
{ k=k-2;//update k value for next n-1
int rows; rows
scanf("%d",&rows); for(i=n-1;i>=1;i--) //Prints next n-1
Pattern(rows); rows
return 0; {
} for(j=1;j<i;j++) //prints numbers
void Pattern(int n) with * in ith line
{ {
// Write Your code here printf("%d*",k);
int i, j, k=2; }
for(i=1;i<=n;i++) printf("%d",k);//prints last number in
{ //Prints first half of the pattern ith line
for(j=1;j<i;j++)//Prints numbers with * printf(" \n");
in ith line k--; //k value for the next row
{ }
printf("%d*",k); return ;
} }
printf("%d",k); //Prints last number in
printf("\n"); in ith line

k++; //Update k value for next row

} //End of first half of the pattern


5. Pattern 5:

Program to print the following pattern #include<stdio.h>


with initial value 3 and n=4 int main( )
3 {
4*4 int rows;
5*5*5 scanf("%d",&rows);
6*6*6*6
Pattern(rows);
return 0;
5*5*5
}
4*4
void Pattern(int n)
3 {
// Write Your code here
return;
}

#include<stdio.h>
int main( ) { k=k-2;//update k value for next n-1
int rows; rows
scanf("%d",&rows); for(i=n-1;i>=1;i--) //Prints next n-1
Pattern(rows); rows
return 0; } {
void Pattern(int n) { for(j=1;j<i;j++) //prints numbers
// Write Your code here with * in ith line
int i, j, k=3; {
for(i=1;i<=n;i++) printf("%d*",k);
{ //Prints first half of the pattern }
for(j=1;j<i;j++)//Prints numbers with * printf("%d",k);//prints last number in
in ith line ith line
{ printf(" \n");
printf("%d*",k); k--; //k value for the next row
} }
printf("%d",k); //Prints last number in return ;
}
printf("\n"); in ith line

k++; //Update k value for next row

} //End of first half of the pattern


6. Pattern 6:

Given an integer N print 2*N lines in the #include<stdio.h>


following manner int main( )
If N=4 and s=3 the pattern generated {
would be int rows,value;
3 scanf("%d%d",&value,&rows);
44 IncrementPatternPrint(value,rows);
555 return 0;
6666 }
6666 void IncrementPatternPrint(int s,int n)
555 {
44 // Write Your code here
3 return;
}
void IncrementPatternPrint(int s,int n)
{
// Write Your code here
int i, j;
for(i=1;i<=n;i++)
{//Prints first half of the pattern
for(j=1;j<=i;j++)
printf("%d",s);
printf("\n");
s++; //Update k value for next row
}
s=s-1;
for(i=n;i>=1;i--) //Prints next n-1 rows
{
for(j=1;j<=i;j++) //prints numwith * in ith line
printf("%d",s);
printf(" \n");
s--; //k value for the next row
}
return;
}
7. Pattern 7:

Program to print the following pattern. #include<stdio.h>


1 int main( )
{
3* 2
int rows;
4 *5* 6 scanf("%d",&rows);
Pattern(rows);
10 *9* 8* 7
return 0;
11* 12* 13* 14* 15 }
void Pattern(int n)
{
// Write Your code here
return;
}
#include<stdio.h>
int main( ) {
int rows;
scanf("%d",&rows);
Pattern(rows);
return 0; }
void Pattern( int n) {
//Write your code here
int i, j, k=1, l;
for(i=1;i<=n;i++) // Prints n rows of the pattern
{
l=k+i-1; // 'l' is used to print numbers in even rows and calculated from 'k '
for(j=1;j<i;j++)
{
if(i%2==1) // Odd row
printf("%d*",k); // numbers in odd row with * are printed
else
printf("%d*",l); //even row numbers in reverse order with * in are printed
k++; // next number in even row
l--; // next number in odd row
}
} // end of i loop and pattern printing completed
return ;
}
8. Spiral Pattern :

Given an integer N, print N lines in the following #include<stdio.h>


manner. int main( ) {
For e.g if N=6 int rows;
1111112 scanf("%d",&rows);
writePatternSpiral(rows);
3222222
return 0; }
3333334 void writePatternSpiral(int
rows)
5444444
{
5555556 // Write Your code here
return; }
7666666 and so on

#include<stdio.h>
int main( ) {
int rows;
scanf("%d",&rows);
writePatternSpiral(rows);
return 0; }
void writePatternSpiral(int rows)
{
// Write Your code here
int i, j, k=1;
for(i=1;i<=rows;i++) // To print n rows
{
if(i%2==1) //Odd row. Here first repeat numbers n times
{
for(j=1;j<=rows;j++) {
printf("%d",k); // Repeated number printed n times
}
printf("%d",++k); //Last number of odd row
}
else //Even row. Here numbers are repeated after first number
{
printf("%d",k--); //first number of even row
for(j=1;j<=rows;j++) {
printf("%d",k); //to print Repeated number n times
}
}
printf("\n");
k++; //To get new number for the next row
}
return;
} //Function Signature Ends
9. Square Pattern 2:

Program the following square pattern. #include<stdio.h>


int main( )
{
1*2*3*4
int row;
9*10*11*12 scanf("%d",&row);
SquarePattern(row);
13*14*15*16
return 0;
5*6*7*8 }
void SquarePattern(int n)
{
// Write Your code here
return;
}
#include<stdio.h> else //Rows expect first or last row
int main( ) {
{ for(j=1;j<n;j++) //To print n-1 numbers
int row; {
scanf("%d",&row); printf("%d*",m++); //n-1 numbers with *
SquarePattern(row); }
return 0; printf("%d",m++); // Last number
} without *
void SquarePattern(int n) }
{ printf("\n");
// Write Your code here //To get new number for the next row
int i, j, k, m; }
k=1; // K to print numbers in first and return ;
last row }
m=2*n+1; //m to prints numbers in other
rows
for(i=1;i<=n;i++) // To print n rows
{
if(i==1||i==n) //First row or last row
{
for(j=1;j<n;j++) //To print First or last
row n-1 numbers
{
printf("%d*",k++); // n-1 numbers with *
}
printf("%d",k++); //Last number without *
}
10. Trapezium Pattern:

Print trapezium pattern: #include<stdio.h>


If N=4 the code should print the int main( )
following pattern. {
1*2*3*4*17*18*19*20 int rows;
- -5*6*7*14*15*16 scanf(“%d”,&rows);
- - - -8*9*12*13 TrapeziumPattern(rows);
- - - - --10*11 return 0;
Again if N=5 the code should print }
1*2*3*4*5*26*27*28*29*30 void TrapeziumPattern (int n)
--6*7*8*9*22*23*24*25 {
----10*11*12*19*20*21 // Write Your code here
------13*14*17*18 return;
--------15*16 }
For N=2 the pattern will be
1*2*5*6
--3*4

#include<stdio.h>
int main( ) {
int rows;
scanf(“%d”,&rows);
TrapeziumPattern(rows);
return 0; }
void TrapeziumPattern (int n)
{
// Write Your code here
int i, j, k, s;
int a = 1; //To get first half numbers in a row
int b = n*n + 1; //To get next half numbers in a row
for (i = n; i >= 1; i--) //To print n rows
{
for (s = 1; s <= n - i; s++) //To print spaces.first row zero,next one and so on
printf("--");
for (j = 1; j <= i; j++) // To print first n numbers with *
printf("%d*", a++);
for (k = 1; k < i; k++) //To print next n-1 numbers with *
printf("%d*", b++);

printf("%d\n", b); // last number should without *


b -= 2*(i - 1); //Updated value for next half of the row
}
return; } // end of the function
11. Pattern 7 :

Program the following pattern. #include<stdio.h>


int main( )
{
1
int row;
232
scanf("%d",&row);
34543
Pattern(row);
4567654
return 0;
567898765
}
void Pattern(int n)
{
// Write Your code here
return;
}

#include<stdio.h>
int main( ) {
int row;
scanf("%d",&row);
Pattern(row);
return 0; }
void Pattern(int n) {
// Write Your code here
int i, row, num = 1, space;
for(row = 1 ; row <= n ; row++ )
{
num = row;
for(space =1;space<=n-row;space++ )
printf(" ");
for (i=1; i <= row ; i++ )
{
printf("%d", num);
num++;
}
num=num-2;
for(i= 1 ; i < row ; i++) {
printf("%d",num);
num--; }
printf("\n"); }
return; }
12. Prime Numbers upto n separated by commas.

A number is said to be prime if it has exactly #include<stdio.h>


two factors. int main( )
Write a Program to print prime numbers {
upto certain range such that each prime int range;
number is separated by a comma except the scanf("%d",&range);
last one. PrimeSeries(range);
Ex1: input: 7 return 0;
Output: 2, 3, 5, 7 }
void PrimeSeries( int n)
Ex2: input: 16 {
Output: 2, 3, 5, 7, 11, 13 // Write Your code here
return;
}
#include<stdio.h>
int main( ) {
int range;
scanf("%d",&range);
PrimeSeries(range);
return 0; }
void PrimeSeries( int n) {
// Write Your code here
int i, j, isprime, count =0; //count prints 1st prime without comma and others with comma
for(i=2;i<=n;i++) {
isprime=1; //if isprime=1, i is prime and printed
for(j=2;j*j<=i;j++) {
if(i%j==0) // i is exactly divisible by j
{
isprime=0; // i is not prime and not printed
break; // j loop is terminated
}
} //End of j loop
if(isprime==1) // i prime number
{
if(count==0) //first prime
printf("%d",i); // first prime printed without comma
else
printf(",%d",i); //Other primes printed with comma first
count++;
} // end of if else
} // end of i loop
return; }
13. GCD of two positive integers
Let a and b are two positive integers. int main()
These integers are divisible by their {
factors. Among these factors some int gcd, a, b;
are in common. The highest number scanf("%d%d",&a,&b);
in these factors is called GREATEST gcd=calculateGCD(a,b);
COMMON DIVISOR(GCD). printf("%d",gcd);
Implement the function which finds return 0;
the GCD of two given positive }
integers. // SOME LIBRARY FUNCTIONALITY MAY BE RESTRICTED
Ex: input: 18 24 // DEFINE ANY FUNCTION NEEDED
Output: 6 // FUNCTION SIGNATURE BEGINS, THIS FUNCTION IS
Here 6 is the highest number in the REQUIRED
common factors of 18 and 24 int calculateGCD(int a, int b)
Ex: input: 25 56 {
Output: 1 // WRITE YOUR CODE HERE
}
// FUNCTION SIGNATURE ENDS

int main()
{
int gcd,a,b;
scanf("%d%d",&a,&b);
printf("%d",calculateGCD(a,b));
return 0;
}
// SOME LIBRARY FUNCTIONALITY MAY BE RESTRICTED
// DEFINE ANY FUNCTION NEEDED
// FUNCTION SIGNATURE BEGINS, THIS FUNCTION IS REQUIRED
int calculateGCD(int a, int b)
{
// WRITE YOUR CODE HERE
if(a==0)
return b;
else
return calculateGCD(b%a,a);
}
// FUNCTION SIGNATURE ENDS
14. GCD of an Array of numbers.

The highest common factor which divides #include<stdio.h>


given two numbers is GCD of given #define SIZE 20
numbers. int main( )
Write a Program to find GCD of an Array of {
positive integers. int n, arr[SIZE], i;
Ex1: input: 10 18 12 6 26 32 scanf("%d",&n);
Output: 2 for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
Ex2: input: 5 2 3 gcd=generalizedGCD(a,n);
Output: 1 printf(“%d”, gcd);
return 0;
}
int generalizedGCD(int *arr, int n)
{
// Write Your code here

}
#include<stdio.h>
#define SIZE 20
int main( ) {
int n, a[SIZE],i,gcd;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
gcd=generalizedGCD(a,n);
printf("%d",gcd);
return 0; }
int generalizedGCD(int *arr, int n)
{// Write Your code here
int i, g=arr[0];
for (int i=1; i<n; i++)
g = calculategcd(arr[i],g);
return g;
}
int calculategcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
15. Program to sort elements in ascending & descending order.
Implement a function to sort first k #include <stdio.h>
elements of the array in ascending int main( ) {
order and remaining elements in int a[30],i,n,k,*p;
descending order. scanf("%d%d",&n,&k);
Ex1: input: len=5 k=3 for(i=0;i<n;i++)
10 18 12 6 26 scanf("%d",&a[i]);
Output: 10 12 18 26 6. p=findArrSort(a,n,k);
Here first 3 elements 10 18 12 for(i=0;i<n;i++)
are arranged in ascending order printf("%d ",p[i]);
and the remaining two elements 6 return 0; }
26 are arranged in descending // SOME LIBRARY FUNCTIONALITY MAY BE RESTRICTED
order. // DEFINE ANY FUNCTION NEEDED
Ex2: input: 5 2 // FUNCTION SIGNATURE BEGINS, THIS
2 1 345 FUNCTION IS REQUIRED
Output: 1 2 5 4 3. int* findArrSort(int* arr,int len,int k)
{
int *arr_new=(int *)malloc(len*sizeof(int));
// Write Your code here
return arr_new;
}
int* findArrSort(int* arr,int len,int k)
{ int *arr_new=(int *)malloc(len*sizeof(int));
// Write your code here
int i,j,temp;
for(i=0;i<len;i++) //First array elements are copied to new array
arr_new[i]=arr[i];
for(i=0;i<k-1; i++) { //To sort first k elements in ascending
for(j=i+1;j<k; j++)
if(arr_new[i]>arr_new[j]) {
temp=arr_new[i];
arr_new[i]=arr_new[j];
arr_new[j]=temp; }
}
for(i=k;i<len-1;i++) //To sort remaining elements in descending
{
for(j=i+1;j<len;j++)
if(arr_new[i]<arr_new[j]) {
temp=arr_new[i];
arr_new[i]=arr_new[j];
arr_new[j]=temp;
} }
return arr_new; //return new array address, which contains sorted elements
} // FUNCTION SIGNATURE ENDS"
16. Program for Alternate sort.
Given a sorted array of positive #include <stdio.h>
integers, rearrange the array int* AlternateArrSort(int* arr,int len);
alternately int main( ) {
i.e first element should be int a[30],i,n,*p;
maximum value, second minimum scanf("%d",&n);
value, third second max, fourth for(i=0;i<n;i++)
second min and so on. scanf("%d",&a[i]);
p=AlternateArrSort(a,n);
Examples: for(i=0;i<n;i++)
printf("%d ",p[i]);
Input : arr[ ] = {1, 2, 3, 4, 5, 6, 7} return 0; }
Output : arr[ ] = {7, 1, 6, 2, 5, 3, 4} // SOME LIBRARY FUNCTIONALITY MAY BE RESTRICTED
// DEFINE ANY FUNCTION NEEDED
// FUNCTION SIGNATURE BEGINS, THIS FUNCTION IS
REQUIRED
int* AlternateArrSort(int* arr,int len)
{
// Write Your code here

}
int* AlternateArrSort(int* arr,int len)
{
// Write Your code here
int *arr_new=(int *)malloc(len*sizeof(int));
int i, k, l;
k=0; //comes from front
l=len-1; //comes from last
for(i=0;i<len;i++)
{
if(i%2==0) //filling even positions
arr_new[i]=arr[l--];
else //filling odd positions
arr_new[i]=arr[k++];
}
return arr_new;
} // FUNCTION SIGNATURE ENDS"
17. Program to print Dissimilar elements.
Given two two arrays find the #include <stdio.h>
dissimilar elements of the arrays int main( ) {
int a[20], b[30], n, m, i, res;
Examples: scanf("%d%d",&n,&m);
Input : arr1[] = { 3, 5,2, 7,4} for(i=0;i<n;i++)
arr2[] = {7, 1, 6, 2, 5, 3, 14} scanf("%d",&a[i]);
for(i=0;i<m; i++)
Output: 4 scanf("%d",&b[i]);
res= distinctElementCount(a, b, n, m);
Here arr1 contains 1 element
printf(“%d”, res);
which is not in arr2 i.e 4. return 0; }
// INCLUDE HEADER FILES NEEDED BY YOUR PROGRAM
Similary arr2 contains 3 elements
// SOME LIBRARY FUNCTIONALITY MAY BE RESTRICTED
which are not arr1 i.e 1, 6, 14 // DEFINE ANY FUNCTION NEEDED
// FUNCTION SIGNATURE BEGINS, THIS FUNCTION IS
Hence the dissimilar numbers
REQUIRED
count is 4 int distinctElementCount(int *arr1, int *arr2, int len1, int len2)
{
// Write Your code here
}
// INCLUDE HEADER FILES NEEDED BY YOUR PROGRAM
// SOME LIBRARY FUNCTIONALITY MAY BE RESTRICTED
// DEFINE ANY FUNCTION NEEDED
// FUNCTION SIGNATURE BEGINS, THIS FUNCTION IS REQUIRED
int distinctElementCount(int *arr1, int *arr2, int len1, int len2)
{
// Write Your code here
int i,j,count=0,dis;
for(i=0;i<len1;i++)
{
for(j=0;j<len2;j++)
{
if(arr1[i]==arr2[j])
count++;
}
}
dis=len1+len2-2*count;
return dis;
}
18. Swapping of An Array elements
Given an array of certain size then #include <stdio.h>
print array elements by swapping int* SwapArr(int *arr,int len);
alternate elements. int main( ) {
int a[20], n, i, *p;
Examples: scanf("%d",&n);
Input : arr1[] = { 3, ,2, 1, 0} for(i=0;i<n;i++)
Output: 2 3 0 1 scanf("%d",&a[i]);
p= SwapArr(a,n);
for(i=0;i<n;i++)
printf("%d ",p[i]);
return 0; }
// INCLUDE HEADER FILES NEEDED BY YOUR PROGRAM
// SOME LIBRARY FUNCTIONALITY MAY BE RESTRICTED
// DEFINE ANY FUNCTION NEEDED
// FUNCTION SIGNATURE BEGINS, THIS FUNCTION IS
REQUIRED
int* SwapArr(int *arr,int len)
{
// Write Your code here
}
// INCLUDE HEADER FILES NEEDED BY YOUR PROGRAM
// SOME LIBRARY FUNCTIONALITY MAY BE RESTRICTED
// DEFINE ANY FUNCTION NEEDED
// FUNCTION SIGNATURE BEGINS, THIS FUNCTION IS REQUIRED
int* SwapArr(int *arr,int len)
{
// Write Your code here
int *swap_arr=(int *)malloc(len*sizeof(int));
int i,j=0;
for(i=0;i<len;i=i+2)
{
if((len%2==1)&&(i==len-1)) //if array size is odd then last number to copied to new array
swap_arr[j]=arr[i];
else
{
swap_arr[j++]=arr[i+1];
swap_arr[j++]=arr[i];
} // else end
}
return swap_arr;
}
19. Balanced Parenthesis
Implement the function to find #include<stdio.h>
balanced parenthesis. Return count if int balancedParentheses(char *str);
they balanced otherwise -1. int main()
{
1. Input: (( )(( ))) char str[MAX];
return 0;
Output: 4 }
// SOME LIBRARY FUNCTIONALITY MAY BE RESTRICTED
2. Input: (( ))) // DEFINE ANY FUNCTION NEEDED
// FUNCTION SIGNATURE BEGINS, THIS FUNCTION IS
Output: -1 REQUIRED
int balancedParentheses(char *str)
{
//WRITE YOUR CODE HERE
}
#include<stdio.h>
int balancedParenthesis(char *str);
int main( ) {
char str[20];
gets(str);
printf("%d",balancedParenthesis(str));
return 0; }
// SOME LIBRARY FUNCTIONALITY MAY BE RESTRICTED
// DEFINE ANY FUNCTION NEEDED
// FUNCTION SIGNATURE BEGINS, THIS FUNCTION IS REQUIRED
int balancedParenthesis(char *str)
{ //WRITE YOUR CODE HERE
int c1=0,c2=0,i=0;
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='(')
c1++;
else
if(str[i]==')')
c2++;
if(c2>c1)
return -1;
}
if((c1!=0)&&(c1==c2))
return c1;
else return -1;
}
20. Digit Occurrence
Implement the function to count the #include<stdio.h>
number occurrences of a digit in an array int finddigitoccurrence(int, int[], int);
of numbers. int main( )
Input is series of numbers( take an array {
to store) and a number to count its int num1,num2[10],n,occ,i;
occurrence. scanf("%d",&num1);
Ex: test case1:- 1 6 7 3 4 5 1 5 8 8 8 8 0 1 scanf("%d",&n);
Output: 4 for(i=0;i<n;i++)
8 is appeared 4 times and it is the highest scanf("%d",&num2[i]);
no.of times in sequence. occ=finddigitoccurrence(num1,num2,n);
printf("%d",occ);
return 0;
}
int finddigitoccurrence(int num1, int num2[ ],int n)
{
// WRITE YOUR CODE HERE
}
// FUNCTION SIGNATURE ENDS

#include<stdio.h>
int finddigitoccurrence(int, int[], int);
int main() {
int num1,num2[10],n,occ,i;
scanf("%d",&num1);
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&num2[i]);
occ=finddigitoccurrence(num1,num2,n);
printf("%d",occ);
return 0; }
int finddigitoccurrence(int num1, int num2[ ],int n)
{
// WRITE YOUR CODE HERE
int count=0,i;
for(i=0;i<n;i++)
{
if(num2[i]==num1)
count++;
}
return count;
} // FUNCTION SIGNATURE ENDS
21. Digit Occurrence
Given a positive integer raning form 0 to #include<stdio.h>
99999999 find the no.of occurrences of a int finddigitoccurrence(int, int);
given digit in that number. int main()
{
Ex: input num1=2, num2=123228, return
int num1,num2,occ;
3, that is the no.of occurrences of num1 in scanf("%d",&num1);
num2. scanf("%d",&num2);
occ=finddigitoccurrence(num1,num2);
printf("%d",occ);
return 0;
}
int finddigitoccurrence(int num1, int num2)
{
// WRITE YOUR CODE HERE
}// FUNCTION SIGNATURE ENDS

#include<stdio.h>
int finddigitoccurrence(int, int);
int main( ) {
int num1,num2,occ;
scanf("%d",&num1);
scanf("%d",&num2);
occ=finddigitoccurrence(num1,num2);
printf("%d",occ);
return 0; }
int finddigitoccurrence(int num1, int num2)
{ // WRITE YOUR CODE HERE
int count=0,rem;
if(num2==0)
{
if(num1==num2)
count++;
}
while(num2>0)
{
rem=num2%10;
if(rem==num1)
count++;
num2=num2/10;
}
return count;
}// FUNCTION SIGNATURE ENDS
22. Delete vowels from a string
In English Language the characters #include <stdio.h>
a,e,i,o,u are called vowels. int vowel_check(char);
Write a function to eliminate vowels char* eliminateVowelString(char *str);
from a given string and return the int main() {
string which consiste of characters int i;
other than vowels. char s[100],*p;
gets(s);
Ex: test case1:- Hello p=eliminateVowelString(s);
Output: Hll. printf("%s",p); return 0; }
char* eliminateVowelString(char *str)
{ //WRITE YOUR CODE HERE
} // FUNCTION SIGNATURE ENDS
char* eliminateVowelString(char *str)
{ //WRITE YOUR CODE HERE
int i=0,j=0,len=0;
char *t;
while(str[i]!='\0') //string length
{
len++;
i++; }
t=(char *)malloc(len*sizeof(char));
for(i=0;str[i]!= '\0'; i++)
{
if(vowel_check(str[i]) == 0)
t [ j ++]= str [ i ] ;
}
t[ j ]=’\0’ ;
return t; }
int vowel_check(char c) {
switch(c) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':return 1; //character is vowel
default:return 0; //not vowel
}
}
23. Search substring in a given string and return its count of occurrences
Given a parent string and a substring. Implement a function #include <stdio.h>
to count how many times a substring is appeared in a int main() {
parent string. int c; char s1[30],s2[30];
Ex: test case1:- gets(s1); gets(s2);
abcdefg bcd c=CountSubString(s1,s2);
Output: 1 printf("%d",c); return 0; }
test case2:- int StringOccurrenceCount(char
abcabc ABCabcabcABCabcabcABCabcabcABCabcabcABC *Parent, char *Sub){ //WRITE YOUR
ABCABCabcABCABCabcABCABCabcABCABCabcABCABCabc CODE HERE
Output: 1 } // FUNCTION SIGNATURE ENDS
int StringOccurrenceCount(char *Parent, char *Sub) { //WRITE YOUR CODE HERE
int i,j,k,count=0,flag;
for(i=0;Parent[i]!='\0';i++)
{ if(Parent[i]>='A' && Parent[i]<='Z')
Parent[i]=Parent[i]+32;
}
for(i=0;Sub[i]!='\0';i++)
{ if(Sub[i]>='A' && Sub[i]<='Z')
Sub[i]=Sub[i]+32;
}
for(i=0;Parent[i]!='\0';i++) //to compare all characters of parent with substring
{
j=0; //substring starting location
if(Sub[j]==Parent[i]) //first characters of substring and parent are equal
{
k=i+1; //to compare other characters of parent with substring
flag=0;
for(j=1;Sub[j]!='\0';j++) //To compare second to last characters of substring
{
if(Sub[j]!=Parent[k++]) //comparision between substring and parent
{
flag=1; //substring not same as parent
break;
}
}
if(flag==0) //substring characters present in parent
count++;
}
}
return count; //returning final count
}
24. Eliminate repeated characters from a given string.
Given a string in which some characters #include <stdio.h>
may be repeated. Implement a function #include<stdlib.h>
to eliminate repeated characters in the char * DeleteRepeatString(char *str);
string and return address of the string int main()
which contains characters of given {
string which are not repeated. char s[30],*p;
gets(s);
Ex: test case1:- p=DeleteRepeatString(s);
Input: Hello printf("%s",p);
Output: Helo return 0;
test case2:- }
Inputt: Good Morning char * DeleteRepeatString(char *str)
Output: God Mrnig { //WRITE YOUR CODE HERE
} // FUNCTION SIGNATURE ENDS

char * DeleteRepeatString(char *str)


{ //Write your code Here
char *new_str; //pointer for the new string
int i,j=0,k,len=0,flag=0;

for(i=0;str[i]!='\0';i++) //find length of given string


len++;
new_str=(char *)malloc(len*sizeof(char)); //create new string
for(i=0;i<len;i++) //all characters checked for repetation
{
flag=0; //indicate ith character not repeated
for(k=0;k<j;k++) //to compare new string characters with given string
{
if((str[i]!=' ')&&(str[i]==new_str[k])) //chracter repeated
{
flag=1; //indicate character repeated
break;
}
}
if(flag==0) //ith character not repeated
new_str[j++]=str[i]; //ith character coiped to new string
}
new_str[j]='\0';
return new_str; //returns the address of new string
}
25. Eliminate repeated numbers from a given string.
Given an array of certain length in #include <stdio.h>
which some numbers may be repeated. #include<stdlib.h>
Implement a function to eliminate int * EliminateArrRepeat(int *arr,int len);
repeated numbers in the array and int main( ) {
return address of the array which int a[30],n,i,*p;
contains numbers of given array which scanf("%d",&n);
are not repeated. for(i=0;i<n;i++)
scanf("%d",&a[i]);
Ex: test case1:- p=EliminateArrRepeat(a,n);
Parent: Hello for(i=0;i<n;i++)
Output: Helo printf("%d ",p[i]);
test case2:- return 0; }
Parent: Good Morning int * EliminateArrRepeat(int *arr,int len)
Output: God Mrnig {//WRITE YOUR CODE HERE
} // FUNCTION SIGNATURE ENDS

int * EliminateArrRepeat(int *arr,int len)


{ //WRITE YOUR CODE HERE
int i,j=0,k,flag;
int *new_arr; //pointer for new array which holds non repeated

new_arr=(int *)malloc(len*sizeof(int)); //new array created

for(i=0;i<len;i++) //check elements of arr for repetation


{
flag=0; //indicated ith element not repeated
for(k=0;k<j;k++) //compares newarr elements with given array
{
if(new_arr[k]==arr[i]) //ith element found in new array
{
flag=1; //ith element repeated
break;
}
}
if(flag==0) //ith element not repeated
new_arr[j++]=arr[i]; //copies ith element into jth position of new array
}
return new_arr; //Returns new array address which contains non repeated
numbers
}
26. Is Same Reflection (Magic mirror).
The inputs to the function consists of two #include <stdio.h>
strings word1 and word2.The function returns int main( ) { char *s1,*s2;
1 if word1 and word2 are right rotations of the gets(s1); gets(s2);
same word and -1 if they are not. printf(“%d”,isSameReflection(s1,s2);
Ex:Input 1: abc cab return 0; }
Output1: 1 int isSameReflection(char* word1, char* word2)
Input:2 ab aa { //WRITE YOUR CODE HERE
Output: -1 } // FUNCTION SIGNATURE ENDS

int isSameReflection(char* word1, char* word2) {


//WRITE YOUR CODE HERE
int len1,len2,i,j=0,k,flag;
char *temp;
len1 = strlen(word1); len2 = strlen(word2);
if (len1 != len2) /* Check if sizes of two strings are same */
return -1;
temp= (char *)malloc(sizeof(char)*(len1*2 + 1)); /* Create a temp by
concatenating word1 with word1*/

for(i=0;word1[i]!='\0';i++)
temp[j++]=word1[i];
for(i=0;word1[i]!='\0';i++)
temp[j++]=word1[i];
temp[j]='\0';
/* Now check if word2 is a substring of temp */
for(i=0;temp[i]!='\0';i++)
{
j=0; //word2 starting location
if(word2[j]==temp[i]) //first characters of word2 and temp are equal
{
k=i+1; //to compare other characters of temp with word2
flag=0;
for(j=1;word2[j]!='\0';j++) //To compare second to last characters of temp and
word2
{
if(word2[j]!=temp[k++]) //comparision between temp and word2
{
flag=1; //substring not same as parent
break;
}
}
if(flag==0) //substring characters present in parent
return 1;
}
}
return -1;
}

Rotate Matrix
int **rotatePictureMethod(int **,int,int,int);
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int **c,i,j;
c=rotatePictureMethod(a,3,3,0);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");

}
return 0;
}

int **rotatePictureMethod(int **matrix,int m,int n,int flag)


{
int **b,i,j,k;
int *c=matrix;
b=(int**)malloc(n*sizeof(int*));
for(i=0;i<n;i++)
{
b[i]=(int*)malloc(m*sizeof(int));
}
if(flag==0)
{
for(j=n-1,k=0;j>=0;j--,k++)
{
for(i=0;i<m;i++)
b[k][i]=*(c+i*n+j);
}
}
else
{
for(j=0;j<n;j++)
{
for(i=m-1,k=0;i>=0;i--,k++)
b[j][k]=*(c+i*n+j);
}
}
return b;
}

Você também pode gostar