C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 6)
6.
What will be the output of the program?
#include<stdio.h>
int sumdig(int);
int main()
{
    int a, b;
    a = sumdig(123);
    b = sumdig(123);
    printf("%d, %d\n", a, b);
    return 0;
}
int sumdig(int n)
{
    int s, d;
    if(n!=0)
    {
        d = n%10;
        n = n/10;
        s = d+sumdig(n);
    }
    else
        return 0;
    return s;
}
4, 4
3, 3
6, 6
12, 12
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
51 comments Page 2 of 6.

Sudhakar said:   9 years ago
None of Them is correct answer.

According to mine at last step condition will fail and else part will be executed which will return 0, both time.

Answer should be 0, 0.

What others think?

Arck said:   9 years ago
If I take 1234 as an input I want 234 as an output. How can I do it?

Shubham Singh said:   1 decade ago
Can anyone tell me that in this program as we know that "if" is executed only once but it will between executed again and again. It is due to sumdig (n) or any other reason.

Megha said:   9 years ago
But in the program, there is no while loop simple if condition is given. Then how it works?

Amisha aggarwal said:   9 years ago
Super @Vivek, your approach is nice.

Trupti said:   9 years ago
Good explanation @Vivek.

Chatrapathi said:   1 decade ago
First :

a = sumdig(123)==>> sumdigt function.

d = n%10==> remainder 3 i.e d=3.
Then it go's to n= n/10==> n= 12.

s=d+sumdigit()==> sumdigit again called does the same here.
n=12 now.

d=n%10 => d=2.
n= n/10 => n=1.

Finally s=3+2+1=6.

Siva said:   9 years ago
Good explanation @Taher Ali.

Poongodi said:   9 years ago
How comes the value of s=3+2+1,6?

Please explain, & clear it.

Shahid said:   8 years ago
@Vivek.

The same logic can't be applied for (d*sumdig(n));

Answer for this (0,0) and (d-(sumdig(n));answer for this (2,2).


Post your comments here:

Your comments will be displayed after verification.