0% found this document useful (0 votes)
16 views33 pages

Wa0279.

The document contains a series of C programming exercises that cover various algorithms and data structures. Topics include recursion, array manipulation, string processing, matrix operations, and mathematical concepts such as permutations and Fibonacci encoding. Each exercise is presented with code snippets and explanations for implementation.

Uploaded by

subiksharajeu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views33 pages

Wa0279.

The document contains a series of C programming exercises that cover various algorithms and data structures. Topics include recursion, array manipulation, string processing, matrix operations, and mathematical concepts such as permutations and Fibonacci encoding. Each exercise is presented with code snippets and explanations for implementation.

Uploaded by

subiksharajeu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Excel Sheet

C LEVEL - 5
1.​ 1) Sum of odd numbers in an array using recursion

#include <stdio.h>
int sum=0;
int func(int arr[],int n,int i){

if(n==i)
return 0;
else{
if(arr[i]%2!=0){
sum+=arr[i];
func(arr,n,i+1);
}
else{
func(arr,n,i+1);
}
}
return sum;
}

int main()
{
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
int sum=func(arr,n,0);
printf("%d",sum);
}
2.​ 2) Delete the element in the given position in an array

#include <stdio.h>
int func(int arr[],int n,int k){
for(int i=0;i<n;i++){
if(i!=k){
printf("%d ",arr[i]);
}
}
}
int main(){
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
int k;
scanf("%d",&k);
func(arr,n,k);
}

IN RECURSION

#include <stdio.h>
void func(int arr[],int n,int k,int i){
if(i==n)
return;
if(i!=k){
printf("%d ",arr[i]);
}
func(arr,n,k,i+1);
}
int main(){
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
int k;
scanf("%d",&k);
func(arr,n,k,0);
}

3.​ 3) Permutation using recursion P(n,r)= n!/(n-r)!

#include <stdio.h>
#include <math.h>

int fact(int n){


if(n==1 || n==0){
return 1;
}
else{
return n*fact(n-1);
}
}

int per(int n,int r){


return fact(n)/fact(n-r);
}

int main()
{
int n,r;
scanf("%d %d",&n,&r);
if(r>n || n<0 || r<0){
printf("Invalida");
}
else{
printf("%d",per(n,r));
}
}

4.​4) Keith Number

#include <stdio.h>
#include <math.h>
int count(int n){
int c=0;
while(n>0){
c++;
n/=10;
}
return c;
}

int isk(int n){


int d=count(n);
int org=n;
int arr[10];
for(int i=d-1;i>=0;i--){
arr[i]=org%10;
org/=10;
}

while(1){
int sum=0;
for(int i=0;i<d;i++){
sum+=arr[i];
}
if(sum==n){
return 1;
}
if(sum>n){
return 0;
}
for(int i=0;i<d-1;i++){
arr[i]=arr[i+1];
}
arr[d-1]=sum;
}
}
int main(){
int n;
scanf("%d",&n);
if(isk(n)){
printf("KEITH");
}
else{
printf("NOT");
}
}

5.​ 5) Longest Substring

#include <stdio.h>
#include <string.h>
int isu(char s[],int i,int j){
for(int k=i;k<j;k++){
for(int l=k+1;l<=j;l++){
if(s[k]==s[l]){
return 0;
}
}
}
return 1;
}
int longs(char s[]){
int n=strlen(s);
int max=0;
int len;
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
if(isu(s,i,j)){
len=j-i+1;
if(len>max){
max=len;
}
}
}
}
return max;
}
int main(){
char str[100];
scanf("%s",str);
printf("%d",longs(str)); }

6.​ 6) Binary to Integer

#include <stdio.h>

int bin(char s[]){


int n=0;
for(int i=0;s[i]!='\0';i++){
n=n*2+s[i]-'0';
}
return n;
}
int main(){
char s[100];
scanf("%s", s);
printf("%d",bin(s));
}

7.​ 7) Minimum for Row, Column, Diagonal

#include <stdio.h>

void row(int m[100][100],int r, int c){


for(int i=0;i<r;i++){
int min=m[i][0];
for(int j=1;j<c;j++){
if(m[i][j]<min)
min=m[i][j];
}
printf("%d\n", min);
}
}

void col(int m[100][100], int r,int c){


for(int j=0;j<c;j++){
int min=m[0][j];
for(int i=1;i<r;i++){
if(m[i][j]<min)
min=m[i][j];
}
printf("%d\n", min);
}
}

void dia(int m[100][100],int r, int c){


int min=m[0][0];
for(int i=1;i<r;i++){
if(m[i][i]<min)
min=m[i][i];
}
printf("%d\n", min);
}
int main(){
int r,c;
scanf("%d %d",&r,&c);
int m[100][100];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
scanf("%d",&m[i][j]);
}
}
row(m,r,c);
col(m,r,c);
if(r==c)
dia(m,r,c);
else
printf("Minimum diagonal can't find as this is not square matrix");
}

8.​ 8) Zeckendorf theorem

#include <stdio.h>
int fibo(int fib[100],int n){
fib[0]=1;
fib[1]=2;
int count=2;
while(1){
int next=fib[count-1]+fib[count-2];
if(next>n){
break;
}
fib[count]=next;
count++;
}
return count;
}

int zec(int n){


int fib[100];
int count=fibo(fib,n);
for(int i=count-1;i>=0;i--){
if(fib[i]<=n){
printf("%d ",fib[i]);
n-=fib[i];
}
}
printf("\n");
}
int main(){
int n;
scanf("%d",&n);
zec(n);
}

9.​ 9) Reverse even index word

#include <stdio.h>
#include <string.h>
void func(char arr[100][100],int n){
for(int i=0;i<n;i++){
if(i%2==0){
int len=strlen(arr[i]);
for(int j=len-1;j>=0;j--){
printf("%c",arr[i][j]);
}
printf("\n");
}
}
}

int main(){
int n;
scanf("%d",&n);
char arr[100][100];
for(int i=0;i<n;i++){
scanf("%s",arr[i]);
}
func(arr,n);
}

10.​ 10) Peak Element

#include <stdio.h>

int ispeak(int mat[100][100], int r,int c ,int i,int j){


int n=mat[i][j];
if(i>0 && mat[i-1][j]>=n){
return 0;
}
if(i<r-1 && mat[i+1][j]>=n){
return 0;
}
if(j>0&& mat[i][j-1]>=n){
return 0;
}
if(j<c-1 && mat[i][j+1]>=n ){
return 0;
}
return 1;
}

int main(){
int r,c;
scanf("%d %d",&r,&c);
int mat[100][100];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
scanf("%d",&mat[i][j]);
}
}
int peak[100];
int p=0;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(ispeak(mat,r,c,i,j)){
peak[p++]=mat[i][j];
}
}
}
printf("%d\n",p);
for(int i=0;i<p;i++){
printf("%d ",peak[i]);
}
}

11.​ 11) Convert Lowercase to Uppercase letters

#include <stdio.h>
#include <ctype.h>
#include <string.h>
int func(char str[100]){
int len=strlen(str);
char ans[100];
int x=0;
for(int i=0;i<len;i++){
if(str[i]>=97 && str[i]<=122){
ans[x++]=toupper(str[i]);
}
else{
ans[x++]=str[i];
}
}
ans[x++]='\0';
printf("%s",ans);
}

int main(){
char str[100];
scanf("%s",str);
func(str);
}

12.​ 12) To get one word from ascending array and one from
descending array

Input : hi how are you


output: are-you hi-how how-hi you-are
strategy(sort words in ascending and descending order)
then match like 1 word from ascending and 1 word from descending

#include <stdio.h>
#include <ctype.h>
#include <string.h>
int func(char str[100]){
int len=strlen(str);
char arr[100][100];
int k=0,j=0;
for(int i=0;i<len;i++){
if(str[i]!=' '){
arr[k][j++]=str[i];
}
else{
arr[k][j]='\0';
k++;
j=0;
}
}
arr[k][j]='\0';
char temp[100];
for(int i=0;i<k;i++){
for(int j=i+1;j<k;j++){
if(strcmp(arr[i],arr[j])>0){
strcpy(temp,arr[i]);
strcpy(arr[i],arr[j]);
strcpy(arr[j],temp);
}
}
}

char des[100][100];
int x=0;
for(int i=k;i>=0;i--){
strcpy(des[x++],arr[i]);
}

for(int i=0;i<=k;i++){
printf("%s-%s ",arr[i],des[i]);
}
}
int main(){
char str[100];
scanf("%[^\n]%*c",str);
func(str);
}

13.​ 13) Longest Palindromic Substring

#include <stdio.h>
#include <string.h>

int isp(char str[100],int i,int j){


while(i<j){
if(str[i]!=str[j]){
return 0;
}
i++;
j--;
}
return 1;
}

int palin(char str[100]){


int len=strlen(str);
int max=1;
int srt=0;
for(int i=0;i<len;i++){
for(int j=i;j<len;j++){
if(isp(str,i,j)){
int cl=j-i+1;
if(cl>max){
max=cl;
srt=i;
}
}
}
}
for(int i=srt;i<srt+max;i++){
printf("%c",str[i]);
}
printf("\n");
}

int main(){
char str[100];
scanf("%s",str);
palin(str);
}

14.​ 14) To return next vowel and next consonant character


for a string

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int isv(char c){


if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u'){
return 1;
}
else{
return 0;
}
}

int nextv(char lo){


if(lo=='a') return 'e';
if(lo=='e') return 'i';
if(lo=='i') return 'o';
if(lo=='o') return 'u';
if(lo=='u') return 'a';
}

int nextc(char lo){


do{
lo++;
if(lo>'z') lo='a';
}while(isv(lo));
return lo;
}

int main(){
char str[100];
scanf("%[^\n]%*c",str);
int len=strlen(str);
char ch;
for(int i=0;i<len;i++){
if(isalpha(str[i])){
char lo=tolower(str[i]);
if(isv(lo)){
ch=nextv(lo);
}
else{
ch=nextc(lo);
}
if(str[i]>=65 && str[i]<=90){
str[i]=toupper(ch);
}
else{
str[i]=tolower(ch);
}
}
}
printf("%s",str);
}
15.​ 15) Fibonacci Encoding

#include <stdio.h>

int fibo(int fib[100], int n) {


fib[0] = 1;
fib[1] = 2;
int count = 2;
while (1) {
int next = fib[count - 1] + fib[count - 2];
if (next > n) {
break;
}
fib[count] = next;
count++;
}
return count;
}

void zec(int n) {
int fib[100], code[100] = {0};
int count = fibo(fib, n);

for (int i = count - 1; i >= 0; i--) {


if (fib[i] <= n) {
code[i] = 1;
n -= fib[i];
i--;
}
}

for(int i=0;i<count;i++){
if(code[i]!=0){
for(int j=i;j<count;j++){
printf("%d",code[j]);
i=count +1;
}
printf("1");
break;
}
}
}

int main() {
int n;
scanf("%d", &n);
zec(n);
return 0;
}

16.​ 16) To add a minimum of row, column and both


diagonals.

#include <stdio.h>

int row(int m[100][100],int r, int c){


int sum=0;
for(int i=0;i<r;i++){
int min=m[i][0];
for(int j=1;j<c;j++){
if(m[i][j]<min){
min=m[i][j];
}
}
sum+=min;
}
return sum;
}

int col(int m[100][100],int r, int c){


int sum=0;
for(int j=0;j<c;j++){
int min=m[0][j];
for(int i=1;i<r;i++){
if(m[i][j]<min){
min=m[i][j];
}
}
sum+=min;
}
return sum;
}

int dia1(int m[100][100],int r,int c){


int min=m[0][0];
for(int i=1;i<r;i++){
if(m[i][i]<min){
min=m[i][i];
}
}
return min;
}

int dia2(int m[100][100],int r,int c){


int min=m[0][r-1];
for(int i=1;i<r;i++){
if(m[i][r-1-i]<min){
min=m[i][r-1-i];
}
}
return min;
}

int main(){
int r,c;
scanf("%d %d",&r,&c);
int m[100][100];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
scanf("%d",&m[i][j]);
}
}
int rm=row(m,r,c);
int cm=col(m,r,c);
int d1,d2;
if(r==c){
d1=dia1(m,r,c);
d2=dia2(m,r,c);
}
else{
printf("CANT FIND MIN FOR DIAGONAL");
}
int tot=rm+cm+d1+d2;
printf("%d",tot);
}

17.​ 17) Minimum path sum

#include <stdio.h>

int min(int a,int b){


if(a<b){
return a;
}
else{
return b;
}
}

int mpath(int m[100][100],int r,int c){


int dp[100][100];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(i==0 && j==0){
dp[i][j]=m[i][j];
}
else if(i==0){
dp[i][j]=dp[i][j-1]+m[i][j];
}
else if(j==0){
dp[i][j]=dp[i-1][j]+m[i][j];
}
else{
dp[i][j]=min(dp[i][j-1],dp[i-1][j])+m[i][j];
}
}
}
return dp[r-1][c-1];
}

int main(){
int r,c;
scanf("%d %d",&r,&c);
int m[100][100];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
scanf("%d",&m[i][j]);
}
}
int ans=mpath(m,r,c);
printf("%d",ans);
}

18.​ 18) Binary search tree


#include <stdio.h>

int bs(int arr[100],int n,int tar){


int low=0,high=n-1;
while(low<=high){
int mid=(low+high)/2;
printf("mid=%d\n",mid);
if(arr[mid]==tar){
return mid;
}
else if(arr[mid]<tar){
low=mid+1;
}
else{
high=mid-1;
}
}
return -1;
}

int main(){
int n;
scanf("%d",&n);
int arr[100];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
int tar;
scanf("%d",&tar);
int ans=bs(arr,n,tar);
if(ans!=-1){
printf("%d",ans);
}
else{
printf("Element not found");
}
}

19.​ 19) Sliding window

#include <stdio.h>

int slid(int arr[100],int n,int k){


for(int i=0;i<=n-k;i++){
int max=arr[i];
for(int j=1;j<k;j++){
if(arr[i+j]>max){
max=arr[i+j];
}
}
printf("%d ",max);
}
}

int main(){
int n;
scanf("%d",&n);
int arr[100];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
int k;
scanf("%d",&k);
slid(arr,n,k);
}

20.​ 20) Good numbers

#include <stdio.h>
int even[]={0,2,4,6,8};
int prime[]={1,3,5,7};
int c=0;

void goodn(char num[10],int p,int n){


if(p==n){
num[n]='\0';
printf("%s ",num);
c++;
return;
}

if(p%2==0){
for(int i=0;i<5;i++){
num[p]=even[i]+'0';
goodn(num,p+1,n);
}
}
else{
for(int i=0;i<4;i++){
num[p]=prime[i]+'0';
goodn(num,p+1,n);
}
}
}
int main(){
int n;
scanf("%d",&n);
char num[10];
goodn(num,0,n);
printf("\nCount %d",c);
}
21.​ 21) Permutation of Input ( Uppercase and lowercase)

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int c=0;
int func(char s[100],int len,int i){
if(i==len){
if(c==0){
return 1;
}
else{
return 1<<c;
}
}
if(isalpha(s[i])){
c++;
}
return func(s,len,i+1);
}

int main(){
char s[100];
scanf("%s",s);
int len=(strlen(s));
printf("%d",func(s,len,0));
}
22.​ 22) Sum, Difference of 2 binary number in integer form

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int sum(int n1,int n2){


printf("%d ",n1+n2);
}
int dif(int n1,int n2){
printf("%d",n1-n2);
}

void bint(char s1[100],char s2[100]){


int n1=0,n2=0;
int l1=strlen(s1);
int l2=strlen(s2);
for(int i=0;s1[i]!='\0';i++){
n1=n1*2+s1[i]-'0';
}
for(int i=0;s2[i]!='\0';i++){
n2=n2*2+s2[i]-'0';
}
sum(n1,n2);
dif(n1,n2);
}

int main(){
char s1[100];
char s2[100];
scanf("%s %s",s1,s2);
bint(s1,s2);
}
23.​ 23) Reverse the words order in a sentence

#include <stdio.h>
#include <string.h>

int reverse(char str[100]){


char s[100][100];
int len=strlen(str);
int k=0,j=0;
for(int i=0;i<len;i++){
if(str[i]!=' '){
s[k][j++]=str[i];
}
else{
s[k][j]='\0';
k++;
j=0;
}
}
s[k][j]='\0';
for(int i=k;i>=0;i--){
printf("%s ",s[i]);
}
}

int main(){
char str[100];
scanf("%[^\n]%*c",str);
reverse(str);
}
24.​ 24) Maximum in Row, column

#include <stdio.h>

int row(int m[100][100],int r,int c){


for(int i=0;i<r;i++){
int max=m[i][0];
for(int j=1;j<c;j++){
if(m[i][j]>max){
max=m[i][j];
}
}
printf("%d\n",max);
}
}

int col(int m[100][100],int r,int c){


for(int j=0;j<c;j++){
int max=m[0][j];
for(int i=1;i<r;i++){
if(m[i][j]>max){
max=m[i][j];
}
}
printf("%d\n",max);
}
}

int main(){
int r,c;
int m[100][100];
scanf("%d %d",&r,&c);
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
scanf("%d",&m[i][j]);
}
}
row(m,r,c);
col(m,r,c);
}

25.​ 25) Largest in Products

#include <stdio.h>

int large(int arr[100],int n){


int temp;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]<arr[j])
{
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
}
}
}
int l1=arr[0]*arr[1]*arr[2];
int l2=arr[0]*arr[n-1]*arr[n-2];
if(l1>l2){
printf("%d",l1);
}
else{
printf("%d",l2);
}
}

int main(){
int n;
scanf("%d",&n);
int arr[100];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
large(arr,n);
}
Notes
1.​ Sum of odd numbers

●​ In main, get size and array then call function with arr,n, i value of 0
●​ In func, check if i==n if so then return 0
●​ Else check if element is odd it so then sum it up
●​ Then recursively call the func with arr,n,i+1
●​ If not odd number, call func with arr,n,i+1

2.​ Delete element by position

●​ In main, get size and array, k


●​ Call func with them
●​ In func, using loop check if i==k if not print the element

3.​ Permutation

●​ In main, get n and r


●​ Check if r>n or n<0 or r<0, if not call func if yes invalid
●​ In func, return fact(n)/fact(n-r)
●​ In fact func, check if n==1 or n==0 if yes return 1
●​ Else return n*fact(n-1)

4.​ Keith Number

●​ In main, get n then call iskeith func in if condition


●​ If true print keith or not keith
●​ In iskeith func, call countdigit func in a variable
●​ In countdigit func, use while loop to increment count with n/=10 and
return count
●​ Back in iskeith func, initialize n to another variable
●​ Then use loop to insert values in an array
●​ Using while(1) sum all the values
●​ Then check if sum==n or sum>n
●​ Again move the array with the new sum

You might also like