Chapter - 6: Functions And Pointers

A 5-digit positive integer is entered through the keyboard, write a recursive and non-recursive function to calculate sum of digits of the 5-digit number.


G
Sections
3
Exercises

A

B

C

D

E

F

G

1

2

3

4

5

6

7

8

9

10

11

12

#include<stdio.h>
#include<conio.h>

int nonrec_calc(int);
int rec_calc(int);

int main()
{
	int num, sum;

	printf("Enter a five digit number : ");
	scanf("%d", &num);

	sum = nonrec_calc(num);
	printf("\n\nSum of digits using non-recursive function : %d\n", sum);
	
	sum = rec_calc(num);
	printf("\n\nSum of digits using recursive function: %d\n", sum);
	
	_getch();
	return 0;

}

//Non recursive function
int nonrec_calc(int num)
{
	int a, sum = 0, i;
	for (i = 0; num; i++)
	{
		a = num % 10;//obtaining the ladt digit
		sum = sum + a;//summing up the digits
		num = num / 10;//decreasing the number by one digit
	}
	return (sum);
}

//Recursive function
int rec_calc(int num)
{
	int sum = 0;
	if (num == 0)
		return sum;
	sum = num % 10 + rec_calc(num / 10);
	return sum;
}

© 2020 Garbage Valuegarbage value logo