Sie sind auf Seite 1von 23

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;
}
for(j=1;j<i;j++) // Prints the numbers with * in
#include<stdio.h> ith Line
int main( ) { {
int rows; printf("%d*",i);
scanf("%d",&rows); }
MirrorPattern(rows); printf("%d",i);
return 0; } printf("\n"); }
void MirrorPattern(int n) { return;
// 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*",i); // Prints the numbers with * in
ith Line
}
printf("%d",i); //Prints the last number
without * ith Line
printf("\n"); 2. Pattern 2:
}

ffor(i=n;i>=1;i--) //Prints second half of


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


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


6. Pattern 6:

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
}
if(i%2==1)
printf("%d",k); // last number in odd row
else
printf("%d",l); // last number in even row
k++; // number in the next odd row
l--; // number in the next even row
printf("\n");
} // end of i loop and pattern printing completed
return ; }
7. Square Pattern 1:

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


int main( ) {
1112 int row;
scanf("%d",&row);
3222
SquarePattern(row);
3334 return 0; }
void SquarePattern(int n)
{
// Write Your code here
return;
}
#include<stdio.h>
int main( ) {
int row;
scanf("%d",&row);
SquarePattern(row);
return 0; }
void SquarePattern(int n) {
// Write Your code here
int i, j, k=1;
for(i=1;i<=n;i++) // To print n rows
{
if(i%2==1) //Odd row. Here first repeat numbers n times
{
for(j=1;j<=n;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<=n;j++) {
printf("%d",k); //to print Repeated number n times
}
}
printf("\n");
k++; //To get new number for the next row
}
return;
}
8. 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>
int main( ) else //Rows expect first or last row
{ {
int row; for(j=1;j<n;j++) //To print n-1 numbers
scanf("%d",&row); {
SquarePattern(row); printf("%d*",m++); //n-1 numbers with *
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 last return ;
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 *
}

9. 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

10. 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; }

11. 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; }

12. 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

13. GCD of an Array of numbers.

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


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

14. 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"

15. 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"

16. 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
Hence the dissimilar numbers // FUNCTION SIGNATURE BEGINS, THIS FUNCTION IS
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;
}

17. 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;
}

14. 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: (( )(( ))) {
Output: 4 char str[MAX];
2. Input: (( ))) return 0;
Output: -1 }
// SOME LIBRARY FUNCTIONALITY MAY BE RESTRICTED
// DEFINE ANY FUNCTION NEEDED
// FUNCTION SIGNATURE BEGINS, THIS FUNCTION IS
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;
}

15a) 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
} // FUNCTION SIGNATURE ENDS

15b) 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

16. 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
}
}

Das könnte Ihnen auch gefallen