110k views
2 votes
Write a recursive function that returns true if the digits of a positive integer are in increasing order; otherwise, the function returns false. Also, write a program to test your function.

1 Answer

6 votes

Answer:

C code implementing the function:

#include<stdio.h>

int isIncreasing(int m);

void main()

{

int y, t;

printf("Enter any positive integer: ");

scanf("%d", &y);

t= isIncreasing(y); //call the functoion isIncreasing(int m)

if(t==1) //if isIncreasing(int m) returns 1, then the digits are in //increasing order

printf("True: The digits are in increasing order");

else

printf("False: The digits are not in increasing order");

}

int isIncreasing(int m)

{

int d1,d2;

if(m/10==0) //if we reach till the left of left most digit, then all before //digits were in order. else it won't go upto that.

{

return 1;

}

d1 = m%10; //d1 holds the right most digit of current m.

if(m%10 !=0)

{

m = m/10; //m is updated to find the rest digits and compare //them.

d2 = m%10;//d2 holds the digit just left of the right most digit of //m.

if(d2 <= d1) // if the right most and it's left digits are in order then //we proceed to left.

{

isIncreasing(m);

}

else //else we stop there itself, no need to move further.

return 0;

}

}

output is given as image.

Write a recursive function that returns true if the digits of a positive integer are-example-1
User Jscul
by
4.9k points