Coding Practice Set

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

1. Code for checking palindrome and explanation.

-----------------------------------------------------------------------------------
--------------------------------------------------
#include <stdio.h>

/*
string palindrome
i.e ; abcdcba
*/

int main()

char str[100];

int len=0,i,flag=0;

scanf("%s",str);

for(i=0;str[i]!='\0';i++)

len++;

if(str[i]>=97 && str[i]<=122)

str[i]=str[i]-32;

for(i=0;i<len/2;i++)

if(str[i]!=str[len-i-1])

flag=1;

break;

if(flag==1)

printf("\nNot palindrome");

else

printf("\nPalindrome");

return 0;

}//this program will take any alphabet and convert to uppercase for palindrome
checking ..i.e leVEl=>LEVEL=>Palindrome..etc
-----------------------------------------------------------------------------------
--------------------------------------------------
2. Gcd and LCM of two numbers
-----------------------------------------------------------------------------------
--------------------------------------------------
//GCD of n numbers in an array

#include <stdio.h>

/*
gcd of n numbers

*/

int gcd(int a,int b)

int c;

if(a>b)

a=a+b;

b=a-b;

a=a-b;

while(a!=0)

if(a<b)

c=b%a;

b=a;

a=c;

return b;

int main()

{
int arr[100],n,i,temp=0;

scanf("%d",&n);

for(i=0;i<n;i++)

scanf("%d",&arr[i]);

for(i=0;i<n-1;i++)

temp=gcd(arr[i],arr[i+1]);

arr[i+1]=temp;

printf("\n Gcd is %d",temp);

return 0;

}
//finding lcm of two//
#include<stdio.h>

int lcm(int a,int b)

int i,l;

if(a>b)

a=a+b;

b=a-b;

a=a-b;

//now a<b

for(i=1;i<=a*b;i++)

if(i%a==0 && i%b==0)

l=i;

break;

}
}

return l;

int main(void)

int a,b,l;

scanf("%d %d",&a,&b);

l=lcm(a,b);

printf("LCM = %d",l);

return 0;

}
-----------------------------------------------------------------------------------
--------------------------------------------------
3. Removing vowels from a line of text..
-----------------------------------------------------------------------------------
--------------------------------------------------
#include<stdio.h>

int main(void)

char arr[100];

int i,j,temp;

scanf("%[^\n]s",arr);

for(i=0;arr[i]!='\0';i++)

if(arr[i]=='A' || arr[i]=='a' || arr[i]=='E' || arr[i]=='e' || arr[i]=='I' ||


arr[i]=='i' || arr[i]=='O' || arr[i]=='o' || arr [i]=='U' || arr[i]=='u')

for(j=i;arr[j]!='\0';j++)

arr[j]=arr[j+1];

puts(arr);
return 0;

}
-----------------------------------------------------------------------------------
--------------------------------------------------
4. Splitting an array by the given index and sort one part in ascending and other
in descending.
5. Ques. 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

6. Ques. 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
7.Programming Pattern to Print 2*N Number of rows for input Pattern?

3
44
555
6666
555
44
3

8. Print the following pattern-

1*2*3*10*11*12
--4*5*8*9
----6*7
9. C Program to check if two given matrices are identical.

10. Given a 2D array, print it in spiral form. See the following examples.

Input:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

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

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

Input : limit = 20
Output : 3 4 5
8 6 10
5 12 13
15 8 17
12 16 20
13. Write Code in C for Bubble Sorting?
14. Write a program to check if a number is Armstrong number?
15. Given�a number and check if a number is perfect or not.
16. Given a key (or data) to be inserted, task is to complete the
functioninsertAtBeginning()�which
inserts the data in front of the linked list and insertAtEnd()�which�appends�the
data at the end of
the linked list.
17.Given a sorted�array�A[]��( 0 based index )�and a key�&quot;k&quot;��you need to
complete the
function�bin_search�to��determine the position of the key if the key is present
in�the array. If the
key is not�present then you have to return�-1. The arguments left and right denotes
the left most
index and right most index of the array�A[]�.

You might also like