Showing Page:
1/6
C code to get largest element of an array by recursion:
#include<stdio.h>
#define MAX 100
int getMaxElement(int []);
int size;
int main(){
int arr[MAX],max,i;
printf("Enter the size of the array: ");
scanf("%d",&size);
printf("Enter %d elements of an array: ", size);
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
max=getMaxElement(arr);
printf("Largest element of an array is: %d",max);
return 0;
}
int getMaxElement(int arr[]){
static int i=0,max =-9999;
if(i < size){
if(max<arr[i])
max=arr[i];
i++;
getMaxElement(arr);
}
return max;
}
Sample output:
Enter the size of the array: 5
Enter 5 elements of an array: 1 4 5 6 2
Largest element of an array is: 6
C code to get largest element of an array without
recursion:
#include<stdio.h>
#define MAX 100
Showing Page:
2/6
int getMaxElement(int []);
int size;
int main(){
int arr[MAX],max,i;
printf("Enter the size of the array: ");
scanf("%d",&size);
printf("Enter %d elements of an array: ", size);
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
max=getMaxElement(arr);
printf("Largest element of an array is: %d",max);
return 0;
}
int getMaxElement(int arr[]){
int i=1,max;
max=arr[0];
while(i < size){
if(max<arr[i])
max=arr[i];
i++;
}
return max;
}
C code to check a number is prime number or not by
recursion:
#include<stdio.h>
int isPrime(int,int);
int main(){
int num,prime;
printf("Enter a positive number: ");
scanf("%d",&num);
Showing Page:
3/6
prime = isPrime(num,num/2);
if(prime==1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}
int isPrime(int num,int i){
if(i==1){
return 1;
}else{
if(num%i==0)
return 0;
else
isPrime(num,i-1);
}
}
Sample output:
Enter a positive number: 13
13 is a prime number
C code to check a number is prime number or not without
recursion:
#include<stdio.h>
int isPrime(int);
int main(){
int num,prime;
printf("Enter a positive number: ");
scanf("%d",&num);
prime = isPrime(num);
if(prime==1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
Showing Page:
4/6
}
int isPrime(int num){
int i=2;
while(i<=num/2){
if(num%i==0)
return 0;
else
i++;
}
return 1;
}
Sum of digits in c using recursion
#include<stdio.h>
int main(){
int num,x;
clrscr();
printf("\nEnter a number: ");
scanf("%d",&num);
x=findsum(num);
printf("Sum of the digits of %d is: %d",num,x);
return 0;
}
int r,s;
int findsum(int n){
if(n){
r=n%10;
s=s+r;
findsum(n/10);
}
else
return s;
}
Showing Page:
5/6
Write a simple code for binary search using function
recursion in c programming language
#include<stdio.h>
int main(){
int a[10],i,n,m,c,l,u;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements of the array: " );
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
l=0,u=n-1;
c=binary(a,n,m,l,u);
if(c==0)
printf("Number is not found.");
else
printf("Number is found.");
return 0;
}
int binary(int a[],int n,int m,int l,int u){
int mid,c=0;
if(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
}
else if(m<a[mid]){
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
}
else
Showing Page:
6/6
return c;
}
Sample output:
Enter the size of an array: 5
Enter the elements of the array: 8 9 10 11 12
Enter the number to be search: 8
Number is found.

Unformatted Attachment Preview

C code to get largest element of an array by recursion: #include #define MAX 100 int getMaxElement(int []); int size; int main(){ int arr[MAX],max,i; printf("Enter the size of the array: "); scanf("%d",&size); printf("Enter %d elements of an array: ", size); for(i=0;i #define MAX 100 int getMaxElement(int []); int size; int main(){ int arr[MAX],max,i; printf("Enter the size of the array: "); scanf("%d",&size); printf("Enter %d elements of an array: ", size); for(i=0;i int isPrime(int,int); int main(){ int num,prime; printf("Enter a positive number: "); scanf("%d",&num); prime = isPrime(num,num/2); if(prime==1) printf("%d is a prime number",num); else printf("%d is not a prime number",num); return 0; } int isPrime(int num,int i){ if(i==1){ return 1; }else{ if(num%i==0) return 0; else isPrime(num,i-1); } } Sample output: Enter a positive number: 13 13 is a prime number C code to check a number is prime number or not without recursion: #include int isPrime(int); int main(){ int num,prime; printf("Enter a positive number: "); scanf("%d",&num); prime = isPrime(num); if(prime==1) printf("%d is a prime number",num); else printf("%d is not a prime number",num); return 0; } int isPrime(int num){ int i=2; while(i<=num/2){ if(num%i==0) return 0; else i++; } return 1; } Sum of digits in c using recursion #include int main(){ int num,x; clrscr(); printf("\nEnter a number: "); scanf("%d",&num); x=findsum(num); printf("Sum of the digits of %d is: %d",num,x); return 0; } int r,s; int findsum(int n){ if(n){ r=n%10; s=s+r; findsum(n/10); } else return s; } Write a simple code for binary search using function recursion in c programming language #include int main(){ int a[10],i,n,m,c,l,u; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements of the array: " ); for(i=0;i
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4