Escolar Documentos
Profissional Documentos
Cultura Documentos
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:
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:
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
#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
#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:
#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++);
#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.
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.
}
#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
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;
}